Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Commit

Permalink
transitive resolve for package deps
Browse files Browse the repository at this point in the history
  • Loading branch information
noise64 committed Nov 14, 2024
1 parent 951fa54 commit 438d81e
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 12 deletions.
33 changes: 21 additions & 12 deletions wasm-rpc-stubgen/src/commands/declarative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ use crate::wit_generate::{
add_stub_as_dependency_to_wit_dir, extract_main_interface_as_wit_dep, AddStubAsDepConfig,
UpdateCargoToml,
};
use crate::wit_resolve::{parse_wit_deps_dir, ResolvedWitApplication};
use crate::wit_resolve::{ResolvedWitApplication, WitDepsResolver};
use crate::{commands, naming, WasmRpcOverride};
use anyhow::{anyhow, Context, Error};
use colored::Colorize;
use glob::glob;
use itertools::Itertools;
use std::cell::OnceCell;
use std::cmp::Ordering;
use std::path::{Path, PathBuf};
use std::process::Command;
Expand All @@ -43,6 +44,7 @@ struct ApplicationContext {
config: Config,
application: Application,
wit: ResolvedWitApplication,
wit_deps: OnceCell<anyhow::Result<WitDepsResolver>>,
}

impl ApplicationContext {
Expand All @@ -54,6 +56,7 @@ impl ApplicationContext {
config,
application,
wit,
wit_deps: OnceCell::new(),
})
}),
)
Expand All @@ -67,6 +70,17 @@ impl ApplicationContext {
}),
)
}

fn wit_deps(&self) -> anyhow::Result<&WitDepsResolver> {
match self
.wit_deps
.get_or_init(|| WitDepsResolver::new(self.application.wit_deps()))
.as_ref()
{
Ok(wit_deps) => Ok(wit_deps),
Err(err) => Err(anyhow!("Failed to init wit dependency resolver? {}", err)),
}
}
}

pub fn init(component_name: ComponentName) -> anyhow::Result<()> {
Expand Down Expand Up @@ -655,28 +669,23 @@ fn create_base_output_wit(
ctx.wit.missing_generic_input_package_deps(component_name)?;

if !missing_package_deps.is_empty() {
log_action("Adding", "common package deps");
log_action("Adding", "package deps");
let _indent = LogIndent::new();

// TODO: transitive deps?
// TODO: use wit dep from app manifest
// TODO: extract dep management, with preferring higher versions
let wit_deps = ctx.wit_deps()?;

let wit_deps = parse_wit_deps_dir(Path::new("wit-deps"))?;
let all_package_deps = wit_deps.package_names_with_deps(&missing_package_deps)?;

for package_name in missing_package_deps {
for package_name in all_package_deps {
log_action(
"Adding",
format!(
"package dependency {}",
package_name.to_string().log_color_highlight()
),
);
let dep = wit_deps
.iter()
.find(|package| package.main.name == package_name)
.ok_or_else(|| anyhow!("Package dependency {} not found", package_name))?;
for source in dep.source_map.source_files() {

for source in wit_deps.package_sources(&package_name)? {
let source = PathExtra::new(source);
let parent = PathExtra::new(
source
Expand Down
7 changes: 7 additions & 0 deletions wasm-rpc-stubgen/src/model/wasm_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,13 @@ impl Application {
)
}

pub fn wit_deps(&self) -> Vec<PathBuf> {
self.common_wasm_build
.as_ref()
.map(|wasm_build| wasm_build._wit_deps.clone())
.unwrap_or_default()
}

pub fn all_wasm_rpc_dependencies(&self) -> BTreeSet<ComponentName> {
self.wasm_components_by_name
.iter()
Expand Down
74 changes: 74 additions & 0 deletions wasm-rpc-stubgen/src/wit_resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,77 @@ pub fn parse_wit_deps_dir(path: &Path) -> Result<Vec<UnresolvedPackageGroup>, Er
})
.collect::<Result<Vec<_>, _>>()
}

pub struct WitDepsResolver {
sources: Vec<PathBuf>,
packages: HashMap<PathBuf, HashMap<PackageName, UnresolvedPackageGroup>>,
}

impl WitDepsResolver {
pub fn new(sources: Vec<PathBuf>) -> anyhow::Result<Self> {
let mut packages = HashMap::<PathBuf, HashMap<PackageName, UnresolvedPackageGroup>>::new();

for source in &sources {
packages.insert(
source.clone(),
parse_wit_deps_dir(source)?
.into_iter()
.map(|package| (package.main.name.clone(), package))
.collect(),
);
}

Ok(Self { sources, packages })
}

pub fn package(&self, package_name: &PackageName) -> anyhow::Result<&UnresolvedPackageGroup> {
for source in &self.sources {
if let Some(package) = self.packages.get(source).unwrap().get(package_name) {
return Ok(package);
}
}
bail!(
"Package {} not found, sources searched: {}",
package_name,
self.sources
.iter()
.map(|s| s.display().to_string())
.join(", ")
)
}

pub fn package_sources(
&self,
package_name: &PackageName,
) -> anyhow::Result<impl Iterator<Item = &Path>> {
self.package(package_name)
.map(|package| package.source_map.source_files())
}

pub fn package_names_with_deps(
&self,
package_names: &[PackageName],
) -> anyhow::Result<HashSet<PackageName>> {
fn visit(
resolver: &WitDepsResolver,
all_package_names: &mut HashSet<PackageName>,
package_name: &PackageName,
) -> anyhow::Result<()> {
if !all_package_names.contains(package_name) {
let package = resolver.package(package_name)?;
all_package_names.insert(package_name.clone());
for package_name in package.main.foreign_deps.keys() {
visit(resolver, all_package_names, package_name)?;
}
}

Ok(())
}

let mut all_package_names = HashSet::<PackageName>::new();
for package_name in package_names {
visit(self, &mut all_package_names, package_name)?;
}
Ok(all_package_names)
}
}

0 comments on commit 438d81e

Please sign in to comment.