diff --git a/.gitignore b/.gitignore index 2d4d9f5..714e51e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -/cache +/data /result /result-* /target diff --git a/Cargo.lock b/Cargo.lock index 0ff795a..131eff8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1459,6 +1459,13 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "license-store-cache" +version = "0.1.1" +dependencies = [ + "askalono", +] + [[package]] name = "link-cplusplus" version = "1.0.8" diff --git a/Cargo.toml b/Cargo.toml index d30dadf..4b74f07 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,19 +1,32 @@ -[package] -name = "nix-init" +[workspace] +members = ["license-store-cache"] + +[workspace.package] version = "0.1.1" authors = ["figsoda "] edition = "2021" +license = "MPL-2.0" + +[workspace.dependencies] +askalono = "0.4.6" + +[package] +name = "nix-init" +version.workspace = true +authors.workspace = true +edition.workspace = true description = "Generate Nix packages from URLs with hash prefetching, dependency inference, license detection, and more" readme = "README.md" homepage = "https://github.com/nix-community/nix-init" repository = "https://github.com/nix-community/nix-init" -license = "MPL-2.0" +license.workspace = true keywords = ["cli", "interactive", "generate", "nix", "package"] categories = ["command-line-utilities"] +include = ["data", "src", "Cargo.lock", "Cargo.toml", "build.rs"] [dependencies] anyhow = "1.0.69" -askalono = "0.4.6" +askalono.workspace = true bstr = "1.2.0" cargo = "0.68.0" chumsky = "1.0.0-alpha.3" @@ -64,7 +77,6 @@ version = "1.26.0" features = ["macros", "process", "rt-multi-thread"] [build-dependencies] -askalono = "0.4.6" clap = { version = "4.1.8", features = ["derive"] } clap_complete = "4.1.4" clap_mangen = "0.2.9" diff --git a/build.rs b/build.rs index 1aacf37..723bc3d 100644 --- a/build.rs +++ b/build.rs @@ -1,4 +1,3 @@ -use askalono::Store; use clap::{CommandFactory, ValueEnum}; use clap_complete::{generate_to, Shell}; use clap_mangen::Man; @@ -12,25 +11,10 @@ use std::{ include!("src/cli.rs"); fn main() { - println!("cargo:rerun-if-changed=cache/askalono-cache.zstd"); + println!("cargo:rerun-if-changed=data/license-store-cache.zstd"); println!("cargo:rerun-if-env-changed=GEN_ARTIFACTS"); println!("cargo:rerun-if-env-changed=SPDX_LICENSE_LIST_DATA"); - // by default, the cache will not be rebuilt - // remove the file to rebuild the cache - let cache = Path::new("cache/askalono-cache.zstd"); - if !cache.is_file() { - create_dir_all("cache").unwrap(); - let mut store = Store::new(); - store - .load_spdx( - env::var_os("SPDX_LICENSE_LIST_DATA").unwrap().as_ref(), - false, - ) - .unwrap(); - store.to_cache(File::create(cache).unwrap()).unwrap(); - } - if let Some(dir) = env::var_os("GEN_ARTIFACTS") { let out = &Path::new(&dir); create_dir_all(out).unwrap(); diff --git a/flake.nix b/flake.nix index d4a2e0d..e6eed4c 100644 --- a/flake.nix +++ b/flake.nix @@ -57,16 +57,35 @@ sourceByRegex ; + src = sourceByRegex self [ + "(license-store-cache|src)(/.*)?" + "Cargo\\.(toml|lock)" + "build.rs" + "rustfmt.toml" + ]; + + license-store-cache = buildPackage { + pname = "license-store-cache"; + + inherit src; + + doCheck = false; + + cargoArtifacts = null; + cargoExtraArgs = "-p license-store-cache"; + + postInstall = '' + cache=$(mktemp) + $out/bin/license-store-cache $cache ${spdx-license-list-data.json}/json/details + rm -rf $out + mv $cache $out + ''; + }; + GET_NIX_LICENSE = callPackage ./src/get_nix_license.nix { }; - SPDX_LICENSE_LIST_DATA = "${spdx-license-list-data.json}/json/details"; args = { - src = sourceByRegex self [ - "src(/.*)?" - "Cargo\\.(toml|lock)" - "build.rs" - "rustfmt.toml" - ]; + inherit src; nativeBuildInputs = [ curl @@ -91,7 +110,12 @@ cargoArtifacts = buildDepsOnly args; cargoExtraArgs = "--no-default-features --features=reqwest/rustls-tls"; - inherit GET_NIX_LICENSE SPDX_LICENSE_LIST_DATA; + postPatch = '' + mkdir -p data + ln -s ${license-store-cache} data/license-store-cache.zstd + ''; + + inherit GET_NIX_LICENSE; GEN_ARTIFACTS = "artifacts"; ZSTD_SYS_USE_PKG_CONFIG = true; @@ -112,9 +136,14 @@ }; devShells.default = mkShell { - inherit GET_NIX_LICENSE SPDX_LICENSE_LIST_DATA; + inherit GET_NIX_LICENSE; NIX_INIT_LOG = "nix_init=trace"; RUST_BACKTRACE = true; + + shellHook = '' + mkdir -p data + ln -sf ${license-store-cache} data/license-store-cache.zstd + ''; }; formatter = nixpkgs-fmt; diff --git a/license-store-cache/Cargo.toml b/license-store-cache/Cargo.toml new file mode 100644 index 0000000..163e200 --- /dev/null +++ b/license-store-cache/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "license-store-cache" +version.workspace = true +authors.workspace = true +edition.workspace = true +license.workspace = true + +[dependencies] +askalono.workspace = true diff --git a/license-store-cache/src/main.rs b/license-store-cache/src/main.rs new file mode 100644 index 0000000..2f102ef --- /dev/null +++ b/license-store-cache/src/main.rs @@ -0,0 +1,14 @@ +use askalono::Store; + +use std::{env::args_os, fs::File}; + +fn main() { + let mut args = args_os(); + args.next().unwrap(); + let cache = File::create(args.next().unwrap()).unwrap(); + let mut store = Store::new(); + store + .load_spdx(args.next().unwrap().as_ref(), false) + .unwrap(); + store.to_cache(cache).unwrap(); +} diff --git a/src/license.rs b/src/license.rs index 198b04a..9d89b4b 100644 --- a/src/license.rs +++ b/src/license.rs @@ -6,7 +6,7 @@ use tracing::debug; use crate::utils::ResultExt; pub static LICENSE_STORE: Lazy> = Lazy::new(|| { - Store::from_cache(include_bytes!("../cache/askalono-cache.zstd") as &[_]).ok_warn() + Store::from_cache(include_bytes!("../data/license-store-cache.zstd") as &[_]).ok_warn() }); include!(env!("GET_NIX_LICENSE"));