Skip to content

Commit

Permalink
refactor(forc-pkg): Abstract out source-related items from pkg (#…
Browse files Browse the repository at this point in the history
…4168)

## Description

In anticipation of the upcoming ipfs support (#3926), this PR refactors
`forc-pkg` to more clearly separate out the "source" abstraction from
the rest of the pkg-handling logic.

We do so by creating a common interface for sources in the form of
traits for pinning and fetching. This should allow for adding in the
upcoming ipfs implementation (or future alternative source kind) in a
manner that doesn't further complicate the rest of package handling.

There is still room for further refactor of `pkg.rs`, but this should do
enough to clear the way for ipfs support and closes #3927.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

Co-authored-by: Joshua Batty <joshpbatty@gmail.com>
  • Loading branch information
mitchmindtree and JoshuaBatty authored Feb 23, 2023
1 parent a1cacd6 commit 9346bf4
Show file tree
Hide file tree
Showing 9 changed files with 1,305 additions and 1,135 deletions.
1 change: 1 addition & 0 deletions forc-pkg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
pub mod lock;
pub mod manifest;
mod pkg;
pub mod source;

pub use lock::Lock;
pub use manifest::{
Expand Down
13 changes: 5 additions & 8 deletions forc-pkg/src/lock.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{pkg, DepKind, Edge};
use crate::{pkg, source, DepKind, Edge};
use anyhow::{anyhow, Result};
use forc_tracing::{println_green, println_red};
use petgraph::{visit::EdgeRef, Direction};
Expand Down Expand Up @@ -60,10 +60,7 @@ impl PkgLock {
pub fn from_node(graph: &pkg::Graph, node: pkg::NodeIx, disambiguate: &HashSet<&str>) -> Self {
let pinned = &graph[node];
let name = pinned.name.clone();
let version = match &pinned.source {
pkg::SourcePinned::Registry(reg) => Some(reg.source.version.clone()),
_ => None,
};
let version = pinned.source.semver();
let source = pinned.source.to_string();
// Collection of all dependencies, so this includes both contract-dependencies and
// lib-dependencies
Expand Down Expand Up @@ -188,7 +185,7 @@ impl Lock {
// required.
let key = pkg.name_disambiguated(&disambiguate).into_owned();
let name = pkg.name.clone();
let source: pkg::SourcePinned = pkg.source.parse().map_err(|e| {
let source: source::Pinned = pkg.source.parse().map_err(|e| {
anyhow!("invalid 'source' entry for package {} lock: {:?}", name, e)
})?;
let pkg = pkg::Pinned { name, source };
Expand Down Expand Up @@ -272,7 +269,7 @@ fn pkg_unique_string(name: &str, source: &str) -> String {
fn pkg_dep_line(
dep_name: Option<&str>,
name: &str,
source: &pkg::SourcePinned,
source: &source::Pinned,
dep_kind: &DepKind,
disambiguate: bool,
) -> PkgDepLine {
Expand Down Expand Up @@ -369,7 +366,7 @@ where

// Only includes source after the name for git sources for friendlier printing.
fn name_or_git_unique_string(pkg: &PkgLock) -> Cow<str> {
match pkg.source.starts_with(pkg::SourceGitPinned::PREFIX) {
match pkg.source.starts_with(source::git::Pinned::PREFIX) {
true => Cow::Owned(pkg.unique_string()),
false => Cow::Borrowed(&pkg.name),
}
Expand Down
24 changes: 24 additions & 0 deletions forc-pkg/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,3 +872,27 @@ impl std::ops::Deref for WorkspaceManifestFile {
&self.manifest
}
}

/// Attempt to find a `Forc.toml` with the given project name within the given directory.
///
/// Returns the path to the package on success, or `None` in the case it could not be found.
pub fn find_within(dir: &Path, pkg_name: &str) -> Option<PathBuf> {
walkdir::WalkDir::new(dir)
.into_iter()
.filter_map(Result::ok)
.filter(|entry| entry.path().ends_with(constants::MANIFEST_FILE_NAME))
.find_map(|entry| {
let path = entry.path();
let manifest = PackageManifest::from_file(path).ok()?;
if manifest.project.name == pkg_name {
Some(path.to_path_buf())
} else {
None
}
})
}

/// The same as [find_within], but returns the package's project directory.
pub fn find_dir_within(dir: &Path, pkg_name: &str) -> Option<PathBuf> {
find_within(dir, pkg_name).and_then(|path| path.parent().map(Path::to_path_buf))
}
Loading

0 comments on commit 9346bf4

Please sign in to comment.