-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
scripts: add version replacer script
- Loading branch information
Showing
10 changed files
with
150 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "versions-replacer" | ||
publish = false | ||
authors.workspace = true | ||
edition.workspace = true | ||
homepage.workspace = true | ||
readme.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
rust-version.workspace = true | ||
version.workspace = true | ||
|
||
[dependencies] | ||
argh = "0.1.12" | ||
cargo_metadata = "0.17.0" | ||
color-eyre = "0.6.2" | ||
once_cell = "1.18.0" | ||
regex = { workspace = true } | ||
serde = { workspace = true, features = ["derive"] } | ||
serde_json = { workspace = true } | ||
walkdir = "2.3.3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod metadata; | ||
pub mod replace; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use std::path::PathBuf; | ||
|
||
use argh::FromArgs; | ||
use color_eyre::{ | ||
eyre::{eyre, Context}, | ||
Result, | ||
}; | ||
use regex::Regex; | ||
use walkdir::WalkDir; | ||
|
||
use versions_replacer::{ | ||
metadata::collect_versions_from_cargo_toml, replace::replace_versions_in_file, | ||
}; | ||
|
||
#[derive(FromArgs)] | ||
/// Replace variables like '{{{{versions.fuels}}}}' with correct versions from Cargo.toml. | ||
/// Uses versions from '[workspace.members]' and '[workspace.metadata.versions-replacer.external-versions]'. | ||
struct VersionsReplacer { | ||
/// path to directory with files containing variables | ||
#[argh(positional)] | ||
path: PathBuf, | ||
/// path to Cargo.toml with versions | ||
#[argh(option)] | ||
manifest_path: PathBuf, | ||
/// regex to filter filenames (example: "\.md$") | ||
#[argh(option)] | ||
filename_regex: Option<Regex>, | ||
} | ||
|
||
fn main() -> Result<()> { | ||
let args: VersionsReplacer = argh::from_env(); | ||
let versions = collect_versions_from_cargo_toml(&args.manifest_path)?; | ||
|
||
for entry in WalkDir::new(&args.path) { | ||
let entry = entry.wrap_err("failed to get directory entry")?; | ||
|
||
if entry.path().is_file() { | ||
if let Some(filename_regex) = &args.filename_regex { | ||
let file_name = entry | ||
.path() | ||
.file_name() | ||
.ok_or_else(|| eyre!("{:?} has an invalid file name", entry.path()))? | ||
.to_str() | ||
.ok_or_else(|| eyre!("filename is not valid UTF-8"))?; | ||
if !filename_regex.is_match(file_name) { | ||
continue; | ||
} | ||
} | ||
|
||
replace_versions_in_file(entry.path(), &versions) | ||
.wrap_err_with(|| format!("failed to replace versions in {:?}", entry.path()))?; | ||
} | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use std::{collections::HashMap, path::Path}; | ||
|
||
use cargo_metadata::MetadataCommand; | ||
use color_eyre::{eyre::Context, Result}; | ||
use serde::Deserialize; | ||
|
||
#[derive(Deserialize)] | ||
#[serde(rename_all = "kebab-case")] | ||
pub struct WorkspaceMetadata { | ||
pub versions_replacer: VersionsReplacerMetadata, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
#[serde(rename_all = "kebab-case")] | ||
pub struct VersionsReplacerMetadata { | ||
pub external_versions: HashMap<String, String>, | ||
} | ||
|
||
pub fn collect_versions_from_cargo_toml( | ||
manifest_path: impl AsRef<Path>, | ||
) -> Result<HashMap<String, String>> { | ||
let metadata = MetadataCommand::new() | ||
.manifest_path(manifest_path.as_ref()) | ||
.exec() | ||
.wrap_err("failed to execute 'cargo metadata'")?; | ||
let version_map = metadata | ||
.workspace_members | ||
.iter() | ||
.map(|package_id| { | ||
let package = &metadata[package_id]; | ||
(package.name.clone(), package.version.to_string()) | ||
}) | ||
.chain( | ||
serde_json::from_value::<WorkspaceMetadata>(metadata.workspace_metadata.clone()) | ||
.wrap_err("failed to parse '[workspace.metadata]'")? | ||
.versions_replacer | ||
.external_versions | ||
.into_iter(), | ||
) | ||
.collect::<HashMap<_, _>>(); | ||
Ok(version_map) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use std::{collections::HashMap, fs, path::Path}; | ||
|
||
use color_eyre::{eyre::Context, Result}; | ||
use once_cell::sync::Lazy; | ||
use regex::{Captures, Regex}; | ||
|
||
pub static VERSIONS_REGEX: Lazy<Regex> = | ||
Lazy::new(|| Regex::new(r"\{\{versions\.([\w_-]+)\}\}").unwrap()); | ||
|
||
pub fn replace_versions_in_file( | ||
path: impl AsRef<Path>, | ||
versions: &HashMap<String, String>, | ||
) -> Result<()> { | ||
let path = path.as_ref(); | ||
let contents = | ||
fs::read_to_string(path).wrap_err_with(|| format!("failed to read {:?}", path))?; | ||
let replaced_contents = | ||
VERSIONS_REGEX.replace_all(&contents, |caps: &Captures| &versions[&caps[1]]); | ||
fs::write(path, replaced_contents.as_bytes()) | ||
.wrap_err_with(|| format!("failed to write back to {:?}", path))?; | ||
Ok(()) | ||
} |