flutter_project_metadata.dart 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// 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,
  /// 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,
}

26 27
String flutterProjectTypeToString(FlutterProjectType type) {
  return getEnumName(type);
28 29
}

30 31
FlutterProjectType? stringToProjectType(String value) {
  FlutterProjectType? result;
32
  for (final FlutterProjectType type in FlutterProjectType.values) {
33
    if (value == flutterProjectTypeToString(type)) {
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
      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;

52 53
  String? get versionChannel => _versionValue('channel');
  String? get versionRevision => _versionValue('revision');
54

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

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

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

102
    return _metadataYaml![key];
103 104
  }
}