Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: mojo/dart/mojom/bin/generate.dart

Issue 1106383006: Dart: Adds mojom pub package. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Cleanup Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 /*
6 * This script should be invoked via 'pub run' for an application that consumes
7 * pub packages containing generated Mojo bindings (.mojom.dart files).
8 *
9 * It should be invoked as follows after 'pub get':
10 * $ pub run mojom:generate
11 */
12
13 import 'dart:async';
14 import 'dart:io';
15
16 import 'package:args/args.dart' as args;
17 import 'package:path/path.dart' as path;
18
19 bool verbose;
20 bool dryRun;
21
22 bool isMojomDart(String path) => path.endsWith('.mojom.dart');
23
24 // Copies .mojom.dart files from a particular package's mojom directory to
25 // the mojom package directory.
26 //
27 // mojomDirectory: A pub packages mojom directory, i.e. $PACKAGE/mojom.
28 // mojomPackage: The mojom package directory.
29 copyMojomDirContents(Directory mojomDirectory, Directory mojomPackage) async {
30 assert(await mojomDirectory.exists());
31 await for (var mojoms in mojomDirectory.list(recursive: true)) {
32 if (mojoms is! File) continue;
33 if (!isMojomDart(mojoms.path)) continue;
34 if (verbose) print("Found $mojoms");
35
36 final relative = path.relative(mojoms.path, from: mojomDirectory.path);
37 final dest = path.join(mojomPackage.path, relative);
38 final destDirectory = new Directory(path.dirname(dest));
39
40 if (verbose || dryRun) {
41 print('Copying $mojoms to $dest');
42 }
43
44 if (!dryRun) {
45 final File source = new File(mojoms.path);
46 if (verbose) print("Ensuring $destDirectory exists");
47 await destDirectory.create(recursive: true);
48 source.copy(dest);
49 }
50 }
51 }
52
53 // If a package has a mojom directory, uses copyMojomDirContents to install them
54 // in the mojom package.
55 //
56 // package: The directory of a pub package in an application's packages
57 // directory.
58 // mojoPackage: The mojom package directory in an application's packages
59 // directory
60 installPackageMojoms(Directory package, Directory mojomPackage) async {
61 if (package.path == mojomPackage.path) return;
62 if (verbose) print("package = $package");
63 final mojomDirectory = new Directory(path.join(package.path, 'mojom'));
64 if (verbose) print("looking for = $mojomDirectory");
65 if (await mojomDirectory.exists()) {
66 copyMojomDirContents(mojomDirectory, mojomPackage);
67 } else if (verbose) {
68 print("$mojomDirectory not found");
69 }
70 }
71
72 main(List<String> arguments) async {
73 final parser = new args.ArgParser()
74 ..addOption('package-root',
75 abbr: 'p',
76 defaultsTo: path.join(Directory.current.path, 'packages'),
77 help: 'An absolute path to an application\'s package root')
78 ..addFlag('dry-run',
79 abbr: 'd',
80 defaultsTo: false,
81 help: 'Print the copy operations that would have been run, but'
82 'do not copy anything.')
83 ..addFlag('verbose', abbr: 'v', defaultsTo: false);
84 final result = parser.parse(arguments);
85 verbose = result['verbose'];
86 dryRun = result['dry-run'];
87
88 final packages = new Directory(result['package-root']);
89 if (!packages.isAbsolute) {
90 print("The --package-root parameter should be an absolute path.");
91 exit(1);
92 }
93 if (verbose) print("packages = $packages");
94 if (!(await packages.exists())) {
95 print("The packages directory $packages does not exist");
96 exit(1);
97 }
98
99 final mojomPackage = new Directory(path.join(packages.path, 'mojom'));
100 if (verbose) print("mojom package = $mojomPackage");
101 if (!(await mojomPackage.exists())) {
102 print("The mojom package directory $mojomPackage does not exist");
103 exit(1);
104 }
105
106 await for (var package in packages.list()) {
107 if (package is Directory) {
108 installPackageMojoms(package, mojomPackage);
109 }
110 }
111 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698