forked from dart-archive/polymer-dart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.dart
188 lines (168 loc) · 6.6 KB
/
deploy.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
/// **Note**: If you already have a `build.dart` in your application, we
/// recommend to use the `package:polymer/builder.dart` library instead.
/// Temporary deploy command used to create a version of the app that can be
/// compiled with dart2js and deployed. Following pub layout conventions, this
/// script will treat any HTML file under a package 'web/' and 'test/'
/// directories as entry points.
///
/// From an application package you can run deploy by creating a small program
/// as follows:
///
/// import "package:polymer/deploy.dart" as deploy;
/// main() => deploy.main();
///
/// This library should go away once `pub deploy` can be configured to run
/// barback transformers.
library polymer.deploy;
import 'dart:io';
import 'package:args/args.dart';
import 'package:code_transformers/tests.dart' show testingDartSdkDirectory;
import 'package:path/path.dart' as path;
import 'src/build/common.dart'
show TransformOptions, LintOptions, phasesForPolymer;
import 'src/build/runner.dart';
import 'transformer.dart';
main(List<String> arguments) {
var args = _parseArgs(arguments);
if (args == null) exit(1);
var test = args['test'];
var outDir = args['out'];
var filters = [];
if (args['file-filter'] != null) {
filters = args['file-filter'].split(',');
}
var options;
if (test == null) {
var transformOps = new TransformOptions(
directlyIncludeJS: args['js'],
contentSecurityPolicy: args['csp'],
releaseMode: !args['debug']);
var phases = createDeployPhases(transformOps);
options = new BarbackOptions(phases, outDir,
// TODO(sigmund): include here also smoke transformer when it's on by
// default.
packagePhases: {'polymer': phasesForPolymer});
} else {
options = _createTestOptions(
test, outDir, args['js'], args['csp'], !args['debug'], filters);
}
if (options == null) exit(1);
print('polymer/deploy.dart: creating a deploy target for '
'"${options.currentPackage}"');
runBarback(options)
.then((_) => print('Done! All files written to "$outDir"'))
.catchError(_reportErrorAndExit);
}
BarbackOptions _createTestOptions(String testFile, String outDir,
bool directlyIncludeJS, bool contentSecurityPolicy, bool releaseMode,
List<String> filters) {
var testDir = path.normalize(path.dirname(testFile));
// A test must be allowed to import things in the package.
// So we must find its package root, given the entry point. We can do this
// by walking up to find pubspec.yaml.
var pubspecDir = _findDirWithFile(path.absolute(testDir), 'pubspec.yaml');
if (pubspecDir == null) {
print('error: pubspec.yaml file not found, please run this script from '
'your package root directory or a subdirectory.');
return null;
}
var packageName = readCurrentPackageFromPubspec(pubspecDir);
// Find the dart-root so we can include all package dependencies and
// transformers from other packages.
var pkgDir = path.join(_findDirWithDir(path.absolute(testDir), 'pkg'), 'pkg');
var phases = createDeployPhases(new TransformOptions(
entryPoints: [path.relative(testFile, from: pubspecDir)],
directlyIncludeJS: directlyIncludeJS,
contentSecurityPolicy: contentSecurityPolicy,
releaseMode: releaseMode,
lint: new LintOptions.disabled()), sdkDir: testingDartSdkDirectory);
var dirs = {};
// Note: we include all packages in pkg/ to keep things simple. Ideally this
// should be restricted to the transitive dependencies of this package.
_subDirs(pkgDir).forEach((p) {
dirs[path.basename(p)] = p;
});
// Note: packageName may be a duplicate of 'polymer', but that's ok (they
// should be the same value).
dirs[packageName] = pubspecDir;
return new BarbackOptions(phases, outDir,
currentPackage: packageName, packageDirs: dirs,
// TODO(sigmund): include here also smoke transformer when it's on by
// default.
packagePhases: {'polymer': phasesForPolymer},
transformTests: true,
fileFilter: filters);
}
String _findDirWithFile(String dir, String filename) {
while (!new File(path.join(dir, filename)).existsSync()) {
var parentDir = path.dirname(dir);
// If we reached root and failed to find it, bail.
if (parentDir == dir) return null;
dir = parentDir;
}
return dir;
}
String _findDirWithDir(String dir, String subdir) {
while (!new Directory(path.join(dir, subdir)).existsSync()) {
var parentDir = path.dirname(dir);
// If we reached root and failed to find it, bail.
if (parentDir == dir) return null;
dir = parentDir;
}
return dir;
}
List<String> _subDirs(String dir) => new Directory(dir)
.listSync(followLinks: false)
.where((d) => d is Directory)
.map((d) => d.path)
.toList();
void _reportErrorAndExit(e, trace) {
print('Uncaught error: $e');
if (trace != null) print(trace);
exit(1);
}
ArgResults _parseArgs(arguments) {
var parser = new ArgParser()
..addFlag('help',
abbr: 'h',
help: 'Displays this help message.',
defaultsTo: false,
negatable: false)
..addOption('out',
abbr: 'o', help: 'Directory to generate files into.', defaultsTo: 'out')
..addOption('file-filter', help: 'Do not copy in files that match \n'
'these filters to the deployed folder, e.g., ".svn"', defaultsTo: null)
..addOption('test', help: 'Deploy the test at the given path.\n'
'Note: currently this will deploy all tests in its directory,\n'
'but it will eventually deploy only the specified test.')
..addFlag('js',
help: 'deploy replaces *.dart scripts with *.dart.js. This flag \n'
'leaves "packages/browser/dart.js" to do the replacement at runtime.',
defaultsTo: true)
..addFlag('debug',
help: 'run in debug mode. For example, use the debug polyfill \n'
'web_components/webcomponents.js instead of the minified one.\n',
defaultsTo: false)
..addFlag('csp', help: 'extracts inlined JavaScript code to comply with \n'
'Content Security Policy restrictions.');
try {
var results = parser.parse(arguments);
if (results['help']) {
_showUsage(parser);
return null;
}
return results;
} on FormatException catch (e) {
print(e.message);
_showUsage(parser);
return null;
}
}
_showUsage(parser) {
print('Usage: dart --package-root=packages/ '
'package:polymer/deploy.dart [options]');
print(parser.getUsage());
}