Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove scarb as dependency (WIP) #61

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions crates/voyager-resolver-cairo/src/compiler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{Context, Result};
use anyhow::{anyhow, Context, Result};
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_defs::ids::ModuleId;
use cairo_lang_filesystem::db::FilesGroup;
Expand Down Expand Up @@ -28,7 +28,6 @@ use crate::graph::{create_graph, get_required_module_for_contracts, EdgeWeight};
// use crate::graph::display_graphviz;
use scarb::compiler::{CompilationUnit, Compiler};
use scarb::core::{TargetKind, Workspace};
use scarb::flock::Filesystem;

pub struct VoyagerGenerator;

Expand Down Expand Up @@ -157,9 +156,6 @@ impl Compiler for VoyagerGenerator {
}
});

// treat target dir as a Filesystem
let target_dir = Filesystem::new(target_dir);

create_attachment_files(&attachment_modules_data, &target_dir)
.with_context(|| "Failed to create attachment files")?;

Expand All @@ -177,7 +173,12 @@ impl Compiler for VoyagerGenerator {
generate_scarb_updated_files(metadata, &target_dir, required_modules)?;

let package_name = unit.main_component().package.id.name.to_string();
let generated_crate_dir = target_dir.path_existent().unwrap().join(package_name);

if !target_dir.exists() {
return Err(anyhow!("unable to locate target directory"));
}

let generated_crate_dir = target_dir.join(package_name);
//Locally run scarb build to make sure that everything compiles correctly before sending the files to voyager.
run_scarb_build(generated_crate_dir.as_str())?;

Expand Down
9 changes: 6 additions & 3 deletions crates/voyager-resolver-cairo/src/compiler/scarb_utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::{anyhow, ensure, Context, Result};
use scarb::flock::Filesystem;
use camino::Utf8Path;
use serde::Deserialize;
use std::fs;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -135,7 +135,7 @@ pub fn get_table_mut<'a>(doc: &'a mut Document, path: &[&str]) -> Result<&'a mut
/// * Generating an updated Scarb.toml file fails.
pub fn generate_scarb_updated_files(
scarb_metadata: scarb_metadata::Metadata,
target_dir: &Filesystem,
target_dir: &Utf8Path,
required_modules: Vec<&CairoModule>,
) -> Result<()> {
let mut metadata = scarb_metadata.clone();
Expand All @@ -153,7 +153,10 @@ pub fn generate_scarb_updated_files(

for package in metadata.packages {
let manifest_path = package.manifest_path;
let target_path = target_dir.path_existent()?.join(package.name);
if !target_dir.exists() {
return Err(anyhow!("unable to locate target dir"));
}
let target_path = target_dir.join(package.name);
generate_updated_scarb_toml(
manifest_path.into_std_path_buf(),
target_path.as_std_path(),
Expand Down
22 changes: 13 additions & 9 deletions crates/voyager-resolver-cairo/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use anyhow::Result;
use anyhow::{anyhow, Result};
use camino::Utf8Path;

use crate::compiler::scarb_utils::read_additional_scarb_manifest_metadata;
use crate::model::{CairoAttachmentModule, CairoImport, CairoModule, ModulePath};

use scarb::core::Workspace;
use scarb::flock::Filesystem;
use std::collections::HashMap;

use std::fs;
Expand Down Expand Up @@ -86,15 +86,17 @@ pub fn get_import_remaps(modules_to_verify: Vec<&CairoModule>) -> Vec<CairoImpor
/// * `target_dir` - The directory in which to generate the .cairo files.
pub fn create_attachment_files(
attachment_modules: &HashMap<ModulePath, CairoAttachmentModule>,
target_dir: &Filesystem,
target_dir: &Utf8Path,
) -> Result<()> {
for (parent_module, attachment_module) in attachment_modules {
let child_modules = &attachment_module.children;
let mut filename = String::new();
let path_split = parent_module.0.split("::");
let crate_name = parent_module.get_crate();
let target_path = target_dir.path_existent()?;
let source_path = target_path.join(crate_name).join("src");
if !target_dir.exists() {
return Err(anyhow!("failed to create attachment files"))
}
let source_path = target_dir.join(crate_name).join("src");
filename = match path_split.clone().count() {
1 => "lib.cairo".to_string(),
_ => {
Expand Down Expand Up @@ -139,12 +141,14 @@ pub fn create_attachment_files(
/// TODO: add comprehensive test for this.
pub fn copy_required_files(
required_modules: &Vec<&CairoModule>,
target_dir: &Filesystem,
target_dir: &Utf8Path,
ws: &Workspace,
) -> Result<()> {
let root_path = ws.root();
let mut root_parts = root_path.components().peekable();
let target_path = target_dir.path_existent()?;
if !target_dir.exists() {
return Err(anyhow!("unable to resolve target dir"));
}

// Skip the first component if it is the Windows or Unix root
if let Some(component) = root_parts.peek() {
Expand All @@ -169,7 +173,7 @@ pub fn copy_required_files(
// Construct the destination path for the .cairo & readme & license file
let root_dir = module.get_root_dir()?;
let filepath_relative = filepath.strip_prefix(root_dir.clone())?;
let dest_path = Path::new(target_path)
let dest_path = Path::new(target_dir)
.join(crate_name.clone())
.join(filepath_relative);

Expand All @@ -190,7 +194,7 @@ pub fn copy_required_files(
// Attempt to copy the readme and license files to the target directory
let root_dir_clone = root_dir.clone();
let base_source_root_path = Path::new(&root_dir_clone);
let base_dest_root_path = Path::new(target_path).join(crate_name);
let base_dest_root_path = Path::new(target_dir).join(crate_name);

let readme_source_path = base_source_root_path.join(&additional_metadata.readme);
let readme_dest_path = base_dest_root_path.join(&additional_metadata.readme);
Expand Down
Loading