Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetch possible example contracts at compile time #1244

Merged
Next Next commit
Fetch possible example contracts at compile time
  • Loading branch information
elizabethengelman committed Mar 6, 2024
commit 939c217dc25cfd38824733e4359ca9a3adeb5276
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
41 changes: 41 additions & 0 deletions cmd/soroban-cli/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,44 @@
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(",");
elizabethengelman marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -45,35 +45,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