Use structopt to capture generate options

This commit is contained in:
Steve Myers 2022-03-27 23:46:25 -07:00
parent ce848725b4
commit c66dfdd52a
No known key found for this signature in database
GPG Key ID: 8105A46B22C2D051
2 changed files with 108 additions and 45 deletions

View File

@ -20,14 +20,10 @@ uniffi = { version = "0.16.0", features = ["builtin-bindgen"] }
thiserror = "1.0" thiserror = "1.0"
anyhow = "=1.0.45" # remove after upgrading to next version of uniffi anyhow = "=1.0.45" # remove after upgrading to next version of uniffi
uniffi_bindgen = "0.16.0" uniffi_bindgen = "0.16.0"
structopt = "0.3"
#uniffi_bindgen = { version = "0.16.0", optional = true }
[build-dependencies] [build-dependencies]
uniffi_build = { version = "0.16.0", features = ["builtin-bindgen"] } uniffi_build = { version = "0.16.0", features = ["builtin-bindgen"] }
[features]
#generate-python = ["uniffi_bindgen"]
[[bin]] [[bin]]
name = "generate" name = "generate-bindings"

View File

@ -1,67 +1,134 @@
use std::fmt;
use std::str::FromStr;
use structopt::StructOpt;
use uniffi_bindgen; use uniffi_bindgen;
pub const BDK_UDL: &str = "src/bdk.udl"; pub const BDK_UDL: &str = "src/bdk.udl";
#[derive(Debug)] #[derive(Debug, PartialEq)]
#[derive(PartialEq)]
pub enum Language { pub enum Language {
KOTLIN, KOTLIN,
// PYTHON, PYTHON,
// SWIFT, SWIFT,
UNSUPPORTED,
} }
impl Language { impl fmt::Display for Language {
fn to_string(self) -> &'static str { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self == Language::KOTLIN { "kotlin" } match self {
// else if self == Language::PYTHON { "python" } Language::KOTLIN => write!(f, "kotlin"),
// else if self == Language::SWIFT { "swift" } Language::SWIFT => write!(f, "swift"),
else { panic!("Not a supported language") } Language::PYTHON => write!(f, "python"),
}
} }
} }
fn parse_language(language_argument: &str) -> Language { #[derive(Debug)]
match language_argument { pub enum Error {
"kotlin" => Language::KOTLIN, UnsupportedLanguage,
// "python" => Language::PYTHON,
// "swift" => Language::SWIFT,
_ => panic!("Unsupported language")
}
} }
fn generate_bindings() -> Result<(), Box<dyn std::error::Error>> { impl fmt::Display for Error {
use std::env; fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let args: Vec<String> = env::args().collect(); write!(f, "{:?}", self)
let language: Language;
let output_directory: &String;
if &args[1] != "--language" {
panic!("Please provide the --language option");
} else {
language = parse_language(&args[2]);
} }
}
if &args[3] != "--out-dir" { impl FromStr for Language {
panic!("Please provide the --out-dir option"); type Err = Error;
} else { fn from_str(s: &str) -> Result<Self, Self::Err> {
output_directory = &args[4] match s {
"kotlin" => Ok(Language::KOTLIN),
"python" => Ok(Language::PYTHON),
"swift" => Ok(Language::SWIFT),
_ => Err(Error::UnsupportedLanguage),
}
} }
}
println!("Chosen language is {:?}", language); fn generate_bindings(opt: &Opt) -> anyhow::Result<(), anyhow::Error> {
println!("Output directory is {:?}", output_directory);
uniffi_bindgen::generate_bindings( uniffi_bindgen::generate_bindings(
&format!("{}/{}", env!("CARGO_MANIFEST_DIR"), BDK_UDL), &format!("{}/{}", env!("CARGO_MANIFEST_DIR"), BDK_UDL),
None, None,
vec![language.to_string()], vec![opt.language.to_string().as_str()],
Some(&output_directory), Some(&opt.out_dir),
false, false,
)?; )?;
Ok(()) Ok(())
} }
fn main() -> Result<(), Box<dyn std::error::Error>> { fn fixup_python_lib_path<O: AsRef<std::path::Path>>(
generate_bindings()?; out_dir: O,
lib_name: &str,
) -> Result<(), Box<dyn std::error::Error>> {
use std::fs;
use std::io::Write;
const LOAD_INDIRECT_DEF: &str = "def loadIndirect():";
let bindings_file = out_dir.as_ref().join("bdk.py");
let mut data = fs::read_to_string(&bindings_file)?;
let pos = data.find(LOAD_INDIRECT_DEF).expect(&format!(
"loadIndirect not found in `{}`",
bindings_file.display()
));
let range = pos..pos + LOAD_INDIRECT_DEF.len();
let replacement = format!(
r#"
def loadIndirect():
import glob
return getattr(ctypes.cdll, glob.glob(os.path.join(os.path.dirname(os.path.abspath(__file__)), '{}.*'))[0])
def _loadIndirectOld():"#,
lib_name
);
data.replace_range(range, &replacement);
let mut file = fs::OpenOptions::new()
.write(true)
.truncate(true)
.open(&bindings_file)?;
file.write(data.as_bytes())?;
Ok(())
}
#[derive(Debug, StructOpt)]
#[structopt(
name = "generate-bindings",
about = "A tool to generate bdk-ffi language bindings"
)]
struct Opt {
/// Language to generate bindings for
#[structopt(env = "UNIFFI_BINDGEN_LANGUAGE", short, long, possible_values(&["kotlin","swift","python"]), parse(try_from_str = Language::from_str))]
language: Language,
/// Output directory to put generated language bindings
#[structopt(env = "UNIFFI_BINDGEN_OUTPUT_DIR", short, long)]
out_dir: String,
/// Python fix up lib path
#[structopt(env = "UNIFFI_BINDGEN_PYTHON_FIXUP_PATH", short, long)]
python_fixup_path: Option<String>,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let opt = Opt::from_args();
println!("{:?}", opt);
println!("Chosen language is {}", opt.language);
println!("Output directory is {}", opt.out_dir);
println!("Python fix up lib path is {:?}", opt.python_fixup_path);
generate_bindings(&opt)?;
if opt.language == Language::PYTHON {
if let Some(name) = opt.python_fixup_path {
println!("Fixing up python lib path");
fixup_python_lib_path(&opt.out_dir, &name)?;
}
}
Ok(()) Ok(())
} }