-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ef80615
Showing
7 changed files
with
232 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
target | ||
Cargo.lock |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
language: rust | ||
addons: | ||
apt: | ||
sources: | ||
- kalakris-cmake | ||
packages: | ||
- cmake |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "cargo-license" | ||
description = "Cargo subcommand to see license of dependencies" | ||
authors = ["Onur Aslan <[email protected]>"] | ||
version = "0.1.0" | ||
license = "MIT" | ||
readme = "README.md" | ||
repository = "https://github.com/onur/cargo-license" | ||
|
||
[dependencies] | ||
cargo = "^0.11.0" | ||
error-chain = "^0.2.1" | ||
toml = "^0.1" | ||
ansi_term = "^0.7.5" | ||
|
||
[[bin]] | ||
name = "cargo-license" | ||
path = "src/cargo-license.rs" |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Onur Aslan <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# cargo-license | ||
|
||
[](https://travis-ci.org/onur/cargo-license) | ||
[](https://raw.githubusercontent.com/onur/cargo-license/master/LICENSE) | ||
|
||
A cargo subcommand to see license of dependencies. | ||
|
||
|
||
## Installation and Usage | ||
|
||
You can install cargo-license with cargo: `cargo install cargo-license` and you | ||
can run it in your project directory with: `cargo license`. | ||
|
||
|
||
## Example | ||
|
||
 |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
extern crate cargo_license; | ||
extern crate ansi_term; | ||
|
||
use std::collections::BTreeMap; | ||
use ansi_term::Colour::Green; | ||
|
||
fn main() { | ||
let dependencies = cargo_license::get_dependencies_from_cargo_lock().unwrap(); | ||
|
||
let mut table: BTreeMap<String, Vec<String>> = BTreeMap::new(); | ||
|
||
for dependency in dependencies { | ||
let license = dependency.get_license(); | ||
if !table.contains_key(&license) { | ||
table.insert(license, vec![dependency.name]); | ||
} else { | ||
table.get_mut(&license).map(|v| v.push(dependency.name.clone())); | ||
} | ||
} | ||
|
||
for (license, crates) in table { | ||
println!("{} ({}): {}", Green.bold().paint(license), crates.len(), crates.join(", ")); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,142 @@ | ||
|
||
|
||
extern crate cargo; | ||
extern crate toml; | ||
#[macro_use] | ||
extern crate error_chain; | ||
|
||
|
||
use std::io; | ||
|
||
|
||
// I thought this crate is a good example to learn error_chain | ||
// but looks like no need of it in this crate | ||
error_chain! { | ||
types { | ||
Error, ErrorKind, ChainErr, Result; | ||
} | ||
|
||
links {} | ||
|
||
foreign_links { | ||
io::Error, Io, "IO Error"; | ||
} | ||
|
||
errors {} | ||
} | ||
|
||
|
||
pub struct Dependency { | ||
pub name: String, | ||
pub version: String, | ||
pub source: String, | ||
} | ||
|
||
|
||
impl Dependency { | ||
fn get_cargo_package(&self) -> cargo::util::CargoResult<cargo::core::Package> { | ||
use cargo::core::{Source, SourceId, Registry}; | ||
use cargo::core::Dependency as CargoDependency; | ||
use cargo::util::{Config, human}; | ||
use cargo::sources::RegistrySource; | ||
|
||
// TODO: crates-license is only working for crates.io registry | ||
if !self.source.starts_with("registry") { | ||
unimplemented!(); | ||
} | ||
|
||
let config = try!(Config::default()); | ||
let source_id = SourceId::from_url(&self.source); | ||
let mut source = RegistrySource::new(&source_id, &config); | ||
|
||
// update crates.io-index registry | ||
try!(source.update()); | ||
|
||
let dep = try!(CargoDependency::parse(&self.name, Some(&self.version), &source_id)); | ||
let deps = try!(source.query(&dep)); | ||
deps.iter() | ||
.map(|p| p.package_id()) | ||
.max() | ||
.map(|pkgid| source.download(pkgid)) | ||
.unwrap_or(Err(human("PKG download error"))) | ||
} | ||
|
||
pub fn get_license(&self) -> String { | ||
// FIXME: So many N/A's | ||
if !self.source.starts_with("registry") { | ||
"N/A".to_owned() | ||
} else { | ||
match self.get_cargo_package() { | ||
Ok(pkg) => pkg.manifest().metadata().license.clone().unwrap_or("N/A".to_owned()), | ||
Err(_) => "N/A".to_owned(), | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
pub fn get_dependencies_from_cargo_lock() -> Result<Vec<Dependency>> { | ||
let toml = { | ||
use std::fs::File; | ||
use std::io::Read; | ||
|
||
let lock_file = try!(File::open("Cargo.lock")); | ||
let mut reader = io::BufReader::new(lock_file); | ||
let mut content = String::new(); | ||
try!(reader.read_to_string(&mut content)); | ||
content | ||
}; | ||
|
||
// This code once was beautiful, but it became ugly after rustfmt | ||
let dependencies: Vec<Dependency> = try!(toml::Parser::new(&toml) | ||
.parse() | ||
.as_ref() | ||
.and_then(|p| p.get("package")) | ||
.and_then(|p| p.as_slice()) | ||
.ok_or("Package not found") | ||
.map(|p| { | ||
p.iter() | ||
.map(|p| { | ||
Dependency { | ||
name: p.as_table() | ||
.and_then(|n| n.get("name")) | ||
.and_then(|n| n.as_str()) | ||
.unwrap() | ||
.to_owned(), | ||
version: p.as_table() | ||
.and_then(|n| { | ||
n.get("version") | ||
}) | ||
.and_then(|n| n.as_str()) | ||
.unwrap() | ||
.to_owned(), | ||
source: p.as_table() | ||
.and_then(|n| { | ||
n.get("source") | ||
}) | ||
.and_then(|n| n.as_str()) | ||
.unwrap() | ||
.to_owned(), | ||
} | ||
}) | ||
.collect() | ||
})); | ||
|
||
Ok(dependencies) | ||
} | ||
|
||
|
||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::get_dependencies_from_cargo_lock; | ||
|
||
#[test] | ||
fn test() { | ||
|
||
for dependency in get_dependencies_from_cargo_lock().unwrap() { | ||
assert!(!dependency.get_license().is_empty()); | ||
} | ||
} | ||
} |