From aa614bd0cc4fbb7a07c43af60470d916651ae8c9 Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum Date: Fri, 7 Oct 2016 15:46:58 -0700 Subject: [PATCH] Print the version number. --- bin/sass.dart | 28 +++++++++++++++++++++++++++- pubspec.yaml | 1 + tool/grind.dart | 8 +++++++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/bin/sass.dart b/bin/sass.dart index fc4fe831..715a251d 100644 --- a/bin/sass.dart +++ b/bin/sass.dart @@ -2,6 +2,9 @@ // MIT-style license that can be found in the LICENSE file or at // https://opensource.org/licenses/MIT. +import 'dart:async'; +import 'dart:isolate'; + import 'package:args/args.dart'; import 'package:stack_trace/stack_trace.dart'; import 'package:path/path.dart' as p; @@ -24,9 +27,19 @@ void main(List args) { abbr: 'c', help: 'Whether to emit terminal colors.', defaultsTo: true) ..addFlag('trace', help: 'Print full Dart stack traces for exceptions.') ..addFlag('help', - abbr: 'h', help: 'Print this usage information.', negatable: false); + abbr: 'h', help: 'Print this usage information.', negatable: false) + ..addFlag('version', + help: 'Print the version of Dart Sass.', negatable: false); var options = argParser.parse(getArguments(args)); + if (options['version']) { + _loadVersion().then((version) { + print(version); + exit(0); + }); + return; + } + if (options['help'] || options.rest.isEmpty) { print("Compile Sass to CSS.\n"); print("Usage: sass \n"); @@ -56,3 +69,16 @@ void main(List args) { exit(1); } } + +/// Loads and returns the current version of Sass. +Future _loadVersion() async { + var version = const String.fromEnvironment('version'); + if (version != null) return version; + + var libDir = p.fromUri( + await Isolate.resolvePackageUri(Uri.parse('package:sass/'))); + var pubspec = readFile(p.join(libDir, '..', 'pubspec.yaml')); + return pubspec.split("\n") + .firstWhere((line) => line.startsWith('version: ')) + .split(" ").last; +} diff --git a/pubspec.yaml b/pubspec.yaml index 64d2bf34..813f5604 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -22,3 +22,4 @@ dev_dependencies: js: "^0.6.0" node_preamble: "^1.0.0" test: "^0.12.0" + yaml: "^2.0.0" diff --git a/tool/grind.dart b/tool/grind.dart index 5591d6f1..7b4ce611 100644 --- a/tool/grind.dart +++ b/tool/grind.dart @@ -7,6 +7,7 @@ import 'dart:isolate'; import 'package:grinder/grinder.dart'; import 'package:node_preamble/preamble.dart' as preamble; +import 'package:yaml/yaml.dart'; main(args) => grind(args); @@ -29,7 +30,8 @@ js() { _ensureBuild(); var destination = new File('build/sass.dart.js'); Dart2js.compile(new File('bin/sass.dart'), - outFile: destination, extraArgs: ['-Dnode=true']); + outFile: destination, + extraArgs: ['-Dnode=true', '-Dversion=${_loadVersion()}']); var text = destination.readAsStringSync(); destination.writeAsStringSync("${preamble.getPreamble()}\n$text"); } @@ -38,3 +40,7 @@ js() { void _ensureBuild() { new Directory('build').createSync(recursive: true); } + +/// Loads the version number from pubspec.yaml. +String _loadVersion() => + loadYaml(new File('pubspec.yaml').readAsStringSync())['version'];