Skip to content

Commit

Permalink
WIP: fetch possible example contracts at compile time
Browse files Browse the repository at this point in the history
  • Loading branch information
elizabethengelman committed Mar 5, 2024
1 parent 195d354 commit 0d6cba6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 29 deletions.
3 changes: 3 additions & 0 deletions cmd/soroban-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ openssl = { version = "=0.10.55", features = ["vendored"] }

[build-dependencies]
crate-git-revision = "0.0.4"
serde.workspace = true
ureq = { version = "2.9.1", features = ["json"] }


[dev-dependencies]
assert_cmd = "2.0.4"
Expand Down
42 changes: 42 additions & 0 deletions cmd/soroban-cli/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
fn main() {
crate_git_revision::init();
let w = &mut std::io::stdout();
__set_example_contracts(w).unwrap();
}

fn __set_example_contracts(w: &mut impl std::io::Write) -> std::io::Result<()> {
// let example_contracts = get_example_contracts().unwrap().join(",");
let example_contracts = get_example_contracts().unwrap().join(",");
writeln!(w, "cargo:rustc-env=EXAMPLE_CONTRACTS={example_contracts}")?;
Ok(())
}

const GITHUB_API_URL: &str =
"https://api.github.com/repos/stellar/soroban-examples/git/trees/main?recursive=1";

#[derive(serde::Deserialize, Debug)]
struct RepoPath {
path: String,
#[serde(rename = "type")]
type_field: String,
}

#[derive(serde::Deserialize, Debug)]
struct ReqBody {
tree: Vec<RepoPath>,
}

fn get_example_contracts() -> Result<Vec<String>, ureq::Error> {
let body: ReqBody = ureq::get(GITHUB_API_URL).call()?.into_json()?;
let mut valid_examples = Vec::new();
for item in body.tree {
if item.type_field == "blob"
|| item.path.starts_with('.')
|| item.path.contains('/')
|| item.path == "hello_world"
{
continue;
}

valid_examples.push(item.path);
}

Ok(valid_examples)
}
31 changes: 2 additions & 29 deletions cmd/soroban-cli/src/commands/contract/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,8 @@ pub struct Cmd {
}

fn possible_example_values() -> ValueParser {
let parser = PossibleValuesParser::new(
[
"account",
"alloc",
"atomic_multiswap",
"atomic_swap",
"auth",
"cross_contract",
"custom_types",
"deep_contract_auth",
"deployer",
"errors",
"eth_abi",
"events",
"fuzzing",
"increment",
"liquidity_pool",
"logging",
"mint-lock",
"simple_account",
"single_offer",
"timelock",
"token",
"upgradeable_contract",
"workspace",
]
.iter()
.map(PossibleValue::new),
);
let example_contracts = env!("EXAMPLE_CONTRACTS").split(",").collect::<Vec<&str>>();
let parser = PossibleValuesParser::new(example_contracts.iter().map(PossibleValue::new));
parser.into()
}

Expand Down

0 comments on commit 0d6cba6

Please sign in to comment.