Skip to content

Commit 759b6e0

Browse files
committed
Add automatic publish and test scripts.
1 parent 524e9ce commit 759b6e0

File tree

5 files changed

+134
-0
lines changed

5 files changed

+134
-0
lines changed

.github/workflows/publish.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Publish
2+
3+
on:
4+
workflow_dispatch:
5+
6+
jobs:
7+
publish:
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- uses: actions/checkout@v2
12+
13+
- uses: subosito/flutter-action@v2
14+
with:
15+
flutter-version: '2.x'
16+
channel: 'stable'
17+
18+
- run: dart pub get
19+
20+
- run: dart run .scripts/publish.dart '${{ secrets.PUB_CREDENTIALS }}' ${{ secrets.GITHUB_TOKEN }}

.github/workflows/test.yaml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Test
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
test:
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- uses: actions/checkout@v2
12+
13+
- uses: subosito/flutter-action@v2
14+
with:
15+
flutter-version: '2.x'
16+
channel: 'stable'
17+
18+
- run: dart pub get
19+
20+
- run: dart run .scripts/test.dart

.scripts/publish.dart

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import 'dart:convert';
2+
import 'dart:io';
3+
4+
import 'package:http/http.dart' as http;
5+
import 'package:path/path.dart' as path;
6+
import 'package:yaml/yaml.dart';
7+
8+
import 'utils.dart';
9+
import 'test.dart' as test;
10+
11+
final changelogExp = RegExp(r'## +(.*?)\s*\n([\s\S]+?)\n\s*##');
12+
13+
Future<void> _dartPublish() async {
14+
await runProcess('dart', ['pub', 'publish', '-f']);
15+
}
16+
17+
Future<void> _githubRelease(String token, String version, String body) async {
18+
final response = await http.post(
19+
Uri.parse(
20+
'https://api.github.com/repos/fontsource/fontsource-flutter/releases'),
21+
headers: {
22+
'accept': 'toapplication/vnd.github.v3+json',
23+
'Authorization': 'token ${token}'
24+
},
25+
body: jsonEncode({'tag_name': 'v$version', 'body': body}),
26+
);
27+
print('GitHub Release: ${response.reasonPhrase}');
28+
final responseError = jsonDecode(response.body)['errors'];
29+
if (responseError != null) throw Exception(responseError);
30+
}
31+
32+
void main(List<String> args) async {
33+
await test.main();
34+
35+
final pubCredentials = args[0];
36+
final githubToken = args[1];
37+
38+
String version;
39+
String body;
40+
try {
41+
try {
42+
final matches =
43+
changelogExp.firstMatch(File('CHANGELOG.md').readAsStringSync());
44+
if (matches == null) throw Exception();
45+
final match1 = matches.group(1);
46+
if (match1 == null) throw Exception();
47+
final match2 = matches.group(2);
48+
if (match2 == null) throw Exception();
49+
version = match1;
50+
body = match2;
51+
} catch (e) {
52+
throw Exception('Invalid changelog.');
53+
}
54+
55+
if (version !=
56+
loadYaml(File('pubspec.yaml').readAsStringSync())['version']) {
57+
throw Exception('Pubspec version differs from changelog.');
58+
}
59+
60+
File(path.join(
61+
Platform.environment['HOME']!, '.config/dart/pub-credentials.json'))
62+
..createSync(recursive: true)
63+
..writeAsStringSync(pubCredentials);
64+
65+
await _dartPublish();
66+
67+
print('');
68+
69+
await _githubRelease(githubToken, version, body);
70+
} catch (e) {
71+
stderr.writeln(e);
72+
exit(1);
73+
}
74+
}

.scripts/test.dart

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import 'utils.dart';
2+
3+
Future<void> main() async {
4+
await runProcess('dart', ['pub', 'get'], 'example');
5+
await runProcess('dart', ['run', 'fontsource'], 'example');
6+
}

.scripts/utils.dart

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import 'dart:io';
2+
3+
Future<void> runProcess(String executable, List<String> arguments,
4+
[String? workingDirectory]) async {
5+
final process = await Process.start(
6+
executable,
7+
arguments,
8+
workingDirectory: workingDirectory,
9+
);
10+
stdout.addStream(process.stdout);
11+
stderr.addStream(process.stderr);
12+
final exitCode = await process.exitCode;
13+
if (exitCode != 0) exit(exitCode);
14+
}

0 commit comments

Comments
 (0)