aboutsummaryrefslogtreecommitdiff
path: root/release
diff options
context:
space:
mode:
authorUlf Lilleengen <[email protected]>2025-08-15 13:30:21 +0200
committerUlf Lilleengen <[email protected]>2025-08-25 19:44:50 +0200
commit864d29bfe1475d52e7da6385ee8123b41184c833 (patch)
treea49807b82f3d750ddbaeb93aec79a695afffaaa8 /release
parent3d004734a2a1db07d0e990462bb3fd5f04d3c7a0 (diff)
fix: update
Diffstat (limited to 'release')
-rw-r--r--release/Cargo.toml7
-rw-r--r--release/config.toml2
-rw-r--r--release/src/cargo.rs194
-rw-r--r--release/src/main.rs52
-rw-r--r--release/src/semver_check.rs110
5 files changed, 334 insertions, 31 deletions
diff --git a/release/Cargo.toml b/release/Cargo.toml
index 52ca01c2e..461021e0a 100644
--- a/release/Cargo.toml
+++ b/release/Cargo.toml
@@ -14,3 +14,10 @@ anyhow = "1"
14petgraph = "0.8.2" 14petgraph = "0.8.2"
15semver = "1.0.26" 15semver = "1.0.26"
16cargo-semver-checks = "0.43.0" 16cargo-semver-checks = "0.43.0"
17log = "0.4"
18simple_logger = "5.0.0"
19temp-file = "0.1.9"
20flate2 = "1.1.1"
21
22[patch.crates-io]
23cargo-semver-checks = { path = "../../cargo-semver-checks" }
diff --git a/release/config.toml b/release/config.toml
index 6b23217fa..f572b450c 100644
--- a/release/config.toml
+++ b/release/config.toml
@@ -27,7 +27,7 @@ embassy-net-ppp = { }
27embassy-net-esp-hosted = {} 27embassy-net-esp-hosted = {}
28embassy-net-driver-channel = {} 28embassy-net-driver-channel = {}
29embassy-net-wiznet = {} 29embassy-net-wiznet = {}
30embassy-net-nrf91 = { features = ["defmt", "nrf9160"] } 30embassy-net-nrf91 = { features = ["defmt", "nrf-pac/nrf9160"] }
31embassy-net-driver = {} 31embassy-net-driver = {}
32embassy-net-tuntap = {} 32embassy-net-tuntap = {}
33embassy-net-adin1110 = {} 33embassy-net-adin1110 = {}
diff --git a/release/src/cargo.rs b/release/src/cargo.rs
new file mode 100644
index 000000000..1a4f79f20
--- /dev/null
+++ b/release/src/cargo.rs
@@ -0,0 +1,194 @@
1//! Tools for working with Cargo.
2
3use std::{
4 ffi::OsStr,
5 path::{Path, PathBuf},
6 process::{Command, Stdio},
7};
8
9use anyhow::{bail, Context as _, Result};
10use clap::ValueEnum as _;
11use serde::{Deserialize, Serialize};
12use toml_edit::{DocumentMut, Formatted, Item, Value};
13
14use crate::{windows_safe_path, Crate};
15
16#[derive(Clone, Debug, PartialEq)]
17pub enum CargoAction {
18 Build(PathBuf),
19 Run,
20}
21
22#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
23pub struct Artifact {
24 pub executable: PathBuf,
25}
26
27/// Execute cargo with the given arguments and from the specified directory.
28pub fn run(args: &[String], cwd: &Path) -> Result<()> {
29 run_with_env::<[(&str, &str); 0], _, _>(args, cwd, [], false)?;
30 Ok(())
31}
32
33/// Execute cargo with the given arguments and from the specified directory.
34pub fn run_with_env<I, K, V>(args: &[String], cwd: &Path, envs: I, capture: bool) -> Result<String>
35where
36 I: IntoIterator<Item = (K, V)> + core::fmt::Debug,
37 K: AsRef<OsStr>,
38 V: AsRef<OsStr>,
39{
40 if !cwd.is_dir() {
41 bail!("The `cwd` argument MUST be a directory");
42 }
43
44 // Make sure to not use a UNC as CWD!
45 // That would make `OUT_DIR` a UNC which will trigger things like the one fixed in https://github.com/dtolnay/rustversion/pull/51
46 // While it's fixed in `rustversion` it's not fixed for other crates we are
47 // using now or in future!
48 let cwd = windows_safe_path(cwd);
49
50 println!(
51 "Running `cargo {}` in {:?} - Environment {:?}",
52 args.join(" "),
53 cwd,
54 envs
55 );
56
57 let mut command = Command::new(get_cargo());
58
59 command
60 .args(args)
61 .current_dir(cwd)
62 .envs(envs)
63 .stdout(if capture { Stdio::piped() } else { Stdio::inherit() })
64 .stderr(if capture { Stdio::piped() } else { Stdio::inherit() });
65
66 if args.iter().any(|a| a.starts_with('+')) {
67 // Make sure the right cargo runs
68 command.env_remove("CARGO");
69 }
70
71 let output = command.stdin(Stdio::inherit()).output()?;
72
73 // Make sure that we return an appropriate exit code here, as Github Actions
74 // requires this in order to function correctly:
75 if output.status.success() {
76 Ok(String::from_utf8_lossy(&output.stdout).to_string())
77 } else {
78 bail!("Failed to execute cargo subcommand `cargo {}`", args.join(" "),)
79 }
80}
81
82fn get_cargo() -> String {
83 // On Windows when executed via `cargo run` (e.g. via the xtask alias) the
84 // `cargo` on the search path is NOT the cargo-wrapper but the `cargo` from the
85 // toolchain - that one doesn't understand `+toolchain`
86 #[cfg(target_os = "windows")]
87 let cargo = if let Ok(cargo) = std::env::var("CARGO_HOME") {
88 format!("{cargo}/bin/cargo")
89 } else {
90 String::from("cargo")
91 };
92
93 #[cfg(not(target_os = "windows"))]
94 let cargo = String::from("cargo");
95
96 cargo
97}
98
99#[derive(Debug, Default)]
100pub struct CargoArgsBuilder {
101 toolchain: Option<String>,
102 subcommand: String,
103 target: Option<String>,
104 features: Vec<String>,
105 args: Vec<String>,
106}
107
108impl CargoArgsBuilder {
109 #[must_use]
110 pub fn toolchain<S>(mut self, toolchain: S) -> Self
111 where
112 S: Into<String>,
113 {
114 self.toolchain = Some(toolchain.into());
115 self
116 }
117
118 #[must_use]
119 pub fn subcommand<S>(mut self, subcommand: S) -> Self
120 where
121 S: Into<String>,
122 {
123 self.subcommand = subcommand.into();
124 self
125 }
126
127 #[must_use]
128 pub fn target<S>(mut self, target: S) -> Self
129 where
130 S: Into<String>,
131 {
132 self.target = Some(target.into());
133 self
134 }
135
136 #[must_use]
137 pub fn features(mut self, features: &[String]) -> Self {
138 self.features = features.to_vec();
139 self
140 }
141
142 #[must_use]
143 pub fn arg<S>(mut self, arg: S) -> Self
144 where
145 S: Into<String>,
146 {
147 self.args.push(arg.into());
148 self
149 }
150
151 #[must_use]
152 pub fn args<S>(mut self, args: &[S]) -> Self
153 where
154 S: Clone + Into<String>,
155 {
156 for arg in args {
157 self.args.push(arg.clone().into());
158 }
159 self
160 }
161
162 pub fn add_arg<S>(&mut self, arg: S) -> &mut Self
163 where
164 S: Into<String>,
165 {
166 self.args.push(arg.into());
167 self
168 }
169
170 #[must_use]
171 pub fn build(&self) -> Vec<String> {
172 let mut args = vec![];
173
174 if let Some(ref toolchain) = self.toolchain {
175 args.push(format!("+{toolchain}"));
176 }
177
178 args.push(self.subcommand.clone());
179
180 if let Some(ref target) = self.target {
181 args.push(format!("--target={target}"));
182 }
183
184 if !self.features.is_empty() {
185 args.push(format!("--features={}", self.features.join(",")));
186 }
187
188 for arg in self.args.iter() {
189 args.push(arg.clone());
190 }
191
192 args
193 }
194}
diff --git a/release/src/main.rs b/release/src/main.rs
index 769611c78..0dbcc5d45 100644
--- a/release/src/main.rs
+++ b/release/src/main.rs
@@ -1,3 +1,4 @@
1use simple_logger::SimpleLogger;
1use std::collections::{BTreeMap, HashMap}; 2use std::collections::{BTreeMap, HashMap};
2use std::fs; 3use std::fs;
3use std::path::{Path, PathBuf}; 4use std::path::{Path, PathBuf};
@@ -11,6 +12,8 @@ use petgraph::{Directed, Direction};
11use toml_edit::{DocumentMut, Item, Value}; 12use toml_edit::{DocumentMut, Item, Value};
12use types::*; 13use types::*;
13 14
15mod cargo;
16mod semver_check;
14mod types; 17mod types;
15 18
16/// Tool to traverse and operate on intra-repo Rust crate dependencies 19/// Tool to traverse and operate on intra-repo Rust crate dependencies
@@ -70,7 +73,7 @@ fn load_release_config(repo: &Path) -> ReleaseConfig {
70} 73}
71 74
72fn update_version(c: &mut Crate, new_version: &str) -> Result<()> { 75fn update_version(c: &mut Crate, new_version: &str) -> Result<()> {
73 let path = &c.path; 76 let path = c.path.join("Cargo.toml");
74 c.version = new_version.to_string(); 77 c.version = new_version.to_string();
75 let content = fs::read_to_string(&path)?; 78 let content = fs::read_to_string(&path)?;
76 let mut doc: DocumentMut = content.parse()?; 79 let mut doc: DocumentMut = content.parse()?;
@@ -84,7 +87,7 @@ fn update_version(c: &mut Crate, new_version: &str) -> Result<()> {
84} 87}
85 88
86fn update_versions(to_update: &Crate, dep: &CrateId, new_version: &str) -> Result<()> { 89fn update_versions(to_update: &Crate, dep: &CrateId, new_version: &str) -> Result<()> {
87 let path = &to_update.path; 90 let path = to_update.path.join("Cargo.toml");
88 let content = fs::read_to_string(&path)?; 91 let content = fs::read_to_string(&path)?;
89 let mut doc: DocumentMut = content.parse()?; 92 let mut doc: DocumentMut = content.parse()?;
90 let mut changed = false; 93 let mut changed = false;
@@ -117,15 +120,16 @@ fn update_versions(to_update: &Crate, dep: &CrateId, new_version: &str) -> Resul
117 Ok(()) 120 Ok(())
118} 121}
119 122
120fn list_crates(path: &PathBuf) -> Result<BTreeMap<CrateId, Crate>> { 123fn list_crates(root: &PathBuf) -> Result<BTreeMap<CrateId, Crate>> {
121 let d = std::fs::read_dir(path)?; 124 let d = std::fs::read_dir(root)?;
122 let release_config = load_release_config(path); 125 let release_config = load_release_config(root);
123 let mut crates = BTreeMap::new(); 126 let mut crates = BTreeMap::new();
124 for c in d { 127 for c in d {
125 let entry = c?; 128 let entry = c?;
126 let name = entry.file_name().to_str().unwrap().to_string(); 129 let name = entry.file_name().to_str().unwrap().to_string();
127 if entry.file_type()?.is_dir() && name.starts_with("embassy-") { 130 if entry.file_type()?.is_dir() && name.starts_with("embassy-") {
128 let entry = entry.path().join("Cargo.toml"); 131 let path = root.join(entry.path());
132 let entry = path.join("Cargo.toml");
129 if entry.exists() { 133 if entry.exists() {
130 let content = fs::read_to_string(&entry)?; 134 let content = fs::read_to_string(&entry)?;
131 let parsed: ParsedCrate = toml::from_str(&content)?; 135 let parsed: ParsedCrate = toml::from_str(&content)?;
@@ -138,7 +142,6 @@ fn list_crates(path: &PathBuf) -> Result<BTreeMap<CrateId, Crate>> {
138 } 142 }
139 } 143 }
140 144
141 let path = path.join(entry);
142 if let Some(config) = release_config.get(&id) { 145 if let Some(config) = release_config.get(&id) {
143 crates.insert( 146 crates.insert(
144 id.clone(), 147 id.clone(),
@@ -191,6 +194,7 @@ fn build_graph(crates: &BTreeMap<CrateId, Crate>) -> (Graph<CrateId, ()>, HashMa
191} 194}
192 195
193fn main() -> Result<()> { 196fn main() -> Result<()> {
197 SimpleLogger::new().init().unwrap();
194 let args = Args::parse(); 198 let args = Args::parse();
195 199
196 let root = args.repo.canonicalize()?; 200 let root = args.repo.canonicalize()?;
@@ -306,7 +310,7 @@ fn main() -> Result<()> {
306 let mut args: Vec<String> = vec![ 310 let mut args: Vec<String> = vec![
307 "publish".to_string(), 311 "publish".to_string(),
308 "--manifest-path".to_string(), 312 "--manifest-path".to_string(),
309 c.path.display().to_string(), 313 c.path.join("Cargo.toml").display().to_string(),
310 ]; 314 ];
311 315
312 if let Some(features) = &c.config.features { 316 if let Some(features) = &c.config.features {
@@ -337,26 +341,9 @@ fn main() -> Result<()> {
337} 341}
338 342
339fn check_semver(c: &Crate) -> Result<()> { 343fn check_semver(c: &Crate) -> Result<()> {
340 let mut args: Vec<String> = vec![ 344 let min_version = semver_check::minimum_update(c)?;
341 "semver-checks".to_string(), 345 println!("Version should be bumped to {:?}", min_version);
342 "--manifest-path".to_string(), 346 Ok(())
343 c.path.display().to_string(),
344 "--default-features".to_string(),
345 ];
346 if let Some(features) = &c.config.features {
347 args.push("--features".into());
348 args.push(features.join(","));
349 }
350
351 let status = ProcessCommand::new("cargo").args(&args).output()?;
352
353 println!("{}", core::str::from_utf8(&status.stdout).unwrap());
354 eprintln!("{}", core::str::from_utf8(&status.stderr).unwrap());
355 if !status.status.success() {
356 return Err(anyhow!("semver check failed"));
357 } else {
358 Ok(())
359 }
360} 347}
361 348
362fn update_changelog(repo: &Path, c: &Crate) -> Result<()> { 349fn update_changelog(repo: &Path, c: &Crate) -> Result<()> {
@@ -366,7 +353,7 @@ fn update_changelog(repo: &Path, c: &Crate) -> Result<()> {
366 "--config".to_string(), 353 "--config".to_string(),
367 repo.join("release").join("release.toml").display().to_string(), 354 repo.join("release").join("release.toml").display().to_string(),
368 "--manifest-path".to_string(), 355 "--manifest-path".to_string(),
369 c.path.display().to_string(), 356 c.path.join("Cargo.toml").display().to_string(),
370 "--execute".to_string(), 357 "--execute".to_string(),
371 "--no-confirm".to_string(), 358 "--no-confirm".to_string(),
372 ]; 359 ];
@@ -386,7 +373,7 @@ fn publish_release(_repo: &Path, c: &Crate, push: bool) -> Result<()> {
386 let mut args: Vec<String> = vec![ 373 let mut args: Vec<String> = vec![
387 "publish".to_string(), 374 "publish".to_string(),
388 "--manifest-path".to_string(), 375 "--manifest-path".to_string(),
389 c.path.display().to_string(), 376 c.path.join("Cargo.toml").display().to_string(),
390 ]; 377 ];
391 378
392 if let Some(features) = &c.config.features { 379 if let Some(features) = &c.config.features {
@@ -415,3 +402,8 @@ fn publish_release(_repo: &Path, c: &Crate, push: bool) -> Result<()> {
415 Ok(()) 402 Ok(())
416 } 403 }
417} 404}
405
406/// Make the path "Windows"-safe
407pub fn windows_safe_path(path: &Path) -> PathBuf {
408 PathBuf::from(path.to_str().unwrap().to_string().replace("\\\\?\\", ""))
409}
diff --git a/release/src/semver_check.rs b/release/src/semver_check.rs
new file mode 100644
index 000000000..b43164334
--- /dev/null
+++ b/release/src/semver_check.rs
@@ -0,0 +1,110 @@
1use std::fs;
2use std::io::Write;
3use std::path::{Path, PathBuf};
4
5use cargo_semver_checks::{Check, GlobalConfig, ReleaseType, Rustdoc};
6
7use crate::cargo::CargoArgsBuilder;
8use crate::types::Crate;
9use crate::windows_safe_path;
10
11/// Return the minimum required bump for the next release.
12/// Even if nothing changed this will be [ReleaseType::Patch]
13pub fn minimum_update(krate: &Crate) -> Result<ReleaseType, anyhow::Error> {
14 println!("Crate = {:?}", krate);
15
16 let package_name = krate.name.clone();
17 let package_path = krate.path.clone();
18 let current_path = build_doc_json(krate)?;
19
20 let baseline = Rustdoc::from_registry_latest_crate_version();
21 let doc = Rustdoc::from_path(&current_path);
22 let mut semver_check = Check::new(doc);
23 semver_check.with_default_features();
24 semver_check.set_baseline(baseline);
25 semver_check.set_packages(vec![package_name]);
26 if let Some(features) = &krate.config.features {
27 let extra_current_features = features.clone();
28 let extra_baseline_features = features.clone();
29 semver_check.set_extra_features(extra_current_features, extra_baseline_features);
30 }
31 if let Some(target) = &krate.config.target {
32 semver_check.set_build_target(target.clone());
33 }
34 let mut cfg = GlobalConfig::new();
35 cfg.set_log_level(Some(log::Level::Trace));
36 let result = semver_check.check_release(&mut cfg)?;
37 log::info!("Result {:?}", result);
38
39 let mut min_required_update = ReleaseType::Patch;
40 for (_, report) in result.crate_reports() {
41 if let Some(required_bump) = report.required_bump() {
42 let required_is_stricter =
43 (min_required_update == ReleaseType::Patch) || (required_bump == ReleaseType::Major);
44 if required_is_stricter {
45 min_required_update = required_bump;
46 }
47 }
48 }
49
50 Ok(min_required_update)
51}
52
53pub(crate) fn build_doc_json(krate: &Crate) -> Result<PathBuf, anyhow::Error> {
54 let target_dir = std::env::var("CARGO_TARGET_DIR");
55
56 let target_path = if let Ok(target) = target_dir {
57 PathBuf::from(target)
58 } else {
59 PathBuf::from(&krate.path).join("target")
60 };
61
62 let current_path = target_path;
63 let current_path = if let Some(target) = &krate.config.target {
64 current_path.join(target.clone())
65 } else {
66 current_path
67 };
68 let current_path = current_path
69 .join("doc")
70 .join(format!("{}.json", krate.name.to_string().replace("-", "_")));
71
72 std::fs::remove_file(&current_path).ok();
73 let features = if let Some(features) = &krate.config.features {
74 features.clone()
75 } else {
76 vec![]
77 };
78
79 log::info!("Building doc json for {} with features: {:?}", krate.name, features);
80
81 let envs = vec![(
82 "RUSTDOCFLAGS",
83 "--cfg docsrs --cfg not_really_docsrs --cfg semver_checks",
84 )];
85
86 // always use `specific nightly` toolchain so we don't have to deal with potentially
87 // different versions of the doc-json
88 let cargo_builder = CargoArgsBuilder::default()
89 .toolchain("nightly-2025-06-29")
90 .subcommand("rustdoc")
91 .features(&features);
92 let cargo_builder = if let Some(target) = &krate.config.target {
93 cargo_builder.target(target.clone())
94 } else {
95 cargo_builder
96 };
97
98 let cargo_builder = cargo_builder
99 .arg("-Zunstable-options")
100 .arg("-Zhost-config")
101 .arg("-Ztarget-applies-to-host")
102 .arg("--lib")
103 .arg("--output-format=json")
104 .arg("-Zbuild-std=alloc,core")
105 .arg("--config=host.rustflags=[\"--cfg=instability_disable_unstable_docs\"]");
106 let cargo_args = cargo_builder.build();
107 log::debug!("{cargo_args:#?}");
108 crate::cargo::run_with_env(&cargo_args, &krate.path, envs, false)?;
109 Ok(current_path)
110}