11import { spawnSync } from "node:child_process" ;
2- import { readFileSync } from "node:fs" ;
3- import { join } from "node:path" ;
2+ import { randomBytes } from "node:crypto" ;
3+ import { readFileSync , unlinkSync , writeFileSync } from "node:fs" ;
4+ import { tmpdir } from "node:os" ;
5+ import { dirname , join } from "node:path" ;
6+ import { fileURLToPath } from "node:url" ;
7+ import { getChangelogSectionForVersion } from "./parse-changelog-section.js" ;
8+
9+ const scriptDir = dirname ( fileURLToPath ( import . meta. url ) ) ;
10+
11+ const GRADLE_PLUGIN_ID = "dev.faststats.proguard-mappings-upload" ;
412
513if ( ! process . env . REPOSITORY_TOKEN ) {
614 console . log ( "Skipping Gradle plugin publish (REPOSITORY_TOKEN unset)" ) ;
715 process . exit ( 0 ) ;
816}
917
10- const pluginDir = join ( import . meta . dir , ".." , "packages" , "proguard-plugin" ) ;
18+ const pluginDir = join ( scriptDir , ".." , "packages" , "proguard-plugin" ) ;
1119const gradlew = join ( pluginDir , "gradlew" ) ;
1220
1321const result = spawnSync ( gradlew , [ "publish" ] , {
@@ -23,8 +31,20 @@ if (result.status !== 0) {
2331
2432const pkg = JSON . parse (
2533 readFileSync ( join ( pluginDir , "package.json" ) , "utf8" ) ,
26- ) as { name : string ; version : string } ;
27- const tag = `${ pkg . name } @${ pkg . version } ` ;
34+ ) as { version : string } ;
35+
36+ const tag = `${ GRADLE_PLUGIN_ID } @${ pkg . version } ` ;
37+ const title = `${ GRADLE_PLUGIN_ID } ${ pkg . version } ` ;
38+
39+ const changelogPath = join ( pluginDir , "CHANGELOG.md" ) ;
40+ const changelogBody = getChangelogSectionForVersion ( changelogPath , pkg . version ) ;
41+
42+ if ( ! changelogBody ) {
43+ console . error (
44+ `No ## ${ pkg . version } section in ${ changelogPath } . Run changeset version so Changesets updates the changelog.` ,
45+ ) ;
46+ process . exit ( 1 ) ;
47+ }
2848
2949if ( process . env . GITHUB_ACTIONS !== "true" ) {
3050 process . exit ( 0 ) ;
@@ -47,37 +67,51 @@ if (view.status === 0) {
4767 process . exit ( 0 ) ;
4868}
4969
70+ const notesPath = join (
71+ tmpdir ( ) ,
72+ `gh-release-notes-${ randomBytes ( 8 ) . toString ( "hex" ) } .md` ,
73+ ) ;
74+ writeFileSync ( notesPath , changelogBody , "utf8" ) ;
75+
5076const createArgs = [
5177 "release" ,
5278 "create" ,
5379 tag ,
5480 "--title" ,
55- ` ${ pkg . name } ${ pkg . version } (Gradle)` ,
56- "--notes" ,
57- "Gradle plugin published to Maven. Two coordinates are normal: the `proguard-plugin` jar and the plugin marker for `dev.faststats.proguard-mappings-upload` (required for the `plugins { id(...) }` block)." ,
81+ title ,
82+ "--notes-file " ,
83+ notesPath ,
5884] ;
5985if ( process . env . GITHUB_SHA ) {
6086 createArgs . push ( "--target" , process . env . GITHUB_SHA ) ;
6187}
6288
63- const created = spawnSync ( "gh" , createArgs , {
64- env : ghEnv ,
65- stdio : [ "inherit" , "inherit" , "pipe" ] ,
66- encoding : "utf8" ,
67- } ) ;
89+ try {
90+ const created = spawnSync ( "gh" , createArgs , {
91+ env : ghEnv ,
92+ stdio : [ "inherit" , "inherit" , "pipe" ] ,
93+ encoding : "utf8" ,
94+ } ) ;
6895
69- if ( created . status !== 0 ) {
70- const err = created . stderr ?? "" ;
71- if (
72- err . includes ( "already_exists" ) ||
73- err . toLowerCase ( ) . includes ( "already exists" )
74- ) {
75- process . exit ( 0 ) ;
96+ if ( created . status !== 0 ) {
97+ const err = created . stderr ?? "" ;
98+ if (
99+ err . includes ( "already_exists" ) ||
100+ err . toLowerCase ( ) . includes ( "already exists" )
101+ ) {
102+ process . exit ( 0 ) ;
103+ }
104+ if ( err ) {
105+ console . error ( err ) ;
106+ }
107+ process . exit ( created . status ?? 1 ) ;
76108 }
77- if ( err ) {
78- console . error ( err ) ;
109+ } finally {
110+ try {
111+ unlinkSync ( notesPath ) ;
112+ } catch {
113+ // ignore
79114 }
80- process . exit ( created . status ?? 1 ) ;
81115}
82116
83117process . exit ( 0 ) ;
0 commit comments