| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
| 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. | |
| 4 | |
| 5 library immic.compiler; | |
| 6 | |
| 7 import 'dart:io'; | |
| 8 import 'dart:convert'; | |
| 9 | |
| 10 import 'package:path/path.dart' as p; | |
| 11 | |
| 12 import 'src/emitter.dart'; | |
| 13 import 'src/importer.dart'; | |
| 14 import 'src/parser.dart'; | |
| 15 import 'src/resolver.dart'; | |
| 16 | |
| 17 import 'src/plugins/idl.dart' as idl; | |
| 18 import 'src/plugins/dart.dart' as dart; | |
| 19 import 'src/plugins/objc.dart' as objc; | |
| 20 import 'src/plugins/java.dart' as java; | |
| 21 | |
| 22 import 'package:path/path.dart' show join, dirname; | |
| 23 | |
| 24 import 'package:package_config/discovery.dart'; | |
| 25 | |
| 26 const List<String> RESOURCES = const [ | |
| 27 "Immi.podspec", | |
| 28 ]; | |
| 29 | |
| 30 class ImportResolverWithPackageFile implements ImportResolver<String> { | |
| 31 final String packagesFile; | |
| 32 final Set<String> visited = new Set(); | |
| 33 Packages packages; | |
| 34 | |
| 35 ImportResolverWithPackageFile(this.packagesFile); | |
| 36 | |
| 37 Future<Null> loadFile() async { | |
| 38 if (packagesFile == null) return; | |
| 39 packages = await loadPackagesFile( | |
| 40 new Uri(scheme: "file", path: packagesFile)); | |
| 41 } | |
| 42 | |
| 43 Future<String> read(String importPath) async { | |
| 44 List<int> bytes = new File(importPath).readAsBytesSync(); | |
| 45 return UTF8.decode(bytes); | |
| 46 } | |
| 47 | |
| 48 String resolve(Import import, String unitPath) { | |
| 49 String importPath; | |
| 50 if (import.package != null) { | |
| 51 if (packages == null) { | |
| 52 throw "Unable to import from packages. " | |
| 53 + "Please specify a file with --packages"; | |
| 54 } | |
| 55 importPath = packages.resolve(pkg(import.package, import.path)).path; | |
| 56 } else { | |
| 57 importPath = p.join(p.dirname(unitPath), import.path); | |
| 58 } | |
| 59 // TODO(zerny): Canonicalize unit paths. | |
| 60 if (visited.contains(importPath)) return null; | |
| 61 visited.add(importPath); | |
| 62 return importPath; | |
| 63 } | |
| 64 | |
| 65 Uri pkg(String packageName, String packagePath) { | |
| 66 var path; | |
| 67 if (packagePath.startsWith('/')) { | |
| 68 path = "$packageName$packagePath"; | |
| 69 } else { | |
| 70 path = "$packageName/$packagePath"; | |
| 71 } | |
| 72 return new Uri(scheme: "package", path: path); | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 void compile(String path, | |
| 77 String outputDirectory, | |
| 78 String packagesFile) async { | |
| 79 List<int> bytes = new File(path).readAsBytesSync(); | |
| 80 String input = UTF8.decode(bytes); | |
| 81 Unit topUnit = parseUnit(input); | |
| 82 var importResolver = new ImportResolverWithPackageFile(packagesFile); | |
| 83 await importResolver.loadFile(); | |
| 84 Map<String, Unit> units = await parseImports(topUnit, importResolver, path); | |
| 85 | |
| 86 resolve(units); | |
| 87 | |
| 88 dart.generate(path, units, outputDirectory); | |
| 89 idl.generate(path, units, outputDirectory); | |
| 90 objc.generate(path, units, outputDirectory); | |
| 91 java.generate(path, units, outputDirectory); | |
| 92 | |
| 93 String resourcesDirectory = join(dirname(Platform.script.path), | |
| 94 '..', 'lib', 'src', 'resources'); | |
| 95 for (String resource in RESOURCES) { | |
| 96 String resourcePath = join(resourcesDirectory, resource); | |
| 97 File file = new File(resourcePath); | |
| 98 String contents = file.readAsStringSync(); | |
| 99 writeToFile(outputDirectory, resource, contents); | |
| 100 } | |
| 101 } | |
| OLD | NEW |