Skip to content

Commit

Permalink
forc: Add util user_forc_directory function (FuelLabs#809)
Browse files Browse the repository at this point in the history
* forc: Add util `user_forc_directory` function

This changes the `forc explorer` tool from using `.fuel` as its user
directory to follow suit of `forc`'s dependency handling and use `.forc`
instead.

Path handling for both has also been fixed to theoretically better
support windows (though I haven't tested).

Both are now implemented in terms of the new `user_forc_directory`
function.

Closes FuelLabs#778.

* Rename const to `USER_FORC_DIRECTORY`, use in shared helper fn

Also refactors `forc_update` and `forc::utils::dependency` in terms of
the updated `user_forc_directory` function.
  • Loading branch information
mitchmindtree authored Feb 20, 2022
1 parent 023a901 commit 323b4e1
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 69 deletions.
17 changes: 8 additions & 9 deletions forc/src/ops/forc_dep_check.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::utils::{dependency, helpers::read_manifest};
use crate::utils::{
dependency,
helpers::{read_manifest, user_forc_directory},
};
use anyhow::{anyhow, Result};
use dirs::home_dir;
use semver::Version;
use std::{
path::{Path, PathBuf},
Expand Down Expand Up @@ -58,14 +60,11 @@ async fn check_dependency(
dependency_name: &str,
dep: &dependency::DependencyDetails,
) -> Result<()> {
let home_dir = match home_dir() {
None => return Err(anyhow!("Couldn't find home directory (`~/`)")),
Some(p) => p.to_str().unwrap().to_owned(),
};

let user_forc_dir = user_forc_directory();
let dep_dir = user_forc_dir.join(dependency_name);
let target_directory = match &dep.branch {
Some(b) => PathBuf::from(format!("{}/.forc/{}/{}", home_dir, dependency_name, &b)),
None => PathBuf::from(format!("{}/.forc/{}/default", home_dir, dependency_name)),
Some(branch) => dep_dir.join(branch),
None => dep_dir.join("default"),
};

// Currently we only handle checks on github-based dependencies
Expand Down
35 changes: 17 additions & 18 deletions forc/src/ops/forc_explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::fs::{create_dir_all, remove_dir_all, remove_file, rename, File};
use std::io::Cursor;
use std::path::PathBuf;

use crate::utils::helpers::user_forc_directory;
use ansi_term::Colour;
use dirs;
use reqwest;
use serde::Deserialize;
use tar::Archive;
Expand Down Expand Up @@ -36,35 +36,34 @@ impl EndPoints {

struct ExplorerAppPaths {}

fn explorer_directory() -> PathBuf {
user_forc_directory().join("explorer")
}

impl ExplorerAppPaths {
pub fn web_app_path() -> PathBuf {
dirs::home_dir().unwrap().join(".fuel/explorer")
explorer_directory()
}
pub fn web_app_version_path(version: &str) -> PathBuf {
dirs::home_dir()
.unwrap()
.join(".fuel/explorer")
.join(version)
explorer_directory().join(version)
}
pub fn web_app_files_path(version: &str) -> PathBuf {
dirs::home_dir()
.unwrap()
.join(format!(".fuel/explorer/{}/www", version))
explorer_directory().join(version).join("www")
}
pub fn build_archive_path(version: &str) -> PathBuf {
dirs::home_dir()
.unwrap()
.join(format!(".fuel/explorer/{}/build.tar", version))
explorer_directory()
.join(version)
.join("build")
.with_extension("tar")
}
pub fn build_archive_unpack_path(version: &str) -> PathBuf {
dirs::home_dir()
.unwrap()
.join(format!(".fuel/explorer/{version}/build"))
explorer_directory().join(version).join("build")
}
pub fn web_app_static_assets_path(version: &str) -> PathBuf {
dirs::home_dir()
.unwrap()
.join(format!(".fuel/explorer/{}/www/static", version))
explorer_directory()
.join(version)
.join("www")
.join("static")
}
}

Expand Down
22 changes: 9 additions & 13 deletions forc/src/ops/forc_update.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use crate::{
cli::UpdateCommand,
ops::forc_dep_check,
utils::{dependency, helpers::read_manifest},
utils::{
dependency,
helpers::{read_manifest, user_forc_directory},
},
};
use anyhow::{anyhow, Result};
use dirs::home_dir;
use std::{path::PathBuf, str};
use sway_utils::{
constants::{self},
find_manifest_dir,
};
use sway_utils::find_manifest_dir;

/// Forc update will update the contents inside the Forc dependencies directory.
/// If a dependency `d` is passed as parameter, it will only try and update that specific dependency.
Expand Down Expand Up @@ -69,11 +68,6 @@ async fn update_dependency(
dependency_name: &str,
dep: &dependency::DependencyDetails,
) -> Result<()> {
let home_dir = match home_dir() {
None => return Err(anyhow!("Couldn't find home directory (`~/`)")),
Some(p) => p.to_str().unwrap().to_owned(),
};

// Currently we only handle updates on github-based dependencies
if let Some(git) = &dep.git {
match &dep.version {
Expand All @@ -83,9 +77,11 @@ async fn update_dependency(
// lookup for a desired version and manually specify it in `Forc.toml`.
Some(version) => println!("Ignoring update for {} at version {}: Forc update not implemented for dependencies with specified tag. To update to another tag, change the tag in `Forc.toml` and run the build command.", dependency_name, version),
None => {
let forc_dir = user_forc_directory();
let dep_dir = forc_dir.join(dependency_name);
let target_directory = match &dep.branch {
Some(b) => PathBuf::from(format!("{}/{}/{}/{}", home_dir, constants::FORC_DEPENDENCIES_DIRECTORY, dependency_name, &b)),
None => PathBuf::from(format!("{}/{}/{}/default", home_dir, constants::FORC_DEPENDENCIES_DIRECTORY, dependency_name)),
Some(branch) => dep_dir.join(branch),
None => dep_dir.join("default"),
};

let current = dependency::get_current_dependency_version(&target_directory)?;
Expand Down
34 changes: 6 additions & 28 deletions forc/src/utils/dependency.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::utils::manifest::Manifest;
use crate::utils::{helpers::user_forc_directory, manifest::Manifest};
use anyhow::{anyhow, bail, Context, Result};
use dirs::home_dir;
use flate2::read::GzDecoder;
use serde::{Deserialize, Serialize};
use std::collections::hash_map::DefaultHasher;
Expand All @@ -12,7 +11,6 @@ use std::{
io::Cursor,
path::{Path, PathBuf},
};
use sway_utils::constants;
use tar::Archive;

// A collection of remote dependency related functions
Expand Down Expand Up @@ -99,41 +97,21 @@ pub fn download_github_dep(
version: &Option<String>,
offline_mode: OfflineMode,
) -> Result<String> {
let home_dir = match home_dir() {
None => return Err(anyhow!("Couldn't find home directory (`~/`)")),
Some(p) => p.to_str().unwrap().to_owned(),
};

// hash the dep name into a number to avoid bad characters
let mut s = DefaultHasher::new();
dep_name.hash(&mut s);
let hashed_dep_name = s.finish().to_string();

// Version tag takes precedence over branch reference.
let forc_dir = user_forc_directory();
let dep_dir = forc_dir.join(hashed_dep_name);
let out_dir = match &version {
Some(v) => PathBuf::from(format!(
"{}/{}/{}/{}",
home_dir,
constants::FORC_DEPENDENCIES_DIRECTORY,
hashed_dep_name,
v
)),
Some(v) => dep_dir.join(v),
// If no version specified, check if a branch was specified
None => match &branch {
Some(b) => PathBuf::from(format!(
"{}/{}/{}/{}",
home_dir,
constants::FORC_DEPENDENCIES_DIRECTORY,
hashed_dep_name,
b
)),
Some(b) => dep_dir.join(b),
// If no version and no branch, use default
None => PathBuf::from(format!(
"{}/{}/{}/default",
home_dir,
constants::FORC_DEPENDENCIES_DIRECTORY,
hashed_dep_name
)),
None => dep_dir.join("default"),
},
};

Expand Down
7 changes: 7 additions & 0 deletions forc/src/utils/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ pub fn default_output_directory(manifest_dir: &Path) -> PathBuf {
manifest_dir.join(DEFAULT_OUTPUT_DIRECTORY)
}

/// Returns the user's `.forc` directory, `$HOME/.forc` by default.
pub fn user_forc_directory() -> PathBuf {
dirs::home_dir()
.expect("unable to find the user home directory")
.join(constants::USER_FORC_DIRECTORY)
}

pub fn print_on_success(
silent_mode: bool,
proj_name: &str,
Expand Down
2 changes: 1 addition & 1 deletion sway-utils/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub const MANIFEST_FILE_NAME: &str = "Forc.toml";
pub const TEST_MANIFEST_FILE_NAME: &str = "Cargo.toml";
pub const TEST_DIRECTORY: &str = "tests/";
pub const SWAY_EXTENSION: &str = "sw";
pub const FORC_DEPENDENCIES_DIRECTORY: &str = ".forc";
pub const USER_FORC_DIRECTORY: &str = ".forc";
pub const SRC_DIR: &str = "src";
pub const SWAY_PREDICATE: &str = "predicate";
pub const SWAY_LIBRARY: &str = "library";
Expand Down

0 comments on commit 323b4e1

Please sign in to comment.