Skip to content

Commit

Permalink
Stop generating the bindings file for projects without wit files and …
Browse files Browse the repository at this point in the history
…dependencies
  • Loading branch information
iawia002 committed Jul 4, 2024
1 parent bc90478 commit 92c886c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 29 deletions.
50 changes: 29 additions & 21 deletions src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,26 @@ impl<'a> BindingsGenerator<'a> {
/// Returns a tuple of the bindings generator and a map of import names.
pub fn new(
resolution: &'a PackageDependencyResolution<'a>,
) -> Result<(Self, HashMap<String, String>)> {
) -> Result<Option<(Self, HashMap<String, String>)>> {
let mut import_name_map = Default::default();
let (resolve, world, source_files) =
Self::create_target_world(resolution, &mut import_name_map).with_context(|| {
format!(
"failed to create a target world for package `{name}` ({path})",
name = resolution.metadata.name,
path = resolution.metadata.manifest_path.display()
)
})?;

Ok((
Self {
resolution,
resolve,
world,
source_files,
},
import_name_map,
))
match Self::create_target_world(resolution, &mut import_name_map).with_context(|| {
format!(
"failed to create a target world for package `{name}` ({path})",
name = resolution.metadata.name,
path = resolution.metadata.manifest_path.display()
)
})? {
Some((resolve, world, source_files)) => Ok(Some((
Self {
resolution,
resolve,
world,
source_files,
},
import_name_map,
))),
None => Ok(None),
}
}

/// Gets the cargo metadata for the package that the bindings are for.
Expand Down Expand Up @@ -223,26 +223,31 @@ impl<'a> BindingsGenerator<'a> {
fn create_target_world(
resolution: &PackageDependencyResolution,
import_name_map: &mut HashMap<String, String>,
) -> Result<(Resolve, WorldId, Vec<PathBuf>)> {
) -> Result<Option<(Resolve, WorldId, Vec<PathBuf>)>> {
log::debug!(
"creating target world for package `{name}` ({path})",
name = resolution.metadata.name,
path = resolution.metadata.manifest_path.display()
);

// A flag used to determine whether the target is empty. It must meet two conditions:
// no wit files and no dependencies.
let mut empty_target = false;
let (mut merged, world_id, source_files) =
if let Some(name) = resolution.metadata.target_package() {
Self::target_package(resolution, name, resolution.metadata.target_world())?
} else if let Some(path) = resolution.metadata.target_path() {
Self::target_local_path(resolution, &path, resolution.metadata.target_world())?
} else {
empty_target = true;
let (merged, world) = Self::target_empty_world(resolution);
(merged, world, Vec::new())
};

// Merge all component dependencies as interface imports
for (id, dependency) in &resolution.resolutions {
log::debug!("importing component dependency `{id}`");
empty_target = false;

let (mut resolve, component_world_id) = dependency
.decode()?
Expand Down Expand Up @@ -271,7 +276,10 @@ impl<'a> BindingsGenerator<'a> {
)?;
}

Ok((merged, world_id, source_files))
if empty_target {
return Ok(None);
};
Ok(Some((merged, world_id, source_files)))
}

fn target_package(
Expand Down
5 changes: 1 addition & 4 deletions src/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,7 @@ impl NewCommand {
}
None => {
if self.is_command() {
Ok(r#"#[allow(warnings)]
mod bindings;
fn main() {
Ok(r#"fn main() {
println!("Hello, world!");
}
"#
Expand Down
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,12 @@ async fn generate_package_bindings(
return Ok(HashMap::new());
}

// If there is no wit files and no dependencies, stop generating the bindings file for it.
let (generator, import_name_map) = match BindingsGenerator::new(resolution)? {
Some(v) => v,
None => return Ok(HashMap::new()),
};

// TODO: make the output path configurable
let output_dir = resolution
.metadata
Expand All @@ -830,7 +836,6 @@ async fn generate_package_bindings(
.then(|| last_modified_time(&bindings_path))
.transpose()?;

let (generator, import_name_map) = BindingsGenerator::new(resolution)?;
match generator.reason(last_modified_exe, last_modified_output)? {
Some(reason) => {
log::debug!(
Expand Down
3 changes: 0 additions & 3 deletions tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ fn it_runs_with_command_component() -> Result<()> {
fs::write(
project.root().join("src/main.rs"),
r#"
#[allow(warnings)]
mod bindings;
fn main() {
if std::env::args().any(|v| v == "--verbose") {
println!("[guest] running component 'my:command'");
Expand Down

0 comments on commit 92c886c

Please sign in to comment.