From 540739be197bd33f24366f03a028f342e61d66f8 Mon Sep 17 00:00:00 2001 From: John Cub Date: Fri, 24 Dec 2021 19:49:50 +0300 Subject: [PATCH] Add `dump` xtask (#93) * Add `dump` xtask * Dump before building in xtask `build` --- xtask/src/commands.rs | 1 + xtask/src/commands/build.rs | 40 +++------------------------------- xtask/src/commands/dump.rs | 43 +++++++++++++++++++++++++++++++++++++ xtask/src/main.rs | 7 +++++- 4 files changed, 53 insertions(+), 38 deletions(-) create mode 100644 xtask/src/commands/dump.rs diff --git a/xtask/src/commands.rs b/xtask/src/commands.rs index 76f610c382a..d6385ac6652 100644 --- a/xtask/src/commands.rs +++ b/xtask/src/commands.rs @@ -1 +1,2 @@ pub mod build; +pub mod dump; diff --git a/xtask/src/commands/build.rs b/xtask/src/commands/build.rs index 04b9358bcc4..5b601e8b5f9 100644 --- a/xtask/src/commands/build.rs +++ b/xtask/src/commands/build.rs @@ -1,20 +1,17 @@ -use fuel_core::schema::build_schema; +use super::dump::dump_schema; use std::{ env, - fs::{self, File}, - io::Write, path::{Path, PathBuf}, process::Command, }; use structopt::StructOpt; -// stored in the root of the workspace -const SCHEMA_URL: &str = "../assets/schema.sdl"; - #[derive(Debug, StructOpt)] pub struct BuildCommand {} pub fn cargo_build_and_dump_schema() -> Result<(), Box> { + dump_schema()?; + let cargo = env::var("CARGO").unwrap_or_else(|_| "cargo".to_string()); let status = Command::new(cargo) .current_dir(project_root()) @@ -25,30 +22,6 @@ pub fn cargo_build_and_dump_schema() -> Result<(), Box> { return Err("cargo build failed".into()); } - dump_schema()?; - - Ok(()) -} - -fn dump_schema() -> Result<(), Box> { - let assets = env::var("CARGO_MANIFEST_DIR") - .map(PathBuf::from) - .map(|f| { - let f = f.as_path().join(SCHEMA_URL); - let dir = f.parent().expect("Failed to read assets dir"); - fs::create_dir_all(dir).expect("Failed to create assets dir"); - - f - }) - .expect("Failed to fetch assets path"); - - File::create(&assets) - .and_then(|mut f| { - f.write_all(build_schema().finish().sdl().as_bytes())?; - f.sync_all() - }) - .expect("Failed to write SDL schema to temporary file"); - Ok(()) } @@ -59,10 +32,3 @@ fn project_root() -> PathBuf { .unwrap() .to_path_buf() } - -/// ensures that latest schema is always committed -#[test] -fn is_latest_schema_committed() { - let current_content = fs::read(SCHEMA_URL).unwrap(); - assert_eq!(current_content, build_schema().finish().sdl().as_bytes()); -} diff --git a/xtask/src/commands/dump.rs b/xtask/src/commands/dump.rs new file mode 100644 index 00000000000..eef617771d6 --- /dev/null +++ b/xtask/src/commands/dump.rs @@ -0,0 +1,43 @@ +use fuel_core::schema::build_schema; +use std::{ + env, + fs::{self, File}, + io::Write, + path::PathBuf, +}; +use structopt::StructOpt; + +// stored in the root of the workspace +const SCHEMA_URL: &str = "../assets/schema.sdl"; + +#[derive(Debug, StructOpt)] +pub struct DumpCommand {} + +pub fn dump_schema() -> Result<(), Box> { + let assets = env::var("CARGO_MANIFEST_DIR") + .map(PathBuf::from) + .map(|f| { + let f = f.as_path().join(SCHEMA_URL); + let dir = f.parent().expect("Failed to read assets dir"); + fs::create_dir_all(dir).expect("Failed to create assets dir"); + + f + }) + .expect("Failed to fetch assets path"); + + File::create(&assets) + .and_then(|mut f| { + f.write_all(build_schema().finish().sdl().as_bytes())?; + f.sync_all() + }) + .expect("Failed to write SDL schema to temporary file"); + + Ok(()) +} + +/// ensures that latest schema is always committed +#[test] +fn is_latest_schema_committed() { + let current_content = fs::read(SCHEMA_URL).unwrap(); + assert_eq!(current_content, build_schema().finish().sdl().as_bytes()); +} diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 9e793957ef7..31d2f560481 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -1,4 +1,7 @@ -use commands::build::{cargo_build_and_dump_schema, BuildCommand}; +use commands::{ + build::{cargo_build_and_dump_schema, BuildCommand}, + dump::{dump_schema, DumpCommand}, +}; use structopt::StructOpt; mod commands; @@ -13,6 +16,7 @@ pub struct Opt { #[derive(Debug, StructOpt)] enum Xtask { Build(BuildCommand), + Dump(DumpCommand), } fn main() -> Result<(), Box> { @@ -20,5 +24,6 @@ fn main() -> Result<(), Box> { match opt.command { Xtask::Build(_) => cargo_build_and_dump_schema(), + Xtask::Dump(_) => dump_schema(), } }