| OLD | NEW |
| 1 #!/usr/bin/env dart | 1 #!/usr/bin/env dart |
| 2 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 2 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 3 // for details. All rights reserved. Use of this source code is governed by a | 3 // for details. All rights reserved. Use of this source code is governed by a |
| 4 // BSD-style license that can be found in the LICENSE file. | 4 // BSD-style license that can be found in the LICENSE file. |
| 5 | 5 |
| 6 /// Command line tool to merge the SDK libraries and our patch files. | 6 /// Command line tool to merge the SDK libraries and our patch files. |
| 7 /// This is currently designed as an offline tool, but we could automate it. | 7 /// This is currently designed as an offline tool, but we could automate it. |
| 8 library dev_compiler.tool.patch_sdk; | 8 library dev_compiler.tool.patch_sdk; |
| 9 | 9 |
| 10 import 'dart:io'; | 10 import 'dart:io'; |
| 11 import 'dart:math' as math; |
| 11 | 12 |
| 12 import 'package:analyzer/analyzer.dart'; | 13 import 'package:analyzer/analyzer.dart'; |
| 13 import 'package:analyzer/src/generated/sdk.dart'; | 14 import 'package:analyzer/src/generated/sdk.dart'; |
| 14 import 'package:path/path.dart' as path; | 15 import 'package:path/path.dart' as path; |
| 15 | 16 |
| 16 void main(List<String> argv) { | 17 void main(List<String> argv) { |
| 17 if (argv.length < 2) { | 18 if (argv.length < 2) { |
| 18 var self = path.relative(path.fromUri(Platform.script)); | 19 var self = path.relative(path.fromUri(Platform.script)); |
| 19 var toolDir = path.relative(path.dirname(path.fromUri(Platform.script))); | 20 var toolDir = path.relative(path.dirname(path.fromUri(Platform.script))); |
| 20 | 21 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 var libraryIn; | 66 var libraryIn; |
| 66 if (library.path.contains(INTERNAL_PATH)) { | 67 if (library.path.contains(INTERNAL_PATH)) { |
| 67 libraryIn = | 68 libraryIn = |
| 68 path.join(privateIn, library.path.replaceAll(INTERNAL_PATH, '')); | 69 path.join(privateIn, library.path.replaceAll(INTERNAL_PATH, '')); |
| 69 } else { | 70 } else { |
| 70 libraryIn = libraryOut; | 71 libraryIn = libraryOut; |
| 71 } | 72 } |
| 72 | 73 |
| 73 var libraryFile = new File(libraryIn); | 74 var libraryFile = new File(libraryIn); |
| 74 if (libraryFile.existsSync()) { | 75 if (libraryFile.existsSync()) { |
| 75 var contents = <String>[]; | 76 var outPaths = <String>[libraryOut]; |
| 76 var paths = <String>[]; | |
| 77 var libraryContents = libraryFile.readAsStringSync(); | 77 var libraryContents = libraryFile.readAsStringSync(); |
| 78 paths.add(libraryOut); | 78 |
| 79 contents.add(libraryContents); | 79 int inputModifyTime = |
| 80 libraryFile.lastModifiedSync().millisecondsSinceEpoch; |
| 81 var partFiles = <File>[]; |
| 80 for (var part in parseDirectives(libraryContents).directives) { | 82 for (var part in parseDirectives(libraryContents).directives) { |
| 81 if (part is PartDirective) { | 83 if (part is PartDirective) { |
| 82 var partPath = part.uri.stringValue; | 84 var partPath = part.uri.stringValue; |
| 83 paths.add(path.join(path.dirname(libraryOut), partPath)); | 85 outPaths.add(path.join(path.dirname(libraryOut), partPath)); |
| 84 contents.add(new File(path.join(path.dirname(libraryIn), partPath)) | 86 |
| 85 .readAsStringSync()); | 87 var partFile = new File(path.join(path.dirname(libraryIn), partPath)); |
| 88 partFiles.add(partFile); |
| 89 inputModifyTime = math.max(inputModifyTime, |
| 90 partFile.lastModifiedSync().millisecondsSinceEpoch); |
| 86 } | 91 } |
| 87 } | 92 } |
| 88 | 93 |
| 89 // See if we can find a patch file. | 94 // See if we can find a patch file. |
| 90 var patchPath = path.join( | 95 var patchPath = path.join( |
| 91 patchIn, path.basenameWithoutExtension(libraryIn) + '_patch.dart'); | 96 patchIn, path.basenameWithoutExtension(libraryIn) + '_patch.dart'); |
| 92 | 97 |
| 93 if (new File(patchPath).existsSync()) { | 98 var patchFile = new File(patchPath); |
| 94 var patchContents = new File(patchPath).readAsStringSync(); | 99 bool patchExists = patchFile.existsSync(); |
| 95 contents = _patchLibrary(contents, patchContents); | 100 if (patchExists) { |
| 101 inputModifyTime = math.max(inputModifyTime, |
| 102 patchFile.lastModifiedSync().millisecondsSinceEpoch); |
| 96 } | 103 } |
| 97 for (var i = 0; i < paths.length; i++) { | 104 |
| 98 var outPath = | 105 // Compute output paths |
| 99 path.join(sdkOut, path.relative(paths[i], from: sdkLibIn)); | 106 outPaths = outPaths |
| 100 _writeSync(outPath, contents[i]); | 107 .map((p) => path.join(sdkOut, path.relative(p, from: sdkLibIn))) |
| 108 .toList(); |
| 109 |
| 110 // Compare output modify time with input modify time. |
| 111 bool needsUpdate = false; |
| 112 for (var outPath in outPaths) { |
| 113 var outFile = new File(outPath); |
| 114 if (!outFile.existsSync() || |
| 115 outFile.lastModifiedSync().millisecondsSinceEpoch < |
| 116 inputModifyTime) { |
| 117 needsUpdate = true; |
| 118 break; |
| 119 } |
| 120 } |
| 121 |
| 122 if (needsUpdate) { |
| 123 var contents = <String>[libraryContents]; |
| 124 contents.addAll(partFiles.map((f) => f.readAsStringSync())); |
| 125 if (patchExists) { |
| 126 var patchContents = patchFile.readAsStringSync(); |
| 127 contents = _patchLibrary(contents, patchContents); |
| 128 } |
| 129 |
| 130 for (var i = 0; i < outPaths.length; i++) { |
| 131 _writeSync(outPaths[i], contents[i]); |
| 132 } |
| 101 } | 133 } |
| 102 } | 134 } |
| 103 } | 135 } |
| 104 } | 136 } |
| 105 | 137 |
| 106 /// Writes a file, creating the directory if needed. | 138 /// Writes a file, creating the directory if needed. |
| 107 void _writeSync(String filePath, String contents) { | 139 void _writeSync(String filePath, String contents) { |
| 108 var outDir = new Directory(path.dirname(filePath)); | 140 var outDir = new Directory(path.dirname(filePath)); |
| 109 if (!outDir.existsSync()) outDir.createSync(recursive: true); | 141 if (!outDir.existsSync()) outDir.createSync(recursive: true); |
| 110 | 142 |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 if (diff != 0) return diff; | 424 if (diff != 0) return diff; |
| 393 return end - other.end; | 425 return end - other.end; |
| 394 } | 426 } |
| 395 } | 427 } |
| 396 | 428 |
| 397 List<SdkLibrary> _getSdkLibraries(String contents) { | 429 List<SdkLibrary> _getSdkLibraries(String contents) { |
| 398 var libraryBuilder = new SdkLibrariesReader_LibraryBuilder(true); | 430 var libraryBuilder = new SdkLibrariesReader_LibraryBuilder(true); |
| 399 parseCompilationUnit(contents).accept(libraryBuilder); | 431 parseCompilationUnit(contents).accept(libraryBuilder); |
| 400 return libraryBuilder.librariesMap.sdkLibraries; | 432 return libraryBuilder.librariesMap.sdkLibraries; |
| 401 } | 433 } |
| OLD | NEW |