aboutsummaryrefslogtreecommitdiff
path: root/embassy-release/src/main.rs
blob: 321d3872c603a6ddabddd083dbf29856b2db8092 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command as ProcessCommand;

use clap::{Parser, Subcommand, ValueEnum};
use regex::Regex;
use serde::Deserialize;
use toml_edit::{DocumentMut, Item, Value};
use walkdir::WalkDir;

/// Tool to traverse and operate on intra-repo Rust crate dependencies
#[derive(Parser, Debug)]
#[command(author, version, about)]
struct Args {
    /// Path to the root crate
    #[arg(value_name = "CRATE_PATH")]
    crate_path: PathBuf,

    /// Command to perform on each crate
    #[command(subcommand)]
    command: Command,

    /// Traversal order
    #[arg(short, long, default_value = "post")]
    order: TraversalOrder,
}

#[derive(Debug, Clone, ValueEnum, PartialEq)]
enum TraversalOrder {
    Pre,
    Post,
}

#[derive(Debug, Subcommand)]
enum Command {
    /// Print all dependencies
    Dependencies,

    /// Release crate
    Release {
        #[command(subcommand)]
        kind: ReleaseKind,
    },
}

#[derive(Debug, Subcommand, Clone, Copy, PartialEq)]
enum ReleaseKind {
    Patch,
    Minor,
}

#[derive(Debug, Deserialize)]
struct CargoToml {
    package: Option<Package>,
    dependencies: Option<Deps>,
}

#[derive(Debug, Deserialize)]
struct Package {
    name: String,
    version: Option<String>,
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum Dep {
    Version(String),
    DetailedTable(HashMap<String, toml::Value>),
}

type Deps = std::collections::HashMap<String, Dep>;

#[derive(Debug, Deserialize)]
struct CrateConfig {
    features: Option<Vec<String>>,
    target: Option<String>,
}

type ReleaseConfig = HashMap<String, CrateConfig>;

fn find_path_deps(cargo_path: &Path) -> Vec<PathBuf> {
    let content = fs::read_to_string(cargo_path).unwrap_or_else(|_| {
        panic!("Failed to read {:?}", cargo_path);
    });
    let parsed: CargoToml = toml::from_str(&content).unwrap_or_else(|e| {
        panic!("Failed to parse {:?}: {}", cargo_path, e);
    });

    let mut paths = vec![];
    if let Some(deps) = parsed.dependencies {
        for (_name, dep) in deps {
            match dep {
                Dep::Version(_) => {
                    // External dependency — skip
                }
                Dep::DetailedTable(table) => {
                    if let Some(toml::Value::String(path)) = table.get("path") {
                        let dep_path = cargo_path.parent().unwrap().join(path).canonicalize().unwrap();
                        paths.push(dep_path);
                    }
                }
            }
        }
    }

    paths
}

fn visit_recursive(
    root_crate: &Path,
    visited: &mut HashSet<PathBuf>,
    output: &mut Vec<PathBuf>,
    order: &TraversalOrder,
) {
    if !visited.insert(root_crate.to_path_buf()) {
        return;
    }

    let cargo_toml = root_crate.join("Cargo.toml");
    let deps = find_path_deps(&cargo_toml);

    if *order == TraversalOrder::Pre {
        output.push(root_crate.to_path_buf());
    }

    let mut deps_sorted = deps;
    deps_sorted.sort();
    for dep in deps_sorted {
        visit_recursive(&dep, visited, output, order);
    }

    if *order == TraversalOrder::Post {
        output.push(root_crate.to_path_buf());
    }
}

fn get_crate_metadata(crate_path: &Path) -> Option<(String, String)> {
    let cargo_toml = crate_path.join("Cargo.toml");
    let content = fs::read_to_string(&cargo_toml).ok()?;
    let parsed: CargoToml = toml::from_str(&content).ok()?;
    let pkg = parsed.package?;
    let name = pkg.name;
    let version = pkg.version?;
    Some((name, version))
}

fn load_release_config() -> ReleaseConfig {
    let config_path = PathBuf::from("release/config.toml");
    if !config_path.exists() {
        return HashMap::new();
    }
    let content = fs::read_to_string(&config_path).expect("Failed to read release/config.toml");
    toml::from_str(&content).expect("Invalid TOML format in release/config.toml")
}

fn bump_dependency_versions(crate_name: &str, new_version: &str) -> Result<(), String> {
    let mut cargo_files: Vec<PathBuf> = WalkDir::new(".")
        .into_iter()
        .filter_map(Result::ok)
        .filter(|e| e.file_name() == "Cargo.toml")
        .map(|e| e.into_path())
        .collect();

    cargo_files.sort();

    for path in cargo_files {
        let content = fs::read_to_string(&path).map_err(|e| format!("Failed to read {}: {}", path.display(), e))?;

        let mut doc: DocumentMut = content
            .parse()
            .map_err(|e| format!("Failed to parse TOML in {}: {}", path.display(), e))?;

        let mut changed = false;

        for section in ["dependencies", "dev-dependencies", "build-dependencies"] {
            if let Some(Item::Table(dep_table)) = doc.get_mut(section) {
                if let Some(item) = dep_table.get_mut(crate_name) {
                    match item {
                        // e.g., foo = "0.1.0"
                        Item::Value(Value::String(_)) => {
                            *item = Item::Value(Value::from(new_version));
                            changed = true;
                        }
                        // e.g., foo = { version = "...", ... }
                        Item::Value(Value::InlineTable(inline)) => {
                            if inline.contains_key("version") {
                                inline["version"] = Value::from(new_version);
                                changed = true;
                            }
                        }
                        _ => {} // Leave unusual formats untouched
                    }
                }
            }
        }

        if changed {
            fs::write(&path, doc.to_string()).map_err(|e| format!("Failed to write {}: {}", path.display(), e))?;
            println!("🔧 Updated {} to {} in {}", crate_name, new_version, path.display());
        }
    }

    Ok(())
}

fn run_release_command(
    crate_path: &Path,
    crate_name: &str,
    version: &str,
    kind: &ReleaseKind,
    config: Option<&CrateConfig>,
) -> Result<(), String> {
    let kind_str = match kind {
        ReleaseKind::Patch => "patch",
        ReleaseKind::Minor => "minor",
    };

    if *kind == ReleaseKind::Minor {
        bump_dependency_versions(crate_name, version)?;
    }

    let mut args: Vec<String> = vec!["release".into(), kind_str.into()];

    if let Some(cfg) = config {
        if let Some(features) = &cfg.features {
            args.push("--features".into());
            args.push(features.join(","));
        }
        if let Some(target) = &cfg.target {
            args.push("--target".into());
            args.push(target.clone());
        }
    }

    let status = ProcessCommand::new("cargo")
        .args(&args)
        .current_dir(crate_path)
        .status()
        .map_err(|e| format!("Failed to run cargo release: {}", e))?;

    if !status.success() {
        return Err(format!("`cargo release {}` failed in crate {}", kind_str, crate_name));
    }

    //args.push("--execute".into());
    //let status = ProcessCommand::new("cargo")
    //    .args(&args)
    //    .current_dir(crate_path)
    //    .status()
    //    .map_err(|e| format!("Failed to run cargo release --execute: {}", e))?;

    //if !status.success() {
    //    return Err(format!(
    //        "`cargo release {kind_str} --execute` failed in crate {crate_name}"
    //    ));
    //}

    Ok(())
}

fn main() {
    let args = Args::parse();
    let root = args.crate_path.canonicalize().expect("Invalid root crate path");

    match args.command {
        Command::Dependencies => {
            let mut visited = HashSet::new();
            let mut ordered = vec![];
            visit_recursive(&root, &mut visited, &mut ordered, &args.order);
            for path in ordered {
                if let Some((name, _)) = get_crate_metadata(&path) {
                    println!("{name}");
                } else {
                    eprintln!("Warning: could not read crate name from {:?}", path);
                }
            }
        }
        Command::Release { kind } => {
            let config = load_release_config();
            let path = root;
            match get_crate_metadata(&path) {
                Some((name, version)) => {
                    println!("🚀 Releasing {name}...");
                    let crate_cfg = config.get(&name);
                    match run_release_command(&path, &name, &version, &kind, crate_cfg) {
                        Ok(_) => {
                            println!("✅ Released {name}");
                        }
                        Err(e) => {
                            eprintln!("❌ Error releasing {name}:\n{e}");
                            eprintln!("\nYou may retry with: `cargo run -- {path:?} release {kind:?}`");
                            std::process::exit(1);
                        }
                    }
                }
                None => {
                    eprintln!("Warning: Could not parse crate metadata in {:?}", path);
                }
            }
        }
    }
}