-
Notifications
You must be signed in to change notification settings - Fork 289
add dependency feature #304
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
Merged
Merged
Changes from 26 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
5a9ec69
begin dependency feature
Luke-zhang-04 1c65108
fix get_deps() implementation --WIP
o2sh cd63dae
match on Option after parsing number of deps
o2sh 596f2cf
rename dependencies --> deps
o2sh 6581979
fix typo
o2sh 230088d
editorconfig did stuff
Luke-zhang-04 1c62703
feat: add dependency insights field
Luke-zhang-04 582b246
feat: add support for go modules
Luke-zhang-04 d79f447
feat: add support for pip
Luke-zhang-04 bd98e3a
rust fmt
Luke-zhang-04 774d2e5
Merge branch 'master' of https://github.com/o2sh/onefetch
Luke-zhang-04 23042b4
rust fmt
Luke-zhang-04 deefcb5
feat: add support for yarn
Luke-zhang-04 f5ea1f7
feat: add support for Cargo
Luke-zhang-04 b966cc5
cargo fmt
Luke-zhang-04 369506c
split deps into multiple files and replace Option with Result for bet…
o2sh 5bb1e05
docs: adding a new package manager
Luke-zhang-04 afc7ef0
change i32 in `package_parsers` to uint
Luke-zhang-04 9311d49
fix: detect `yarn.lock` with absolute directory
Luke-zhang-04 a19ced8
refactor: simplify regex in package parsers
Luke-zhang-04 d60ecfe
catch dependencies instead of `.unwrap()`
Luke-zhang-04 6fdb61d
refactor: move `is_package_file` to Detector impl`
Luke-zhang-04 0b8caa0
refactor: use `map.contains_key()` instead of iterating
Luke-zhang-04 3d94170
add a comment
Luke-zhang-04 8824206
make contributing clearer
Luke-zhang-04 978c9c8
Update CONTRIBUTING.md
o2sh 1d6d95b
fix: handle Cargo.toml without dependency field
Luke-zhang-04 483faa6
Merge branch 'master' of https://github.com/Luke-zhang-04/onefetch
Luke-zhang-04 aa62542
Merge branch 'master' of https://github.com/o2sh/onefetch
Luke-zhang-04 2677401
update CONTRIBUTING.md
Luke-zhang-04 ba97550
fix: check for `=>` in go.mod
Luke-zhang-04 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use { | ||
crate::onefetch::error::*, | ||
std::collections::HashMap, | ||
std::{ffi::OsStr, fs}, | ||
}; | ||
|
||
mod package_manager; | ||
mod package_parser; | ||
|
||
type DependencyParser = fn(&str) -> Result<usize>; | ||
|
||
pub struct DependencyDetector { | ||
package_managers: HashMap<String, (DependencyParser, package_manager::PackageManager)>, | ||
} | ||
|
||
impl DependencyDetector { | ||
pub fn new() -> Self { | ||
let mut package_managers: HashMap< | ||
String, | ||
(DependencyParser, package_manager::PackageManager), | ||
> = HashMap::new(); | ||
|
||
package_managers.insert( | ||
String::from("Cargo.toml"), | ||
(package_parser::cargo, package_manager::PackageManager::Cargo), | ||
); | ||
package_managers.insert( | ||
String::from("go.mod"), | ||
(package_parser::go_modules, package_manager::PackageManager::GoModules), | ||
); | ||
package_managers.insert( | ||
String::from("package.json"), | ||
(package_parser::npm, package_manager::PackageManager::Npm), | ||
); | ||
package_managers.insert( | ||
String::from("requirements.txt"), | ||
(package_parser::pip, package_manager::PackageManager::Pip), | ||
); | ||
|
||
DependencyDetector { package_managers } | ||
} | ||
|
||
pub fn get_deps_info(&self, dir: &str) -> Result<String> { | ||
let deps = fs::read_dir(dir) | ||
.chain_err(|| "Could not read directory")? | ||
.filter_map(std::result::Result::ok) | ||
.map(|entry| entry.path()) | ||
.filter(|entry| { | ||
entry.is_file() | ||
&& entry | ||
.file_name() | ||
.map(OsStr::to_string_lossy) | ||
.map(|s| self.package_managers.contains_key(s.as_ref())) | ||
.unwrap_or_default() | ||
}) | ||
.map(|entry| { | ||
let (parser, found_package_manager) = | ||
&self.package_managers[entry.file_name().unwrap().to_str().unwrap()]; | ||
let contents = fs::read_to_string(entry)?; | ||
let number_of_deps = parser(&contents)?; | ||
let used_package_manager; | ||
|
||
// If a yarn.lock file is found and the current package manager | ||
// is NPM, change the package manager to Yarn instead | ||
if found_package_manager == &package_manager::PackageManager::Npm | ||
&& std::path::Path::new(&format!("{}yarn.lock", dir)).exists() | ||
{ | ||
used_package_manager = &package_manager::PackageManager::Yarn; | ||
} else { | ||
used_package_manager = found_package_manager; | ||
} | ||
|
||
Ok(format!("{} ({})", number_of_deps, used_package_manager)) | ||
}) | ||
.filter_map(Result::ok) | ||
.collect::<Vec<_>>(); | ||
|
||
let output = deps.join(", "); | ||
|
||
Ok(output) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#[derive(PartialEq)] | ||
pub enum PackageManager { | ||
Cargo, | ||
GoModules, | ||
Npm, | ||
Pip, | ||
Yarn, | ||
} | ||
|
||
impl std::fmt::Display for PackageManager { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
match *self { | ||
PackageManager::Cargo => write!(f, "Cargo"), | ||
PackageManager::GoModules => write!(f, "Go Modules"), | ||
PackageManager::Npm => write!(f, "Npm"), | ||
PackageManager::Pip => write!(f, "Pip"), | ||
PackageManager::Yarn => write!(f, "Yarn"), | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use crate::onefetch::error::*; | ||
use {regex::Regex, toml::Value}; | ||
|
||
pub fn cargo(contents: &str) -> Result<usize> { | ||
let parsed = contents.parse::<Value>()?; | ||
|
||
Ok(parsed["dependencies"].as_table().unwrap().len()) | ||
} | ||
|
||
pub fn go_modules(contents: &str) -> Result<usize> { | ||
let count = Regex::new(r"v[0-9]+")?.find_iter(contents).count(); | ||
|
||
Ok(count) | ||
} | ||
|
||
pub fn npm(contents: &str) -> Result<usize> { | ||
let parsed = json::parse(contents)?; | ||
|
||
Ok(parsed["dependencies"].len()) | ||
Luke-zhang-04 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
pub fn pip(contents: &str) -> Result<usize> { | ||
let count = Regex::new(r"(^|\n)[A-z]+")?.find_iter(contents).count(); | ||
|
||
Ok(count) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.