| Index: mojo/dart/mojom/bin/init.dart
|
| diff --git a/mojo/dart/mojom/bin/init.dart b/mojo/dart/mojom/bin/init.dart
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..83fcb59eaf80d5fa963c90b8633ed9e7b863be97
|
| --- /dev/null
|
| +++ b/mojo/dart/mojom/bin/init.dart
|
| @@ -0,0 +1,107 @@
|
| +// Copyright 2015 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +/*
|
| +* This script should be invoked via 'pub run' for an application that consumes
|
| +* pub packages containing generated Mojo bindings (.mojom.dart files).
|
| +*
|
| +* It should be invoked as follows after 'pub get':
|
| +* $ pub run mojom:init -p `pwd`/packages
|
| +*/
|
| +
|
| +import 'dart:async';
|
| +import 'dart:io';
|
| +
|
| +import 'package:args/args.dart' as args;
|
| +import 'package:path/path.dart' as path;
|
| +
|
| +bool verbose;
|
| +bool dryRun;
|
| +
|
| +bool isMojomDart(String path) => path.endsWith('.mojom.dart');
|
| +
|
| +// Copies .mojom.dart files from a particular package's mojom directory to
|
| +// the mojom package directory.
|
| +//
|
| +// mojomDirectory: A pub packages mojom directory, i.e. $PACKAGE/mojom.
|
| +// mojomPackage: The mojom package directory.
|
| +copyMojomDirContents(Directory mojomDirectory, Directory mojomPackage) async {
|
| + assert(await mojomDirectory.exists());
|
| + await for (var mojoms in mojomDirectory.list(recursive: true)) {
|
| + if (mojoms is! File) continue;
|
| + if (!isMojomDart(mojoms.path)) continue;
|
| + if (verbose) print("Found $mojoms");
|
| +
|
| + final relative = path.relative(mojoms.path, from: mojomDirectory.path);
|
| + final dest = path.join(mojomPackage.path, relative);
|
| + final destDirectory = new Directory(path.dirname(dest));
|
| +
|
| + if (verbose || dryRun) {
|
| + print('Copying $mojoms to $dest');
|
| + }
|
| +
|
| + if (!dryRun) {
|
| + final File source = new File(mojoms.path);
|
| + if (verbose) print("Ensuring $destDirectory exists");
|
| + await destDirectory.create(recursive: true);
|
| + source.copy(dest);
|
| + }
|
| + }
|
| +}
|
| +
|
| +// If a package has a mojom directory, uses copyMojomDirContents to install them
|
| +// in the mojom package.
|
| +//
|
| +// package: The directory of a pub package in an application's packages
|
| +// directory.
|
| +// mojoPackage: The mojom package directory in an application's packages
|
| +// directory
|
| +installPackageMojoms(Directory package, Directory mojomPackage) async {
|
| + if (package.path == mojomPackage.path) return;
|
| + if (verbose) print("package = $package");
|
| + final mojomDirectory = new Directory(path.join(package.path, 'mojom'));
|
| + if (verbose) print("looking for = $mojomDirectory");
|
| + if (await mojomDirectory.exists()) {
|
| + copyMojomDirContents(mojomDirectory, mojomPackage);
|
| + } else if (verbose) {
|
| + print("$mojomDirectory not found");
|
| + }
|
| +}
|
| +
|
| +main(List<String> arguments) async {
|
| + final parser = new args.ArgParser()
|
| + ..addOption('package-root', abbr: 'p', defaultsTo: 'packages',
|
| + help: 'An absolute path to an application\'s package root')
|
| + ..addFlag('dry-run', abbr: 'd', defaultsTo: false,
|
| + help: 'Print the copy operations that would have been run, but'
|
| + 'do not copy anything.')
|
| + ..addFlag('verbose', abbr: 'v', defaultsTo: false);
|
| + final result = parser.parse(arguments);
|
| + verbose = result['verbose'];
|
| + dryRun = result['dry-run'];
|
| +
|
| + final packages = new Directory(result['package-root']);
|
| + if (!packages.isAbsolute) {
|
| + print("The --package-root parameter should be an absolute path.");
|
| + exit(1);
|
| + }
|
| + if (verbose) print("packages = $packages");
|
| + if (!(await packages.exists())) {
|
| + print("The packages directory $packages does not exist");
|
| + exit(1);
|
| + }
|
| +
|
| + final mojomPackage = new Directory( path.join(packages.path, 'mojom'));
|
| + if (verbose) print("mojom package = $mojomPackage");
|
| + if (!(await mojomPackage.exists())) {
|
| + print("The mojom package directory $mojomPackage does not exist");
|
| + exit(1);
|
| + }
|
| +
|
| + await for (var package in packages.list()) {
|
| + if (package is Directory) {
|
| + installPackageMojoms(package, mojomPackage);
|
| + }
|
| + }
|
| +}
|
|
|