| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library serialization.elements; |
| 6 |
| 7 import 'dart:convert'; |
| 8 |
| 9 import 'package:analyzer/src/generated/source.dart'; |
| 10 import 'package:analyzer/src/summary/api_signature.dart'; |
| 11 import 'package:analyzer/src/summary/format.dart'; |
| 12 import 'package:analyzer/src/summary/idl.dart'; |
| 13 import 'package:analyzer/src/summary/package_bundle_reader.dart'; |
| 14 import 'package:convert/convert.dart'; |
| 15 import 'package:crypto/crypto.dart'; |
| 16 |
| 17 /** |
| 18 * Object that gathers information uses it to assemble a new |
| 19 * [PackageBundleBuilder]. |
| 20 */ |
| 21 class PackageBundleAssembler { |
| 22 /** |
| 23 * Value that will be stored in [PackageBundle.majorVersion] for any summaries |
| 24 * created by this code. When making a breaking change to the summary format, |
| 25 * this value should be incremented by 1 and [currentMinorVersion] should be |
| 26 * reset to zero. |
| 27 */ |
| 28 static const int currentMajorVersion = 1; |
| 29 |
| 30 /** |
| 31 * Value that will be stored in [PackageBundle.minorVersion] for any summaries |
| 32 * created by this code. When making a non-breaking change to the summary |
| 33 * format that clients might need to be aware of (such as adding a kind of |
| 34 * data that was previously not summarized), this value should be incremented |
| 35 * by 1. |
| 36 */ |
| 37 static const int currentMinorVersion = 0; |
| 38 |
| 39 final List<String> _linkedLibraryUris = <String>[]; |
| 40 final List<LinkedLibraryBuilder> _linkedLibraries = <LinkedLibraryBuilder>[]; |
| 41 final List<String> _unlinkedUnitUris = <String>[]; |
| 42 final List<UnlinkedUnitBuilder> _unlinkedUnits = <UnlinkedUnitBuilder>[]; |
| 43 final Map<String, UnlinkedUnitBuilder> _unlinkedUnitMap = |
| 44 <String, UnlinkedUnitBuilder>{}; |
| 45 final List<String> _unlinkedUnitHashes; |
| 46 final List<PackageDependencyInfoBuilder> _dependencies = |
| 47 <PackageDependencyInfoBuilder>[]; |
| 48 final bool _excludeHashes; |
| 49 |
| 50 /** |
| 51 * Create a [PackageBundleAssembler]. If [excludeHashes] is `true`, hash |
| 52 * computation will be skipped. |
| 53 */ |
| 54 PackageBundleAssembler({bool excludeHashes: false}) |
| 55 : _excludeHashes = excludeHashes, |
| 56 _unlinkedUnitHashes = excludeHashes ? null : <String>[]; |
| 57 |
| 58 void addLinkedLibrary(String uri, LinkedLibraryBuilder library) { |
| 59 _linkedLibraries.add(library); |
| 60 _linkedLibraryUris.add(uri); |
| 61 } |
| 62 |
| 63 void addUnlinkedUnit(Source source, UnlinkedUnitBuilder unit) { |
| 64 addUnlinkedUnitWithHash(source.uri.toString(), unit, |
| 65 _excludeHashes ? null : _hash(source.contents.data)); |
| 66 } |
| 67 |
| 68 void addUnlinkedUnitWithHash( |
| 69 String uri, UnlinkedUnitBuilder unit, String hash) { |
| 70 _unlinkedUnitUris.add(uri); |
| 71 _unlinkedUnits.add(unit); |
| 72 _unlinkedUnitMap[uri] = unit; |
| 73 _unlinkedUnitHashes?.add(hash); |
| 74 } |
| 75 |
| 76 /** |
| 77 * Assemble a new [PackageBundleBuilder] using the gathered information. |
| 78 */ |
| 79 PackageBundleBuilder assemble() { |
| 80 return new PackageBundleBuilder( |
| 81 linkedLibraryUris: _linkedLibraryUris, |
| 82 linkedLibraries: _linkedLibraries, |
| 83 unlinkedUnitUris: _unlinkedUnitUris, |
| 84 unlinkedUnits: _unlinkedUnits, |
| 85 unlinkedUnitHashes: _unlinkedUnitHashes, |
| 86 majorVersion: currentMajorVersion, |
| 87 minorVersion: currentMinorVersion, |
| 88 dependencies: _dependencies, |
| 89 apiSignature: _computeApiSignature()); |
| 90 } |
| 91 |
| 92 /** |
| 93 * Use the dependency information in [summaryDataStore] to populate the |
| 94 * dependencies in the package bundle being assembled. |
| 95 */ |
| 96 void recordDependencies(SummaryDataStore summaryDataStore) { |
| 97 _dependencies.addAll(summaryDataStore.dependencies); |
| 98 } |
| 99 |
| 100 /** |
| 101 * Compute the API signature for this package bundle. |
| 102 */ |
| 103 String _computeApiSignature() { |
| 104 ApiSignature apiSignature = new ApiSignature(); |
| 105 for (String unitUri in _unlinkedUnitMap.keys.toList()..sort()) { |
| 106 apiSignature.addString(unitUri); |
| 107 _unlinkedUnitMap[unitUri].collectApiSignature(apiSignature); |
| 108 } |
| 109 return apiSignature.toHex(); |
| 110 } |
| 111 |
| 112 /** |
| 113 * Compute a hash of the given file contents. |
| 114 */ |
| 115 String _hash(String contents) { |
| 116 return hex.encode(md5.convert(UTF8.encode(contents)).bytes); |
| 117 } |
| 118 } |
| OLD | NEW |