template.dart 16.8 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:file/file.dart';
6 7
import 'package:package_config/package_config.dart';
import 'package:package_config/package_config_types.dart';
8

9
import 'base/common.dart';
10
import 'base/context.dart';
11
import 'base/file_system.dart';
12 13
import 'base/logger.dart';
import 'base/template.dart';
14
import 'cache.dart';
15
import 'dart/package_map.dart';
16

17 18 19 20
/// The Kotlin keywords which are not Java keywords.
/// They are escaped in Kotlin files.
///
/// https://kotlinlang.org/docs/keyword-reference.html
21
const List<String> kReservedKotlinKeywords = <String>['when', 'in', 'is'];
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/// 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();

55
/// Expands templates in a directory to a destination. All files that must
56 57 58
/// undergo template expansion should end with the '.tmpl' extension. All files
/// that should be replaced with the corresponding image from
/// flutter_template_images should end with the '.img.tmpl' extension. All other
59 60
/// files are ignored. In case the contents of entire directories must be copied
/// as is, the directory itself can end with '.tmpl' extension. Files within
61 62 63
/// such a directory may also contain the '.tmpl' or '.img.tmpl' extensions and
/// will be considered for expansion. In case certain files need to be copied
/// but without template expansion (data files, etc.), the '.copy.tmpl'
64 65 66
/// extension may be used. Furthermore, templates may contain additional
/// test files intended to run on the CI. Test files must end in `.test.tmpl`
/// and are only included when the --implementation-tests flag is enabled.
67
///
68 69 70
/// Folders with platform/language-specific content must be named
/// '<platform>-<language>.tmpl'.
///
71 72
/// Files in the destination will contain none of the '.tmpl', '.copy.tmpl',
/// 'img.tmpl', or '-<language>.tmpl' extensions.
73
class Template {
74
  factory Template(Directory templateSource, Directory? imageSourceDir, {
75 76 77
    required FileSystem fileSystem,
    required Logger logger,
    required TemplateRenderer templateRenderer,
78
    Set<Uri>? templateManifest,
79 80 81
  }) {
    return Template._(
      <Directory>[templateSource],
82
      imageSourceDir != null ? <Directory>[imageSourceDir] : <Directory>[],
83 84 85 86 87 88 89 90 91
      fileSystem: fileSystem,
      logger: logger,
      templateRenderer: templateRenderer,
      templateManifest: templateManifest,
    );
  }

  Template._(
    List<Directory> templateSources, this.imageSourceDirectories, {
92 93 94 95
    required FileSystem fileSystem,
    required Logger logger,
    required TemplateRenderer templateRenderer,
    required Set<Uri>? templateManifest,
96
  }) : _fileSystem = fileSystem,
97 98
       _logger = logger,
       _templateRenderer = templateRenderer,
99
       _templateManifest = templateManifest ?? <Uri>{} {
100 101 102 103
    for (final Directory sourceDirectory in templateSources) {
      if (!sourceDirectory.existsSync()) {
        throwToolExit('Template source directory does not exist: ${sourceDirectory.absolute.path}');
      }
104 105
    }

106 107 108 109 110 111
    final Map<FileSystemEntity, Directory> templateFiles = <FileSystemEntity, Directory>{
      for (final Directory sourceDirectory in templateSources)
        for (final FileSystemEntity entity in sourceDirectory.listSync(recursive: true))
          entity: sourceDirectory,
    };
    for (final FileSystemEntity entity in templateFiles.keys.whereType<File>()) {
112
      if (_templateManifest.isNotEmpty && !_templateManifest.contains(Uri.file(entity.absolute.path))) {
113
        _logger.printTrace('Skipping ${entity.absolute.path}, missing from the template manifest.');
114 115 116
        // Skip stale files in the flutter_tools directory.
        continue;
      }
117

118
      final String relativePath = fileSystem.path.relative(entity.path,
119
          from: templateFiles[entity]!.absolute.path);
120
      if (relativePath.contains(templateExtension)) {
121
        // If '.tmpl' appears anywhere within the path of this entity, it is
122
        // a candidate for rendering. This catches cases where the folder
123
        // itself is a template.
124
        _templateFilePaths[relativePath] = fileSystem.path.absolute(entity.path);
125 126 127 128
      }
    }
  }

129
  static Future<Template> fromName(String name, {
130 131 132 133
    required FileSystem fileSystem,
    required Set<Uri>? templateManifest,
    required Logger logger,
    required TemplateRenderer templateRenderer,
134
  }) async {
Ian Hickson's avatar
Ian Hickson committed
135
    // All named templates are placed in the 'templates' directory
136 137
    final Directory templateDir = templatePathProvider.directoryInPackage(name, fileSystem);
    final Directory imageDir = await templatePathProvider.imageDirectory(name, fileSystem, logger);
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    return Template._(
      <Directory>[templateDir],
      <Directory>[imageDir],
      fileSystem: fileSystem,
      logger: logger,
      templateRenderer: templateRenderer,
      templateManifest: templateManifest,
    );
  }

  static Future<Template> merged(List<String> names, Directory directory, {
    required FileSystem fileSystem,
    required Set<Uri> templateManifest,
    required Logger logger,
    required TemplateRenderer templateRenderer,
  }) async {
    // All named templates are placed in the 'templates' directory
    return Template._(
      <Directory>[
        for (final String name in names)
158
          templatePathProvider.directoryInPackage(name, fileSystem),
159 160 161
      ],
      <Directory>[
        for (final String name in names)
162 163
          if ((await templatePathProvider.imageDirectory(name, fileSystem, logger)).existsSync())
            await templatePathProvider.imageDirectory(name, fileSystem, logger),
164
      ],
165
      fileSystem: fileSystem,
166 167
      logger: logger,
      templateRenderer: templateRenderer,
168 169
      templateManifest: templateManifest,
    );
Ian Hickson's avatar
Ian Hickson committed
170 171
  }

172
  final FileSystem _fileSystem;
173
  final Logger _logger;
174
  final Set<Uri> _templateManifest;
175 176
  final TemplateRenderer _templateRenderer;

177 178
  static const String templateExtension = '.tmpl';
  static const String copyTemplateExtension = '.copy.tmpl';
179
  static const String imageTemplateExtension = '.img.tmpl';
180
  static const String testTemplateExtension = '.test.tmpl';
181
  final Pattern _kTemplateLanguageVariant = RegExp(r'(\w+)-(\w+)\.tmpl.*');
182
  final List<Directory> imageSourceDirectories;
183

184
  final Map<String /* relative */, String /* absolute source */> _templateFilePaths = <String, String>{};
185

186 187 188
  /// Render the template into [directory].
  ///
  /// May throw a [ToolExit] if the directory is not writable.
189 190
  int render(
    Directory destination,
191
    Map<String, Object?> context, {
192
    bool overwriteExisting = true,
193
    bool printStatusWhenWriting = true,
194
  }) {
195 196 197
    try {
      destination.createSync(recursive: true);
    } on FileSystemException catch (err) {
198
      _logger.printError(err.toString());
199 200
      throwToolExit('Failed to flutter create at ${destination.path}.');
    }
Devon Carew's avatar
Devon Carew committed
201
    int fileCount = 0;
202
    final bool implementationTests = (context['implementationTests'] as bool?) ?? false;
203

204 205 206 207 208
    /// Returns the resolved destination path corresponding to the specified
    /// raw destination path, after performing language filtering and template
    /// expansion on the path itself.
    ///
    /// Returns null if the given raw destination path has been filtered.
209 210
    String? renderPath(String relativeDestinationPath) {
      final Match? match = _kTemplateLanguageVariant.matchAsPrefix(relativeDestinationPath);
211
      if (match != null) {
212 213
        final String platform = match.group(1)!;
        final String? language = context['${platform}Language'] as String?;
214
        if (language != match.group(2)) {
215
          return null;
216
        }
217 218
        relativeDestinationPath = relativeDestinationPath.replaceAll('$platform-$language.tmpl', platform);
      }
219

220
      final bool android = (context['android'] as bool?) ?? false;
221 222 223 224
      if (relativeDestinationPath.contains('android') && !android) {
        return null;
      }

225
      final bool ios = (context['ios'] as bool?) ?? false;
226 227 228
      if (relativeDestinationPath.contains('ios') && !ios) {
        return null;
      }
229

230
      // Only build a web project if explicitly asked.
231
      final bool web = (context['web'] as bool?) ?? false;
232 233 234
      if (relativeDestinationPath.contains('web') && !web) {
        return null;
      }
235
      // Only build a Linux project if explicitly asked.
236
      final bool linux = (context['linux'] as bool?) ?? false;
237 238 239
      if (relativeDestinationPath.startsWith('linux.tmpl') && !linux) {
        return null;
      }
240
      // Only build a macOS project if explicitly asked.
241
      final bool macOS = (context['macos'] as bool?) ?? false;
242 243 244
      if (relativeDestinationPath.startsWith('macos.tmpl') && !macOS) {
        return null;
      }
245
      // Only build a Windows project if explicitly asked.
246
      final bool windows = (context['windows'] as bool?) ?? false;
247 248 249
      if (relativeDestinationPath.startsWith('windows.tmpl') && !windows) {
        return null;
      }
250

251 252 253 254
      final String? projectName = context['projectName'] as String?;
      final String? androidIdentifier = context['androidIdentifier'] as String?;
      final String? pluginClass = context['pluginClass'] as String?;
      final String? pluginClassSnakeCase = context['pluginClassSnakeCase'] as String?;
255
      final String destinationDirPath = destination.absolute.path;
256 257
      final String pathSeparator = _fileSystem.path.separator;
      String finalDestinationPath = _fileSystem.path
258
        .join(destinationDirPath, relativeDestinationPath)
259
        .replaceAll(copyTemplateExtension, '')
260
        .replaceAll(imageTemplateExtension, '')
261
        .replaceAll(testTemplateExtension, '')
262
        .replaceAll(templateExtension, '');
263

264
      if (android && androidIdentifier != null) {
265 266 267
        finalDestinationPath = finalDestinationPath
            .replaceAll('androidIdentifier', androidIdentifier.replaceAll('.', pathSeparator));
      }
268
      if (projectName != null) {
269
        finalDestinationPath = finalDestinationPath.replaceAll('projectName', projectName);
270
      }
271 272 273 274
      // This must be before the pluginClass replacement step.
      if (pluginClassSnakeCase != null) {
        finalDestinationPath = finalDestinationPath.replaceAll('pluginClassSnakeCase', pluginClassSnakeCase);
      }
275
      if (pluginClass != null) {
276
        finalDestinationPath = finalDestinationPath.replaceAll('pluginClass', pluginClass);
277
      }
278 279 280 281
      return finalDestinationPath;
    }

    _templateFilePaths.forEach((String relativeDestinationPath, String absoluteSourcePath) {
282
      final bool withRootModule = context['withRootModule'] as bool? ?? false;
283
      if (!withRootModule && absoluteSourcePath.contains('flutter_root')) {
284
        return;
285
      }
286

287 288 289 290
      if (!implementationTests && absoluteSourcePath.contains(testTemplateExtension)) {
        return;
      }

291
      final String? finalDestinationPath = renderPath(relativeDestinationPath);
292
      if (finalDestinationPath == null) {
293
        return;
294
      }
295 296
      final File finalDestinationFile = _fileSystem.file(finalDestinationPath);
      final String relativePathForLogging = _fileSystem.path.relative(finalDestinationFile.path);
297 298 299 300 301

      // Step 1: Check if the file needs to be overwritten.

      if (finalDestinationFile.existsSync()) {
        if (overwriteExisting) {
302
          finalDestinationFile.deleteSync(recursive: true);
303
          if (printStatusWhenWriting) {
304
            _logger.printStatus('  $relativePathForLogging (overwritten)');
305
          }
306 307
        } else {
          // The file exists but we cannot overwrite it, move on.
308
          if (printStatusWhenWriting) {
309
            _logger.printTrace('  $relativePathForLogging (existing - skipped)');
310
          }
311 312 313
          return;
        }
      } else {
314
        if (printStatusWhenWriting) {
315
          _logger.printStatus('  $relativePathForLogging (created)');
316
        }
317 318
      }

319
      fileCount += 1;
Devon Carew's avatar
Devon Carew committed
320

321
      finalDestinationFile.createSync(recursive: true);
322
      final File sourceFile = _fileSystem.file(absoluteSourcePath);
323

324
      // Step 2: If the absolute paths ends with a '.copy.tmpl', this file does
325 326
      //         not need mustache rendering but needs to be directly copied.

327 328
      if (sourceFile.path.endsWith(copyTemplateExtension)) {
        sourceFile.copySync(finalDestinationFile.path);
329 330 331 332

        return;
      }

333 334 335 336
      // Step 3: If the absolute paths ends with a '.img.tmpl', this file needs
      //         to be copied from the template image package.

      if (sourceFile.path.endsWith(imageTemplateExtension)) {
337 338 339
        final List<File> potentials = <File>[
          for (final Directory imageSourceDir in imageSourceDirectories)
            _fileSystem.file(_fileSystem.path
340
                .join(imageSourceDir.path, relativeDestinationPath.replaceAll(imageTemplateExtension, ''))),
341 342 343 344 345 346 347 348 349
        ];

        if (potentials.any((File file) => file.existsSync())) {
          final File imageSourceFile = potentials.firstWhere((File file) => file.existsSync());

          imageSourceFile.copySync(finalDestinationFile.path);
        } else {
          throwToolExit('Image File not found ${finalDestinationFile.path}');
        }
350 351 352 353 354

        return;
      }

      // Step 4: If the absolute path ends with a '.tmpl', this file needs
355 356
      //         rendering via mustache.

357
      if (sourceFile.path.endsWith(templateExtension)) {
358
        final String templateContents = sourceFile.readAsStringSync();
359 360 361 362 363
        final String? androidIdentifier = context['androidIdentifier'] as String?;
        if (finalDestinationFile.path.endsWith('.kt') && androidIdentifier != null) {
          context['androidIdentifier'] = _escapeKotlinKeywords(androidIdentifier);
        }

364 365 366 367 368 369 370
        // Use a copy of the context,
        // since the original is used in rendering other templates.
        final Map<String, Object?> localContext = finalDestinationFile.path.endsWith('.yaml')
          ? _createEscapedContextCopy(context)
          : context;

        final String renderedContents = _templateRenderer.renderString(templateContents, localContext);
371 372 373 374 375 376

        finalDestinationFile.writeAsStringSync(renderedContents);

        return;
      }

377
      // Step 5: This file does not end in .tmpl but is in a directory that
378
      //         does. Directly copy the file to the destination.
379
      sourceFile.copySync(finalDestinationFile.path);
380
    });
Devon Carew's avatar
Devon Carew committed
381 382

    return fileCount;
383 384 385
  }
}

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
/// Create a copy of the given [context], escaping its values when necessary.
///
/// Returns the copied context.
Map<String, Object?> _createEscapedContextCopy(Map<String, Object?> context) {
  final Map<String, Object?> localContext = Map<String, Object?>.of(context);

  final String? description = localContext['description'] as String?;

  if (description != null && description.isNotEmpty) {
    localContext['description'] = escapeYamlString(description);
  }

  return localContext;
}

401 402 403 404 405 406 407
String _escapeKotlinKeywords(String androidIdentifier) {
  final List<String> segments = androidIdentifier.split('.');
  final List<String> correctedSegments = segments.map(
    (String segment) => kReservedKotlinKeywords.contains(segment) ? '`$segment`' : segment
  ).toList();
  return correctedSegments.join('.');
}
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428

String escapeYamlString(String value) {
  final StringBuffer result = StringBuffer();
  result.write('"');
  for (final int rune in value.runes) {
    result.write(
      switch (rune) {
        0x00 => r'\0',
        0x09 => r'\t',
        0x0A => r'\n',
        0x0D => r'\r',
        0x22 => r'\"',
        0x5C => r'\\',
        < 0x20 => '\\x${rune.toRadixString(16).padLeft(2, "0")}',
        _ => String.fromCharCode(rune),
      }
    );
  }
  result.write('"');
  return result.toString();
}