-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #350 from o2sh/feature/macro_package_managers
Refactoring of package manager module
- Loading branch information
Showing
4 changed files
with
131 additions
and
131 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,22 +1,122 @@ | ||
use strum::EnumIter; | ||
|
||
#[derive(PartialEq, EnumIter)] | ||
pub enum PackageManager { | ||
Cargo, | ||
GoModules, | ||
Npm, | ||
Pip, | ||
Pub, | ||
} | ||
|
||
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::Pub => write!(f, "pub"), | ||
use crate::onefetch::error::*; | ||
use std::collections::HashMap; | ||
use {regex::Regex, strum::EnumIter, toml::Value, yaml_rust::YamlLoader}; | ||
|
||
macro_rules! define_package_managers { | ||
($( { $name:ident, $display:literal, [$(($file:literal, $parser:expr))+] } ),+ ,) => { | ||
|
||
#[derive(PartialEq, EnumIter)] | ||
pub enum PackageManager { | ||
$( | ||
$name , | ||
)+ | ||
} | ||
|
||
impl std::fmt::Display for PackageManager { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
match *self { | ||
$( PackageManager::$name => write!(f, $display), )+ | ||
} | ||
} | ||
} | ||
|
||
pub fn build() -> HashMap<String, (fn(&str) -> Result<usize>, PackageManager)> { | ||
let mut package_managers: HashMap< | ||
String, | ||
(fn(&str) -> Result<usize>, PackageManager) | ||
> = HashMap::new(); | ||
|
||
$( | ||
$( | ||
package_managers.insert(String::from($file), ($parser, PackageManager::$name)); | ||
)+ | ||
)+ | ||
|
||
package_managers | ||
} | ||
|
||
} | ||
} | ||
|
||
define_package_managers! { | ||
{ Cargo, "cargo", [ ("Cargo.toml", cargo) ] }, | ||
{ GoModules, "go modules", [ ("go.mod", go_modules) ] }, | ||
{ Npm, "npm", [ ("package.json", npm) ] }, | ||
{ Pip, "pip", [ ("requirements.txt", pip_requirement) ("pyproject.toml", pip_pyproject) ("Pipfile", pip_pipfile) ] }, | ||
{ Pub, "pub", [ ("pubspec.yaml", pub_packages) ] }, | ||
} | ||
|
||
fn cargo(contents: &str) -> Result<usize> { | ||
let parsed = contents.parse::<Value>()?; | ||
let count = parsed.get("dependencies"); | ||
|
||
match count { | ||
Some(val) => Ok(val.as_table().unwrap().len()), | ||
None => Ok(0), | ||
} | ||
} | ||
|
||
fn go_modules(contents: &str) -> Result<usize> { | ||
let mut count = 0; | ||
let mut start = false; | ||
for line in contents.lines() { | ||
if line.contains("require") { | ||
start = true; | ||
continue; | ||
} | ||
|
||
if start && line.contains(')') { | ||
break; | ||
} | ||
|
||
if start { | ||
count += 1; | ||
} | ||
} | ||
|
||
Ok(count) | ||
} | ||
|
||
fn npm(contents: &str) -> Result<usize> { | ||
let parsed = json::parse(contents)?; | ||
|
||
Ok(parsed["dependencies"].len()) | ||
} | ||
|
||
fn pip_requirement(contents: &str) -> Result<usize> { | ||
let count = Regex::new(r"(^|\n)[A-z]+")?.find_iter(contents).count(); | ||
|
||
Ok(count) | ||
} | ||
|
||
fn pip_pyproject(contents: &str) -> Result<usize> { | ||
let parsed = contents.parse::<Value>()?; | ||
let count = parsed | ||
.get("tool") | ||
.and_then(|tool| tool.get("poetry")) | ||
.and_then(|poetry| poetry.get("dependencies")); | ||
match count { | ||
Some(val) => Ok(val.as_table().unwrap().len()), | ||
None => Ok(0), | ||
} | ||
} | ||
|
||
fn pip_pipfile(contents: &str) -> Result<usize> { | ||
let parsed = contents.parse::<Value>()?; | ||
let count = parsed.get("packages"); | ||
|
||
match count { | ||
Some(val) => Ok(val.as_table().unwrap().len()), | ||
None => Ok(0), | ||
} | ||
} | ||
|
||
fn pub_packages(contents: &str) -> Result<usize> { | ||
match YamlLoader::load_from_str(contents) { | ||
Ok(parsed) => match &parsed[0]["dependencies"].as_hash() { | ||
Some(deps) => Ok(deps.len()), | ||
None => Ok(0), | ||
}, | ||
Err(_) => Ok(0), | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.