@@ -24,10 +24,12 @@ class CompileCommand extends Command {
24
24
CompileCommand ({MessageHandler messageHandler})
25
25
: this .messageHandler = messageHandler ?? print {
26
26
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.' );
27
30
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.' );
31
33
CompilerOptions .addArguments (argParser);
32
34
AnalyzerOptions .addArguments (argParser);
33
35
}
@@ -50,8 +52,23 @@ Root of source files. Generated library names are relative to this root.
50
52
} else {
51
53
buildRoot = Directory .current.path;
52
54
}
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));
55
72
56
73
JSModuleFile module = compiler.compile (unit, compilerOptions);
57
74
module.errors.forEach (messageHandler);
@@ -71,9 +88,17 @@ Root of source files. Generated library names are relative to this root.
71
88
}
72
89
}
73
90
74
- String _moduleForLibrary (Source source) {
91
+ String _moduleForLibrary (String moduleRoot, Source source) {
75
92
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 ' );
77
102
}
78
103
79
104
throw usageException (
0 commit comments