Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

Commit

Permalink
Drop global variable hack
Browse files Browse the repository at this point in the history
Add TemplatePlaceholderBuilder which only creates a `.ng_placeholder`
Replace the collection of input assets in a global variable with a check for
the placeholder file.

This also removes the last usages of the old "transformer" internally, so we no longer generate .ng_meta.json and .ng_summary.json, both reducing the number of files per target by 1 and also improving the speed of compiles.

Closes #390.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=158060754
  • Loading branch information
alorenzen authored and matanlurey committed Jun 5, 2017
1 parent 9201a89 commit a0db89e
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 32 deletions.
1 change: 1 addition & 0 deletions lib/src/source_gen/source_gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:source_gen/source_gen.dart';
import 'template_compiler/generator.dart';
import 'template_compiler/generator_options.dart';

export 'template_compiler/generator.dart' show TemplatePlaceholderBuilder;
export 'template_compiler/generator_options.dart';

Builder createSourceGenTemplateCompiler(GeneratorOptions options) =>
Expand Down
28 changes: 19 additions & 9 deletions lib/src/source_gen/template_compiler/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ class TemplateGenerator extends Generator {

@override
Future<String> generate(Element element, BuildStep buildStep) async {
// Because buildStep.hasInput() does not currently work as needed, we
// instead track the inputs ourselves. Thus, we await for all of the inputs
// to be collected, before continuing. This only works because of
// implementation details in package:build which will probably change, so it
// is recommended that you do not copy this.
assets.add(buildStep.inputId);
await new Future(() {});

if (element is! LibraryElement) return null;
return runZoned(() async {
var config = new CompilerConfig(
Expand All @@ -44,7 +36,7 @@ class TemplateGenerator extends Generator {
useLegacyStyleEncapsulation: _options.useLegacyStyleEncapsulation,
profileType: codegenModeToProfileType(_options.codegenMode));
var outputs = await processTemplates(element, buildStep, config,
collectAssets: _options.collectAssets);
usePlaceholder: _options.usePlaceholder);
if (outputs == null) return _emptyNgDepsContents;
return buildGeneratedCode(
outputs,
Expand All @@ -64,3 +56,21 @@ class TemplateGenerator extends Generator {

final String _emptyNgDepsContents = prettyToSource(
new MethodBuilder.returnVoid('initReflector').buildTopLevelAst());

/// Generates an empty `.ng_placeholder` file which is used as a signal to later
/// builders which files will eventually get `.template.dart` generated.
class TemplatePlaceholderBuilder implements Builder {
const TemplatePlaceholderBuilder();

@override
final buildExtensions = const {
'.dart': const ['.ng_placeholder']
};

@override
Future build(BuildStep buildStep) {
buildStep.writeAsString(
buildStep.inputId.changeExtension('.ng_placeholder'), '');
return new Future.value(null);
}
}
8 changes: 4 additions & 4 deletions lib/src/source_gen/template_compiler/generator_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ class GeneratorOptions {
/// * polyfill-unscoped-rule
final bool useLegacyStyleEncapsulation;

/// Whether to use the global set of visible assets instead of
/// buildStep.hasInput().
/// Whether to use the .ng_placeholder file instead of just the .dart file for
/// checks to buildStep.hasInput().
///
/// For bazel workspaces, this should be [true] (default), since
/// buildStep.hasInput() doesn't work in all cases. In other workspaces, like
/// with build_runner, this should be [false].
final bool collectAssets;
final bool usePlaceholder;

GeneratorOptions({
this.reflectPropertiesAsAttributes: false,
this.codegenMode: '',
this.useLegacyStyleEncapsulation: false,
this.collectAssets: true,
this.usePlaceholder: true,
});
}
2 changes: 1 addition & 1 deletion lib/src/source_gen/template_compiler/ng_deps_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import 'compile_metadata.dart';
/// system) are required.
Future<NgDepsModel> resolveNgDepsFor(
LibraryElement library, {
@required Future<bool> hasInput(String uri),
@required FutureOr<bool> hasInput(String uri),
@required bool isLibrary(String uri),
}) async {
// Visit and find all 'reflectables'.
Expand Down
23 changes: 11 additions & 12 deletions lib/src/source_gen/template_compiler/template_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,9 @@ import 'find_components.dart';
import 'ng_deps_visitor.dart';
import 'template_compiler_outputs.dart';

// Because buildStep.canRead() does not currently work as needed, we instead
// track the inputs ourselves. Thus, we await for all of the inputs to be
// collected, before continuing. This only works because of implementation
// details in package:build which will probably change, so it is recommended
// that you do not copy this.
final Set<AssetId> assets = new Set();

Future<TemplateCompilerOutputs> processTemplates(
LibraryElement element, BuildStep buildStep, CompilerConfig config,
{bool collectAssets: true}) async {
{bool usePlaceholder: true}) async {
final templateCompiler = createTemplateCompiler(buildStep, config);
final resolver = await buildStep.resolver;
final ngDepsModel = await logElapsedAsync(
Expand All @@ -34,10 +27,16 @@ Future<TemplateCompilerOutputs> processTemplates(
// a.dart, and a.dart imports b.dart, we can assume that there will be
// a generated b.template.dart that we need to import/initReflector().
hasInput: (uri) async {
final asset = new AssetId.resolve(uri, from: buildStep.inputId);
return collectAssets
? assets.contains(asset)
: await buildStep.canRead(asset);
if (usePlaceholder) {
final placeholder = ''
'${uri.substring(0, uri.length - '.dart'.length)}'
'.ng_placeholder';
return await buildStep.canRead(
new AssetId.resolve(placeholder, from: buildStep.inputId));
} else {
return await buildStep
.canRead(new AssetId.resolve(uri, from: buildStep.inputId));
}
},
// For a given import or export directive, return whether a generated
// .template.dart file already exists. If it does we will need to link
Expand Down
2 changes: 2 additions & 0 deletions lib/src/transform/common/names.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const ALL_EXTENSIONS = const [
META_EXTENSION,
SUMMARY_META_EXTENSION,
TEMPLATE_EXTENSION,
'.ng_placeholder',
'.dart'
];

Expand All @@ -40,6 +41,7 @@ bool isGenerated(String uri) {
SHIMMED_STYLESHEET_EXTENSION,
SUMMARY_META_EXTENSION,
TEMPLATE_EXTENSION,
'.ng_placeholder',
].any((ext) => uri.endsWith(ext));
}

Expand Down
7 changes: 5 additions & 2 deletions lib/src/transform/transformer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ class AngularTransformerGroup extends TransformerGroup {
'using the Dart analyzer. See https://goo.gl/68VhMa for details.');
}
phases = [
[new BuilderTransformer(new TemplatePlaceholderBuilder())],
[new ReflectionRemover(options)],
[
new DeferredRewriter(),
new StylesheetCompiler(options),
new BuilderTransformer(createSourceGenTemplateCompiler(
new GeneratorOptions(
codegenMode: options.codegenMode,
useLegacyStyleEncapsulation:
options.useLegacyStyleEncapsulation,
collectAssets: false)))
options.useLegacyStyleEncapsulation)))
]
];
} else if (options.inlineViews) {
Expand Down
5 changes: 3 additions & 2 deletions lib/transform/codegen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ class CodegenTransformer extends TransformerGroup {
}
phases = [
[new AssetConsumer()],
[new BuilderTransformer(new TemplatePlaceholderBuilder())],
[
new StylesheetCompiler(options),
new BuilderTransformer(createSourceGenTemplateCompiler(
new GeneratorOptions(
codegenMode: options.codegenMode,
useLegacyStyleEncapsulation:
options.useLegacyStyleEncapsulation,
collectAssets: false)))
options.useLegacyStyleEncapsulation)))
]
];
} else if (options.inlineViews) {
Expand Down
4 changes: 2 additions & 2 deletions test/source_gen/template_compiler/generate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Future main(List<String> args) async {
..addAction(
new GeneratorBuilder([
new TemplateGenerator(new GeneratorOptions(
codegenMode: 'release', collectAssets: false))
codegenMode: 'release', usePlaceholder: false))
],
generatedExtension: updateGoldens
? '.template_release.golden'
Expand All @@ -38,7 +38,7 @@ Future main(List<String> args) async {
..addAction(
new GeneratorBuilder([
new TemplateGenerator(new GeneratorOptions(
codegenMode: 'debug', collectAssets: false))
codegenMode: 'debug', usePlaceholder: false))
],
generatedExtension: updateGoldens
? '.template_debug.golden'
Expand Down

0 comments on commit a0db89e

Please sign in to comment.