Skip to content

Commit

Permalink
chore: Cross platform zipping
Browse files Browse the repository at this point in the history
  • Loading branch information
scsmithr committed Jul 11, 2023
1 parent 1400810 commit b99632b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 270 deletions.
6 changes: 4 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ dist triple=target_triple: protoc
just build --release --target {{triple}}
src_path="target/{{triple}}/release/{{executable_name}}"
dest_path="target/dist/glaredb-{{triple}}.zip"
zip -j $dest_path $src_path
mkdir -p target/dist
cargo xtask zip --src $src_path --dst $dest_path
# Run tests with arbitrary arguments.
test *args: protoc
Expand Down Expand Up @@ -98,6 +99,7 @@ default_target_triple := if os_arch == "macos-x86_64" {
} else {
error("Unsupported platform: " + os_arch)
}

target_triple:= env_var_or_default("DIST_TARGET_TRIPLE", default_target_triple)

protoc_url := if os_arch == "macos-x86_64" {
Expand All @@ -114,4 +116,4 @@ protoc_url := if os_arch == "macos-x86_64" {
error("Unsupported platform: " + os_arch)
}

executable_name:= if os() == "windows" {"glaredb.exe"} else {"glaredb"}
executable_name:= if os() == "windows" {"glaredb.exe"} else {"glaredb"}
169 changes: 35 additions & 134 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use anyhow::Result;
use clap::{Parser, Subcommand};
use std::{ffi::OsStr, io::Cursor};
use target::Target;
use xshell::{cmd, Shell};
use zip::ZipArchive;

mod target;
mod util;
use std::fs::File;
use std::io;
use std::path::{Path, PathBuf};
use xshell::Shell;
use zip::write::FileOptions;
use zip::ZipWriter;

#[derive(Parser)]
#[clap(name = "xtask")]
Expand All @@ -18,147 +17,49 @@ struct Cli {

#[derive(Subcommand)]
enum Commands {
/// Build glaredb.
Build {
/// Build with the release profile.
#[clap(short, long)]
release: bool,
},

/// Run unit tests.
UnitTests,

/// Run doc tests.
DocTests,

/// Run SQL Logic Tests.
#[clap(alias = "slt")]
SqlLogicTests {
rest: Vec<String>,
},

/// Run tests with arbitrary arguments.
Test {
rest: Vec<String>,
},

/// Run clippy.
Clippy,

/// Check formatting.
FmtCheck,

/// Build the dist binary for release.
///
/// A zip archive will be placed in `target/dist` containing the release
/// binary.
/// Zip a folder to some destination.
///
/// By default, the compilation target matches the host machine. This can be
/// overridden via the `DIST_TARGET_TRIPLE` environment variable.
Dist,
Protoc,
/// Useful for cross-platform zipping.
Zip {
/// Source folder.
#[clap(long)]
src: String,
/// Destination path.
#[clap(long)]
dst: String,
},
}

fn main() -> Result<()> {
let cli = Cli::parse();
let sh = &Shell::new()?;
sh.change_dir(util::project_root());
sh.set_var("CARGO_TERM_COLOR", "always"); // Always print output with color.

let target = Target::from_cfg()?;
ensure_protoc(sh, &target)?;

match cli.command {
Commands::Build { release } => run_build(sh, release)?,
Commands::UnitTests => run_tests(sh, ["--lib", "--bins"])?,
Commands::DocTests => run_tests(sh, ["--doc"])?,
Commands::SqlLogicTests { rest } => {
let args = ["--test", "sqllogictests", "--"]
.into_iter()
.chain(rest.iter().map(|s| s.as_str()));
run_tests(sh, args)?
Commands::Zip { src, dst } => {
let src = PathBuf::from(src);
let dst = PathBuf::from(dst);
zip(&src, &dst)?
}
Commands::Test { rest } => run_tests(sh, rest)?,
Commands::Clippy => run_clippy(sh)?,
Commands::FmtCheck => run_fmt_check(sh)?,
Commands::Dist => run_dist(sh, &target)?,
Commands::Protoc => {}
}

Ok(())
}

fn run_build(sh: &Shell, release: bool) -> Result<()> {
let mut cmd = cmd!(sh, "cargo build --bin glaredb");
if release {
cmd = cmd.arg("--release");
}
cmd.run()?;
Ok(())
}

fn run_tests<I>(sh: &Shell, rest: I) -> Result<()>
where
I: IntoIterator,
I::Item: AsRef<OsStr>,
{
let cmd = sh.cmd("cargo").arg("test").args(rest);
cmd.run()?;
Ok(())
}

fn run_clippy(sh: &Shell) -> Result<()> {
cmd!(sh, "cargo clippy --all-features -- --deny warnings").run()?;
Ok(())
}

fn run_fmt_check(sh: &Shell) -> Result<()> {
cmd!(sh, "cargo fmt --check").run()?;
Ok(())
}

fn run_dist(sh: &Shell, target: &Target) -> Result<()> {
let triple = target.dist_target_triple()?;
cmd!(sh, "cargo build --release --bin glaredb --target {triple}").run()?;

// TODO: Code signing goes here.

sh.remove_path(util::project_root().join("target").join("dist"))?;
sh.create_dir(util::project_root().join("target").join("dist"))?;

let src_path = util::project_root()
.join("target")
.join(target.dist_target_triple()?)
.join("release")
.join(target.executable_name());
let dest_path = util::project_root()
.join("target")
.join("dist")
.join(target.dist_zip_name()?);

util::zip(&src_path, &dest_path)?;

println!("Dist zip: {:?}", dest_path);
Ok(())
}

/// Check if protoc is in path. If not, download and set the PROTOC env var.
fn ensure_protoc(sh: &Shell, target: &Target) -> Result<()> {
const PROTOC_OUT_DIR: &str = "deps/protoc";
const PROTOC_PATH: &str = "deps/protoc/bin/protoc";

if cmd!(sh, "protoc --version").run().is_err() {
if sh.path_exists(util::project_root().join(PROTOC_PATH)) {
println!("Downloaded protoc already exists");
} else {
println!("Missing protoc, downloading...");
sh.remove_path(util::project_root().join(PROTOC_OUT_DIR))?;
let res = reqwest::blocking::get(target.protoc_url()?)?;
ZipArchive::new(Cursor::new(res.bytes()?))?
.extract(util::project_root().join(PROTOC_OUT_DIR))?;
}

sh.set_var("PROTOC", util::project_root().join(PROTOC_PATH));
}
/// Zip a source file and write a zip to some destination dest.
fn zip(src_path: &Path, dest_path: &Path) -> Result<()> {
let file = File::create(dest_path)?;
let mut writer = ZipWriter::new(io::BufWriter::new(file));
writer.start_file(
src_path.file_name().unwrap().to_str().unwrap(),
FileOptions::default()
.unix_permissions(0o755)
.compression_method(zip::CompressionMethod::Deflated)
.compression_level(Some(9)),
)?;
let mut input = io::BufReader::new(File::open(src_path)?);
io::copy(&mut input, &mut writer)?;

writer.finish()?;
Ok(())
}
98 changes: 0 additions & 98 deletions xtask/src/target.rs

This file was deleted.

36 changes: 0 additions & 36 deletions xtask/src/util.rs

This file was deleted.

0 comments on commit b99632b

Please sign in to comment.