Skip to content

Commit

Permalink
Merge pull request #149 from ebroto/feature/license-with-askalono
Browse files Browse the repository at this point in the history
Replace license crate with askalono
  • Loading branch information
o2sh authored Nov 5, 2019
2 parents ce4756e + 018c9c2 commit a2f6352
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@

# nix result from nix-build
result

# generated license cache
resources/licenses/cache.bin.gz
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "resources/licenses/spdx"]
path = resources/licenses/spdx
url = https://github.com/spdx/license-list-data
112 changes: 105 additions & 7 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ exclude = ["assets/*.png"]
colored= "1.8.0"
git2 = {version = "0.7.5", default-features = false}
tokei = "10.0"
license = "0.8.1"
askalono = "0.3.0"
bytecount = "0.5.1"
clap = "2.33.0"
strum = "0.16.0"
strum_macros = "0.16.0"
image = "0.22.3"

[build-dependencies]
askalono = "0.3.0"

[target.'cfg(windows)'.dependencies]
ansi_term = "0.12"

Expand Down
23 changes: 23 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
extern crate askalono;

use std::fs::File;
use std::path::Path;

use askalono::Store;

const EMBEDDED_CACHE: &str = "resources/licenses/cache.bin.gz";

fn main() {
if Path::new(EMBEDDED_CACHE).exists() {
println!("cargo:warning=onefetch license cache file already exists; not re-building");
return;
}

let mut store = Store::new();
store
.load_spdx(Path::new("resources/licenses/spdx/json/details"), false)
.expect("Couldn't create a store from SPDX data. Have submodules been initialized?");

let mut cache = File::create(EMBEDDED_CACHE).unwrap();
store.to_cache(&mut cache).unwrap();
}
1 change: 1 addition & 0 deletions resources/licenses/spdx
Submodule spdx added at 5f2f40
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub enum Error {
ReferenceInfoError,
/// Image probably doesn't exist or has wrong format
ImageLoadError,
/// Could not initialize the license detector
LicenseDetectorError,
}

impl std::fmt::Debug for Error {
Expand All @@ -29,6 +31,7 @@ impl std::fmt::Debug for Error {
Error::BareGitRepo => "Unable to run onefetch on bare git repos",
Error::ReferenceInfoError => "Error while retrieving reference information",
Error::ImageLoadError => "Could not load the specified image",
Error::LicenseDetectorError => "Could not initialize the license detector",
};
write!(f, "{}", content)
}
Expand Down
8 changes: 5 additions & 3 deletions src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::process::Command;
use colored::{Color, ColoredString, Colorize};
use git2::Repository;
use image::DynamicImage;
use license;
use license::Detector;

use crate::image_backends;
use crate::language::Language;
Expand Down Expand Up @@ -581,6 +581,8 @@ impl Info {
}

fn get_project_license(dir: &str) -> Result<String> {
let detector = Detector::new()?;

let output = fs::read_dir(dir)
.map_err(|_| Error::ReadDirectory)?
.filter_map(std::result::Result::ok)
Expand All @@ -596,9 +598,9 @@ impl Info {
}, // TODO: multiple prefixes, like COPYING?
)
.filter_map(|entry| {
license::from_text_ext(&fs::read_to_string(entry).unwrap_or_else(|_| "".into()))
let contents = fs::read_to_string(entry).unwrap_or_default();
detector.analyze(&contents)
})
.map(|license| license.name().to_string())
.collect::<Vec<_>>()
.join(", ");

Expand Down
26 changes: 26 additions & 0 deletions src/license.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use askalono::{Store, TextData};

use crate::Error;

type Result<T> = std::result::Result<T, Error>;

static CACHE_DATA: &[u8] = include_bytes!("../resources/licenses/cache.bin.gz");

pub struct Detector {
store: Store,
}

impl Detector {
pub fn new() -> Result<Self> {
Store::from_cache(CACHE_DATA)
.map(|store| Self { store })
.map_err(|_| Error::LicenseDetectorError)
}

pub fn analyze(&self, text: &str) -> Option<String> {
self.store
.analyze(&TextData::from(text))
.ok()
.map(|license| license.name)
}
}
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
extern crate bytecount;

extern crate askalono;
extern crate colored;
extern crate git2;
extern crate license;
extern crate tokei;
#[macro_use]
extern crate clap;
Expand Down Expand Up @@ -33,6 +33,7 @@ mod error;
mod image_backends;
mod info;
mod language;
mod license;

use ascii_art::AsciiArt;
use commit_info::CommitInfo;
Expand Down

0 comments on commit a2f6352

Please sign in to comment.