From 961f7014ae6df4c36ba3f9ed0f3a8eccc54f0077 Mon Sep 17 00:00:00 2001 From: Daniel Macovei Date: Thu, 21 Sep 2023 10:03:38 -0500 Subject: [PATCH] use client path methods --- src/commands/bundle.rs | 29 +++++------ src/commands/dependencies.rs | 24 +++------- src/commands/lock.rs | 93 ++++++++++++++---------------------- 3 files changed, 57 insertions(+), 89 deletions(-) diff --git a/src/commands/bundle.rs b/src/commands/bundle.rs index 558e40aa..d48a7a5c 100644 --- a/src/commands/bundle.rs +++ b/src/commands/bundle.rs @@ -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}; @@ -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, + }); + } } } } diff --git a/src/commands/dependencies.rs b/src/commands/dependencies.rs index 6e18e421..74a327da 100644 --- a/src/commands/dependencies.rs +++ b/src/commands/dependencies.rs @@ -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}; @@ -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 { @@ -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 { diff --git a/src/commands/lock.rs b/src/commands/lock.rs index 213796ab..1b46701f 100644 --- a/src/commands/lock.rs +++ b/src/commands/lock.rs @@ -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}; @@ -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()); @@ -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(()) @@ -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::, - 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::, + instance_id, + arg.1, + )?; + } } } }