Skip to content

Commit 4b6e3fe

Browse files
committed
1 parent 5c41457 commit 4b6e3fe

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

pkg/dev_compiler/lib/src/compiler/command.dart

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ class CompileCommand extends Command {
2424
CompileCommand({MessageHandler messageHandler})
2525
: this.messageHandler = messageHandler ?? print {
2626
argParser.addOption('out', abbr: 'o', help: 'Output file (required)');
27+
argParser.addOption('module-root',
28+
help: 'Root module directory. '
29+
'Generated module paths are relative to this root.');
2730
argParser.addOption('build-root',
28-
help: '''
29-
Root of source files. Generated library names are relative to this root.
30-
''');
31+
help: 'Root of source files. '
32+
'Generated library names are relative to this root.');
3133
CompilerOptions.addArguments(argParser);
3234
AnalyzerOptions.addArguments(argParser);
3335
}
@@ -50,8 +52,23 @@ Root of source files. Generated library names are relative to this root.
5052
} else {
5153
buildRoot = Directory.current.path;
5254
}
53-
var unit = new BuildUnit(path.basenameWithoutExtension(outPath), buildRoot,
54-
argResults.rest, _moduleForLibrary);
55+
var moduleRoot = argResults['module-root'] as String;
56+
String modulePath;
57+
if (moduleRoot != null) {
58+
moduleRoot = path.absolute(moduleRoot);
59+
if (!path.isWithin(moduleRoot, outPath)) {
60+
usageException('Output file $outPath must be within the module root '
61+
'directory $moduleRoot');
62+
}
63+
modulePath =
64+
path.withoutExtension(path.relative(outPath, from: moduleRoot));
65+
} else {
66+
moduleRoot = path.dirname(outPath);
67+
modulePath = path.basenameWithoutExtension(outPath);
68+
}
69+
70+
var unit = new BuildUnit(modulePath, buildRoot, argResults.rest,
71+
(source) => _moduleForLibrary(moduleRoot, source));
5572

5673
JSModuleFile module = compiler.compile(unit, compilerOptions);
5774
module.errors.forEach(messageHandler);
@@ -71,9 +88,17 @@ Root of source files. Generated library names are relative to this root.
7188
}
7289
}
7390

74-
String _moduleForLibrary(Source source) {
91+
String _moduleForLibrary(String moduleRoot, Source source) {
7592
if (source is InSummarySource) {
76-
return path.basenameWithoutExtension(source.summaryPath);
93+
var summaryPath = source.summaryPath;
94+
if (path.isWithin(moduleRoot, summaryPath)) {
95+
return path
96+
.withoutExtension(path.relative(summaryPath, from: moduleRoot));
97+
}
98+
99+
throw usageException(
100+
'Imported file ${source.uri} is not within the module root '
101+
'directory $moduleRoot');
77102
}
78103

79104
throw usageException(

0 commit comments

Comments
 (0)