Skip to content

Commit

Permalink
Add dump xtask (#93)
Browse files Browse the repository at this point in the history
* Add `dump` xtask

* Dump before building in xtask `build`
  • Loading branch information
AlicanC authored Dec 24, 2021
1 parent 7a233e1 commit 540739b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 38 deletions.
1 change: 1 addition & 0 deletions xtask/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod build;
pub mod dump;
40 changes: 3 additions & 37 deletions xtask/src/commands/build.rs
Original file line number Diff line number Diff line change
@@ -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<dyn std::error::Error>> {
dump_schema()?;

let cargo = env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
let status = Command::new(cargo)
.current_dir(project_root())
Expand All @@ -25,30 +22,6 @@ pub fn cargo_build_and_dump_schema() -> Result<(), Box<dyn std::error::Error>> {
return Err("cargo build failed".into());
}

dump_schema()?;

Ok(())
}

fn dump_schema() -> Result<(), Box<dyn std::error::Error>> {
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(())
}

Expand All @@ -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());
}
43 changes: 43 additions & 0 deletions xtask/src/commands/dump.rs
Original file line number Diff line number Diff line change
@@ -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<dyn std::error::Error>> {
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());
}
7 changes: 6 additions & 1 deletion xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,12 +16,14 @@ pub struct Opt {
#[derive(Debug, StructOpt)]
enum Xtask {
Build(BuildCommand),
Dump(DumpCommand),
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
let opt = Opt::from_args();

match opt.command {
Xtask::Build(_) => cargo_build_and_dump_schema(),
Xtask::Dump(_) => dump_schema(),
}
}

0 comments on commit 540739b

Please sign in to comment.