Unverified Commit d5a0fcd5 authored by Lau Ching Jun's avatar Lau Ching Jun Committed by GitHub

Locate the template directory using a TemplatePathProvider. (#132156)

So that the paths can be overridden.
parent 547bdefb
......@@ -191,7 +191,7 @@ class BuildableIOSApp extends IOSApp {
// Template asset's images are in flutter_template_images package.
Future<String> _templateImageAssetDirNameForImages(String asset) async {
final Directory imageTemplate = await templateImageDirectory(null, globals.fs, globals.logger);
final Directory imageTemplate = await templatePathProvider.imageDirectory(null, globals.fs, globals.logger);
return globals.fs.path.join(imageTemplate.path, _templateImageAssetDirNameSuffix(asset));
}
......
......@@ -7,6 +7,7 @@ import 'package:package_config/package_config.dart';
import 'package:package_config/package_config_types.dart';
import 'base/common.dart';
import 'base/context.dart';
import 'base/file_system.dart';
import 'base/logger.dart';
import 'base/template.dart';
......@@ -19,6 +20,38 @@ import 'dart/package_map.dart';
/// https://kotlinlang.org/docs/keyword-reference.html
const List<String> kReservedKotlinKeywords = <String>['when', 'in', 'is'];
/// Provides the path where templates used by flutter_tools are stored.
class TemplatePathProvider {
const TemplatePathProvider();
/// Returns the directory containing the 'name' template directory.
Directory directoryInPackage(String name, FileSystem fileSystem) {
final String templatesDir = fileSystem.path.join(Cache.flutterRoot!,
'packages', 'flutter_tools', 'templates');
return fileSystem.directory(fileSystem.path.join(templatesDir, name));
}
/// Returns the directory containing the 'name' template directory in
/// flutter_template_images, to resolve image placeholder against.
/// if 'name' is null, return the parent template directory.
Future<Directory> imageDirectory(String? name, FileSystem fileSystem, Logger logger) async {
final String toolPackagePath = fileSystem.path.join(
Cache.flutterRoot!, 'packages', 'flutter_tools');
final String packageFilePath = fileSystem.path.join(toolPackagePath, '.dart_tool', 'package_config.json');
final PackageConfig packageConfig = await loadPackageConfigWithLogging(
fileSystem.file(packageFilePath),
logger: logger,
);
final Uri? imagePackageLibDir = packageConfig['flutter_template_images']?.packageUriRoot;
final Directory templateDirectory = fileSystem.directory(imagePackageLibDir)
.parent
.childDirectory('templates');
return name == null ? templateDirectory : templateDirectory.childDirectory(name);
}
}
TemplatePathProvider get templatePathProvider => context.get<TemplatePathProvider>() ?? const TemplatePathProvider();
/// Expands templates in a directory to a destination. All files that must
/// undergo template expansion should end with the '.tmpl' extension. All files
/// that should be replaced with the corresponding image from
......@@ -100,8 +133,8 @@ class Template {
required TemplateRenderer templateRenderer,
}) async {
// All named templates are placed in the 'templates' directory
final Directory templateDir = _templateDirectoryInPackage(name, fileSystem);
final Directory imageDir = await templateImageDirectory(name, fileSystem, logger);
final Directory templateDir = templatePathProvider.directoryInPackage(name, fileSystem);
final Directory imageDir = await templatePathProvider.imageDirectory(name, fileSystem, logger);
return Template._(
<Directory>[templateDir],
<Directory>[imageDir],
......@@ -122,12 +155,12 @@ class Template {
return Template._(
<Directory>[
for (final String name in names)
_templateDirectoryInPackage(name, fileSystem),
templatePathProvider.directoryInPackage(name, fileSystem),
],
<Directory>[
for (final String name in names)
if ((await templateImageDirectory(name, fileSystem, logger)).existsSync())
await templateImageDirectory(name, fileSystem, logger),
if ((await templatePathProvider.imageDirectory(name, fileSystem, logger)).existsSync())
await templatePathProvider.imageDirectory(name, fileSystem, logger),
],
fileSystem: fileSystem,
logger: logger,
......@@ -344,30 +377,6 @@ class Template {
}
}
Directory _templateDirectoryInPackage(String name, FileSystem fileSystem) {
final String templatesDir = fileSystem.path.join(Cache.flutterRoot!,
'packages', 'flutter_tools', 'templates');
return fileSystem.directory(fileSystem.path.join(templatesDir, name));
}
/// Returns the directory containing the 'name' template directory in
/// flutter_template_images, to resolve image placeholder against.
/// if 'name' is null, return the parent template directory.
Future<Directory> templateImageDirectory(String? name, FileSystem fileSystem, Logger logger) async {
final String toolPackagePath = fileSystem.path.join(
Cache.flutterRoot!, 'packages', 'flutter_tools');
final String packageFilePath = fileSystem.path.join(toolPackagePath, '.dart_tool', 'package_config.json');
final PackageConfig packageConfig = await loadPackageConfigWithLogging(
fileSystem.file(packageFilePath),
logger: logger,
);
final Uri? imagePackageLibDir = packageConfig['flutter_template_images']?.packageUriRoot;
final Directory templateDirectory = fileSystem.directory(imagePackageLibDir)
.parent
.childDirectory('templates');
return name == null ? templateDirectory : templateDirectory.childDirectory(name);
}
String _escapeKotlinKeywords(String androidIdentifier) {
final List<String> segments = androidIdentifier.split('.');
final List<String> correctedSegments = segments.map(
......
......@@ -51,8 +51,9 @@ void main() {
FileSystem: () => MemoryFileSystem.test(),
ProcessManager: () => FakeProcessManager.any(),
};
const TemplatePathProvider templatePathProvider = TemplatePathProvider();
testUsingContext('templateImageDirectory returns parent template directory if passed null name', () async {
testUsingContext('templatePathProvider.imageDirectory returns parent template directory if passed null name', () async {
final String packageConfigPath = globals.fs.path.join(
Cache.flutterRoot!,
'packages',
......@@ -77,7 +78,7 @@ void main() {
}
''');
expect(
(await templateImageDirectory(null, globals.fs, globals.logger)).path,
(await templatePathProvider.imageDirectory(null, globals.fs, globals.logger)).path,
globals.fs.path.absolute(
'flutter_template_images',
'templates',
......@@ -85,7 +86,7 @@ void main() {
);
}, overrides: overrides);
testUsingContext('templateImageDirectory returns the directory containing the `name` template directory', () async {
testUsingContext('templatePathProvider.imageDirectory returns the directory containing the `name` template directory', () async {
final String packageConfigPath = globals.fs.path.join(
Cache.flutterRoot!,
'packages',
......@@ -109,7 +110,7 @@ void main() {
}
''');
expect(
(await templateImageDirectory('app_shared', globals.fs, globals.logger)).path,
(await templatePathProvider.imageDirectory('app_shared', globals.fs, globals.logger)).path,
globals.fs.path.absolute(
'flutter_template_images',
'templates',
......
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