-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.dart
209 lines (177 loc) · 6 KB
/
main.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
// Copyright (c) 2023, 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:js_interop';
import 'dart:js_util';
import 'package:path/path.dart' as path;
import 'node/actions/core.dart';
import 'node/actions/exec.dart';
import 'node/actions/http_client.dart';
import 'node/actions/tool_cache.dart';
import 'node/fs.dart';
import 'node/os.dart';
import 'node/process.dart';
void main(List<String> args) async {
try {
// sdk
var sdk = core.getInput('sdk');
if (sdk.isEmpty) {
sdk = 'stable';
}
// flavor
var flavor = core.getInput('flavor');
if (flavor.isEmpty) {
flavor = sdk == 'main' ? 'raw' : 'release';
} else if (flavor != 'raw' && flavor != 'release') {
core.setFailed("Unrecognized build flavor '$flavor'.");
return;
}
final raw = flavor == 'raw';
// os
final os = getPlatform();
// architecture
var architecture = core.getInput('architecture');
if (architecture.isEmpty) {
architecture = getArch();
}
// calculate version and channel
String version;
String channel;
if (sdk == 'stable' || sdk == 'beta' || sdk == 'dev') {
channel = sdk;
version =
raw ? 'latest' : (await latestPublishedVersion(channel, flavor));
} else if (sdk == 'main') {
// Check for `main` first and fall back to `be`. This handles the channel
// rename from `be` to `main` (also tracked as b/299435467).
try {
channel = 'main';
await latestPublishedVersion(channel, flavor);
} catch (_) {
channel = 'be';
}
version = 'latest';
} else {
version = sdk;
// Derive the channel from the version string.
if (sdk.contains('dev')) {
channel = 'dev';
} else if (sdk.contains('beta')) {
channel = 'beta';
} else if (sdk.contains('main')) {
core.setFailed('Versions cannot be specified for main channel builds.');
return;
} else {
channel = 'stable';
}
}
core.info('Installing the $os-$architecture Dart SDK version $version from '
'the $channel ($flavor) channel.');
// Calculate url based on https://dart.dev/tools/sdk/archive#download-urls.
final url = 'https://storage.googleapis.com/dart-archive/'
'channels/$channel/$flavor/$version/sdk/'
'dartsdk-$os-$architecture-release.zip';
// Use a cached sdk or download and cache the sdk; using a 'raw' sdk flavor
// disables caching.
final toolName = raw ? 'dart_raw' : 'dart';
var sdkPath = !raw ? toolCache.find(toolName, version, architecture) : '';
if (sdkPath.isNotEmpty) {
core.info('Using cached sdk from $sdkPath.');
} else {
core.info('$url ...');
final archivePath =
await promiseToFuture<String>(toolCache.downloadTool(url));
var extractedFolder =
await promiseToFuture<String>(toolCache.extractZip(archivePath));
extractedFolder = path.join(extractedFolder, 'dart-sdk');
sdkPath = await promiseToFuture<String>(
toolCache.cacheDir(extractedFolder, toolName, version, architecture));
}
final pubCache = path.join(
process.env(os == 'windows' ? 'USERPROFILE' : 'HOME')!, '.pub-cache');
core.exportVariable('DART_HOME', sdkPath);
core.addPath(path.join(sdkPath, 'bin'));
core.exportVariable('PUB_CACHE', pubCache);
core.addPath(path.join(pubCache, 'bin'));
// Create the OIDC token used for pub.dev publishing.
await createPubOIDCToken();
// Configure the outputs.
if (raw) {
core.setOutput('dart-version', getVersionFromSdk(sdkPath));
} else {
core.setOutput('dart-version', version);
}
// Report success; print version.
await promiseToFuture(exec.exec(
'dart',
['--version'.toJS].toJS,
));
} catch (error) {
core.setFailed('$error');
}
}
String getVersionFromSdk(String sdkPath) {
final versionFilePath = path.join(sdkPath, 'version');
return fs.readFileSync(versionFilePath, 'utf8').trim();
}
/// Returns 'x64', 'ia32', 'arm', or 'arm64'.
String getArch() {
const supported = ['x64', 'ia32', 'arm', 'arm64'];
return supported.contains(os.arch) ? os.arch : 'x64';
}
/// Returns 'linux', 'windows', or 'macos'.
String getPlatform() {
return switch (os.platform) {
'win32' => 'windows',
'darwin' => 'macos',
_ => 'linux'
};
}
// When enabled through env variables, create an OIDC token for publishing on
// pub.dev.
Future<void> createPubOIDCToken() async {
final tokenRequestUrl = process.env('ACTIONS_ID_TOKEN_REQUEST_URL');
final tokenRequestToken = process.env('ACTIONS_ID_TOKEN_REQUEST_TOKEN');
if (tokenRequestUrl == null || tokenRequestToken == null) {
return;
}
final token =
await promiseToFuture<String>(core.getIDToken('https://pub.dev'));
core.exportVariable('PUB_TOKEN', token);
await promiseToFuture(exec.exec(
'dart',
[
'pub'.toJS,
'token'.toJS,
'add'.toJS,
'https://pub.dev'.toJS,
'--env-var'.toJS,
'PUB_TOKEN'.toJS,
].toJS,
));
}
// https://storage.googleapis.com/dart-archive/channels/stable/release/latest/VERSION
// {
// "date": "2023-02-07",
// "version": "2.19.2",
// "revision": "e46b4f59490230778e907bde2eedb06b062d31be"
// }
// Query google storage for the most recent published SDK version for the given
// channel and flavor.
Future<String> latestPublishedVersion(String channel, String flavor) async {
final url = 'https://storage.googleapis.com/dart-archive/channels/'
'$channel/$flavor/latest/VERSION';
final http = HttpClient(
'setup-dart',
<JSAny>[].toJS,
jsify({
'allowRedirects': true,
'maxRedirects': 3,
'allowRetries': true,
'maxRetries': 3,
}) as JSObject?,
);
var response = await promiseToFuture<JSObject>(http.getJson(url));
var result = getProperty<JSObject>(response, 'result');
return getProperty(result, 'version');
}