Index: lib/src/options.dart |
diff --git a/lib/src/options.dart b/lib/src/options.dart |
index 968ce65508ade5a00632b97e71c320b819f09411..238dafa74180c63a47300da2df84a6ee126b8aa0 100644 |
--- a/lib/src/options.dart |
+++ b/lib/src/options.dart |
@@ -11,6 +11,8 @@ import 'package:args/args.dart'; |
import 'package:cli_util/cli_util.dart' show getSdkDir; |
import 'package:dev_compiler/config.dart'; |
import 'package:logging/logging.dart' show Level; |
+import 'package:path/path.dart' as path; |
+import 'package:yaml/yaml.dart'; |
/// Options used by our TypeResolver. |
class ResolverOptions { |
@@ -191,6 +193,11 @@ class CompilerOptions implements RulesOptions, ResolverOptions, JSCodeOptions { |
@override |
final bool emitSourceMaps; |
+ /// Location for runtime files, such as `dart_core.js`. By default this is |
+ /// inferred to be under `lib/runtime/` in the location of the `dev_compiler` |
+ /// package (if we can infer where that is located). |
+ final String runtimeDir; |
+ |
CompilerOptions({this.allowConstCasts: true, this.checkSdk: false, |
this.dumpInfo: false, this.dumpInfoFile, this.dumpSrcDir, |
this.forceCompile: false, this.formatOutput: false, |
@@ -205,7 +212,7 @@ class CompilerOptions implements RulesOptions, ResolverOptions, JSCodeOptions { |
this.nonnullableTypes: TypeOptions.NONNULLABLE_TYPES, this.help: false, |
this.useMockSdk: false, this.dartSdkPath, this.logLevel: Level.SEVERE, |
this.emitSourceMaps: true, this.entryPointFile: null, |
- this.serverMode: false, this.port: 8080}); |
+ this.serverMode: false, this.port: 8080, this.runtimeDir}); |
} |
/// Parses options from the command-line |
@@ -217,6 +224,10 @@ CompilerOptions parseOptions(List<String> argv) { |
if (sdkPath == null && !args['mock-sdk']) { |
sdkPath = getSdkDir(argv).path; |
} |
+ var runtimeDir = args['runtime-dir']; |
+ if (runtimeDir == null) { |
+ runtimeDir = _computeRuntimeDir(); |
+ } |
return new CompilerOptions( |
allowConstCasts: args['allow-const-casts'], |
checkSdk: args['sdk-check'], |
@@ -247,7 +258,8 @@ CompilerOptions parseOptions(List<String> argv) { |
emitSourceMaps: args['source-maps'], |
entryPointFile: args.rest.length == 0 ? null : args.rest.first, |
serverMode: args['server'], |
- port: int.parse(args['port'])); |
+ port: int.parse(args['port']), |
+ runtimeDir: runtimeDir); |
} |
final ArgParser argParser = new ArgParser() |
@@ -298,6 +310,8 @@ final ArgParser argParser = new ArgParser() |
'the list of directories where to look for packages.', defaultsTo: '') |
..addFlag('source-maps', |
help: 'Whether to emit source map files', defaultsTo: true) |
+ ..addOption('runtime-dir', |
+ help: 'Where to find dev_compiler\'s runtime files', defaultsTo: null) |
// general options |
..addFlag('help', abbr: 'h', help: 'Display this message') |
@@ -314,3 +328,54 @@ final ArgParser argParser = new ArgParser() |
abbr: 'f', |
help: 'Dump info json file (requires dump-info)', |
defaultsTo: null); |
+ |
+/// Tries to find the `lib/runtime/` directory of the dev_compiler package. This |
+/// works when running devc from it's sources or from a snapshot that is |
+/// activated via `pub global activate`. |
+String _computeRuntimeDir() { |
+ var scriptUri = Platform.script; |
+ var scriptPath = scriptUri.path; |
+ var file = path.basename(scriptPath); |
+ var dir = path.dirname(scriptPath); |
+ var lastdir = path.basename(dir); |
+ dir = path.dirname(dir); |
+ |
+ // Both the source devc.dart and the snapshot generated by pub global activate |
+ // are under a bin folder. |
+ if (lastdir != 'bin') return null; |
+ |
+ // And both under a project directory containing a pubspec.lock file. |
+ var lockfile = path.join(dir, 'pubspec.lock'); |
+ if (!new File(lockfile).existsSync()) return null; |
+ |
+ // If running from sources we found it! |
+ if (file == 'devc.dart') return path.join(dir, 'lib', 'runtime'); |
+ |
+ // If running from a pub global snapshot, we need to read the lock file to |
+ // find where the actual sources are located in the pub cache. |
+ if (file == 'devc.dart.snapshot') { |
+ // Note: this depends on implementation details of pub. |
+ var yaml = loadYaml(new File(lockfile).readAsStringSync()); |
+ var info = yaml['packages']['dev_compiler']; |
+ if (info == null) return null; |
+ |
+ var cacheDir; |
+ if (info['source'] == 'hosted') { |
+ cacheDir = path.join( |
+ 'hosted', 'pub.dartlang.org', 'dev_compiler-${info["version"]}'); |
+ } else if (info['source'] == 'git') { |
+ var ref = info['description']['resolved-ref']; |
+ cacheDir = path.join('git', 'dev_compiler-${ref}'); |
+ } |
+ |
+ // We should be under "/path/to/pub-cache/global_packages/dev_compiler". |
+ // The pub-cache directory is two levels up, but we verify that the layout |
+ // looks correct. |
+ if (path.basename(dir) != 'dev_compiler') return null; |
+ dir = path.dirname(dir); |
+ if (path.basename(dir) != 'global_packages') return null; |
+ dir = path.dirname(dir); |
+ return path.join(dir, cacheDir, 'lib', 'runtime'); |
+ } |
+ return null; |
+} |