Skip to content

Commit

Permalink
add subcommand support
Browse files Browse the repository at this point in the history
  • Loading branch information
Br1ght0ne committed Feb 27, 2024
1 parent 91c11f5 commit d43109f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ jobs:
uses: FuelLabs/github-actions/.github/workflows/mdbook-docs.yml@master
with:
docs-src-path: "docs/src"
pre-command: 'cargo run --package versions-replacer -- ./docs --manifest-path ./Cargo.toml --filename-regex "\.md$"'
pre-command: 'cargo run --package versions-replacer -- --manifest-path ./Cargo.toml replace ./docs --filename-regex "\.md$"'
spellcheck-config-path: 'docs/.spellcheck.yml'
89 changes: 59 additions & 30 deletions scripts/versions-manager/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,54 +12,83 @@ use versions_manager::{
use walkdir::WalkDir;

#[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]'.
/// Manage version-related tasks for CI.
struct VersionsManager {
/// path to directory with files containing variables
#[argh(positional)]
path: PathBuf,
/// path to Cargo.toml with versions
#[argh(option)]
manifest_path: PathBuf,
#[argh(subcommand)]
subcommand: VersionsManagerSubcommandEnum,
}

#[derive(FromArgs)]
#[argh(subcommand)]
enum VersionsManagerSubcommandEnum {
Replace(Replacer),
}

#[derive(FromArgs)]
/// Replace variables like '{{versions.fuels}}' with correct versions from Cargo.toml.
/// Uses versions from '[workspace.members]' and '[workspace.metadata.versions-manager.external-versions]'.
#[argh(subcommand, name = "replace")]
struct Replacer {
/// path to directory with files containing variables
#[argh(positional)]
path: PathBuf,
/// regex to filter filenames (example: "\.md$")
#[argh(option)]
filename_regex: Option<Regex>,
}

fn main() -> Result<()> {
let args: VersionsManager = argh::from_env();
let versions = collect_versions_from_cargo_toml(&args.manifest_path)?;
use VersionsManagerSubcommandEnum::*;

let mut total_replacements: Vec<usize> = Vec::new();
let VersionsManager {
manifest_path,
subcommand,
} = argh::from_env();
match subcommand {
Replace(Replacer {
path,
filename_regex,
}) => {
let versions = collect_versions_from_cargo_toml(manifest_path)?;

for entry in WalkDir::new(&args.path) {
let entry = entry.wrap_err("failed to get directory entry")?;
let mut total_replacements: Vec<usize> = Vec::new();

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;
for entry in WalkDir::new(path) {
let entry = entry.wrap_err("failed to get directory entry")?;

if entry.path().is_file() {
if let Some(filename_regex) = &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;
}
}

let replacement_count = replace_versions_in_file(entry.path(), &versions)
.wrap_err_with(|| {
format!("failed to replace versions in {:?}", entry.path())
})?;
if replacement_count > 0 {
total_replacements.push(replacement_count);
}
}
}

let replacement_count = replace_versions_in_file(entry.path(), &versions)
.wrap_err_with(|| format!("failed to replace versions in {:?}", entry.path()))?;
if replacement_count > 0 {
total_replacements.push(replacement_count);
}
println!(
"replaced {} variables across {} files",
total_replacements.iter().sum::<usize>(),
total_replacements.len()
);
}
}

println!(
"replaced {} variables across {} files",
total_replacements.iter().sum::<usize>(),
total_replacements.len()
);
Ok(())
}

0 comments on commit d43109f

Please sign in to comment.