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

import 'package:meta/meta.dart';
6
import 'package:platform/platform.dart';
7 8 9 10
import 'package:yaml/yaml.dart';

import 'utils.dart';

11 12 13
Platform get platform => _platform ??= const LocalPlatform();
Platform _platform;

14 15
/// Loads manifest data from `manifest.yaml` file or from [yaml], if present.
Manifest loadTaskManifest([ String yaml ]) {
16
  final dynamic manifestYaml = yaml == null
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    ? loadYaml(file('manifest.yaml').readAsStringSync())
    : loadYamlNode(yaml);

  _checkType(manifestYaml is Map, manifestYaml, 'Manifest', 'dictionary');
  return _validateAndParseManifest(manifestYaml);
}

/// Contains CI task information.
class Manifest {
  Manifest._(this.tasks);

  /// CI tasks.
  final List<ManifestTask> tasks;
}

/// A CI task.
class ManifestTask {
  ManifestTask._({
    @required this.name,
    @required this.description,
    @required this.stage,
    @required this.requiredAgentCapabilities,
39 40
    @required this.isFlaky,
    @required this.timeoutInMinutes,
41
  }) {
42
    final String taskName = 'task "$name"';
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    _checkIsNotBlank(name, 'Task name', taskName);
    _checkIsNotBlank(description, 'Task description', taskName);
    _checkIsNotBlank(stage, 'Task stage', taskName);
    _checkIsNotBlank(requiredAgentCapabilities, 'requiredAgentCapabilities', taskName);
  }

  /// Task name as it appears on the dashboard.
  final String name;

  /// A human-readable description of the task.
  final String description;

  /// The stage this task should run in.
  final String stage;

  /// Capabilities required of the build agent to be able to perform this task.
59
  final List<String> requiredAgentCapabilities;
60 61 62 63 64 65 66 67

  /// Whether this test is flaky.
  ///
  /// Flaky tests are not considered when deciding if the build is broken.
  final bool isFlaky;

  /// An optional custom timeout specified in minutes.
  final int timeoutInMinutes;
68 69 70 71 72 73 74 75 76 77 78 79 80 81

  /// Whether the task is supported by the current host platform
  bool isSupportedByHost() {
    final Set<String> supportedHosts = Set<String>.from(
      requiredAgentCapabilities.map<String>(
        (String str) => str.split('/')[0]
      )
    );
    String hostPlatform = platform.operatingSystem;
    if (hostPlatform == 'macos') {
      hostPlatform = 'mac'; // package:platform uses 'macos' while manifest.yaml uses 'mac'
    }
    return supportedHosts.contains(hostPlatform);
  }
82 83 84 85 86 87 88 89 90 91 92 93 94 95
}

/// Thrown when the manifest YAML is not valid.
class ManifestError extends Error {
  ManifestError(this.message);

  final String message;

  @override
  String toString() => '$ManifestError: $message';
}

// There's no good YAML validator, at least not for Dart, so we validate
// manually. It's not too much code and produces good error messages.
96
Manifest _validateAndParseManifest(Map<dynamic, dynamic> manifestYaml) {
97
  _checkKeys(manifestYaml, 'manifest', const <String>['tasks']);
98
  return Manifest._(_validateAndParseTasks(manifestYaml['tasks']));
99 100 101 102
}

List<ManifestTask> _validateAndParseTasks(dynamic tasksYaml) {
  _checkType(tasksYaml is Map, tasksYaml, 'Value of "tasks"', 'dictionary');
103
  final List<dynamic> sortedKeys = tasksYaml.keys.toList()..sort();
104
  return sortedKeys.map<ManifestTask>((dynamic taskName) => _validateAndParseTask(taskName, tasksYaml[taskName])).toList();
105 106 107 108 109 110 111 112 113
}

ManifestTask _validateAndParseTask(dynamic taskName, dynamic taskYaml) {
  _checkType(taskName is String, taskName, 'Task name', 'string');
  _checkType(taskYaml is Map, taskYaml, 'Value of task "$taskName"', 'dictionary');
  _checkKeys(taskYaml, 'Value of task "$taskName"', const <String>[
    'description',
    'stage',
    'required_agent_capabilities',
114 115
    'flaky',
    'timeout_in_minutes',
116 117
  ]);

118 119 120 121 122 123 124 125 126 127
  final dynamic isFlaky = taskYaml['flaky'];
  if (isFlaky != null) {
    _checkType(isFlaky is bool, isFlaky, 'flaky', 'boolean');
  }

  final dynamic timeoutInMinutes = taskYaml['timeout_in_minutes'];
  if (timeoutInMinutes != null) {
    _checkType(timeoutInMinutes is int, timeoutInMinutes, 'timeout_in_minutes', 'integer');
  }

128
  final List<dynamic> capabilities = _validateAndParseCapabilities(taskName, taskYaml['required_agent_capabilities']);
129
  return ManifestTask._(
130 131 132 133
    name: taskName,
    description: taskYaml['description'],
    stage: taskYaml['stage'],
    requiredAgentCapabilities: capabilities,
134 135
    isFlaky: isFlaky ?? false,
    timeoutInMinutes: timeoutInMinutes,
136 137 138 139 140 141
  );
}

List<String> _validateAndParseCapabilities(String taskName, dynamic capabilitiesYaml) {
  _checkType(capabilitiesYaml is List, capabilitiesYaml, 'required_agent_capabilities', 'list');
  for (int i = 0; i < capabilitiesYaml.length; i++) {
142
    final dynamic capability = capabilitiesYaml[i];
143 144
    _checkType(capability is String, capability, 'required_agent_capabilities[$i]', 'string');
  }
145
  return capabilitiesYaml.cast<String>();
146 147 148 149
}

void _checkType(bool isValid, dynamic value, String variableName, String typeName) {
  if (!isValid) {
150
    throw ManifestError(
151 152 153 154 155 156 157
      '$variableName must be a $typeName but was ${value.runtimeType}: $value',
    );
  }
}

void _checkIsNotBlank(dynamic value, String variableName, String ownerName) {
  if (value == null || value.isEmpty) {
158
    throw ManifestError('$variableName must not be empty in $ownerName.');
159 160 161
  }
}

162
void _checkKeys(Map<dynamic, dynamic> map, String variableName, List<String> allowedKeys) {
163 164
  for (String key in map.keys) {
    if (!allowedKeys.contains(key)) {
165
      throw ManifestError(
166 167 168 169 170
        'Unrecognized property "$key" in $variableName. '
        'Allowed properties: ${allowedKeys.join(', ')}');
    }
  }
}