Skip to content

Commit

Permalink
use client path methods
Browse files Browse the repository at this point in the history
  • Loading branch information
macovedj committed Sep 21, 2023
1 parent 8741986 commit 961f701
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 89 deletions.
29 changes: 13 additions & 16 deletions src/commands/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use anyhow::Result;
use clap::Args;
use std::{fs, path::Path};
use warg_client::{
storage::{FileSystemContentStorage, FileSystemRegistryStorage, RegistryStorage},
storage::{
ContentStorage, FileSystemContentStorage, FileSystemRegistryStorage, RegistryStorage,
},
Client,
};
use warg_protocol::{package::ReleaseState, registry::PackageId};
Expand Down Expand Up @@ -75,25 +77,20 @@ impl<'a> Bundler<'a> {
if let Some(name) = name {
let mut version_and_name = name.split('@');
let identifier = version_and_name.next();
let version = version_and_name.next();
if let Some(name) = identifier {
let pkg_id = PackageId::new(name)?;
if let Some(info) = self.client.registry().load_package(&pkg_id).await?
{
let state = &info.state.releases().last().unwrap().state;
if let ReleaseState::Released { content } = state {
let full_digest = content.to_string();
let digest = full_digest.split(':').last().unwrap();
let mut content_path = String::from(
"/Users/interpretations/Library/Caches/warg/content/sha256/",
);
content_path.push_str(&digest);
let path = Path::new(&content_path);
let bytes = fs::read(path)?;
component.section(&RawSection {
id: ComponentSectionId::Component.into(),
data: &bytes,
});
let release_state = &info.state.releases().last().unwrap().state;
if let ReleaseState::Released { content } = release_state {
let path = self.client.content().content_location(content);
if let Some(p) = path {
let bytes = fs::read(p)?;
component.section(&RawSection {
id: ComponentSectionId::Component.into(),
data: &bytes,
});
}
}
}
}
Expand Down
24 changes: 7 additions & 17 deletions src/commands/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use clap::Args;
use ptree::{output::print_tree, TreeBuilder};
use std::fs;
use warg_client::{
storage::{PackageInfo, RegistryStorage},
storage::{ContentStorage, PackageInfo, RegistryStorage},
FileSystemClient,
};
use warg_protocol::{package::ReleaseState, registry::PackageId, VersionReq};
Expand Down Expand Up @@ -60,14 +60,9 @@ impl DependenciesCommand {
let latest = pkg.state.releases().last();
if let Some(l) = latest {
if let ReleaseState::Released { content } = &l.state {
let stringified = content.to_string();
let sha = stringified.split(':').last();
if let Some(sha) = sha {
let path = format!(
"/Users/interpretations/Library/Caches/warg/content/sha256/{}",
sha
);
let bytes = fs::read(path)?;
let path = client.content().content_location(content);
if let Some(p) = path {
let bytes = fs::read(p)?;
let deps = parser.parse(&bytes)?;
for dep in deps {
if let ComponentImportName::Unlocked(name) = dep.name {
Expand Down Expand Up @@ -98,14 +93,9 @@ impl DependenciesCommand {
if let Some(l) = latest {
let mut tree = TreeBuilder::new(format!("{0}@{1}", info.id, l.version));
if let ReleaseState::Released { content } = &l.state {
let stringified = content.to_string();
let sha = stringified.split(':').last();
if let Some(sha) = sha {
let path = format!(
"/Users/interpretations/Library/Caches/warg/content/sha256/{}",
sha
);
let bytes = fs::read(&path)?;
let path = client.content().content_location(content);
if let Some(p) = path {
let bytes = fs::read(&p)?;
let deps = parser.parse(&bytes)?;
for dep in deps {
if let ComponentImportName::Unlocked(name) = dep.name {
Expand Down
93 changes: 37 additions & 56 deletions src/commands/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use indexmap::IndexSet;
use semver::{Version, VersionReq};
use std::{collections::HashMap, fs, path::Path};
use warg_client::{
storage::{PackageInfo, RegistryStorage},
storage::{PackageInfo, RegistryStorage, ContentStorage},
FileSystemClient,
};
use warg_protocol::{package::ReleaseState, registry::PackageId};
Expand Down Expand Up @@ -100,19 +100,15 @@ impl LockListBuilder {
if let Some(pkg_name) = identifier {
let id = PackageId::new(pkg_name)?;
if let Some(info) = client.registry().load_package(&id).await? {
let mut content_path = String::from(
"/Users/interpretations/Library/Caches/warg/content/sha256/",
);
let release = info.state.releases().last();
if let Some(r) = release {
let state = &r.state;
if let ReleaseState::Released { content } = state {
let full_digest = content.to_string();
let digest = full_digest.split(':').last().unwrap();
content_path.push_str(digest);
let path = Path::new(&content_path);
let bytes = fs::read(path)?;
self.parse_package(client, &bytes).await?;
let path = client.content().content_location(content);
if let Some(p) = path {
let bytes = fs::read(p)?;
self.parse_package(client, &bytes).await?;
}
}
}
self.lock_list.insert(name.to_string());
Expand All @@ -125,18 +121,15 @@ impl LockListBuilder {

#[async_recursion]
async fn build_list(&mut self, client: &FileSystemClient, info: &PackageInfo) -> Result<()> {
let mut content_path =
String::from("/Users/interpretations/Library/Caches/warg/content/sha256/");
let release = info.state.releases().last();
if let Some(r) = release {
let state = &r.state;
if let ReleaseState::Released { content } = state {
let full_digest = content.to_string();
let digest = full_digest.split(':').last().unwrap();
content_path.push_str(digest);
let path = Path::new(&content_path);
let bytes = fs::read(path)?;
self.parse_package(client, &bytes).await?;
let path = client.content().content_location(content);
if let Some(p) = path {
let bytes = fs::read(p)?;
self.parse_package(client, &bytes).await?;
}
}
}
Ok(())
Expand Down Expand Up @@ -263,52 +256,40 @@ impl LockCommand {
},
(None, None) => inf.state.releases().last(),
}
// let v = v.replace(['{', '}', '>', '=', '<'], "");
// let maybe = inf.state.releases().find(|r| r.version.to_string() == v);
// if let Some(m) = maybe {
// Some(m)
// } else {
// inf.state.releases().last()
// }
} else {
inf.state.releases().last()
};
if let Some(r) = release {
let state = &r.state;
if let ReleaseState::Released { content } = state {
let full_digest = content.to_string();
let digest = full_digest.split(':').last().unwrap();
let mut content_path = String::from(
"/Users/interpretations/Library/Caches/warg/content/sha256/",
);
content_path.push_str(digest);
let path = Path::new(&content_path);
let mut locked_package = package.split('@').next().unwrap().to_string();
locked_package.push_str(&format!("@{}", &r.version.to_string()));
let component =
wasm_compose::graph::Component::from_file(locked_package, path)?;
let component_index = composer.add_component(component)?;
let instance_id = composer.instantiate(component_index)?;

let added = composer.get_component(component_index);
handled.insert(package, instance_id);
let mut args = Vec::new();
if let Some(added) = added {
for (index, name, _) in added.imports() {
let iid = handled.get(name);
if let Some(arg) = iid {
args.push((arg, index));
}
// }
}
}
for arg in args {
composer.connect(
*arg.0,
None::<ExportIndex>,
instance_id,
arg.1,
)?;
let path = client.content().content_location(content);
if let Some(p) = path {
let component =
wasm_compose::graph::Component::from_file(locked_package, p)?;
let component_index = composer.add_component(component)?;
let instance_id = composer.instantiate(component_index)?;

let added = composer.get_component(component_index);
handled.insert(package, instance_id);
let mut args = Vec::new();
if let Some(added) = added {
for (index, name, _) in added.imports() {
let iid = handled.get(name);
if let Some(arg) = iid {
args.push((arg, index));
}
}
}
for arg in args {
composer.connect(
*arg.0,
None::<ExportIndex>,
instance_id,
arg.1,
)?;
}
}
}
}
Expand Down

0 comments on commit 961f701

Please sign in to comment.