-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathvalidation.dart
210 lines (191 loc) · 6.96 KB
/
validation.dart
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
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io';
import '../../code_assets_builder.dart';
import 'link_mode.dart';
Future<ValidationErrors> validateCodeAssetBuildConfig(
BuildConfig config) async =>
_validateCodeConfig(
'BuildConfig', config.targetOS, config.dryRun, config.codeConfig);
Future<ValidationErrors> validateCodeAssetLinkConfig(LinkConfig config) async =>
_validateCodeConfig(
'LinkConfig', config.targetOS, false, config.codeConfig);
ValidationErrors _validateCodeConfig(
String configName, OS targetOS, bool dryRun, CodeConfig codeConfig) {
// The dry run will be removed soon.
if (dryRun) return const [];
final errors = <String>[];
switch (targetOS) {
case OS.macOS:
if (codeConfig.targetMacOSVersion == null) {
errors.add('$configName.targetOS is OS.macOS but '
'$configName.codeConfig.targetMacOSVersion was missing');
}
break;
case OS.iOS:
if (codeConfig.targetIOSSdk == null) {
errors.add('$configName.targetOS is OS.iOS but '
'$configName.codeConfig.targetIOSSdk was missing');
}
if (codeConfig.targetIOSVersion == null) {
errors.add('$configName.targetOS is OS.iOS but '
'$configName.codeConfig.targetIOSVersion was missing');
}
break;
case OS.android:
if (codeConfig.targetAndroidNdkApi == null) {
errors.add('$configName.targetOS is OS.android but '
'$configName.codeConfig.targetAndroidNdkApi was missing');
}
break;
}
final compilerConfig = codeConfig.cCompiler;
final compiler = compilerConfig.compiler?.toFilePath();
if (compiler != null && !File(compiler).existsSync()) {
errors.add('$configName.codeConfig.compiler ($compiler) does not exist.');
}
final linker = compilerConfig.linker?.toFilePath();
if (linker != null && !File(linker).existsSync()) {
errors.add('$configName.codeConfig.linker ($linker) does not exist.');
}
final archiver = compilerConfig.archiver?.toFilePath();
if (archiver != null && !File(archiver).existsSync()) {
errors.add('$configName.codeConfig.archiver ($archiver) does not exist.');
}
final envScript = compilerConfig.envScript?.toFilePath();
if (envScript != null && !File(envScript).existsSync()) {
errors.add('$configName.codeConfig.envScript ($envScript) does not exist.');
}
return errors;
}
Future<ValidationErrors> validateCodeAssetBuildOutput(
BuildConfig config,
BuildOutput output,
) =>
_validateCodeAssetBuildOrLinkOutput(config, config.codeConfig,
output.encodedAssets, config.dryRun, output, true);
Future<ValidationErrors> validateCodeAssetLinkOutput(
LinkConfig config,
LinkOutput output,
) =>
_validateCodeAssetBuildOrLinkOutput(
config, config.codeConfig, output.encodedAssets, false, output, false);
/// Validates that the given code assets can be used together in an application.
///
/// Some restrictions - e.g. unique shared library names - have to be validated
/// on the entire application build and not on individual `hook/build.dart`
/// invocations.
Future<ValidationErrors> validateCodeAssetInApplication(
List<EncodedAsset> assets) async {
final fileNameToEncodedAssetId = <String, Set<String>>{};
for (final asset in assets) {
if (asset.type != CodeAsset.type) continue;
_groupCodeAssetsByFilename(
CodeAsset.fromEncoded(asset), fileNameToEncodedAssetId);
}
final errors = <String>[];
_validateNoDuplicateDylibNames(errors, fileNameToEncodedAssetId);
return errors;
}
Future<ValidationErrors> _validateCodeAssetBuildOrLinkOutput(
HookConfig config,
CodeConfig codeConfig,
List<EncodedAsset> encodedAssets,
bool dryRun,
HookOutput output,
bool isBuild,
) async {
final errors = <String>[];
final ids = <String>{};
final fileNameToEncodedAssetId = <String, Set<String>>{};
for (final asset in encodedAssets) {
if (asset.type != CodeAsset.type) continue;
_validateCodeAssets(
config,
codeConfig,
dryRun,
CodeAsset.fromEncoded(asset),
errors,
ids,
isBuild,
);
_groupCodeAssetsByFilename(
CodeAsset.fromEncoded(asset), fileNameToEncodedAssetId);
}
_validateNoDuplicateDylibNames(errors, fileNameToEncodedAssetId);
return errors;
}
void _validateCodeAssets(
HookConfig config,
CodeConfig codeConfig,
bool dryRun,
CodeAsset codeAsset,
List<String> errors,
Set<String> ids,
bool isBuild,
) {
final id = codeAsset.id;
final prefix = 'package:${config.packageName}/';
if (isBuild && !id.startsWith(prefix)) {
errors.add('Code asset "$id" does not start with "$prefix".');
}
if (!ids.add(id)) {
errors.add('More than one code asset with same "$id" id.');
}
final preference = codeConfig.linkModePreference;
final linkMode = codeAsset.linkMode;
if ((linkMode is DynamicLoading && preference == LinkModePreference.static) ||
(linkMode is StaticLinking && preference == LinkModePreference.dynamic)) {
errors.add('CodeAsset "$id" has a link mode "$linkMode", which '
'is not allowed by by the config link mode preference '
'"$preference".');
}
final os = codeAsset.os;
if (config.targetOS != os) {
final error = 'CodeAsset "$id" has a os "$os", which '
'is not the target os "${config.targetOS}".';
errors.add(error);
}
final architecture = codeAsset.architecture;
if (!dryRun) {
if (architecture == null) {
errors.add('CodeAsset "$id" has no architecture.');
} else if (architecture != codeConfig.targetArchitecture) {
errors.add('CodeAsset "$id" has an architecture "$architecture", which '
'is not the target architecture "${codeConfig.targetArchitecture}".');
}
}
final file = codeAsset.file;
if (file == null && !dryRun) {
errors.add('CodeAsset "$id" has no file.');
}
if (file != null && !dryRun && !File.fromUri(file).existsSync()) {
errors.add('CodeAsset "$id" has a file "${file.toFilePath()}", which '
'does not exist.');
}
}
void _groupCodeAssetsByFilename(
CodeAsset codeAsset,
Map<String, Set<String>> fileNameToEncodedAssetId,
) {
final file = codeAsset.file;
if (file != null) {
final fileName = file.pathSegments.where((s) => s.isNotEmpty).last;
fileNameToEncodedAssetId[fileName] ??= {};
fileNameToEncodedAssetId[fileName]!.add(codeAsset.id);
}
}
void _validateNoDuplicateDylibNames(
List<String> errors, Map<String, Set<String>> fileNameToEncodedAssetId) {
for (final fileName in fileNameToEncodedAssetId.keys) {
final assetIds = fileNameToEncodedAssetId[fileName]!;
if (assetIds.length > 1) {
final assetIdsString = assetIds.map((e) => '"$e"').join(', ');
final error =
'Duplicate dynamic library file name "$fileName" for the following'
' asset ids: $assetIdsString.';
errors.add(error);
}
}
}