diff --git a/packages/flutter_tools/lib/src/ios/application_package.dart b/packages/flutter_tools/lib/src/ios/application_package.dart index 514b7d6798..c760492cd4 100644 --- a/packages/flutter_tools/lib/src/ios/application_package.dart +++ b/packages/flutter_tools/lib/src/ios/application_package.dart @@ -191,7 +191,7 @@ class BuildableIOSApp extends IOSApp { // Template asset's images are in flutter_template_images package. Future _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)); } diff --git a/packages/flutter_tools/lib/src/template.dart b/packages/flutter_tools/lib/src/template.dart index 19a8c948a7..b86046ae94 100644 --- a/packages/flutter_tools/lib/src/template.dart +++ b/packages/flutter_tools/lib/src/template.dart @@ -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 kReservedKotlinKeywords = ['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 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() ?? 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._( [templateDir], [imageDir], @@ -122,12 +155,12 @@ class Template { return Template._( [ for (final String name in names) - _templateDirectoryInPackage(name, fileSystem), + templatePathProvider.directoryInPackage(name, fileSystem), ], [ 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 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 segments = androidIdentifier.split('.'); final List correctedSegments = segments.map( diff --git a/packages/flutter_tools/test/general.shard/template_test.dart b/packages/flutter_tools/test/general.shard/template_test.dart index 4f3cc55334..fa4bf6823d 100644 --- a/packages/flutter_tools/test/general.shard/template_test.dart +++ b/packages/flutter_tools/test/general.shard/template_test.dart @@ -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',