Skip to content

Commit

Permalink
Moved find_manifest functions from sway_util into forc_util (Fu…
Browse files Browse the repository at this point in the history
…elLabs#957)

* moved `find_manifest` functions into `forc_util`

Co-authored-by: John Adler <[email protected]>
  • Loading branch information
eureka-cpu and adlerjohn authored Mar 23, 2022
1 parent dba70a3 commit a09af8f
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 36 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions forc-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,32 @@ pub mod restricted;

pub const DEFAULT_OUTPUT_DIRECTORY: &str = "out";

/// Continually go up in the file tree until a specified file is found.
#[allow(clippy::branches_sharing_code)]
pub fn find_parent_dir_with_file(starter_path: &Path, file_name: &str) -> Option<PathBuf> {
let mut path = std::fs::canonicalize(starter_path).ok()?;
let empty_path = PathBuf::from("/");
while path != empty_path {
path.push(file_name);
if path.exists() {
path.pop();
return Some(path);
} else {
path.pop();
path.pop();
}
}
None
}
/// Continually go up in the file tree until a Forc manifest file is found.
pub fn find_manifest_dir(starter_path: &Path) -> Option<PathBuf> {
find_parent_dir_with_file(starter_path, constants::MANIFEST_FILE_NAME)
}
/// Continually go up in the file tree until a Cargo manifest file is found.
pub fn find_cargo_manifest_dir(starter_path: &Path) -> Option<PathBuf> {
find_parent_dir_with_file(starter_path, "Cargo.toml")
}

pub fn is_sway_file(file: &Path) -> bool {
let res = file.extension();
Some(OsStr::new(constants::SWAY_EXTENSION)) == res
Expand Down
4 changes: 2 additions & 2 deletions forc/src/ops/forc_build.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::cli::BuildCommand;
use anyhow::{anyhow, bail, Result};
use forc_pkg::{self as pkg, lock, Lock, Manifest};
use forc_util::{default_output_directory, lock_path};
use forc_util::{default_output_directory, find_manifest_dir, lock_path};
use std::{
fs::{self, File},
path::PathBuf,
};
use sway_utils::{find_manifest_dir, MANIFEST_FILE_NAME};
use sway_utils::MANIFEST_FILE_NAME;

pub fn build(command: BuildCommand) -> Result<pkg::Compiled> {
let BuildCommand {
Expand Down
4 changes: 2 additions & 2 deletions forc/src/ops/forc_clean.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::cli::CleanCommand;
use anyhow::{anyhow, bail, Result};
use forc_util::default_output_directory;
use forc_util::{default_output_directory, find_cargo_manifest_dir, find_manifest_dir};
use std::{path::PathBuf, process};
use sway_utils::{find_cargo_manifest_dir, find_manifest_dir, MANIFEST_FILE_NAME};
use sway_utils::MANIFEST_FILE_NAME;

pub fn clean(command: CleanCommand) -> Result<()> {
let CleanCommand { path } = command;
Expand Down
3 changes: 2 additions & 1 deletion forc/src/ops/forc_deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ use crate::ops::forc_build;
use crate::utils::cli_error::CliError;
use anyhow::Result;
use forc_pkg::Manifest;
use forc_util::find_manifest_dir;
use fuel_gql_client::client::FuelClient;
use fuel_tx::{Output, Salt, Transaction};
use fuel_vm::prelude::*;
use std::path::PathBuf;
use sway_core::{parse, TreeType};
use sway_utils::{constants::*, find_manifest_dir};
use sway_utils::constants::*;

pub async fn deploy(command: DeployCommand) -> Result<fuel_tx::ContractId, CliError> {
let curr_dir = if let Some(ref path) = command.path {
Expand Down
4 changes: 2 additions & 2 deletions forc/src/ops/forc_fmt.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::cli::{BuildCommand, FormatCommand};
use crate::ops::forc_build;
use forc_util::{println_green, println_red};
use forc_util::{find_manifest_dir, println_green, println_red};
use prettydiff::{basic::DiffOp, diff_lines};
use std::default::Default;
use std::{fmt, fs, io, path::Path, sync::Arc};
use sway_fmt::{get_formatted_data, FormattingOptions};
use sway_utils::{constants, find_manifest_dir, get_sway_files};
use sway_utils::{constants, get_sway_files};
use taplo::formatter as taplo_fmt;

pub fn format(command: FormatCommand) -> Result<(), FormatError> {
Expand Down
3 changes: 2 additions & 1 deletion forc/src/ops/forc_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use crate::cli::{BuildCommand, RunCommand};
use crate::ops::forc_build;
use crate::utils::cli_error::CliError;
use forc_pkg::Manifest;
use forc_util::find_manifest_dir;
use fuel_gql_client::client::FuelClient;
use fuel_tx::Transaction;
use futures::TryFutureExt;
use std::path::PathBuf;
use std::str::FromStr;
use sway_core::{parse, TreeType};
use sway_utils::{constants::*, find_manifest_dir};
use sway_utils::constants::*;
use tokio::process::Child;

pub async fn run(command: RunCommand) -> Result<(), CliError> {
Expand Down
3 changes: 1 addition & 2 deletions forc/src/ops/forc_update.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::cli::UpdateCommand;
use anyhow::{anyhow, Result};
use forc_pkg::{self as pkg, lock, Lock, Manifest};
use forc_util::lock_path;
use forc_util::{find_manifest_dir, lock_path};
use std::{fs, path::PathBuf};
use sway_utils::find_manifest_dir;

/// Running `forc update` will check for updates for the entire dependency graph and commit new
/// semver-compatible versions to the `Forc.lock` file. For git dependencies, the commit is updated
Expand Down
1 change: 1 addition & 0 deletions sway-lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ description = "LSP server for Sway."

[dependencies]
dashmap = "4.0.2"
forc-util = { version = "0.7.0", path = "../forc-util" }
tower-lsp = "0.16.0"
ropey = "1.2"
serde_json = "1.0.60"
Expand Down
3 changes: 2 additions & 1 deletion sway-lsp/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crate::core::{
document::{DocumentError, TextDocument},
session::Session,
};
use forc_util::find_manifest_dir;
use std::sync::Arc;
use sway_utils::helpers::{find_manifest_dir, get_sway_files};
use sway_utils::helpers::get_sway_files;
use tower_lsp::lsp_types::*;
use tower_lsp::{jsonrpc, Client, LanguageServer};

Expand Down
25 changes: 0 additions & 25 deletions sway-utils/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,6 @@ use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};

/// Continually go up in the file tree until a specified file is found.
#[allow(clippy::branches_sharing_code)]
pub fn find_parent_dir_with_file(starter_path: &Path, file_name: &str) -> Option<PathBuf> {
let mut path = std::fs::canonicalize(starter_path).ok()?;
let empty_path = PathBuf::from("/");
while path != empty_path {
path.push(file_name);
if path.exists() {
path.pop();
return Some(path);
} else {
path.pop();
path.pop();
}
}
None
}
/// Continually go up in the file tree until a Forc manifest file is found.
pub fn find_manifest_dir(starter_path: &Path) -> Option<PathBuf> {
find_parent_dir_with_file(starter_path, constants::MANIFEST_FILE_NAME)
}
/// Continually go up in the file tree until a Cargo manifest file is found.
pub fn find_cargo_manifest_dir(starter_path: &Path) -> Option<PathBuf> {
find_parent_dir_with_file(starter_path, "Cargo.toml")
}
pub fn get_sway_files(path: PathBuf) -> Vec<PathBuf> {
let mut files = vec![];
let mut dir_entries = vec![path];
Expand Down

0 comments on commit a09af8f

Please sign in to comment.