-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathset_tlsn_version.rs
executable file
·128 lines (109 loc) · 3.94 KB
/
set_tlsn_version.rs
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
#!/usr/bin/env cargo +nightly -Zscript
---
[package]
name = "set_tlsn_version"
version = "0.0.0"
edition = "2021"
publish = false
[dependencies]
clap = { version = "4.0", features = ["derive"] }
serde_yaml = "0.9"
toml_edit = "0.22.22"
walkdir = "2.5.0"
---
// This scripts sets the TLSNotary version in all relevant files. Run it with:
// ./set_tlsn_version <version>
use clap::Parser;
use serde_yaml::Value;
use std::fs::{self, read_to_string};
use std::path::Path;
use toml_edit::{value, DocumentMut};
use walkdir::WalkDir;
#[derive(Parser)]
#[command(name = "set_tlsn_version")]
#[command(about = "Sets the TLSNotary version in all relevant files", long_about = None)]
struct Args {
/// Version number to set (example: 0.1.0-alpha.8)
version: String,
/// Workspace path (default is current directory)
#[arg(short, long, default_value = ".")]
workspace: String,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Args::parse();
// Process all Cargo.toml files in the workspace
for entry in WalkDir::new(&args.workspace)
.into_iter()
.filter_map(Result::ok)
.filter(|entry| entry.file_name() == "Cargo.toml")
{
if let Err(e) = update_version_in_cargo_toml(entry.path(), &args.version) {
eprintln!(
"Failed to update version in {}: {}",
entry.path().display(),
e
);
}
}
let open_api_path = Path::new(&args.workspace).join("crates/notary/server/openapi.yaml");
update_version_in_open_api(&open_api_path, &args.version)?;
println!("Version update process completed.");
Ok(())
}
/// Update the version in the Cargo.toml file
///
/// Skip files with "publish = false" or "version = 0.0.0"
fn update_version_in_cargo_toml(
cargo_toml_path: &Path,
new_version: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let cargo_toml_content = read_to_string(cargo_toml_path)
.map_err(|e| format!("Failed to read {}: {}", cargo_toml_path.display(), e))?;
let mut doc = cargo_toml_content.parse::<DocumentMut>().map_err(|e| {
format!(
"Invalid TOML format in {}: {}",
cargo_toml_path.display(),
e
)
})?;
if let Some(package) = doc.get_mut("package") {
if package.get("publish").and_then(|p| p.as_bool()) == Some(false) {
return Ok(());
}
if let Some(version) = package.get_mut("version") {
if version.as_str() == Some("0.0.0") {
return Err(format!(
"\"version\" is \"0.0.0\" and \"publish\" is true in {}",
cargo_toml_path.display()
)
.into());
}
*version = value(new_version);
fs::write(cargo_toml_path, doc.to_string())
.map_err(|e| format!("Failed to write {}: {}", cargo_toml_path.display(), e))?;
println!("Updated version in {}", cargo_toml_path.display());
}
}
Ok(())
}
/// Update the version in the OpenAPI yaml file
fn update_version_in_open_api(
path: &Path,
new_version: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let yaml_content =
read_to_string(path).map_err(|e| format!("Failed to read {}: {}", path.display(), e))?;
let mut doc: Value = serde_yaml::from_str(&yaml_content)
.map_err(|e| format!("Invalid YAML format in {}: {}", path.display(), e))?;
if let Some(info) = doc.get_mut("info") {
if let Some(version) = info.get_mut("version") {
*version = Value::String(new_version.to_string());
let updated_yaml = serde_yaml::to_string(&doc)
.map_err(|e| format!("Failed to serialize YAML for {}: {}", path.display(), e))?;
fs::write(path, updated_yaml)
.map_err(|e| format!("Failed to write {}: {}", path.display(), e))?;
println!("Updated version in {}", path.display());
}
}
Ok(())
}