Skip to content

Commit

Permalink
Upgrade to Clap 4
Browse files Browse the repository at this point in the history
  • Loading branch information
badboy committed Jun 5, 2023
1 parent 49f6033 commit 6e4170d
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 23 deletions.
4 changes: 2 additions & 2 deletions fixtures/benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ bench = false

[dependencies]
uniffi = {path = "../../uniffi"}
clap = { version = "3.1", features = ["cargo", "std", "derive"] }
criterion = "0.4.0"
clap = { version = "4", features = ["cargo", "std", "derive"] }
criterion = "0.5.1"

[build-dependencies]
uniffi = {path = "../../uniffi", features = ["build"] }
Expand Down
3 changes: 2 additions & 1 deletion fixtures/swift-bridging-header-compile/tests/clang.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use camino::Utf8PathBuf;
use std::process::Command;
use uniffi::TargetLanguage;
use uniffi_testing::UniFFITestHelper;

#[test]
Expand All @@ -13,7 +14,7 @@ fn clang() -> Result<(), anyhow::Error> {
uniffi::generate_bindings(
&Utf8PathBuf::from("src/swift-bridging-header-compile.udl"),
None,
vec!["swift"],
vec![TargetLanguage::Swift],
Some(&out_dir),
None,
false,
Expand Down
4 changes: 2 additions & 2 deletions uniffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ uniffi_core = { path = "../uniffi_core", version = "=0.23.0" }
uniffi_macros = { path = "../uniffi_macros", version = "=0.23.0" }
anyhow = "1"
camino = { version = "1.0.8", optional = true }
clap = { version = "3.1", features = ["cargo", "std", "derive"], optional = true }
clap = { version = "4", features = ["cargo", "std", "derive"], optional = true }

[dev-dependencies]
trybuild = "1"
Expand All @@ -35,7 +35,7 @@ build = [ "dep:uniffi_build" ]
bindgen = ["dep:uniffi_bindgen"]
# Support for `uniffi_bindgen_main()`. Enable this feature for your
# `uniffi-bindgen` binaries.
cli = [ "bindgen", "dep:clap", "dep:camino" ]
cli = [ "bindgen", "uniffi_bindgen?/clap", "dep:clap", "dep:camino" ]
# Support for running example/fixture tests for `uniffi-bindgen`. You probably
# don't need to enable this.
bindgen-tests = [ "dep:uniffi_bindgen" ]
Expand Down
7 changes: 4 additions & 3 deletions uniffi/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use camino::Utf8PathBuf;
use clap::{Parser, Subcommand};
use uniffi_bindgen::bindings::TargetLanguage;

// Structs to help our cmdline parsing. Note that docstrings below form part
// of the "help" output.
Expand All @@ -23,8 +24,8 @@ enum Commands {
/// Generate foreign language bindings
Generate {
/// Foreign language(s) for which to build bindings.
#[clap(long, short, possible_values = &["kotlin", "python", "swift", "ruby"])]
language: Vec<String>,
#[clap(long, short, value_enum)]
language: Vec<TargetLanguage>,

/// Directory in which to write generated files. Default is same folder as .udl file.
#[clap(long, short)]
Expand Down Expand Up @@ -109,7 +110,7 @@ pub fn run_main() -> anyhow::Result<()> {
uniffi_bindgen::generate_bindings(
&source,
config.as_deref(),
language.iter().map(String::as_str).collect(),
language,
out_dir.as_deref(),
lib_file.as_deref(),
!no_format,
Expand Down
4 changes: 3 additions & 1 deletion uniffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ pub use uniffi_bindgen::bindings::ruby::run_test as ruby_run_test;
#[cfg(feature = "bindgen-tests")]
pub use uniffi_bindgen::bindings::swift::run_test as swift_run_test;
#[cfg(feature = "bindgen")]
pub use uniffi_bindgen::{generate_bindings, generate_component_scaffolding, print_json};
pub use uniffi_bindgen::{
bindings::TargetLanguage, generate_bindings, generate_component_scaffolding, print_json,
};
#[cfg(feature = "build")]
pub use uniffi_build::generate_scaffolding;
#[cfg(feature = "bindgen-tests")]
Expand Down
1 change: 1 addition & 0 deletions uniffi_bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ toml = "0.5"
weedle2 = { version = "4.0.0", path = "../weedle2" }
uniffi_meta = { path = "../uniffi_meta", version = "=0.23.0" }
uniffi_testing = { path = "../uniffi_testing", version = "=0.23.0" }
clap = { version = "4", default-features = false, features = ["std", "derive"], optional = true }
13 changes: 11 additions & 2 deletions uniffi_bindgen/src/bindings/kotlin/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use crate::{bindings::RunScriptOptions, library_mode::generate_bindings};
use crate::{
bindings::{RunScriptOptions, TargetLanguage},
library_mode::generate_bindings,
};
use anyhow::{bail, Context, Result};
use camino::{Utf8Path, Utf8PathBuf};
use std::env;
Expand Down Expand Up @@ -34,7 +37,13 @@ pub fn run_script(
let test_helper = UniFFITestHelper::new(crate_name)?;
let out_dir = test_helper.create_out_dir(tmp_dir, &script_path)?;
let cdylib_path = test_helper.copy_cdylib_to_out_dir(&out_dir)?;
generate_bindings(&cdylib_path, None, &["kotlin".into()], &out_dir, false)?;
generate_bindings(
&cdylib_path,
None,
&[TargetLanguage::Kotlin],
&out_dir,
false,
)?;
let jar_file = build_jar(crate_name, &out_dir, options)?;

let mut command = kotlinc_command(options);
Expand Down
1 change: 1 addition & 0 deletions uniffi_bindgen/src/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub mod swift;
/// a few `TryFrom` implementations to help guess the correct target language from
/// e.g. a file extension of command-line argument.
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
pub enum TargetLanguage {
Kotlin,
Swift,
Expand Down
13 changes: 11 additions & 2 deletions uniffi_bindgen/src/bindings/python/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use crate::{bindings::RunScriptOptions, library_mode::generate_bindings};
use crate::{
bindings::{RunScriptOptions, TargetLanguage},
library_mode::generate_bindings,
};
use anyhow::{Context, Result};
use camino::Utf8Path;
use std::env;
Expand Down Expand Up @@ -35,7 +38,13 @@ pub fn run_script(
let test_helper = UniFFITestHelper::new(crate_name)?;
let out_dir = test_helper.create_out_dir(tmp_dir, &script_path)?;
let cdylib_path = test_helper.copy_cdylib_to_out_dir(&out_dir)?;
generate_bindings(&cdylib_path, None, &["python".into()], &out_dir, false)?;
generate_bindings(
&cdylib_path,
None,
&[TargetLanguage::Python],
&out_dir,
false,
)?;

let pythonpath = env::var_os("PYTHONPATH").unwrap_or_else(|| OsString::from(""));
let pythonpath = env::join_paths(
Expand Down
3 changes: 2 additions & 1 deletion uniffi_bindgen/src/bindings/ruby/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use crate::bindings::TargetLanguage;
use crate::library_mode::generate_bindings;
use anyhow::{bail, Context, Result};
use camino::Utf8Path;
Expand Down Expand Up @@ -33,7 +34,7 @@ pub fn test_script_command(
let test_helper = UniFFITestHelper::new(fixture_name)?;
let out_dir = test_helper.create_out_dir(tmp_dir, &script_path)?;
let cdylib_path = test_helper.copy_cdylib_to_out_dir(&out_dir)?;
generate_bindings(&cdylib_path, None, &["ruby".into()], &out_dir, false)?;
generate_bindings(&cdylib_path, None, &[TargetLanguage::Ruby], &out_dir, false)?;

let rubypath = env::var_os("RUBYLIB").unwrap_or_else(|| OsString::from(""));
let rubypath = env::join_paths(
Expand Down
8 changes: 6 additions & 2 deletions uniffi_bindgen/src/bindings/swift/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use crate::{bindings::RunScriptOptions, library_mode::generate_bindings};
use crate::{
bindings::{RunScriptOptions, TargetLanguage},
library_mode::generate_bindings,
};
use anyhow::{bail, Context, Result};
use camino::{Utf8Path, Utf8PathBuf};
use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};
Expand Down Expand Up @@ -123,7 +126,8 @@ struct GeneratedSources {

impl GeneratedSources {
fn new(crate_name: &str, cdylib_path: &Utf8Path, out_dir: &Utf8Path) -> Result<Self> {
let sources = generate_bindings(cdylib_path, None, &["swift".into()], out_dir, false)?;
let sources =
generate_bindings(cdylib_path, None, &[TargetLanguage::Swift], out_dir, false)?;
let main_source = sources
.iter()
.find(|s| s.package.name == crate_name)
Expand Down
5 changes: 3 additions & 2 deletions uniffi_bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ pub mod library_mode;
pub mod macro_metadata;
pub mod scaffolding;

use bindings::TargetLanguage;
pub use interface::ComponentInterface;
use scaffolding::RustScaffolding;

Expand Down Expand Up @@ -268,7 +269,7 @@ pub fn generate_component_scaffolding(
pub fn generate_bindings(
udl_file: &Utf8Path,
config_file_override: Option<&Utf8Path>,
target_languages: Vec<&str>,
target_languages: Vec<TargetLanguage>,
out_dir_override: Option<&Utf8Path>,
library_file: Option<&Utf8Path>,
try_format_code: bool,
Expand All @@ -287,7 +288,7 @@ pub fn generate_bindings(
&config.bindings,
&component,
&out_dir,
language.try_into()?,
language,
try_format_code,
)?;
}
Expand Down
12 changes: 7 additions & 5 deletions uniffi_bindgen/src/library_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
/// all of them at once.
/// - UniFFI can figure out the package/module names for each crate, eliminating the external
/// package maps.
use crate::{bindings, macro_metadata, parse_udl, ComponentInterface, Config, Result};
use crate::{
bindings::{self, TargetLanguage},
macro_metadata, parse_udl, ComponentInterface, Config, Result,
};
use anyhow::{bail, Context};
use camino::Utf8Path;
use cargo_metadata::{MetadataCommand, Package};
Expand All @@ -31,7 +34,7 @@ use uniffi_meta::group_metadata;
pub fn generate_bindings(
library_path: &Utf8Path,
crate_name: Option<String>,
target_languages: &[String],
target_languages: &[TargetLanguage],
out_dir: &Utf8Path,
try_format_code: bool,
) -> Result<Vec<Source>> {
Expand Down Expand Up @@ -74,9 +77,8 @@ pub fn generate_bindings(
}

for source in sources.iter() {
for language in target_languages {
let language: bindings::TargetLanguage = language.as_str().try_into()?;
if cdylib_name.is_none() && language != bindings::TargetLanguage::Swift {
for &language in target_languages {
if cdylib_name.is_none() && language != TargetLanguage::Swift {
bail!("Generate bindings for {language} requires a cdylib, but {library_path} was given");
}
bindings::write_bindings(
Expand Down

0 comments on commit 6e4170d

Please sign in to comment.