Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup xtasks and add the set-version xtask #21

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .cargo/config.toml
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 --"
65 changes: 64 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ description = "The Cairo Language Server"
license = "Apache-2.0"
repository = "https://github.com/software-mansion/cairols"

[workspace]
members = ["xtask"]

[features]
testing = []

Expand Down
11 changes: 11 additions & 0 deletions xtask/Cargo.toml
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"
34 changes: 34 additions & 0 deletions xtask/src/main.rs
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,));
mkaput marked this conversation as resolved.
Show resolved Hide resolved

#[derive(Parser)]
struct Args {
#[command(subcommand)]
command: Command,
}

fn main() -> Result<()> {
Args::parse().command.main()
}
71 changes: 71 additions & 0 deletions xtask/src/set_version.rs
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(())
}
Loading