Skip to content

Commit

Permalink
schema: Add option to generate separate JSON schema files
Browse files Browse the repository at this point in the history
  • Loading branch information
uint committed Nov 7, 2022
1 parent ded2c78 commit 4fde2e5
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
37 changes: 30 additions & 7 deletions packages/schema-derive/src/generate_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,46 @@ pub fn write_api_impl(input: Options) -> Block {
{
#[cfg(target_arch = "wasm32")]
compile_error!("can't compile schema generator for the `wasm32` arch\nhint: are you trying to compile a smart contract without specifying `--lib`?");
use ::std::env::current_dir;
use ::std::env;
use ::std::fs::{create_dir_all, write};

use ::cosmwasm_schema::{remove_schemas, Api, QueryResponses};

let mut out_dir = current_dir().unwrap();
if env::args().find(|arg| arg == "--help").is_some() {
println!("USAGE:");
println!(" cargo schema [OPTIONS]");
println!();
println!("FLAGS:");
println!(" --basic");
println!(" Generate pure JSON schema files rather than the \"unified\" format.");
println!(" --help");
println!(" Print this helpfile.");
return;
}

let basic = env::args().find(|arg| arg == "--basic").is_some();

let mut out_dir = env::current_dir().unwrap();
out_dir.push("schema");
create_dir_all(&out_dir).unwrap();
remove_schemas(&out_dir).unwrap();

let path = out_dir.join(concat!(#name, ".json"));

let api = #api_object.render();

let json = api.to_string().unwrap();
write(&path, json + "\n").unwrap();
println!("Exported the full API as {}", path.to_str().unwrap());
if basic {
for (filename, json) in api.to_schema_files().unwrap() {
let path = out_dir.join(filename);

write(&path, json + "\n").unwrap();
println!("Exported {}", path.to_str().unwrap());
}
} else {
let path = out_dir.join(concat!(#name, ".json"));

let json = api.to_string().unwrap();
write(&path, json + "\n").unwrap();
println!("Exported the full API as {}", path.to_str().unwrap());
}
}
}
}
Expand Down
42 changes: 42 additions & 0 deletions packages/schema/src/idl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,48 @@ impl JsonApi {
serde_json::to_string_pretty(&self).map_err(Into::into)
}

pub fn to_schema_files(&self) -> Result<Vec<(String, String)>, EncodeError> {
let mut result = vec![(
"instantiate.json".to_string(),
serde_json::to_string_pretty(&self.instantiate)?,
)];

if let Some(execute) = &self.execute {
result.push((
"execute.json".to_string(),
serde_json::to_string_pretty(&execute)?,
));
}
if let Some(query) = &self.execute {
result.push((
"query.json".to_string(),
serde_json::to_string_pretty(&query)?,
));
}
if let Some(migrate) = &self.execute {
result.push((
"migrate.json".to_string(),
serde_json::to_string_pretty(&migrate)?,
));
}
if let Some(sudo) = &self.execute {
result.push((
"sudo.json".to_string(),
serde_json::to_string_pretty(&sudo)?,
));
}
if let Some(responses) = &self.responses {
for (name, response) in responses {
result.push((
format!("response_to_{}.json", name),
serde_json::to_string_pretty(&response)?,
));
}
}

Ok(result)
}

pub fn to_writer(&self, writer: impl std::io::Write) -> Result<(), EncodeError> {
serde_json::to_writer_pretty(writer, self).map_err(Into::into)
}
Expand Down

0 comments on commit 4fde2e5

Please sign in to comment.