zellij/xtask/src/main.rs
har7an d1f50150f6
WIP: Use xtask as build system (#2012)
* xtask: Implement a new build system

xtask is a cargo alias that is used to extend the cargo build system
with custom commands. For an introduction to xtask, see here:
https://github.com/matklad/cargo-xtask/

The idea is that instead of writing makefiles, xtask requires no
additional dependencies except `cargo` and `rustc`, which must be
available to build the project anyway.

This commit provides a basic implementation of the `build` and `test`
subcommands.

* xtask/deps: Add 'which'

* xtask/test: Handle error when cargo not found

* xtask/flags: Add more commands

to perform different useful tasks. Includes:

- clippy
- format
- "make" (composite)
- "install" (composite)

Also add more options to `build` to selectively compile plugins or leave
them out entirely.

* xtask/main: Return error when cargo not found

* xtask/build: Add more subtasks

- `wasm_opt_plugins` and
- `manpage`

that perform other build commands. Add thorough documentation on what
each of these does and also handle the new `build` cli flags
appropriately.

* xtask/clippy: Add job to run clippy

* xtask/format: Add job to run rustfmt

* xtask/pipeline: Add composite commands

that perform multiple atomic xtask commands sequentially in a pipeline
sort of fashion.

* xtask/deps: Pin dependencies

* xtask/main: Integrate new jobs

and add documentation.

* xtask: Implement 'dist'

which performs an 'install' and copies the resulting zellij binary along
with some other assets to a `target/dist` folder.

* cargo: Update xflags version

* xtask: Measure task time, update tty title

* xtask: Update various tasks

* xtask: wasm-opt plugins in release builds

automatically.

* xtask/build: Copy debug plugins to assets folder

* xtask: Add 'run' subcommand

* xtask: Add arbitrary args to test and run

* xtask: Rearrange CLI commands in help

* xtask: Add deprecation notice

* docs: Replace `cargo make` with `xtask`

* github: Use `xtask` in workflows.

* xtask: Add support for CI commands

* xtask: Streamline error handling

* github: Use new xtask commands in CI

* xtask: Add 'publish' job

* xtask/publish: Add retry when publish fails

* xtask: Apply rustfmt

* xtask: Refine 'make' deprecation warning

* xtask: add task to build manpage

* contributing: Fix e2e commands

* xtask/run: Add missing `--`

to pass all arguments following `xtask run` directly to the zellij
binary being run.

* xtask: Stay in invocation dir

and make all tasks that need it change to the project root dir
themselves.

* xtask/run: Add `--data-dir` flag

which will allow very quick iterations when not changing the plugins
between builds.

* xtask/ci: Install dependencies without asking

* utils: Allow including plugins from target folder

* utils/assets: Reduce asset map complexity

* utils/consts: Update asset map docs

* xtask: Fix plugin includes

* xtask/test: Build plugins first

because the zellij binary needs to include the plugins.

* xtask/test: Fix formatting

* xtask: Add notice on how to disable it
2022-12-17 13:27:18 +00:00

152 lines
5.4 KiB
Rust

//! See <https://github.com/matklad/cargo-xtask/>.
//!
//! This binary defines various auxiliary build commands, which are not expressible with just
//! `cargo`. Notably, it provides tests via `cargo test -p xtask` for code generation and `cargo
//! xtask install` for installation of rust-analyzer server and client.
//!
//! This binary is integrated into the `cargo` command line by using an alias in `.cargo/config`.
// Current default "flow":
// - format-flow: `cargo fmt`
// - format-toml-conditioned-flow: ??
// - build: `cargo build`
// - test: `cargo test`
// - clippy: `cargo clippy --all-targets --all-features -- --deny warnings $@`
//
// # Install flow:
// - build-plugins-release: `cargo build --release ...`
// - wasm-opt-plugins: `wasm-opt ...`
// - build-release: `cargo build --release`
// - install-mandown: `cargo install mandown`
// - manpage: |
// mkdir -p ${root_dir}/assets/man
// mandown ${root_dir}/docs/MANPAGE.md 1 > ${root_dir}/assets/man/zellij.1
// - install: `cp target/release/zellij "$1"`
//
// # Release flow:
// - workspace: cargo make --profile development -- release
//
// # Publish flow:
// - update-default-config:
// - build-plugins-release: `cargo build --release ...`
// - wasm-opt-plugins: `wasm-opt ...`
// - release-commit:
// - commit-all: `git commit -aem "chore(release): v${CRATE_VERSION}"`
// - tag-release: `git tag --annotate --message "Version ${CRATE_VERSION}"
// "v${CRATE_VERSION}"`
// - `git push --atomic origin main "v${CRATE_VERSION}"`
// - publish-zellij: `cargo publish [tile, client, server, utils, tile-utils, zellij]`
mod build;
mod ci;
mod clippy;
mod dist;
mod flags;
mod format;
mod pipelines;
mod test;
use anyhow::Context;
use std::{
env,
path::{Path, PathBuf},
time::Instant,
};
use xshell::Shell;
lazy_static::lazy_static! {
pub static ref WORKSPACE_MEMBERS: Vec<&'static str> = vec![
"default-plugins/compact-bar",
"default-plugins/status-bar",
"default-plugins/strider",
"default-plugins/tab-bar",
"zellij-utils",
"zellij-tile-utils",
"zellij-tile",
"zellij-client",
"zellij-server",
".",
];
}
fn main() -> anyhow::Result<()> {
let shell = &Shell::new()?;
let flags = flags::Xtask::from_env()?;
let now = Instant::now();
match flags.subcommand {
flags::XtaskCmd::Deprecated(_flags) => deprecation_notice(),
flags::XtaskCmd::Dist(flags) => pipelines::dist(shell, flags),
flags::XtaskCmd::Build(flags) => build::build(shell, flags),
flags::XtaskCmd::Clippy(flags) => clippy::clippy(shell, flags),
flags::XtaskCmd::Format(flags) => format::format(shell, flags),
flags::XtaskCmd::Test(flags) => test::test(shell, flags),
flags::XtaskCmd::Manpage(_flags) => build::manpage(shell),
// Pipelines
// These are composite commands, made up of multiple "stages" defined above.
flags::XtaskCmd::Make(flags) => pipelines::make(shell, flags),
flags::XtaskCmd::Install(flags) => pipelines::install(shell, flags),
flags::XtaskCmd::Run(flags) => pipelines::run(shell, flags),
flags::XtaskCmd::Ci(flags) => ci::main(shell, flags),
flags::XtaskCmd::Publish(flags) => pipelines::publish(shell, flags),
}?;
let elapsed = now.elapsed().as_secs();
status(&format!("xtask (done after {} s)", elapsed));
println!("\n\n>> Command took {} s", elapsed);
Ok(())
}
fn project_root() -> PathBuf {
Path::new(
&env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()),
)
.ancestors()
.nth(1)
.unwrap()
.to_path_buf()
}
pub fn cargo() -> anyhow::Result<PathBuf> {
std::env::var_os("CARGO")
.map_or_else(|| which::which("cargo"), |exe| Ok(PathBuf::from(exe)))
.context("Couldn't find 'cargo' executable")
}
// Set terminal title to 'msg'
pub fn status(msg: &str) {
print!("\u{1b}]0;{}\u{07}", msg);
}
fn deprecation_notice() -> anyhow::Result<()> {
Err(anyhow::anyhow!(
" !!! cargo make has been deprecated by zellij !!!
Our build system is now `cargo xtask`. Don't worry, you won't have to install
anything!
- To get an overview of the new build tasks, run `cargo xtask --help`
- Quick compatibility table:
| cargo make task | cargo xtask equivalent |
| ------------------------------- | ----------------------------- |
| make | xtask |
| make format | xtask format |
| make build | xtask build |
| make test | xtask test |
| make run | xtask run |
| make run -l strider | xtask run -- -l strider |
| make clippy | xtask clippy |
| make clippy -W clippy::pedantic | N/A |
| make install /path/to/binary | xtask install /path/to/binary |
| make publish | xtask publish |
| make manpage | xtask manpage |
In order to disable xtask during the transitioning period: Delete/comment the
`[alias]` section in `.cargo/config.toml` and use `cargo make` as before.
If you're unhappy with `xtask` and decide to disable it, please tell us why so
we can discuss this before making it final for the next release. Thank you!
"
))
}