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

Commit

Permalink
SourceGen: Add flag to use source_gen from the transformer.
Browse files Browse the repository at this point in the history
This will allow external developers to experiment with the solution to #48

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=155672118
  • Loading branch information
alorenzen authored and matanlurey committed May 12, 2017
1 parent 4c35282 commit c118238
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 20 deletions.
5 changes: 4 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ linter:
- avoid_empty_else
- avoid_init_to_null
#- avoid_return_types_on_setters
- await_only_futures

# https://github.com/dart-lang/linter/issues/465
#- await_only_futures

- camel_case_types
- cancel_subscriptions
#- close_sinks
Expand Down
3 changes: 3 additions & 0 deletions lib/source_gen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
library angular2.source_gen;

export 'src/source_gen/source_gen.dart';
12 changes: 12 additions & 0 deletions lib/src/source_gen/source_gen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:angular2/src/transform/common/names.dart';
import 'package:build/build.dart';
import 'package:source_gen/source_gen.dart';

import 'template_compiler/generator.dart';
import 'template_compiler/generator_options.dart';

export 'template_compiler/generator_options.dart';

Builder createSourceGenTemplateCompiler(GeneratorOptions options) =>
new GeneratorBuilder([new TemplateGenerator(options)],
generatedExtension: TEMPLATE_EXTENSION, isStandalone: true);
11 changes: 11 additions & 0 deletions lib/src/transform/common/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const LAZY_TRANSFORMERS = 'lazy_transformers';
const TRANSLATIONS = 'translations';
const IGNORE_REAL_TEMPLATE_ISSUES_PARAM = 'ignore_real_template_issues';
const USE_LEGACY_STYLE_ENCAPSULATION = 'use_legacy_style_encapsulation';
const USE_ANALYZER = 'use_analyzer';

const CODEGEN_DEBUG_MODE = 'debug';

Expand Down Expand Up @@ -118,6 +119,13 @@ class TransformerOptions {
/// * polyfill-unscoped-rule
final bool useLegacyStyleEncapsulation;

/// Whether to use the new analyzer-based codegen.
///
/// When [true], this will use the analyzer to resolve types before running
/// the angular template compiler, resulting in sounder and better performing
/// code.
final bool useAnalyzer;

TransformerOptions._internal(
this.entryPoints,
this.entryPointGlobs,
Expand All @@ -139,6 +147,7 @@ class TransformerOptions {
this.ignoreRealTemplateIssues,
this.checkDeferredImportInitialization,
this.useLegacyStyleEncapsulation,
this.useAnalyzer,
});

factory TransformerOptions(
Expand All @@ -161,6 +170,7 @@ class TransformerOptions {
bool ignoreRealTemplateIssues: false,
bool checkDeferredImportInitialization: false,
bool useLegacyStyleEncapsulation: false,
bool useAnalyzer: false,
}) {
var annotationMatcher = new AnnotationMatcher()
..addAll(customAnnotationDescriptors);
Expand All @@ -185,6 +195,7 @@ class TransformerOptions {
ignoreRealTemplateIssues: ignoreRealTemplateIssues,
checkDeferredImportInitialization: checkDeferredImportInitialization,
useLegacyStyleEncapsulation: useLegacyStyleEncapsulation,
useAnalyzer: useAnalyzer,
);
}
}
37 changes: 20 additions & 17 deletions lib/src/transform/common/options_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ TransformerOptions parseBarbackSettings(BarbackSettings settings) {
var formatCode = _readBool(config, FORMAT_CODE_PARAM, defaultValue: false);
var useLegacyStyleEncapsulation =
_readBool(config, USE_LEGACY_STYLE_ENCAPSULATION, defaultValue: false);
var useAnalyzer = _readBool(config, USE_ANALYZER, defaultValue: false);
String mirrorModeVal =
config.containsKey(MIRROR_MODE_PARAM) ? config[MIRROR_MODE_PARAM] : '';
var mirrorMode = MirrorMode.none;
Expand All @@ -42,23 +43,25 @@ TransformerOptions parseBarbackSettings(BarbackSettings settings) {
mirrorMode = MirrorMode.none;
break;
}
var transformerOptions = new TransformerOptions(entryPoints,
modeName: settings.mode.name,
mirrorMode: mirrorMode,
initReflector: initReflector,
codegenMode: codegenMode,
customAnnotationDescriptors: _readCustomAnnotations(config),
reflectPropertiesAsAttributes: reflectPropertiesAsAttributes,
platformDirectives: platformDirectives,
platformPipes: platformPipes,
resolvedIdentifiers: resolvedIdentifiers,
errorOnMissingIdentifiers: errorOnMissingIdentifiers,
inlineViews: _readBool(config, INLINE_VIEWS_PARAM, defaultValue: false),
lazyTransformers:
_readBool(config, LAZY_TRANSFORMERS, defaultValue: false),
translations: _readAssetId(config, TRANSLATIONS),
formatCode: formatCode,
useLegacyStyleEncapsulation: useLegacyStyleEncapsulation);
var transformerOptions = new TransformerOptions(
entryPoints,
modeName: settings.mode.name,
mirrorMode: mirrorMode,
initReflector: initReflector,
codegenMode: codegenMode,
customAnnotationDescriptors: _readCustomAnnotations(config),
reflectPropertiesAsAttributes: reflectPropertiesAsAttributes,
platformDirectives: platformDirectives,
platformPipes: platformPipes,
resolvedIdentifiers: resolvedIdentifiers,
errorOnMissingIdentifiers: errorOnMissingIdentifiers,
inlineViews: _readBool(config, INLINE_VIEWS_PARAM, defaultValue: false),
lazyTransformers: _readBool(config, LAZY_TRANSFORMERS, defaultValue: false),
translations: _readAssetId(config, TRANSLATIONS),
formatCode: formatCode,
useLegacyStyleEncapsulation: useLegacyStyleEncapsulation,
useAnalyzer: useAnalyzer,
);

_checkEntryPointsExist(transformerOptions);
return transformerOptions;
Expand Down
15 changes: 14 additions & 1 deletion lib/src/transform/transformer.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:angular2/source_gen.dart';
import 'package:barback/barback.dart';
import 'package:build_barback/build_barback.dart';
import 'package:dart_style/dart_style.dart';

import 'common/eager_transformer_wrapper.dart';
Expand Down Expand Up @@ -26,7 +28,18 @@ class AngularTransformerGroup extends TransformerGroup {

factory AngularTransformerGroup(TransformerOptions options) {
Iterable<Iterable> phases;
if (options.inlineViews) {
if (options.useAnalyzer) {
phases = [
[
new BuilderTransformer(createSourceGenTemplateCompiler(
new GeneratorOptions(
codegenMode: options.codegenMode,
useLegacyStyleEncapsulation:
options.useLegacyStyleEncapsulation,
collectAssets: false)))
]
];
} else if (options.inlineViews) {
phases = [
[new InlinerForTest(options)]
];
Expand Down
16 changes: 15 additions & 1 deletion lib/transform/codegen.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
library angular2.transform.codegen.dart;

import 'package:angular2/source_gen.dart';
import 'package:angular2/src/transform/asset_consumer/transformer.dart';
import 'package:angular2/src/transform/common/eager_transformer_wrapper.dart';
import 'package:angular2/src/transform/common/formatter.dart' as formatter;
Expand All @@ -11,6 +12,7 @@ import 'package:angular2/src/transform/inliner_for_test/transformer.dart';
import 'package:angular2/src/transform/stylesheet_compiler/transformer.dart';
import 'package:angular2/src/transform/template_compiler/transformer.dart';
import 'package:barback/barback.dart';
import 'package:build_barback/build_barback.dart';
import 'package:dart_style/dart_style.dart';

export 'package:angular2/src/transform/common/options.dart';
Expand All @@ -33,7 +35,19 @@ class CodegenTransformer extends TransformerGroup {

factory CodegenTransformer(TransformerOptions options) {
Iterable<Iterable> phases;
if (options.inlineViews) {
if (options.useAnalyzer) {
phases = [
[new AssetConsumer()],
[
new BuilderTransformer(createSourceGenTemplateCompiler(
new GeneratorOptions(
codegenMode: options.codegenMode,
useLegacyStyleEncapsulation:
options.useLegacyStyleEncapsulation,
collectAssets: false)))
]
];
} else if (options.inlineViews) {
phases = [
[new InlinerForTest(options)]
];
Expand Down

0 comments on commit c118238

Please sign in to comment.