|
2 | 2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | 3 | // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
|
| 5 | +import 'dart:async'; |
5 | 6 | import 'dart:io'; |
6 | 7 |
|
7 | | -import '../exact_file_tweak.dart'; |
| 8 | +import 'package:path/path.dart' as p; |
| 9 | +import 'package:yaml/yaml.dart' as yaml; |
| 10 | + |
| 11 | +import '../repo_tweak.dart'; |
8 | 12 | import '../utils.dart'; |
9 | 13 |
|
10 | 14 | final _instance = AutoPublishTweak._(); |
11 | 15 |
|
12 | | -class AutoPublishTweak extends ExactFileTweak { |
| 16 | +class AutoPublishTweak extends RepoTweak { |
13 | 17 | factory AutoPublishTweak() => _instance; |
14 | 18 |
|
15 | 19 | AutoPublishTweak._() |
16 | 20 | : super( |
17 | 21 | id: 'auto-publish', |
18 | 22 | description: |
19 | | - 'configure a github action to enable package auto-publishing', |
20 | | - filePath: '.github/workflows/publish.yaml', |
| 23 | + 'configure github actions to enable package auto-publishing', |
21 | 24 | ); |
22 | 25 |
|
23 | 26 | @override |
24 | 27 | bool shouldRunByDefault(Directory checkout, String repoSlug) => |
25 | 28 | monoRepo(checkout, repoSlug); |
26 | 29 |
|
27 | 30 | @override |
28 | | - String expectedContent(Directory checkout, String repoSlug) { |
| 31 | + FutureOr<FixResult> fix(Directory checkout, String repoSlug) { |
29 | 32 | final org = repoSlug.split('/').first; |
30 | | - final branch = gitDefaultBranch(checkout) ?? 'main'; |
31 | | - final glob = singlePackageRepo(checkout) |
32 | | - ? "'v[0-9]+.[0-9]+.[0-9]+'" |
33 | | - : "'[A-z]+-v[0-9]+.[0-9]+.[0-9]+'"; |
34 | | - |
35 | | - // Substitute for the github org, default branch, and glob pattern values. |
36 | | - return publishContents |
37 | | - .replaceAll('{org}', org) |
38 | | - .replaceAll('{branch}', branch) |
39 | | - .replaceAll('{glob}', glob); |
| 33 | + final fixes = <String>[]; |
| 34 | + |
| 35 | + if (!monoRepo(checkout, repoSlug) && singlePackageRepo(checkout)) { |
| 36 | + const relPath = '.github/workflows/publish.yaml'; |
| 37 | + final pubspec = yaml.loadYaml( |
| 38 | + File(p.join(checkout.path, 'pubspec.yaml')).readAsStringSync(), |
| 39 | + ) as yaml.YamlMap?; |
| 40 | + if (pubspec != null && |
| 41 | + pubspec['publish_to'] != 'none' && |
| 42 | + pubspec['version'] != null) { |
| 43 | + final useFlutter = _usesFlutter(pubspec); |
| 44 | + final content = singlePackagePublishContents |
| 45 | + .replaceAll('{org}', org) |
| 46 | + .replaceAll('{use_flutter}', '$useFlutter'); |
| 47 | + _writeIfChanged(checkout, relPath, content, fixes); |
| 48 | + } |
| 49 | + } else { |
| 50 | + final legacyFile = File( |
| 51 | + p.join(checkout.path, '.github/workflows/publish.yaml'), |
| 52 | + ); |
| 53 | + if (legacyFile.existsSync()) { |
| 54 | + legacyFile.deleteSync(); |
| 55 | + fixes.add('.github/workflows/publish.yaml has been removed.'); |
| 56 | + } |
| 57 | + |
| 58 | + final packages = _findPublishablePackages(checkout); |
| 59 | + for (final pkg in packages) { |
| 60 | + final relPath = '.github/workflows/publish_${pkg.name}.yaml'; |
| 61 | + final content = monoRepoPackagePublishContents |
| 62 | + .replaceAll('{org}', org) |
| 63 | + .replaceAll('{package}', pkg.name) |
| 64 | + .replaceAll('{directory}', pkg.relativeDir) |
| 65 | + .replaceAll('{use_flutter}', '${pkg.useFlutter}'); |
| 66 | + _writeIfChanged(checkout, relPath, content, fixes); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return fixes.isEmpty ? FixResult.noFixesMade : FixResult(fixes: fixes); |
40 | 71 | } |
41 | 72 | } |
42 | 73 |
|
43 | | -const publishContents = r''' |
| 74 | +void _writeIfChanged( |
| 75 | + Directory checkout, |
| 76 | + String relPath, |
| 77 | + String content, |
| 78 | + List<String> fixes, |
| 79 | +) { |
| 80 | + final file = File(p.join(checkout.path, relPath)); |
| 81 | + if (!file.existsSync()) { |
| 82 | + file.parent.createSync(recursive: true); |
| 83 | + file.writeAsStringSync(content); |
| 84 | + fixes.add('$relPath has been created.'); |
| 85 | + } else if (file.readAsStringSync() != content) { |
| 86 | + file.writeAsStringSync(content); |
| 87 | + fixes.add('$relPath has been updated.'); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +List<_PackageInfo> _findPublishablePackages(Directory root) { |
| 92 | + final results = <_PackageInfo>[]; |
| 93 | + void walk(Directory dir) { |
| 94 | + final pubspecFile = File(p.join(dir.path, 'pubspec.yaml')); |
| 95 | + if (pubspecFile.existsSync()) { |
| 96 | + final map = |
| 97 | + yaml.loadYaml(pubspecFile.readAsStringSync()) as yaml.YamlMap?; |
| 98 | + if (map != null && |
| 99 | + map['publish_to'] != 'none' && |
| 100 | + map['version'] != null) { |
| 101 | + final name = map['name'] as String; |
| 102 | + final relDir = p.posix.joinAll( |
| 103 | + p.split(p.relative(dir.path, from: root.path)), |
| 104 | + ); |
| 105 | + results.add(_PackageInfo(name, relDir, _usesFlutter(map))); |
| 106 | + return; |
| 107 | + } |
| 108 | + if (dir.path != root.path) { |
| 109 | + return; |
| 110 | + } |
| 111 | + } |
| 112 | + for (final child in dir.listSync().whereType<Directory>()) { |
| 113 | + final base = p.basename(child.path); |
| 114 | + if (!base.startsWith('.') && |
| 115 | + base != 'example' && |
| 116 | + base != 'test' && |
| 117 | + base != 'test_data' && |
| 118 | + base != 'test_fixtures') { |
| 119 | + walk(child); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + walk(root); |
| 125 | + results.sort((a, b) => a.name.compareTo(b.name)); |
| 126 | + return results; |
| 127 | +} |
| 128 | + |
| 129 | +bool _usesFlutter(yaml.YamlMap? pubspec) { |
| 130 | + if (pubspec == null) return false; |
| 131 | + final deps = pubspec['dependencies'] as yaml.YamlMap?; |
| 132 | + final devDeps = pubspec['dev_dependencies'] as yaml.YamlMap?; |
| 133 | + final env = pubspec['environment'] as yaml.YamlMap?; |
| 134 | + return (deps != null && deps.containsKey('flutter')) || |
| 135 | + (devDeps != null && devDeps.containsKey('flutter_test')) || |
| 136 | + (env != null && env.containsKey('flutter')); |
| 137 | +} |
| 138 | + |
| 139 | +class _PackageInfo { |
| 140 | + final String name; |
| 141 | + final String relativeDir; |
| 142 | + final bool useFlutter; |
| 143 | + |
| 144 | + _PackageInfo(this.name, this.relativeDir, this.useFlutter); |
| 145 | +} |
| 146 | + |
| 147 | +const singlePackagePublishContents = r''' |
44 | 148 | # A CI configuration to auto-publish pub packages. |
45 | 149 |
|
46 | 150 | name: Publish |
47 | 151 |
|
48 | 152 | on: |
49 | | - pull_request: |
50 | | - branches: [ {branch} ] |
51 | 153 | push: |
52 | | - tags: [ {glob} ] |
| 154 | + tags: [ 'v[0-9]+.[0-9]+.[0-9]+*' ] |
| 155 | +
|
| 156 | +jobs: |
| 157 | + publish: |
| 158 | + if: ${{ github.repository_owner == '{org}' }} |
| 159 | + permissions: |
| 160 | + id-token: write # Required for authentication using OIDC |
| 161 | + uses: dart-lang/setup-dart/.github/workflows/publish.yml@a5bff6576dfc51b47eb77d4b7084bba6a4daf8d2 # main |
| 162 | + with: |
| 163 | + use-flutter: {use_flutter} |
| 164 | +'''; |
| 165 | + |
| 166 | +const monoRepoPackagePublishContents = r''' |
| 167 | +# A CI configuration to publish package:{package} to pub.dev. |
| 168 | +
|
| 169 | +name: Publish package:{package} |
| 170 | +
|
| 171 | +on: |
| 172 | + push: |
| 173 | + tags: |
| 174 | + - '{package}-v[0-9]+.[0-9]+.[0-9]+*' |
53 | 175 |
|
54 | 176 | jobs: |
55 | 177 | publish: |
56 | 178 | if: ${{ github.repository_owner == '{org}' }} |
57 | | - uses: dart-lang/ecosystem/.github/workflows/publish.yaml@main |
58 | 179 | permissions: |
59 | 180 | id-token: write # Required for authentication using OIDC |
60 | | - pull-requests: write # Required for writing the pull request note |
| 181 | + uses: dart-lang/setup-dart/.github/workflows/publish.yml@a5bff6576dfc51b47eb77d4b7084bba6a4daf8d2 # main |
| 182 | + with: |
| 183 | + working-directory: {directory} |
| 184 | + use-flutter: {use_flutter} |
61 | 185 | '''; |
0 commit comments