Skip to content

Commit 4456ef8

Browse files
authored
feat: make xtask spec generation tasks go to stdout when no output is… (#89)
Closes CHAIN-72
1 parent 27aab25 commit 4456ef8

File tree

3 files changed

+35
-24
lines changed

3 files changed

+35
-24
lines changed

xtask/src/flags.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ xflags::xflags! {
6161
optional -c, --base-chain-spec base_chain_spec: PathBuf
6262

6363
/// Output file for the chain spec.
64-
required -o, --out output: PathBuf
64+
optional -o, --out output: PathBuf
6565

6666
repeated --gran gran_keys: String
6767

@@ -142,7 +142,7 @@ pub struct Replica {
142142
#[derive(Debug)]
143143
pub struct GenerateSpec {
144144
pub base_chain_spec: Option<PathBuf>,
145-
pub out: PathBuf,
145+
pub out: Option<PathBuf>,
146146
pub gran: Vec<String>,
147147
pub aura: Vec<String>,
148148
pub balance: Vec<String>,

xtask/src/generate_spec.rs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,26 @@ pub type Balance = u64;
3737
pub type Nonce = u32;
3838
pub type RefCount = u32;
3939

40-
fn generate_replica_spec(gen_replica: flags::GenReplica, out: PathBuf, sudo: Option<String>) {
41-
// Create a temporary directory
42-
let temp_dir = tempfile::Builder::new()
43-
.prefix("torus-replica-spec")
44-
.tempdir()
45-
.expect("failed to create tempdir")
46-
.into_path();
47-
40+
fn generate_replica_spec(
41+
gen_replica: flags::GenReplica,
42+
out: Option<PathBuf>,
43+
sudo: Option<String>,
44+
) {
4845
// Create Replica command
4946
let replica_cmd = flags::Replica {
50-
output: Some(out),
47+
output: out,
5148
sudo,
5249
api_url: gen_replica.api_url.clone(),
5350
};
5451

5552
// Call the targetchain_spec function
56-
targetchain_spec(&replica_cmd, &temp_dir);
53+
targetchain_spec(&replica_cmd);
5754

5855
// The file is already written by targetchain_spec, no need to write again
5956
}
6057

6158
/// Function moved from build_spec.rs
62-
pub fn targetchain_spec(flags: &flags::Replica, dir: &Path) -> PathBuf {
59+
pub fn targetchain_spec(flags: &flags::Replica) -> Option<PathBuf> {
6360
let spec = tokio::runtime::Builder::new_multi_thread()
6461
.enable_all()
6562
.build()
@@ -78,13 +75,16 @@ pub fn targetchain_spec(flags: &flags::Replica, dir: &Path) -> PathBuf {
7875

7976
let js = serde_json::to_string_pretty(&js).unwrap();
8077

81-
let chain_path = flags
82-
.output
83-
.clone()
84-
.unwrap_or_else(|| dir.join("spec.json"));
85-
std::fs::write(&chain_path, js).unwrap();
86-
87-
chain_path
78+
match &flags.output {
79+
Some(chain_path) => {
80+
std::fs::write(chain_path, js).unwrap();
81+
Some(chain_path.clone())
82+
}
83+
None => {
84+
println!("{js}");
85+
None
86+
}
87+
}
8888
}
8989

9090
/// Sets the sudo key in the genesis state
@@ -264,9 +264,16 @@ fn generate_new_spec(gen_new: &flags::GenNew, cmd: &flags::GenerateSpec) {
264264
customize_spec(&mut json, cmd);
265265

266266
// Write the result to the output file
267-
let serialized = serde_json::to_vec(&json).expect("failed to generate spec file");
267+
let serialized = serde_json::to_string_pretty(&json).expect("failed to generate spec file");
268268

269-
std::fs::write(&cmd.out, serialized).expect("failed to write resulting spec file");
269+
match &cmd.out {
270+
Some(output_path) => {
271+
std::fs::write(output_path, serialized).expect("failed to write resulting spec file");
272+
}
273+
None => {
274+
println!("{serialized}");
275+
}
276+
}
270277
}
271278

272279
// Function to customize a spec file based on the provided flags

xtask/src/run.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub(super) fn run(mut r: flags::Run) {
4747
_ => {}
4848
}
4949

50-
let (chain_spec, _local_seal) = match &r.subcommand {
50+
let (chain_spec, _local_seal) = match &mut r.subcommand {
5151
flags::RunCmd::Local(local) => {
5252
let chain_path = local
5353
.chain_spec
@@ -65,7 +65,11 @@ pub(super) fn run(mut r: flags::Run) {
6565
(chain_path, true)
6666
}
6767
flags::RunCmd::Replica(replica) => {
68-
let chain_path = crate::generate_spec::targetchain_spec(replica, &path);
68+
if replica.output.is_none() {
69+
replica.output = Some(path.join("spec.json"));
70+
}
71+
72+
let chain_path = crate::generate_spec::targetchain_spec(replica).unwrap();
6973
let chain_path_str = chain_path.to_str().expect("invalid string").to_string();
7074
(chain_path_str, false)
7175
}

0 commit comments

Comments
 (0)