Skip to content

Commit 12f638c

Browse files
authored
Reorganized package, put example it it's own directory, etc (#9)
* Reorganized package, put example it it's own directory, etc * Add missing copyright headers * Pipe through documentation on serve [director:port] * Print helpful error with bad command usage * Enable travis * Be very careful when invoking the build script via isolate Closes #10 * latest angular
1 parent bf1e804 commit 12f638c

26 files changed

+282
-91
lines changed

.gitignore

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
packages
2-
/*/build/
3-
.pub/
4-
pubspec.lock
5-
6-
# Files generated by dart tools
71
.dart_tool
8-
doc/
2+
.packages
3+
pubspec.lock

.travis.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Created with https://github.com/dart-lang/mono_repo
2+
language: dart
3+
4+
jobs:
5+
include:
6+
- stage: analyzer_and_format
7+
script: ./tool/travis.sh dartfmt
8+
env: PKG="example"
9+
dart: dev
10+
- stage: analyzer_and_format
11+
script: ./tool/travis.sh dartanalyzer
12+
env: PKG="example"
13+
dart: dev
14+
- stage: analyzer_and_format
15+
script: ./tool/travis.sh dartfmt
16+
env: PKG="webdev"
17+
dart: dev
18+
- stage: analyzer_and_format
19+
script: ./tool/travis.sh dartanalyzer
20+
env: PKG="webdev"
21+
dart: dev
22+
23+
stages:
24+
- analyzer_and_format
25+
26+
# Only building master means that we don't run two builds for each pull request.
27+
branches:
28+
only: [master]
29+
30+
cache:
31+
directories:
32+
- $HOME/.pub-cache

example/.mono_repo.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# See https://github.com/dart-lang/mono_repo for details
2+
dart:
3+
- dev
4+
5+
stages:
6+
- analyzer_and_format:
7+
- dartfmt
8+
- dartanalyzer: --fatal-infos --fatal-warnings .
9+
# - unit_test:
10+
# - test
File renamed without changes.

webdev/example/lib/app_component.dart renamed to example/lib/app_component.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
import 'package:angular/angular.dart';
26

37
import 'src/hello_world/hello_world.dart';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:angular/angular.dart';
6+
7+
@Component(
8+
selector: 'hello-world',
9+
styleUrls: const ['hello_world.css'],
10+
templateUrl: 'hello_world.html',
11+
)
12+
class HelloWorldComponent {
13+
// Nothing here.
14+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div>Hello World</div>

example/pubspec.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: webdev_example_app
2+
description: A web app example for webdev CLI.
3+
4+
environment:
5+
sdk: ">=2.0.0-dev.32.0 <2.0.0"
6+
7+
dependencies:
8+
angular: ^5.0.0-alpha+8
9+
10+
dev_dependencies:
11+
build_runner: ^0.8.0
12+
build_web_compilers: ^0.3.4

webdev/example/web/index.html renamed to example/web/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
<title>webdev example</title>
55
<meta charset="utf-8">
66
<meta name="viewport" content="width=device-width, initial-scale=1">
7-
<script defer src="main.dart" type="application/dart"></script>
8-
<script defer src="packages/browser/dart.js"></script>
7+
<script defer src="main.dart.js"></script>
98
<link rel="stylesheet" href="styles.css">
109
</head>
1110
<body>

example/web/main.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:angular/experimental.dart';
6+
7+
// ignore: uri_has_not_been_generated
8+
import 'package:webdev_example_app/app_component.template.dart' as ng;
9+
10+
main() {
11+
// ignore: argument_type_not_assignable
12+
bootstrapFactory(ng.AppComponentNgFactory);
13+
}
File renamed without changes.

tool/travis.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
# Created with https://github.com/dart-lang/mono_repo
3+
4+
if [ -z "$PKG" ]; then
5+
echo -e '\033[31mPKG environment variable must be set!\033[0m'
6+
exit 1
7+
fi
8+
9+
if [ "$#" == "0" ]; then
10+
echo -e '\033[31mAt least one task argument must be provided!\033[0m'
11+
exit 1
12+
fi
13+
14+
pushd $PKG
15+
pub upgrade || exit $?
16+
17+
EXIT_CODE=0
18+
19+
while (( "$#" )); do
20+
TASK=$1
21+
case $TASK in
22+
dartanalyzer) echo
23+
echo -e '\033[1mTASK: dartanalyzer\033[22m'
24+
echo -e 'dartanalyzer --fatal-infos --fatal-warnings .'
25+
dartanalyzer --fatal-infos --fatal-warnings . || EXIT_CODE=$?
26+
;;
27+
dartfmt) echo
28+
echo -e '\033[1mTASK: dartfmt\033[22m'
29+
echo -e 'dartfmt -n --set-exit-if-changed .'
30+
dartfmt -n --set-exit-if-changed . || EXIT_CODE=$?
31+
;;
32+
*) echo -e "\033[31mNot expecting TASK '${TASK}'. Error!\033[0m"
33+
EXIT_CODE=1
34+
;;
35+
esac
36+
37+
shift
38+
done
39+
40+
exit $EXIT_CODE

webdev/.mono_repo.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# See https://github.com/dart-lang/mono_repo for details
2+
dart:
3+
- dev
4+
5+
stages:
6+
- analyzer_and_format:
7+
- dartfmt
8+
- dartanalyzer: --fatal-infos --fatal-warnings .
9+
# - unit_test:
10+
# - test

webdev/bin/webdev.dart

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1+
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
import 'dart:async';
6+
import 'dart:io';
27

3-
import 'package:webdev/webdev.dart';
8+
import 'package:args/command_runner.dart';
9+
import 'package:io/ansi.dart';
10+
import 'package:io/io.dart';
11+
import 'package:webdev/src/webdev_command_runner.dart';
412

513
Future main(List<String> args) async {
6-
await webdevCommandRunner().run(args);
14+
try {
15+
await webdevCommandRunner().run(args);
16+
} on UsageException catch (e) {
17+
print(yellow.wrap(e.message));
18+
print(' ');
19+
print(e.usage);
20+
exitCode = ExitCode.usage.code;
21+
}
722
}

webdev/example/lib/src/hello_world/hello_world.dart

Lines changed: 0 additions & 10 deletions
This file was deleted.

webdev/example/lib/src/hello_world/hello_world.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

webdev/example/pubspec.yaml

Lines changed: 0 additions & 25 deletions
This file was deleted.

webdev/example/web/main.dart

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import 'dart:async';
2-
import 'dart:isolate';
1+
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
34

5+
import 'dart:async';
46
import 'build_runner_command_base.dart';
57

68
/// Command to execute pub run build_runner build.
@@ -12,12 +14,5 @@ class BuildCommand extends BuildRunnerCommandBase {
1214
final description = 'Run builders to build a package.';
1315

1416
@override
15-
Future run() async {
16-
final arguments = ['build'];
17-
arguments.addAll(argResults.arguments);
18-
var exitPort = new ReceivePort();
19-
await Isolate.spawnUri(await buildRunnerScript, arguments, null,
20-
onExit: exitPort.sendPort, automaticPackageResolution: true);
21-
await exitPort.first;
22-
}
17+
Future run() => runCore('build');
2318
}

webdev/lib/src/command/build_runner_command_base.dart

Lines changed: 98 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
import 'dart:async';
26
import 'dart:io';
7+
import 'dart:isolate';
38

49
import 'package:args/command_runner.dart';
10+
import 'package:stack_trace/stack_trace.dart';
511

612
/// Extend to get a command with the arguments common to all build_runner
713
/// commands.
@@ -19,13 +25,97 @@ abstract class BuildRunnerCommandBase extends Command {
1925
help: 'Enables verbose logging.');
2026
}
2127

22-
Future<Uri> get buildRunnerScript async {
23-
// TODO(nshahan) build_runner will expose this as a function call that will
24-
// be imported to avoid running a binary in a transitive dependency with
25-
// pub run.
26-
final executable = 'pub';
27-
final arguments = ['run', 'build_runner', 'generate-build-script'];
28-
final results = await Process.run(executable, arguments);
29-
return new Uri.file(results.stdout.toString().trim());
28+
Future runCore(String command) async {
29+
final arguments = [command]..addAll(argResults.arguments);
30+
31+
// Heavily inspired by dart-lang/build @ 0c77443dd7
32+
// /build_runner/bin/build_runner.dart#L58-L85
33+
var exitPort = new ReceivePort();
34+
var errorPort = new ReceivePort();
35+
var messagePort = new ReceivePort();
36+
var errorListener = errorPort.listen((e) {
37+
stderr.writeln('\n\nYou have hit a bug in build_runner');
38+
stderr.writeln('Please file an issue with reproduction steps at '
39+
'https://github.com/dart-lang/build/issues\n\n');
40+
final error = e[0];
41+
final trace = e[1] as String;
42+
stderr.writeln(error);
43+
stderr.writeln(new Trace.parse(trace).terse);
44+
if (exitCode == 0) exitCode = 1;
45+
});
46+
await Isolate.spawnUri(
47+
await _buildRunnerScript(), arguments, messagePort.sendPort,
48+
onExit: exitPort.sendPort,
49+
onError: errorPort.sendPort,
50+
automaticPackageResolution: true);
51+
StreamSubscription exitCodeListener;
52+
exitCodeListener = messagePort.listen((isolateExitCode) {
53+
if (isolateExitCode is! int) {
54+
throw new StateError(
55+
'Bad response from isolate, expected an exit code but got '
56+
'$isolateExitCode');
57+
}
58+
exitCode = isolateExitCode as int;
59+
exitCodeListener.cancel();
60+
exitCodeListener = null;
61+
});
62+
await exitPort.first;
63+
await errorListener.cancel();
64+
await exitCodeListener?.cancel();
3065
}
3166
}
67+
68+
Future<Uri> _buildRunnerScript() async {
69+
var dataUri = new Uri.dataFromString(_bootstrapScript);
70+
71+
var messagePort = new ReceivePort();
72+
var exitPort = new ReceivePort();
73+
var errorPort = new ReceivePort();
74+
75+
await Isolate.spawnUri(dataUri, [], messagePort.sendPort,
76+
onExit: exitPort.sendPort,
77+
onError: errorPort.sendPort,
78+
errorsAreFatal: true,
79+
packageConfig: new Uri.file('.packages'));
80+
81+
var allErrorsFuture = errorPort.forEach((error) {
82+
var errorList = error as List;
83+
var message = errorList[0] as String;
84+
var stack = new StackTrace.fromString(errorList[1] as String);
85+
86+
stderr.writeln(message);
87+
stderr.writeln(stack);
88+
});
89+
90+
var items = await Future.wait([
91+
messagePort.toList(),
92+
allErrorsFuture,
93+
exitPort.first.whenComplete(() {
94+
messagePort.close();
95+
errorPort.close();
96+
})
97+
]);
98+
99+
var messages = items[0] as List;
100+
if (messages.isEmpty) {
101+
throw new StateError('An error occurred while running booting.');
102+
}
103+
104+
assert(messages.length == 1);
105+
return new Uri.file(messages.single as String);
106+
}
107+
108+
const _bootstrapScript = r'''
109+
import 'dart:io';
110+
import 'dart:isolate';
111+
112+
import 'package:build_runner/build_script_generate.dart';
113+
import 'package:path/path.dart' as p;
114+
115+
void main(List<String> args, [SendPort sendPort]) async {
116+
var buildScript = await generateBuildScript();
117+
var scriptFile = new File(scriptLocation)..createSync(recursive: true);
118+
scriptFile.writeAsStringSync(buildScript);
119+
sendPort.send(p.absolute(scriptLocation));
120+
}
121+
''';

0 commit comments

Comments
 (0)