improve things

This commit is contained in:
nora 2022-02-10 05:59:35 +01:00
parent f437754618
commit a7dba08990
9 changed files with 185 additions and 26 deletions

View file

@ -7,6 +7,7 @@ edition = "2021"
[dependencies]
anyhow = "1.0.54"
clap = { version = "3.1.1", features = ["derive"] }
heck = "0.4.0"
itertools = "0.10.3"
strong-xml = "0.6.3"
strong-xml = "0.6.3"

View file

@ -4,13 +4,14 @@ mod parser;
mod random;
mod write;
use anyhow::Context;
use anyhow::{bail, Context};
use heck::ToUpperCamelCase;
use std::fs;
use std::fs::File;
use std::io::Write;
use std::iter::Peekable;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str::FromStr;
use strong_xml::XmlRead;
@ -105,6 +106,15 @@ struct Codegen {
output: Box<dyn Write>,
}
fn fmt(path: &Path) -> anyhow::Result<()> {
println!("Formatting {path:?}...");
let status = Command::new("rustfmt").arg(path).status()?;
if !status.success() {
bail!("error formatting {path:?}");
}
Ok(())
}
pub fn main() -> anyhow::Result<()> {
let this_file = PathBuf::from_str(file!()).context("own file path")?;
let xtask_root = this_file
@ -125,8 +135,8 @@ pub fn main() -> anyhow::Result<()> {
let amqp = Amqp::from_str(&content).context("parse amqp spec file")?;
let transport_output =
File::create(transport_generated_path).context("transport output file create")?;
let core_output = File::create(core_generated_path).context("core output file create")?;
File::create(&transport_generated_path).context("transport output file create")?;
let core_output = File::create(&core_generated_path).context("core output file create")?;
Codegen {
output: Box::new(transport_output),
@ -138,6 +148,9 @@ pub fn main() -> anyhow::Result<()> {
}
.core_codegen(&amqp);
fmt(&transport_generated_path)?;
fmt(&core_generated_path)?;
Ok(())
}
impl Codegen {

View file

@ -1,24 +1,41 @@
use std::path::PathBuf;
mod codegen;
mod test_js;
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[clap(author)]
struct Args {
#[clap(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Generate method definitions/parser/writer
Generate,
/// Run Javascript integration tests
TestJs,
}
fn main() -> anyhow::Result<()> {
let command = std::env::args().nth(1).unwrap_or_else(|| {
eprintln!("Error: No task provided");
help();
std::process::exit(1);
});
let args: Args = Args::parse();
match command.as_str() {
"generate" | "gen" => codegen::main(),
_ => {
eprintln!("Unknown command {command}.");
Ok(())
}
match args.command {
Commands::Generate => codegen::main(),
Commands::TestJs => test_js::main(),
}
}
fn help() {
println!(
"Available tasks:
generate, gen - Generate amqp method code in `amqp_transport/src/methods/generated.rs and amqp_core/src/methods/generated.rs"
);
pub fn project_root() -> PathBuf {
PathBuf::from(file!())
.parent()
.expect("src directory path")
.parent()
.expect("xtask root path")
.parent()
.expect("project root path")
.to_path_buf()
}

20
xtask/src/test_js.rs Normal file
View file

@ -0,0 +1,20 @@
use crate::project_root;
use anyhow::{bail, Result};
use std::process::Command;
pub fn main() -> Result<()> {
let test_js_root = project_root().join("tests-js");
let status = Command::new("yarn").current_dir(&test_js_root).status()?;
if !status.success() {
bail!("yarn install failed");
}
let status = Command::new("yarn")
.arg("test")
.current_dir(&test_js_root)
.status()?;
if !status.success() {
bail!("yarn tests failed");
}
Ok(())
}