flutter_project_metadata.dart 2.98 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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:yaml/yaml.dart';

import 'base/file_system.dart';
import 'base/logger.dart';
import 'base/utils.dart';

enum FlutterProjectType {
  /// This is the default project with the user-managed host code.
  /// It is different than the "module" template in that it exposes and doesn't
  /// manage the platform code.
  app,
16 17
  /// A List/Detail app template that follows community best practices.
  skeleton,
18 19 20 21 22 23 24 25 26 27
  /// The is a project that has managed platform host code. It is an application with
  /// ephemeral .ios and .android directories that can be updated automatically.
  module,
  /// This is a Flutter Dart package project. It doesn't have any native
  /// components, only Dart.
  package,
  /// This is a native plugin project.
  plugin,
}

28 29
String flutterProjectTypeToString(FlutterProjectType type) {
  return getEnumName(type);
30 31
}

32 33
FlutterProjectType? stringToProjectType(String value) {
  FlutterProjectType? result;
34
  for (final FlutterProjectType type in FlutterProjectType.values) {
35
    if (value == flutterProjectTypeToString(type)) {
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
      result = type;
      break;
    }
  }
  return result;
}

/// A wrapper around the `.metadata` file.
class FlutterProjectMetadata {
  FlutterProjectMetadata(
    File metadataFile,
    Logger logger,
    )  : _metadataFile = metadataFile,
      _logger = logger;

  final File _metadataFile;
  final Logger _logger;

54 55
  String? get versionChannel => _versionValue('channel');
  String? get versionRevision => _versionValue('revision');
56

57
  FlutterProjectType? get projectType {
58 59 60 61 62 63 64 65 66
    final dynamic projectTypeYaml = _metadataValue('project_type');
    if (projectTypeYaml is String) {
      return stringToProjectType(projectTypeYaml);
    } else {
      _logger.printTrace('.metadata project_type version is malformed.');
      return null;
    }
  }

67 68
  YamlMap? _versionYaml;
  String? _versionValue(String key) {
69 70 71 72 73 74 75 76 77
    if (_versionYaml == null) {
      final dynamic versionYaml = _metadataValue('version');
      if (versionYaml is YamlMap) {
        _versionYaml = versionYaml;
      } else {
        _logger.printTrace('.metadata version is malformed.');
        return null;
      }
    }
78 79
    if (_versionYaml != null && _versionYaml!.containsKey(key) && _versionYaml![key] is String) {
      return _versionYaml![key] as String;
80 81 82 83
    }
    return null;
  }

84
  YamlMap? _metadataYaml;
85 86 87 88 89
  dynamic _metadataValue(String key) {
    if (_metadataYaml == null) {
      if (!_metadataFile.existsSync()) {
        return null;
      }
90 91 92 93 94 95
      dynamic metadataYaml;
      try {
        metadataYaml = loadYaml(_metadataFile.readAsStringSync());
      } on YamlException {
        // Handled in return below.
      }
96 97 98 99 100 101 102 103
      if (metadataYaml is YamlMap) {
        _metadataYaml = metadataYaml;
      } else {
        _logger.printTrace('.metadata is malformed.');
        return null;
      }
    }

104
    return _metadataYaml![key];
105 106
  }
}