flutter_manifest.dart 14.3 KB
Newer Older
1 2 3 4
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:meta/meta.dart';
6
import 'package:pub_semver/pub_semver.dart';
7 8 9
import 'package:yaml/yaml.dart';

import 'base/file_system.dart';
10
import 'base/user_messages.dart';
11
import 'base/utils.dart';
12
import 'cache.dart';
13
import 'globals.dart';
14
import 'plugins.dart';
15

16
/// A wrapper around the `flutter` section in the `pubspec.yaml` file.
17 18 19
class FlutterManifest {
  FlutterManifest._();

20 21
  /// Returns an empty manifest.
  static FlutterManifest empty() {
22
    final FlutterManifest manifest = FlutterManifest._();
23 24 25 26 27
    manifest._descriptor = const <String, dynamic>{};
    manifest._flutterDescriptor = const <String, dynamic>{};
    return manifest;
  }

28
  /// Returns null on invalid manifest. Returns empty manifest on missing file.
29
  static FlutterManifest createFromPath(String path) {
30 31
    if (path == null || !fs.isFileSync(path))
      return _createFromYaml(null);
32
    final String manifest = fs.file(path).readAsStringSync();
33
    return createFromString(manifest);
34
  }
35

36
  /// Returns null on missing or invalid manifest
37
  @visibleForTesting
38
  static FlutterManifest createFromString(String manifest) {
39 40 41
    return _createFromYaml(loadYaml(manifest));
  }

42
  static FlutterManifest _createFromYaml(dynamic yamlDocument) {
43
    final FlutterManifest pubspec = FlutterManifest._();
44
    if (yamlDocument != null && !_validate(yamlDocument))
45 46
      return null;

47 48 49 50 51 52 53 54 55 56 57 58 59 60
    final Map<dynamic, dynamic> yamlMap = yamlDocument;
    if (yamlMap != null) {
      pubspec._descriptor = yamlMap.cast<String, dynamic>();
    } else {
      pubspec._descriptor = <String, dynamic>{};
    }

    final Map<dynamic, dynamic> flutterMap = pubspec._descriptor['flutter'];
    if (flutterMap != null) {
      pubspec._flutterDescriptor = flutterMap.cast<String, dynamic>();
    } else {
      pubspec._flutterDescriptor = <String, dynamic>{};
    }

61 62 63 64 65 66 67 68 69
    return pubspec;
  }

  /// A map representation of the entire `pubspec.yaml` file.
  Map<String, dynamic> _descriptor;

  /// A map representation of the `flutter` section in the `pubspec.yaml` file.
  Map<String, dynamic> _flutterDescriptor;

70
  /// True if the `pubspec.yaml` file does not exist.
71 72
  bool get isEmpty => _descriptor.isEmpty;

73
  /// The string value of the top-level `name` property in the `pubspec.yaml` file.
74
  String get appName => _descriptor['name'] ?? '';
75

76 77 78
  // Flag to avoid printing multiple invalid version messages.
  bool _hasShowInvalidVersionMsg = false;

79 80 81
  /// The version String from the `pubspec.yaml` file.
  /// Can be null if it isn't set or has a wrong format.
  String get appVersion {
82 83 84 85 86 87 88 89 90 91 92
    final String verStr = _descriptor['version']?.toString();
    if (verStr == null) {
      return null;
    }

    Version version;
    try {
      version = Version.parse(verStr);
    } on Exception {
      if (!_hasShowInvalidVersionMsg) {
        printStatus(userMessages.invalidVersionSettingHintMessage(verStr), emphasis: true);
93 94 95
        _hasShowInvalidVersionMsg = true;
      }
    }
96
    return version?.toString();
97 98 99 100 101 102 103
  }

  /// The build version name from the `pubspec.yaml` file.
  /// Can be null if version isn't set or has a wrong format.
  String get buildName {
    if (appVersion != null && appVersion.contains('+'))
      return appVersion.split('+')?.elementAt(0);
104
    else
105 106 107 108 109
      return appVersion;
  }

  /// The build version number from the `pubspec.yaml` file.
  /// Can be null if version isn't set or has a wrong format.
110
  String get buildNumber {
111 112
    if (appVersion != null && appVersion.contains('+')) {
      final String value = appVersion.split('+')?.elementAt(1);
113
      return value;
114 115 116 117 118
    } else {
      return null;
    }
  }

119 120 121 122
  bool get usesMaterialDesign {
    return _flutterDescriptor['uses-material-design'] ?? false;
  }

123 124 125 126 127 128 129
  /// True if this Flutter module should use AndroidX dependencies.
  ///
  /// If false the deprecated Android Support library will be used.
  bool get usesAndroidX {
    return _flutterDescriptor['module']['androidX'] ?? false;
  }

130
  /// True if this manifest declares a Flutter module project.
131
  ///
132 133 134
  /// A Flutter project is considered a module when it has a `module:`
  /// descriptor. A Flutter module project supports integration into an
  /// existing host app, and has managed platform host code.
135
  ///
136 137
  /// Such a project can be created using `flutter create -t module`.
  bool get isModule => _flutterDescriptor.containsKey('module');
138 139 140 141 142 143 144 145 146 147 148 149

  /// True if this manifest declares a Flutter plugin project.
  ///
  /// A Flutter project is considered a plugin when it has a `plugin:`
  /// descriptor. A Flutter plugin project wraps custom Android and/or
  /// iOS code in a Dart interface for consumption by other Flutter app
  /// projects.
  ///
  /// Such a project can be created using `flutter create -t plugin`.
  bool get isPlugin => _flutterDescriptor.containsKey('plugin');

  /// Returns the Android package declared by this manifest in its
150
  /// module or plugin descriptor. Returns null, if there is no
151 152
  /// such declaration.
  String get androidPackage {
153 154
    if (isModule)
      return _flutterDescriptor['module']['androidPackage'];
155 156 157 158 159 160 161 162
    if (isPlugin) {
      final YamlMap plugin = _flutterDescriptor['plugin'];
      if (plugin.containsKey('platforms')) {
        return plugin['platforms']['android']['package'];
      } else {
        return plugin['androidPackage'];
      }
    }
163 164
    return null;
  }
165

166
  /// Returns the iOS bundle identifier declared by this manifest in its
167
  /// module descriptor. Returns null if there is no such declaration.
168
  String get iosBundleIdentifier {
169 170
    if (isModule)
      return _flutterDescriptor['module']['iosBundleIdentifier'];
171 172 173
    return null;
  }

174
  List<Map<String, dynamic>> get fontsDescriptor {
175 176 177 178
    return fonts.map((Font font) => font.descriptor).toList();
  }

  List<Map<String, dynamic>> get _rawFontsDescriptor {
179 180 181 182
    final List<dynamic> fontList = _flutterDescriptor['fonts'];
    return fontList == null
        ? const <Map<String, dynamic>>[]
        : fontList.map<Map<String, dynamic>>(castStringKeyedMap).toList();
183 184
  }

185
  List<Uri> get assets {
186 187 188 189 190 191 192 193 194
    final List<dynamic> assets = _flutterDescriptor['assets'];
    if (assets == null) {
      return const <Uri>[];
    }
    return assets
        .cast<String>()
        .map<String>(Uri.encodeFull)
        ?.map<Uri>(Uri.parse)
        ?.toList();
195 196 197 198 199 200 201 202 203 204 205 206 207 208
  }

  List<Font> _fonts;

  List<Font> get fonts {
    _fonts ??= _extractFonts();
    return _fonts;
  }

  List<Font> _extractFonts() {
    if (!_flutterDescriptor.containsKey('fonts'))
      return <Font>[];

    final List<Font> fonts = <Font>[];
209
    for (Map<String, dynamic> fontFamily in _rawFontsDescriptor) {
210
      final List<dynamic> fontFiles = fontFamily['fonts'];
211 212 213 214 215 216 217 218 219 220 221
      final String familyName = fontFamily['family'];
      if (familyName == null) {
        printError('Warning: Missing family name for font.', emphasis: true);
        continue;
      }
      if (fontFiles == null) {
        printError('Warning: No fonts specified for font $familyName', emphasis: true);
        continue;
      }

      final List<FontAsset> fontAssets = <FontAsset>[];
222
      for (Map<dynamic, dynamic> fontFile in fontFiles) {
223 224 225 226 227 228
        final String asset = fontFile['asset'];
        if (asset == null) {
          printError('Warning: Missing asset in fonts for $familyName', emphasis: true);
          continue;
        }

229
        fontAssets.add(FontAsset(
230
          Uri.parse(asset),
231 232 233 234 235
          weight: fontFile['weight'],
          style: fontFile['style'],
        ));
      }
      if (fontAssets.isNotEmpty)
236
        fonts.add(Font(fontFamily['family'], fontAssets));
237 238 239 240 241 242
    }
    return fonts;
  }
}

class Font {
243 244 245 246
  Font(this.familyName, this.fontAssets)
    : assert(familyName != null),
      assert(fontAssets != null),
      assert(fontAssets.isNotEmpty);
247 248 249 250 251 252 253

  final String familyName;
  final List<FontAsset> fontAssets;

  Map<String, dynamic> get descriptor {
    return <String, dynamic>{
      'family': familyName,
254
      'fonts': fontAssets.map<Map<String, dynamic>>((FontAsset a) => a.descriptor).toList(),
255 256 257 258 259 260 261 262
    };
  }

  @override
  String toString() => '$runtimeType(family: $familyName, assets: $fontAssets)';
}

class FontAsset {
263 264
  FontAsset(this.assetUri, {this.weight, this.style})
    : assert(assetUri != null);
265

266
  final Uri assetUri;
267 268 269 270 271 272 273 274 275 276 277
  final int weight;
  final String style;

  Map<String, dynamic> get descriptor {
    final Map<String, dynamic> descriptor = <String, dynamic>{};
    if (weight != null)
      descriptor['weight'] = weight;

    if (style != null)
      descriptor['style'] = style;

278
    descriptor['asset'] = assetUri.path;
279 280 281 282
    return descriptor;
  }

  @override
283
  String toString() => '$runtimeType(asset: ${assetUri.path}, weight; $weight, style: $style)';
284 285
}

286 287 288
@visibleForTesting
String buildSchemaDir(FileSystem fs) {
  return fs.path.join(
289
    fs.path.absolute(Cache.flutterRoot), 'packages', 'flutter_tools', 'schema',
290 291 292 293 294 295 296
  );
}

@visibleForTesting
String buildSchemaPath(FileSystem fs) {
  return fs.path.join(
    buildSchemaDir(fs),
297 298
    'pubspec_yaml.json',
  );
299 300
}

301 302 303
/// This method should be kept in sync with the schema in
/// `$FLUTTER_ROOT/packages/flutter_tools/schema/pubspec_yaml.json`,
/// but avoid introducing depdendencies on packages for simple validation.
304
bool _validate(YamlMap manifest) {
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
  final List<String> errors = <String>[];
  for (final MapEntry<dynamic, dynamic> kvp in manifest.entries) {
    if (kvp.key is! String) {
      errors.add('Expected YAML key to be a a string, but got ${kvp.key}.');
      continue;
    }
    switch (kvp.key) {
      case 'name':
        if (kvp.value is! String) {
          errors.add('Expected "${kvp.key}" to be a string, but got ${kvp.value}.');
        }
        break;
      case 'flutter':
        if (kvp.value == null) {
          continue;
        }
        if (kvp.value is! YamlMap) {
          errors.add('Expected "${kvp.key}" section to be an object or null, but got ${kvp.value}.');
        }
        _validateFlutter(kvp.value, errors);
        break;
      default:
        // additionalProperties are allowed.
        break;
    }
  }
331

332
  if (errors.isNotEmpty) {
333
    printStatus('Error detected in pubspec.yaml:', emphasis: true);
334
    printError(errors.join('\n'));
335 336
    return false;
  }
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

  return true;
}

void _validateFlutter(YamlMap yaml, List<String> errors) {
  if (yaml == null || yaml.entries == null) {
    return;
  }
  for (final MapEntry<dynamic, dynamic> kvp in yaml.entries) {
    if (kvp.key is! String) {
      errors.add('Expected YAML key to be a a string, but got ${kvp.key} (${kvp.value.runtimeType}).');
      continue;
    }
    switch (kvp.key) {
      case 'uses-material-design':
        if (kvp.value is! bool) {
          errors.add('Expected "${kvp.key}" to be a bool, but got ${kvp.value} (${kvp.value.runtimeType}).');
        }
        break;
      case 'assets':
      case 'services':
        if (kvp.value is! YamlList || kvp.value[0] is! String) {
          errors.add('Expected "${kvp.key}" to be a list, but got ${kvp.value} (${kvp.value.runtimeType}).');
        }
        break;
      case 'fonts':
        if (kvp.value is! YamlList || kvp.value[0] is! YamlMap) {
          errors.add('Expected "${kvp.key}" to be a list, but got ${kvp.value} (${kvp.value.runtimeType}).');
365 366
        } else {
          _validateFonts(kvp.value, errors);
367 368 369 370 371 372 373
        }
        break;
      case 'module':
        if (kvp.value is! YamlMap) {
          errors.add('Expected "${kvp.key}" to be an object, but got ${kvp.value} (${kvp.value.runtimeType}).');
        }

374 375 376
        if (kvp.value['androidX'] != null && kvp.value['androidX'] is! bool) {
          errors.add('The "androidX" value must be a bool if set.');
        }
377 378 379 380 381 382 383 384 385 386 387
        if (kvp.value['androidPackage'] != null && kvp.value['androidPackage'] is! String) {
          errors.add('The "androidPackage" value must be a string if set.');
        }
        if (kvp.value['iosBundleIdentifier'] != null && kvp.value['iosBundleIdentifier'] is! String) {
          errors.add('The "iosBundleIdentifier" section must be a string if set.');
        }
        break;
      case 'plugin':
        if (kvp.value is! YamlMap) {
          errors.add('Expected "${kvp.key}" to be an object, but got ${kvp.value} (${kvp.value.runtimeType}).');
        }
388 389
        final List<String> pluginErrors = Plugin.validatePluginYaml(kvp.value);
        errors.addAll(pluginErrors);
390 391 392 393 394 395
        break;
      default:
        errors.add('Unexpected child "${kvp.key}" found under "flutter".');
        break;
    }
  }
396
}
397 398 399 400 401

void _validateFonts(YamlList fonts, List<String> errors) {
  if (fonts == null) {
    return;
  }
402
  const Set<int> fontWeights = <int>{
403
    100, 200, 300, 400, 500, 600, 700, 800, 900,
404
  };
405 406 407 408 409 410
  for (final dynamic fontListEntry in fonts) {
    if (fontListEntry is! YamlMap) {
      errors.add('Unexpected child "$fontListEntry" found under "fonts". Expected a map.');
      continue;
    }
    final YamlMap fontMap = fontListEntry;
411 412 413 414 415 416 417 418
    for (dynamic key in fontMap.keys.where((dynamic key) => key != 'family' && key != 'fonts')) {
      errors.add('Unexpected child "$key" found under "fonts".');
    }
    if (fontMap['family'] != null && fontMap['family'] is! String) {
      errors.add('Font family must either be null or a String.');
    }
    if (fontMap['fonts'] == null) {
      continue;
419 420 421
    } else if (fontMap['fonts'] is! YamlList) {
      errors.add('Expected "fonts" to either be null or a list.');
      continue;
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
    }
    for (final YamlMap fontListItem in fontMap['fonts']) {
      for (final MapEntry<dynamic, dynamic> kvp in fontListItem.entries) {
        if (kvp.key is! String) {
          errors.add('Expected "${kvp.key}" under "fonts" to be a string.');
        }
        switch(kvp.key) {
          case 'asset':
            if (kvp.value is! String) {
              errors.add('Expected font asset ${kvp.value} ((${kvp.value.runtimeType})) to be a string.');
            }
            break;
          case 'weight':
            if (!fontWeights.contains(kvp.value)) {
              errors.add('Invalid value ${kvp.value} ((${kvp.value.runtimeType})) for font -> weight.');
            }
            break;
          case 'style':
            if (kvp.value != 'normal' && kvp.value != 'italic') {
              errors.add('Invalid value ${kvp.value} ((${kvp.value.runtimeType})) for font -> style.');
            }
            break;
          default:
            errors.add('Unexpected key ${kvp.key} ((${kvp.value.runtimeType})) under font.');
            break;
        }
      }
    }
  }
451
}