-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setup xtasks and add the
set-version
xtask
commit-id:faa4c382
- Loading branch information
Showing
6 changed files
with
184 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
[alias] | ||
lint = "clippy --all-targets --all-features -- --no-deps" | ||
docs = "doc --all-features --no-deps" | ||
xtask = "run --quiet --package xtask --" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,11 @@ | ||
[package] | ||
name = "xtask" | ||
version = "1.0.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
anyhow = "1" | ||
clap = { version = "4.5", features = ["derive"] } | ||
semver = "1" | ||
toml_edit = "0.22.22" | ||
xshell = "0.2.7" |
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,34 @@ | ||
use anyhow::Result; | ||
use clap::Parser; | ||
|
||
macro_rules! command { | ||
($enum_name:ident ( $($module:ident,)+ )) => { | ||
$(mod $module;)+ | ||
|
||
#[derive(::clap::Subcommand)] | ||
#[allow(non_camel_case_types)] | ||
enum $enum_name { | ||
$($module(crate::$module::Args),)+ | ||
} | ||
|
||
impl $enum_name { | ||
fn main(self) -> ::anyhow::Result<()> { | ||
match self { | ||
$(Self::$module(args) => crate::$module::main(args),)+ | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
command!(Command(set_version,)); | ||
|
||
#[derive(Parser)] | ||
struct Args { | ||
#[command(subcommand)] | ||
command: Command, | ||
} | ||
|
||
fn main() -> Result<()> { | ||
Args::parse().command.main() | ||
} |
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,71 @@ | ||
use anyhow::{Result, ensure}; | ||
use clap::Parser; | ||
use semver::{Prerelease, Version}; | ||
use toml_edit::{DocumentMut, value}; | ||
use xshell::{Shell, cmd}; | ||
|
||
pub fn expected_version() -> Result<Version> { | ||
// NOTE: We are deliberately not using cargo_metadata to reduce build times of xtasks. | ||
|
||
let sh = Shell::new()?; | ||
let cargo_lock = sh.read_file("Cargo.lock")?.parse::<DocumentMut>()?; | ||
let packages = cargo_lock["package"].as_array_of_tables().unwrap(); | ||
let compiler = { | ||
let pkgs = packages | ||
.into_iter() | ||
.filter(|pkg| pkg["name"].as_str().unwrap() == "cairo-lang-compiler") | ||
.collect::<Vec<_>>(); | ||
ensure!( | ||
pkgs.len() == 1, | ||
"expected exactly one cairo-lang-compiler package in Cargo.lock, found: {}", | ||
pkgs.len() | ||
); | ||
pkgs.into_iter().next().unwrap() | ||
}; | ||
let compiler_version = compiler["version"].as_str().unwrap(); | ||
Ok(compiler_version.parse()?) | ||
} | ||
|
||
/// Synchronise the `cairo-language-server` crate version with the `cairo-lang-*` crates. | ||
#[derive(Default, Parser)] | ||
pub struct Args { | ||
/// Do not edit any files, just inform what would be done. | ||
#[arg(long, default_value_t = false)] | ||
pub dry_run: bool, | ||
|
||
/// Set a custom value for the `build` metadata. | ||
#[arg(long)] | ||
pub build: Option<String>, | ||
|
||
/// Clear the pre-release identifier from the version. | ||
#[arg(long, default_value_t = false)] | ||
pub no_pre_release: bool, | ||
} | ||
|
||
pub fn main(args: Args) -> Result<()> { | ||
let sh = Shell::new()?; | ||
|
||
let mut cargo_toml = sh.read_file("Cargo.toml")?.parse::<DocumentMut>()?; | ||
let package = cargo_toml["package"].as_table_mut().unwrap(); | ||
|
||
let mut version = expected_version()?; | ||
|
||
if let Some(build) = args.build { | ||
version.build = build.parse()?; | ||
} | ||
if args.no_pre_release { | ||
version.pre = Prerelease::EMPTY; | ||
} | ||
|
||
package["version"] = value(version.to_string()); | ||
|
||
eprintln!("[package]\n{package}"); | ||
|
||
if !args.dry_run { | ||
sh.write_file("Cargo.toml", cargo_toml.to_string())?; | ||
|
||
cmd!(sh, "cargo fetch").run()?; | ||
} | ||
|
||
Ok(()) | ||
} |