Skip to content

Commit 3084a61

Browse files
authored
Setup xtasks and add the set-version xtask (#21)
1 parent fc674e1 commit 3084a61

File tree

6 files changed

+184
-1
lines changed

6 files changed

+184
-1
lines changed

.cargo/config.toml

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
[alias]
22
lint = "clippy --all-targets --all-features -- --no-deps"
33
docs = "doc --all-features --no-deps"
4+
xtask = "run --quiet --package xtask --"

Cargo.lock

+64-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ description = "The Cairo Language Server"
88
license = "Apache-2.0"
99
repository = "https://github.com/software-mansion/cairols"
1010

11+
[workspace]
12+
members = ["xtask"]
13+
1114
[features]
1215
testing = []
1316

xtask/Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "xtask"
3+
version = "1.0.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
anyhow = "1"
8+
clap = { version = "4.5", features = ["derive"] }
9+
semver = "1"
10+
toml_edit = "0.22.22"
11+
xshell = "0.2.7"

xtask/src/main.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use anyhow::Result;
2+
use clap::Parser;
3+
4+
macro_rules! command {
5+
($enum_name:ident ( $($module:ident,)+ )) => {
6+
$(mod $module;)+
7+
8+
#[derive(::clap::Subcommand)]
9+
#[allow(non_camel_case_types)]
10+
enum $enum_name {
11+
$($module(crate::$module::Args),)+
12+
}
13+
14+
impl $enum_name {
15+
fn main(self) -> ::anyhow::Result<()> {
16+
match self {
17+
$(Self::$module(args) => crate::$module::main(args),)+
18+
}
19+
}
20+
}
21+
}
22+
}
23+
24+
command!(Command(set_version,));
25+
26+
#[derive(Parser)]
27+
struct Args {
28+
#[command(subcommand)]
29+
command: Command,
30+
}
31+
32+
fn main() -> Result<()> {
33+
Args::parse().command.main()
34+
}

xtask/src/set_version.rs

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use anyhow::{Result, ensure};
2+
use clap::Parser;
3+
use semver::{Prerelease, Version};
4+
use toml_edit::{DocumentMut, value};
5+
use xshell::{Shell, cmd};
6+
7+
pub fn expected_version() -> Result<Version> {
8+
// NOTE: We are deliberately not using cargo_metadata to reduce build times of xtasks.
9+
10+
let sh = Shell::new()?;
11+
let cargo_lock = sh.read_file("Cargo.lock")?.parse::<DocumentMut>()?;
12+
let packages = cargo_lock["package"].as_array_of_tables().unwrap();
13+
let compiler = {
14+
let pkgs = packages
15+
.into_iter()
16+
.filter(|pkg| pkg["name"].as_str().unwrap() == "cairo-lang-compiler")
17+
.collect::<Vec<_>>();
18+
ensure!(
19+
pkgs.len() == 1,
20+
"expected exactly one cairo-lang-compiler package in Cargo.lock, found: {}",
21+
pkgs.len()
22+
);
23+
pkgs.into_iter().next().unwrap()
24+
};
25+
let compiler_version = compiler["version"].as_str().unwrap();
26+
Ok(compiler_version.parse()?)
27+
}
28+
29+
/// Synchronise the `cairo-language-server` crate version with the `cairo-lang-*` crates.
30+
#[derive(Default, Parser)]
31+
pub struct Args {
32+
/// Do not edit any files, just inform what would be done.
33+
#[arg(long, default_value_t = false)]
34+
pub dry_run: bool,
35+
36+
/// Set a custom value for the `build` metadata.
37+
#[arg(long)]
38+
pub build: Option<String>,
39+
40+
/// Clear the pre-release identifier from the version.
41+
#[arg(long, default_value_t = false)]
42+
pub no_pre_release: bool,
43+
}
44+
45+
pub fn main(args: Args) -> Result<()> {
46+
let sh = Shell::new()?;
47+
48+
let mut cargo_toml = sh.read_file("Cargo.toml")?.parse::<DocumentMut>()?;
49+
let package = cargo_toml["package"].as_table_mut().unwrap();
50+
51+
let mut version = expected_version()?;
52+
53+
if let Some(build) = args.build {
54+
version.build = build.parse()?;
55+
}
56+
if args.no_pre_release {
57+
version.pre = Prerelease::EMPTY;
58+
}
59+
60+
package["version"] = value(version.to_string());
61+
62+
eprintln!("[package]\n{package}");
63+
64+
if !args.dry_run {
65+
sh.write_file("Cargo.toml", cargo_toml.to_string())?;
66+
67+
cmd!(sh, "cargo fetch").run()?;
68+
}
69+
70+
Ok(())
71+
}

0 commit comments

Comments
 (0)