OLD | NEW |
(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:init -p `pwd`/packages |
| 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', abbr: 'p', defaultsTo: 'packages', |
| 75 help: 'An absolute path to an application\'s package root') |
| 76 ..addFlag('dry-run', abbr: 'd', defaultsTo: false, |
| 77 help: 'Print the copy operations that would have been run, but' |
| 78 'do not copy anything.') |
| 79 ..addFlag('verbose', abbr: 'v', defaultsTo: false); |
| 80 final result = parser.parse(arguments); |
| 81 verbose = result['verbose']; |
| 82 dryRun = result['dry-run']; |
| 83 |
| 84 final packages = new Directory(result['package-root']); |
| 85 if (!packages.isAbsolute) { |
| 86 print("The --package-root parameter should be an absolute path."); |
| 87 exit(1); |
| 88 } |
| 89 if (verbose) print("packages = $packages"); |
| 90 if (!(await packages.exists())) { |
| 91 print("The packages directory $packages does not exist"); |
| 92 exit(1); |
| 93 } |
| 94 |
| 95 final mojomPackage = new Directory( path.join(packages.path, 'mojom')); |
| 96 if (verbose) print("mojom package = $mojomPackage"); |
| 97 if (!(await mojomPackage.exists())) { |
| 98 print("The mojom package directory $mojomPackage does not exist"); |
| 99 exit(1); |
| 100 } |
| 101 |
| 102 await for (var package in packages.list()) { |
| 103 if (package is Directory) { |
| 104 installPackageMojoms(package, mojomPackage); |
| 105 } |
| 106 } |
| 107 } |
OLD | NEW |