Unverified Commit 3b5668c9 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] hide usage of package:mustache behind interface (#51500)

parent 08d079f6
......@@ -6,13 +6,14 @@ import 'dart:async';
import 'runner.dart' as runner;
import 'src/base/context.dart';
import 'src/base/template.dart';
// The build_runner code generation is provided here to make it easier to
// avoid introducing the dependency into google3. Not all build* packages
// are synced internally.
import 'src/build_runner/build_runner.dart';
import 'src/build_runner/mustache_template.dart';
import 'src/build_runner/resident_web_runner.dart';
import 'src/build_runner/web_compilation_delegate.dart';
import 'src/codegen.dart';
import 'src/commands/analyze.dart';
import 'src/commands/assemble.dart';
......@@ -110,8 +111,10 @@ Future<void> main(List<String> args) async {
// the build runner packages are not synced internally.
CodeGenerator: () => const BuildRunner(),
WebCompilationProxy: () => BuildRunnerWebCompilationProxy(),
// The web runner is not supported internally because it depends
// The web runner is not supported in google3 because it depends
// on dwds.
WebRunnerFactory: () => DwdsWebRunnerFactory(),
// The mustache dependency is different in google3
TemplateRenderer: () => const MustacheTemplateRenderer(),
});
}
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/// An indirection around our mustache templating system to avoid a
/// dependency on mustache..
abstract class TemplateRenderer {
const TemplateRenderer();
String renderString(String template, dynamic context, {bool htmlEscapeValues = false});
}
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:mustache/mustache.dart';
import '../base/template.dart';
/// An indirection around mustache use to allow google3 to use a different dependency.
class MustacheTemplateRenderer extends TemplateRenderer {
const MustacheTemplateRenderer();
@override
String renderString(String template, dynamic context, {bool htmlEscapeValues = false}) {
return Template(template, htmlEscapeValues: htmlEscapeValues).renderString(context);
}
}
......@@ -8,8 +8,6 @@ import '../../asset.dart';
import '../../base/file_system.dart';
import '../../devfs.dart';
import '../../globals.dart' as globals;
import '../../plugins.dart';
import '../../project.dart';
import '../build_system.dart';
import '../depfile.dart';
import 'dart.dart';
......@@ -119,45 +117,3 @@ class CopyAssets extends Target {
);
}
}
/// Rewrites the `.flutter-plugins` file of [project] based on the plugin
/// dependencies declared in `pubspec.yaml`.
// TODO(jonahwiliams): this should be per platform and located in build
// outputs.
class FlutterPlugins extends Target {
const FlutterPlugins();
@override
String get name => 'flutter_plugins';
@override
List<Target> get dependencies => const <Target>[];
@override
List<Source> get inputs => const <Source>[
Source.pattern('{FLUTTER_ROOT}/packages/flutter_tools/lib/src/build_system/targets/assets.dart'),
Source.pattern('{PROJECT_DIR}/pubspec.yaml'),
];
@override
List<Source> get outputs => const <Source>[
Source.pattern('{PROJECT_DIR}/.flutter-plugins'),
];
@override
Future<void> build(Environment environment) async {
// The pubspec may change for reasons other than plugins changing, so we compare
// the manifest before writing. Some hosting build systems use timestamps
// so we need to be careful to avoid tricking them into doing more work than
// necessary.
final FlutterProject project = FlutterProject.fromDirectory(environment.projectDir);
final List<Plugin> plugins = findPlugins(project);
final String pluginManifest = plugins
.map<String>((Plugin p) => '${p.name}=${globals.fsUtils.escapePath(p.path)}')
.join('\n');
final File flutterPluginsFile = environment.projectDir.childFile('.flutter-plugins');
if (!flutterPluginsFile.existsSync() || flutterPluginsFile.readAsStringSync() != pluginManifest) {
flutterPluginsFile.writeAsStringSync(pluginManifest);
}
}
}
......@@ -17,6 +17,7 @@ import 'base/io.dart';
import 'base/logger.dart';
import 'base/net.dart';
import 'base/os.dart';
import 'base/template.dart';
import 'base/terminal.dart';
import 'base/user_messages.dart';
import 'cache.dart';
......@@ -160,3 +161,6 @@ PlistParser _defaultInstance;
/// The [ChromeLauncher] instance.
ChromeLauncher get chromeLauncher => context.get<ChromeLauncher>();
/// The global template renderer
TemplateRenderer get templateRenderer => context.get<TemplateRenderer>();
......@@ -5,7 +5,6 @@
import 'dart:async';
import 'package:meta/meta.dart';
import 'package:mustache/mustache.dart' as mustache;
import 'package:yaml/yaml.dart';
import 'android/gradle.dart';
......@@ -23,8 +22,8 @@ import 'windows/property_sheet.dart';
import 'windows/visual_studio_solution_utils.dart';
void _renderTemplateToFile(String template, dynamic context, String filePath) {
final String renderedTemplate =
mustache.Template(template, htmlEscapeValues: false).renderString(context);
final String renderedTemplate = globals.templateRenderer
.renderString(template, context, htmlEscapeValues: false);
final File file = globals.fs.file(filePath);
file.createSync(recursive: true);
file.writeAsStringSync(renderedTemplate);
......
......@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:mustache/mustache.dart' as mustache;
import 'base/common.dart';
import 'base/file_system.dart';
import 'cache.dart';
......@@ -182,7 +180,7 @@ class Template {
if (sourceFile.path.endsWith(templateExtension)) {
final String templateContents = sourceFile.readAsStringSync();
final String renderedContents = mustache.Template(templateContents).renderString(context);
final String renderedContents = globals.templateRenderer.renderString(templateContents, context);
finalDestinationFile.writeAsStringSync(renderedContents);
......
......@@ -90,18 +90,4 @@ flutter:
ProcessManager: () => FakeProcessManager.any(),
Platform: () => platform,
});
testUsingContext('FlutterPlugins updates required files as needed', () async {
fileSystem.file('pubspec.yaml')
..writeAsStringSync('name: foo\ndependencies:\n foo: any\n');
await const FlutterPlugins().build(Environment.test(
fileSystem.currentDirectory,
));
expect(fileSystem.file('.flutter-plugins'), exists);
}, overrides: <Type, Generator>{
FileSystem: () => fileSystem,
ProcessManager: () => FakeProcessManager.any(),
});
}
......@@ -14,8 +14,10 @@ import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/os.dart';
import 'package:flutter_tools/src/base/process.dart';
import 'package:flutter_tools/src/base/signals.dart';
import 'package:flutter_tools/src/base/template.dart';
import 'package:flutter_tools/src/base/terminal.dart';
import 'package:flutter_tools/src/base/time.dart';
import 'package:flutter_tools/src/build_runner/mustache_template.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/context_runner.dart';
import 'package:flutter_tools/src/dart/pub.dart';
......@@ -129,6 +131,7 @@ void testUsingContext(
Signals: () => FakeSignals(),
Pub: () => ThrowingPub(), // prevent accidentally using pub.
GitHubTemplateCreator: () => MockGitHubTemplateCreator(),
TemplateRenderer: () => const MustacheTemplateRenderer(),
},
body: () {
final String flutterRoot = getFlutterRoot();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment