@@ -10,53 +10,45 @@ pub struct Release<'a> {
1010 workspace : & ' a Workspace ,
1111 /// The workspace version, such as `0.2.0`.
1212 version : String ,
13- /// The latest release tag `HEAD` contains, such as `v0.1.0`; `None` before
14- /// the first release.
15- previous : Option < String > ,
1613 /// Runs the checks and the publish dry run, and only prints what the other
1714 /// steps would do.
1815 dry_run : bool ,
1916}
2017
2118impl < ' a > Release < ' a > {
2219 pub fn new ( workspace : & ' a Workspace , dry_run : bool ) -> Result < Self > {
23- let tags = workspace. git ( & [
24- "tag" ,
25- "--list" ,
26- "v*" ,
27- "--merged" ,
28- "HEAD" ,
29- "--sort=-v:refname" ,
30- ] ) ?;
3120 Ok ( Self {
3221 workspace,
3322 version : workspace. version ( ) ?. to_owned ( ) ,
34- previous : tags. lines ( ) . next ( ) . map ( str:: to_owned) ,
3523 dry_run,
3624 } )
3725 }
3826
3927 /// Checks that `HEAD` is the latest `origin/main`, that the version isn't
40- /// tagged yet, and that the crates' API changes fit it. Then publishes the
41- /// SDKs, tags `HEAD` `v<version>`, pushes the tag, creates its GitHub
42- /// release, and closes its minor version's milestone, such as `v0.2`.
28+ /// tagged yet, and that the crates' API changes fit it. Then writes the
29+ /// release notes, publishes the SDKs, tags `HEAD` `v<version>`, pushes the
30+ /// tag, creates its GitHub release, and closes its minor version's
31+ /// milestone, such as `v0.2`.
4332 pub fn run ( & self ) -> Result < ( ) > {
4433 self . check ( ) ?;
45- self . semver ( ) ?;
34+ let previous = self . previous ( ) ?;
35+ let previous = previous. as_deref ( ) ;
36+ self . semver ( previous) ?;
37+ let tag = self . tag ( ) ;
38+ let notes = self
39+ . workspace
40+ . root ( )
41+ . join ( format ! ( "target/release-notes-{tag}.md" ) ) ;
42+ fs:: write ( & notes, self . notes ( previous) ?)
43+ . with_context ( || format ! ( "write {}" , notes. display( ) ) ) ?;
44+ println ! ( "the release notes are in {}" , notes. display( ) ) ;
4645 Publish {
4746 workspace : self . workspace ,
4847 dry_run : self . dry_run ,
4948 allow_dirty : false ,
5049 }
5150 . run ( ) ?;
52- let tag = self . tag ( ) ;
5351 let title = format ! ( "SourceOrigin {}" , self . version) ;
54- let notes = self
55- . workspace
56- . root ( )
57- . join ( format ! ( "target/release-notes-{tag}.md" ) ) ;
58- fs:: write ( & notes, self . notes ( ) ?) . with_context ( || format ! ( "write {}" , notes. display( ) ) ) ?;
59- println ! ( "the release notes are in {}" , notes. display( ) ) ;
6052 let notes = notes. to_string_lossy ( ) ;
6153 self . step ( "git" , & [ "tag" , "--annotate" , & tag, "--message" , & title] ) ?;
6254 self . step ( "git" , & [ "push" , "origin" , & tag] ) ?;
@@ -82,9 +74,10 @@ impl<'a> Release<'a> {
8274 }
8375
8476 /// Fails unless `HEAD` is the latest `origin/main`, and the version isn't
85- /// tagged on `origin` yet.
77+ /// tagged on `origin` yet. Fetches `origin`'s tags too, so the previous
78+ /// release is known.
8679 fn check ( & self ) -> Result < ( ) > {
87- self . workspace . git ( & [ "fetch" , "origin" , "main" ] ) ?;
80+ self . workspace . git ( & [ "fetch" , "--tags" , " origin", "main" ] ) ?;
8881 ensure ! (
8982 self . workspace. git( & [ "rev-parse" , "HEAD" ] ) ?
9083 == self . workspace. git( & [ "rev-parse" , "FETCH_HEAD" ] ) ?,
@@ -101,10 +94,27 @@ impl<'a> Release<'a> {
10194 Ok ( ( ) )
10295 }
10396
104- /// Fails when a published crate's API breaks compatibility with the previous
105- /// release without a version that allows it, by `cargo semver-checks`.
106- fn semver ( & self ) -> Result < ( ) > {
107- let Some ( previous) = & self . previous else {
97+ /// The latest release tag `HEAD` contains, such as `v0.1.0`, other than this
98+ /// release's own, which a failed earlier run may have left; `None` before
99+ /// the first release.
100+ fn previous ( & self ) -> Result < Option < String > > {
101+ let tag = self . tag ( ) ;
102+ let tags = self . workspace . git ( & [
103+ "tag" ,
104+ "--list" ,
105+ "v*" ,
106+ "--merged" ,
107+ "HEAD" ,
108+ "--sort=-v:refname" ,
109+ ] ) ?;
110+ Ok ( tags. lines ( ) . find ( |line| * line != tag) . map ( str:: to_owned) )
111+ }
112+
113+ /// Fails when a published crate's API breaks compatibility with the
114+ /// `previous` release without a version that allows it, by
115+ /// `cargo semver-checks`.
116+ fn semver ( & self , previous : Option < & str > ) -> Result < ( ) > {
117+ let Some ( previous) = previous else {
108118 println ! ( "no earlier release to check the crates' API against" ) ;
109119 return Ok ( ( ) ) ;
110120 } ;
@@ -122,9 +132,10 @@ impl<'a> Release<'a> {
122132 }
123133
124134 /// The release notes: what was published, then the pull requests merged
125- /// since the previous release, by the titles' Conventional Commits type.
126- fn notes ( & self ) -> Result < String > {
127- let npm = self . workspace . npm_package ( ) ?;
135+ /// since the `previous` release, by the titles' Conventional Commits type.
136+ /// The npm package is listed only when its version changed since then,
137+ /// since only then is it published.
138+ fn notes ( & self , previous : Option < & str > ) -> Result < String > {
128139 let mut notes = String :: from ( "Published:\n \n " ) ;
129140 for package in self . workspace . published ( ) {
130141 let ( name, version) = ( & package. name , & package. version ) ;
@@ -133,16 +144,19 @@ impl<'a> Release<'a> {
133144 "- [`{name}` {version}](https://crates.io/crates/{name}/{version}) on crates.io"
134145 ) ?;
135146 }
136- let ( name, version) = ( & npm. name , & npm. version ) ;
137- writeln ! (
138- notes,
139- "- [`{name}` {version}](https://www.npmjs.com/package/{name}/v/{version}) on npm"
140- ) ?;
147+ let npm = self . workspace . npm_package ( None ) ?;
148+ let released = previous
149+ . map ( |tag| self . workspace . npm_package ( Some ( tag) ) )
150+ . transpose ( ) ?;
151+ if released. is_none_or ( |released| released. version != npm. version ) {
152+ let ( name, version) = ( & npm. name , & npm. version ) ;
153+ writeln ! (
154+ notes,
155+ "- [`{name}` {version}](https://www.npmjs.com/package/{name}/v/{version}) on npm"
156+ ) ?;
157+ }
141158
142- let range = self
143- . previous
144- . as_ref ( )
145- . map_or_else ( || "HEAD" . to_owned ( ) , |tag| format ! ( "{tag}..HEAD" ) ) ;
159+ let range = previous. map_or_else ( || "HEAD" . to_owned ( ) , |tag| format ! ( "{tag}..HEAD" ) ) ;
146160 let log =
147161 self . workspace
148162 . git ( & [ "log" , "--first-parent" , "--reverse" , "--format=%s" , & range] ) ?;
0 commit comments