|
| 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 | +} |
0 commit comments