| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library dev_compiler.src.codegen.code_generator; | 5 library dev_compiler.src.codegen.code_generator; |
| 6 | 6 |
| 7 import 'dart:io'; | |
| 8 | |
| 9 import 'package:analyzer/src/generated/ast.dart' show CompilationUnit; | |
| 10 import 'package:analyzer/src/generated/element.dart' | 7 import 'package:analyzer/src/generated/element.dart' |
| 11 show CompilationUnitElement, LibraryElement; | 8 show CompilationUnitElement, LibraryElement; |
| 12 import 'package:analyzer/src/generated/engine.dart' show AnalysisContext; | 9 import 'package:analyzer/src/generated/engine.dart' show AnalysisContext; |
| 13 import 'package:path/path.dart' as path; | 10 import 'package:path/path.dart' as path; |
| 14 | 11 |
| 15 import 'package:dev_compiler/devc.dart' show AbstractCompiler; | 12 import 'package:dev_compiler/src/compiler.dart' show AbstractCompiler; |
| 16 import 'package:dev_compiler/src/info.dart'; | 13 import 'package:dev_compiler/src/info.dart'; |
| 17 import 'package:dev_compiler/src/utils.dart' show canonicalLibraryName; | 14 import 'package:dev_compiler/src/utils.dart' show canonicalLibraryName; |
| 18 import 'package:dev_compiler/src/checker/rules.dart'; | 15 import 'package:dev_compiler/src/checker/rules.dart'; |
| 19 import 'package:dev_compiler/src/options.dart' show CodegenOptions; | 16 import 'package:dev_compiler/src/options.dart' show CodegenOptions; |
| 20 | 17 |
| 21 abstract class CodeGenerator { | 18 abstract class CodeGenerator { |
| 22 final AbstractCompiler compiler; | 19 final AbstractCompiler compiler; |
| 23 final String outDir; | |
| 24 final Uri root; | |
| 25 final TypeRules rules; | 20 final TypeRules rules; |
| 26 final AnalysisContext context; | 21 final AnalysisContext context; |
| 27 final CodegenOptions options; | 22 final CodegenOptions options; |
| 28 | 23 |
| 29 CodeGenerator(AbstractCompiler compiler) | 24 CodeGenerator(AbstractCompiler compiler) |
| 30 : compiler = compiler, | 25 : compiler = compiler, |
| 31 outDir = path.absolute(compiler.options.codegenOptions.outputDir), | |
| 32 root = compiler.entryPointUri, | |
| 33 rules = compiler.rules, | 26 rules = compiler.rules, |
| 34 context = compiler.context, | 27 context = compiler.context, |
| 35 options = compiler.options.codegenOptions; | 28 options = compiler.options.codegenOptions; |
| 36 | 29 |
| 37 /// Return a hash, if any, that can be used for caching purposes. When two | 30 /// Return a hash, if any, that can be used for caching purposes. When two |
| 38 /// invocations to this function return the same hash, the underlying | 31 /// invocations to this function return the same hash, the underlying |
| 39 /// code-generator generated the same code. | 32 /// code-generator generated the same code. |
| 40 String generateLibrary(LibraryUnit unit, LibraryInfo info); | 33 String generateLibrary(LibraryUnit unit); |
| 41 | 34 |
| 42 static List<String> _searchPaths = () { | 35 static List<String> _searchPaths = () { |
| 43 // TODO(vsm): Can we remove redundancy with multi_package_resolver logic? | 36 // TODO(vsm): Can we remove redundancy with multi_package_resolver logic? |
| 44 var packagePaths = | 37 var packagePaths = |
| 45 new String.fromEnvironment('package_paths', defaultValue: null); | 38 new String.fromEnvironment('package_paths', defaultValue: null); |
| 46 if (packagePaths == null) return null; | 39 if (packagePaths == null) return null; |
| 47 var list = packagePaths.split(','); | 40 var list = packagePaths.split(','); |
| 48 // Normalize the paths. | 41 // Normalize the paths. |
| 49 list = new List<String>.from(list.map(_dirToPrefix)); | 42 list = new List<String>.from(list.map(_dirToPrefix)); |
| 50 // The current directory is implicitly in the search path. | 43 // The current directory is implicitly in the search path. |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 suffix = resolvedDir.substring(prefix.length); | 100 suffix = resolvedDir.substring(prefix.length); |
| 108 break; | 101 break; |
| 109 } | 102 } |
| 110 } | 103 } |
| 111 } | 104 } |
| 112 assert(suffix != null); | 105 assert(suffix != null); |
| 113 assert(path.isRelative(suffix)); | 106 assert(path.isRelative(suffix)); |
| 114 return _convertIfPackage(suffix); | 107 return _convertIfPackage(suffix); |
| 115 } | 108 } |
| 116 | 109 |
| 117 String makeOutputDirectory(LibraryInfo info, CompilationUnit unit) { | |
| 118 var suffix = _getOutputDirectory(info.name, unit.element); | |
| 119 var fileDir = path.join(outDir, suffix); | |
| 120 var dir = new Directory(fileDir); | |
| 121 if (!dir.existsSync()) { | |
| 122 dir.createSync(recursive: true); | |
| 123 } | |
| 124 return fileDir; | |
| 125 } | |
| 126 | |
| 127 static Uri uriFor(LibraryElement lib) { | 110 static Uri uriFor(LibraryElement lib) { |
| 128 var unitElement = lib.definingCompilationUnit; | 111 var unitElement = lib.definingCompilationUnit; |
| 129 var uri = unitElement.source.uri; | 112 var uri = unitElement.source.uri; |
| 130 if (uri.scheme == 'dart') return uri; | 113 if (uri.scheme == 'dart') return uri; |
| 131 if (uri.scheme == 'package') return uri; | 114 if (uri.scheme == 'package') return uri; |
| 132 var suffix = _getOutputDirectory(canonicalLibraryName(lib), unitElement); | 115 var suffix = _getOutputDirectory(canonicalLibraryName(lib), unitElement); |
| 133 suffix = path.join(suffix, uri.pathSegments.last); | 116 suffix = path.join(suffix, uri.pathSegments.last); |
| 134 var parts = path.split(suffix); | 117 var parts = path.split(suffix); |
| 135 var index = parts.indexOf('packages'); | 118 var index = parts.indexOf('packages'); |
| 136 if (index < 0) { | 119 if (index < 0) { |
| 137 // Not a package. | 120 // Not a package. |
| 138 // TODO(leafp) These may need to be adjusted | 121 // TODO(leafp) These may need to be adjusted |
| 139 // relative to the import location | 122 // relative to the import location |
| 140 return new Uri(path: suffix); | 123 return new Uri(path: suffix); |
| 141 } | 124 } |
| 142 assert(index == 0); | 125 assert(index == 0); |
| 143 return new Uri( | 126 return new Uri( |
| 144 scheme: 'package', path: path.joinAll(parts.sublist(index + 1))); | 127 scheme: 'package', path: path.joinAll(parts.sublist(index + 1))); |
| 145 } | 128 } |
| 146 } | 129 } |
| OLD | NEW |