Skip to content

Commit

Permalink
Dev
Browse files Browse the repository at this point in the history
* xtask: 
- improve cross-compatibility, 
- remove build stage in `package` and `install`, 
- add npm ci stage to `package` and `install`
- add args `package` and `install`

* ci/cd: draft-release action
  • Loading branch information
SpontanCombust authored Jan 27, 2024
1 parent b9b4da0 commit 3586abe
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 73 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ jobs:
with:
toolchain: stable
- name: Build with Cargo
uses: actions-rs/cargo@v1
with:
command: build
args: --release
run: cargo build --release --workspace
- name: Run tests
run: cargo test --verbose
48 changes: 48 additions & 0 deletions .github/workflows/draft-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: draft-release

on:
push:
tags: '*'

env:
CARGO_TERM_COLOR: always

jobs:
release:
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
include:
- os: ubuntu-latest
target: x86_64-unknown-linux-gnu
- os: windows-latest
target: x86_64-pc-windows-msvc

runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup npm
uses: actions/setup-node@v4
- name: Setup rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: ${{ matrix.target }}
override: true

- name: Build xtask
run: cargo build --package xtask --target ${{ matrix.target }} --release
- name: Prepare and pack the client
run: |
cargo xtask prep-lsp --target ${{ matrix.target }} --release
cargo xtask package --out-dir . --out-name "witcherscript-ide-${{ github.ref_name }}-${{ matrix.target }}"
- name: Create draft release
uses: ncipollo/release-action@v1
with:
artifacts: "*.vsix"
draft: true
allowUpdates: true
generateReleaseNotes: true
2 changes: 2 additions & 0 deletions xtask/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

This crate provides automation scripts for the repo. They are OS-agnostic as they don't directly use any specific shell. All you need is the `cargo xtask` command.

Beware, these tasks are used in github workflows, so make sure any breaking changes are also reflected in `.github/workflows`.

More about xtask workflow [here](https://github.com/matklad/cargo-xtask).
22 changes: 17 additions & 5 deletions xtask/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,24 @@ pub struct Cli {

#[derive(Subcommand)]
pub enum Commands {
/// Copy debug build of the LSP server to the VSCode client
CopyLsp,
/// Copy release build of the LSP server to the VSCode client
CopyLspRelease,
// Build and copy LSP server into VSCode's extension directory
PrepLsp {
/// Should LSP be built with optimised release profile
#[arg(long)]
release: bool,
/// Compilation target triple
#[arg(long)]
target: Option<String>
},
/// Build and package VSCode extension into a .vsix file
Package,
Package {
/// Output directory for the .vsix file; default is the current working directory
#[arg(long)]
out_dir: Option<String>,
/// Name of the output file without the extension
#[arg(long)]
out_name: Option<String>
},
/// Build, package and install the VSCode extension
Install
}
17 changes: 0 additions & 17 deletions xtask/src/commands/copy_lsp.rs

This file was deleted.

17 changes: 0 additions & 17 deletions xtask/src/commands/copy_lsp_release.rs

This file was deleted.

16 changes: 6 additions & 10 deletions xtask/src/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,26 @@ use anyhow::{Context, bail};
use xshell::{Shell, cmd};


const LSP_SRC: &str = "./target/release/witcherscript-lsp.exe";
const LSP_DST: &str = "./editors/vscode/server/bin";
const EXT_DIR: &str = "./editors/vscode";
const VSIX_NAME: &str = "witcherscript-ide.vsix";

pub fn install() -> anyhow::Result<()> {
let sh = Shell::new()?;

println!("Building LSP release...");
cmd!(sh, "cargo build --package witcherscript-lsp --release").run()?;

sh.copy_file(LSP_SRC, LSP_DST)?;
println!("Copied LSP into {}", LSP_DST);

sh.change_dir(EXT_DIR);

if cfg!(unix) {
cmd!(sh, "npm --version").run().with_context(|| "npm is required")?;
cmd!(sh, "vsce --version").run().with_context(|| "vsce is required: npm install -g vsce")?;
cmd!(sh, "code --version").run().with_context(|| "Visual Studio Code is required")?;

cmd!(sh, "npm ci").run()?;
cmd!(sh, "npm run package").run()?;

} else {
cmd!(sh, "cmd.exe /c npm --version").run().with_context(|| "npm is required")?;
cmd!(sh, "cmd.exe /c vsce --version").run().with_context(|| "vsce is required: npm install -g vsce")?;
cmd!(sh, "cmd.exe /c code --version").run().with_context(|| "Visual Studio Code is required")?;

cmd!(sh, "cmd.exe /c npm ci").run()?;
cmd!(sh, "cmd.exe /c npm run package").run()?;
}

Expand All @@ -44,6 +36,10 @@ pub fn install() -> anyhow::Result<()> {
if !installed_extensions.contains("witcherscript-ide") {
bail!("Could not install the Visual Studio Code extension.");
}

// Remove the vsix file
// If you want to keep it use xtask package instead
sh.remove_path(VSIX_NAME)?;

Ok(())
}
6 changes: 2 additions & 4 deletions xtask/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
mod copy_lsp;
mod copy_lsp_release;
mod prep_lsp;
mod package;
mod install;

pub use copy_lsp::copy_lsp;
pub use copy_lsp_release::copy_lsp_release;
pub use prep_lsp::prep_lsp;
pub use package::package;
pub use install::install;
34 changes: 21 additions & 13 deletions xtask/src/commands/package.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,47 @@
use std::path::PathBuf;
use anyhow::Context;
use xshell::{Shell, cmd};


const LSP_SRC: &str = "./target/release/witcherscript-lsp.exe";
const LSP_DST: &str = "./editors/vscode/server/bin";
const EXT_DIR: &str = "./editors/vscode";
const VSIX_NAME: &str = "witcherscript-ide.vsix";

pub fn package() -> anyhow::Result<()> {
pub fn package(out_dir: Option<String>, out_name: Option<String>) -> anyhow::Result<()> {
let sh = Shell::new()?;

println!("Building LSP release...");
cmd!(sh, "cargo build --package witcherscript-lsp --release").run()?;

sh.copy_file(LSP_SRC, LSP_DST)?;
println!("Copied LSP into {}", LSP_DST);

// normalize the output path so it stays valid when we change cwd
let out_dir = if let Some(out_dir) = out_dir {
// can't just use Option::map because of error propagation here
Some(PathBuf::from(&out_dir).canonicalize()?)
} else {
None
};

sh.change_dir(EXT_DIR);

if cfg!(unix) {
cmd!(sh, "npm --version").run().with_context(|| "npm is required")?;
cmd!(sh, "vsce --version").run().with_context(|| "vsce is required: npm install -g vsce")?;

cmd!(sh, "npm ci").run()?;
cmd!(sh, "npm run package").run()?;
} else {
cmd!(sh, "cmd.exe /c npm --version").run().with_context(|| "npm is required")?;
cmd!(sh, "cmd.exe /c vsce --version").run().with_context(|| "vsce is required: npm install -g vsce")?;

cmd!(sh, "cmd.exe /c npm ci").run()?;
cmd!(sh, "cmd.exe /c npm run package").run()?;
}

let version = env!("CARGO_PKG_VERSION");
sh.copy_file(VSIX_NAME, format!("witcherscript-ide-{version}.vsix"))?;
let vsix_file = format!("{}.vsix", out_name.unwrap_or("witcherscript-ide".to_string()));
let vsix_dst = if let Some(output_dir) = out_dir {
output_dir.join(vsix_file)
} else {
PathBuf::from(&vsix_file).canonicalize()?
};

sh.copy_file(VSIX_NAME, vsix_dst.as_os_str())?;
println!("Copied vsix package into {}", vsix_dst.display());

// remove the original vsix file
sh.remove_path(VSIX_NAME)?;

Ok(())
Expand Down
42 changes: 42 additions & 0 deletions xtask/src/commands/prep_lsp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::path::PathBuf;
use xshell::{Shell, cmd};


const LSP_DST: &str = "./editors/vscode/server/bin";

pub fn prep_lsp(release: bool, target: Option<String>) -> anyhow::Result<()> {
let sh = Shell::new()?;

let mut build = cmd!(sh, "cargo build --package witcherscript-lsp");

let mut lsp_src = PathBuf::from("./target");
if let Some(target) = target {
build = build.arg("--target").arg(&target);
lsp_src.push(target);
}

if release {
build = build.arg("--release");
lsp_src.push("release");
} else {
lsp_src.push("debug");
}

lsp_src.push("witcherscript-lsp");

if cfg!(windows) {
lsp_src.set_extension("exe");
}

println!("Building the LSP...");
build.run()?;


// make sure destination folder exists
sh.create_dir(LSP_DST)?;

sh.copy_file(lsp_src, LSP_DST)?;
println!("Copied LSP into {}", LSP_DST);

Ok(())
}
5 changes: 2 additions & 3 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ fn main() -> anyhow::Result<()> {
let cli = Cli::parse();

match cli.command {
cli::Commands::CopyLsp => commands::copy_lsp(),
cli::Commands::CopyLspRelease => commands::copy_lsp_release(),
cli::Commands::Package => commands::package(),
cli::Commands::PrepLsp { release, target } => commands::prep_lsp(release, target),
cli::Commands::Package { out_dir, out_name } => commands::package(out_dir, out_name),
cli::Commands::Install => commands::install()
}
}

0 comments on commit 3586abe

Please sign in to comment.