initial commit

This commit is contained in:
Dmitriy Pleshevskiy 2024-03-16 23:32:58 +03:00
commit 5b75b9d516
Signed by: pleshevskiy
GPG Key ID: 17041163DA10A9A2
16 changed files with 875 additions and 0 deletions

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
# direnv
.direnv
.envrc
# rust
/target
# custom
/test-dir

325
Cargo.lock generated Normal file
View File

@ -0,0 +1,325 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "anstream"
version = "0.6.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d96bd03f33fe50a863e394ee9718a706f988b9079b20c3784fb726e7678b62fb"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc"
[[package]]
name = "anstyle-parse"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7"
dependencies = [
"anstyle",
"windows-sys",
]
[[package]]
name = "clap"
version = "4.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b230ab84b0ffdf890d5a10abdbc8b83ae1c4918275daea1ab8801f71536b2651"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "307bc0538d5f0f83b8248db3087aa92fe504e4691294d0c96c0eabc33f47ba47"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce"
[[package]]
name = "colorchoice"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "hashbrown"
version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604"
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "indexmap"
version = "2.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b0b929d511467233429c45a44ac1dcaa21ba0f5ba11e4879e6ed28ddb4f9df4"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]]
name = "memchr"
version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
[[package]]
name = "proc-macro2"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e"
dependencies = [
"unicode-ident",
]
[[package]]
name = "project-cook"
version = "0.1.0"
dependencies = [
"clap",
"serde",
"toml",
]
[[package]]
name = "quote"
version = "1.0.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef"
dependencies = [
"proc-macro2",
]
[[package]]
name = "serde"
version = "1.0.197"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.197"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_spanned"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1"
dependencies = [
"serde",
]
[[package]]
name = "strsim"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ee073c9e4cd00e28217186dbe12796d692868f432bf2e97ee73bed0c56dfa01"
[[package]]
name = "syn"
version = "2.0.52"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b699d15b36d1f02c3e7c69f8ffef53de37aefae075d8488d4ba1a7788d574a07"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "toml"
version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af06656561d28735e9c1cd63dfd57132c8155426aa6af24f36a00a351f88c48e"
dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"toml_edit",
]
[[package]]
name = "toml_datetime"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1"
dependencies = [
"serde",
]
[[package]]
name = "toml_edit"
version = "0.22.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18769cd1cec395d70860ceb4d932812a0b4d06b1a4bb336745a4d21b9496e992"
dependencies = [
"indexmap",
"serde",
"serde_spanned",
"toml_datetime",
"winnow",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675"
[[package]]
name = "windows_i686_gnu"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3"
[[package]]
name = "windows_i686_msvc"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8"
[[package]]
name = "winnow"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dffa400e67ed5a4dd237983829e66475f0a4a26938c4b04c21baede6262215b8"
dependencies = [
"memchr",
]

11
Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "project-cook"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = { version = "4.5.2", features = ["derive"] }
serde = { version = "1.0.197", features = ["derive"] }
toml = "0.8.11"

6
cook.toml Normal file
View File

@ -0,0 +1,6 @@
[gitignore]
direnv = true
rust = true
custom = """
/test-dir
"""

130
flake.lock Normal file
View File

@ -0,0 +1,130 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1710146030,
"narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"flake-utils_2": {
"inputs": {
"systems": "systems_2"
},
"locked": {
"lastModified": 1705309234,
"narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1710377395,
"narHash": "sha256-KMubsUWtVr7L55pXMBibBDBdmk3xrjbBPduc0E8z28c=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "db001797591bf76f7b8d4c4ed3b49233391e0c97",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1706487304,
"narHash": "sha256-LE8lVX28MV2jWJsidW13D2qrHU/RUUONendL2Q/WlJg=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "90f456026d284c22b3e3497be980b2e47d0b28ac",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay"
}
},
"rust-overlay": {
"inputs": {
"flake-utils": "flake-utils_2",
"nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 1710382258,
"narHash": "sha256-2FW1q+o34VBweYQiEkRaSEkNMq3ecrn83VzETeGiVbY=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "8ce81e71ab04a7e906fae62da086d6ee5d6cfc21",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
},
"systems_2": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

31
flake.nix Normal file
View File

@ -0,0 +1,31 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";
rust-overlay.url = "github:oxalica/rust-overlay";
};
outputs =
{ self
, nixpkgs
, flake-utils
, rust-overlay
}:
flake-utils.lib.eachDefaultSystem (system:
let
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs { inherit system overlays; };
in
{
devShell = pkgs.mkShell {
nativeBuildInputs = [ ];
buildInputs = [
pkgs.rust-bin.stable.latest.default
];
packages = with pkgs; [
rust-analyzer
];
RUST_SRC_PATH = pkgs.rustPlatform.rustLibSrc;
};
});
}

107
src/cli.rs Normal file
View File

@ -0,0 +1,107 @@
use std::{collections::HashMap, env::current_dir, fs, path::PathBuf};
use clap::Parser;
use crate::{
config::read_config,
module::{direnv::DirenvModule, gitignore::GitIgnoreModule, Module},
};
use self::{direnv::DirenvModuleCliArgs, gitignore::GitIgnoreModuleCliArgs};
mod direnv;
mod gitignore;
#[derive(Debug, Clone, clap::Args)]
struct CookCommand {
modules: Option<Vec<String>>,
}
#[derive(Debug, Clone, clap::Subcommand)]
enum AddModuleCommand {
Direnv(DirenvModuleCliArgs),
#[clap(name = "gitignore")]
GitIgnore(GitIgnoreModuleCliArgs),
#[clap(external_subcommand)]
External(Vec<String>),
}
#[derive(Debug, Clone, clap::Args)]
struct AddCommand {
#[clap(trailing_var_arg = true, allow_hyphen_values = true)]
module_args: Option<Vec<String>>,
}
#[derive(Debug, Clone, clap::Subcommand)]
#[clap(propagate_version = true)]
enum Command {
Cook(CookCommand),
Add {
#[clap(subcommand)]
command: AddModuleCommand,
},
}
#[derive(Debug, clap::Parser)]
#[clap(author, version)]
struct Args {
#[clap(short, long = "path")]
project_path: Option<PathBuf>,
#[clap(subcommand)]
command: Command,
}
pub fn cook(
current_dir: PathBuf,
cook_files: HashMap<String, String>,
) -> Result<(), Box<dyn std::error::Error>> {
for (file_name, file_content) in cook_files {
let file_path = &current_dir.join(file_name);
if file_content.trim().is_empty() {
fs::remove_file(file_path)?;
} else {
fs::write(file_path, file_content)?;
}
}
Ok(())
}
pub fn run() -> Result<(), Box<dyn std::error::Error>> {
let args = Args::parse();
let current_dir = if let Some(project_path) = args.project_path {
project_path.canonicalize()?
} else {
current_dir()?
};
let config = read_config(&current_dir)?;
match args.command {
Command::Cook(_cook_args) => {
let mut cook_files: HashMap<String, String> = HashMap::new();
if let Some(cfg) = config.direnv {
cook_files.extend(DirenvModule.cook(cfg.into()))
}
if let Some(cfg) = config.gitignore {
cook_files.extend(GitIgnoreModule.cook(cfg.into()));
}
cook(current_dir, cook_files)
}
Command::Add { command } => {
let mut cook_files: HashMap<String, String> = HashMap::new();
match command {
AddModuleCommand::Direnv(args) => cook_files.extend(DirenvModule.cook(args.into())),
AddModuleCommand::GitIgnore(args) => {
cook_files.extend(GitIgnoreModule.cook(args.into()))
}
AddModuleCommand::External(_) => {
unimplemented!("external commands")
}
};
cook(current_dir, cook_files)
}
}
}

23
src/cli/direnv.rs Normal file
View File

@ -0,0 +1,23 @@
use crate::module::direnv::DirenvModuleArgs;
#[derive(Debug, Clone, clap::ValueEnum)]
pub enum DirenvCliNixValue {
Flake,
}
#[derive(Debug, Clone, clap::Args)]
pub struct DirenvModuleCliArgs {
#[clap(long)]
pub nix: Option<DirenvCliNixValue>,
#[clap(long)]
pub nodejs: bool,
}
impl From<DirenvModuleCliArgs> for DirenvModuleArgs {
fn from(args: DirenvModuleCliArgs) -> Self {
DirenvModuleArgs {
nix_flake: matches!(args.nix, Some(DirenvCliNixValue::Flake)),
nodejs: args.nodejs,
}
}
}

27
src/cli/gitignore.rs Normal file
View File

@ -0,0 +1,27 @@
use crate::module::gitignore::GitIgnoreModuleArgs;
#[derive(Debug, Clone, clap::Args)]
pub struct GitIgnoreModuleCliArgs {
#[clap(long)]
pub env: bool,
#[clap(long)]
pub direnv: bool,
#[clap(long)]
pub rust: bool,
#[clap(long)]
pub nodejs: bool,
#[clap(long)]
pub custom: Option<String>,
}
impl From<GitIgnoreModuleCliArgs> for GitIgnoreModuleArgs {
fn from(args: GitIgnoreModuleCliArgs) -> Self {
GitIgnoreModuleArgs {
env: args.env,
direnv: args.direnv,
rust: args.rust,
nodejs: args.nodejs,
custom: args.custom,
}
}
}

54
src/config.rs Normal file
View File

@ -0,0 +1,54 @@
use std::{fs, io, path::PathBuf};
use self::{direnv::DirenvModuleConfig, gitignore::GitIgnoreModuleConfig};
mod direnv;
mod gitignore;
#[derive(serde::Deserialize, serde::Serialize)]
pub struct Config {
pub direnv: Option<DirenvModuleConfig>,
pub gitignore: Option<GitIgnoreModuleConfig>,
}
#[derive(Debug)]
pub enum Error {
ReadFile,
Deserialize,
Unknown,
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::ReadFile => f.write_str("Cannot read file"),
Self::Deserialize => f.write_str("Cannot deserialize configuration"),
Self::Unknown => f.write_str("Unknown error"),
}
}
}
impl std::error::Error for Error {}
impl From<io::Error> for Error {
fn from(value: io::Error) -> Self {
match value.kind() {
io::ErrorKind::NotFound | io::ErrorKind::InvalidData => Self::ReadFile,
_ => Self::Unknown,
}
}
}
impl From<toml::de::Error> for Error {
fn from(_value: toml::de::Error) -> Self {
Self::Deserialize
}
}
pub fn read_config(workdir: &PathBuf) -> Result<Config, Error> {
let content = fs::read_to_string(workdir.join("cook.toml"))?;
let res = toml::from_str(&content).inspect_err(|e| {
dbg!(e);
})?;
Ok(res)
}

19
src/config/direnv.rs Normal file
View File

@ -0,0 +1,19 @@
use crate::module::direnv::DirenvModuleArgs;
#[derive(Default, serde::Deserialize, serde::Serialize)]
pub struct DirenvModuleConfig {
pub nix: Option<String>,
pub nodejs: Option<bool>,
}
impl From<DirenvModuleConfig> for DirenvModuleArgs {
fn from(cfg: DirenvModuleConfig) -> Self {
DirenvModuleArgs {
nix_flake: cfg
.nix
.map(|v| v.to_lowercase() == "flake")
.unwrap_or_default(),
nodejs: cfg.nodejs.unwrap_or_default(),
}
}
}

22
src/config/gitignore.rs Normal file
View File

@ -0,0 +1,22 @@
use crate::module::gitignore::GitIgnoreModuleArgs;
#[derive(Default, serde::Deserialize, serde::Serialize)]
pub struct GitIgnoreModuleConfig {
pub env: Option<bool>,
pub direnv: Option<bool>,
pub rust: Option<bool>,
pub nodejs: Option<bool>,
pub custom: Option<String>,
}
impl From<GitIgnoreModuleConfig> for GitIgnoreModuleArgs {
fn from(cfg: GitIgnoreModuleConfig) -> Self {
GitIgnoreModuleArgs {
env: cfg.env.unwrap_or_default(),
direnv: cfg.direnv.unwrap_or_default(),
rust: cfg.rust.unwrap_or_default(),
nodejs: cfg.nodejs.unwrap_or_default(),
custom: cfg.custom,
}
}
}

9
src/main.rs Normal file
View File

@ -0,0 +1,9 @@
#![deny(clippy::all, clippy::pedantic)]
mod cli;
mod config;
mod module;
fn main() -> Result<(), Box<dyn std::error::Error>> {
cli::run()
}

10
src/module.rs Normal file
View File

@ -0,0 +1,10 @@
use std::collections::HashMap;
pub mod direnv;
pub mod gitignore;
pub trait Module {
type ModuleArgs;
fn cook(&self, args: Self::ModuleArgs) -> HashMap<String, String>;
}

39
src/module/direnv.rs Normal file
View File

@ -0,0 +1,39 @@
use std::collections::HashMap;
use super::Module;
const NIX_FLAKE_PART: &str = "\
# nix
use flake";
const NODEJS_PART: &str = "\
# nodejs
layout node";
pub struct DirenvModuleArgs {
pub nix_flake: bool,
pub nodejs: bool,
}
fn make_gitignore_content(args: DirenvModuleArgs) -> String {
[
args.nix_flake.then_some(NIX_FLAKE_PART),
args.nodejs.then_some(NODEJS_PART),
]
.iter()
.flatten()
.cloned()
.collect::<Vec<_>>()
.join("\n")
.to_string()
}
pub struct DirenvModule;
impl Module for DirenvModule {
type ModuleArgs = DirenvModuleArgs;
fn cook(&self, args: Self::ModuleArgs) -> HashMap<String, String> {
HashMap::from([(String::from(".envrc"), make_gitignore_content(args))])
}
}

55
src/module/gitignore.rs Normal file
View File

@ -0,0 +1,55 @@
use std::collections::HashMap;
use super::Module;
const ENV_PART: &str = "\
# env
.env*
!env.example";
const DIRENV_PART: &str = "\
# direnv
.direnv
.envrc";
const RUST_PART: &str = "\
# rust
/target";
const NODEJS_PART: &str = "\
# nodejs
node_modules/";
pub struct GitIgnoreModuleArgs {
pub env: bool,
pub direnv: bool,
pub rust: bool,
pub nodejs: bool,
pub custom: Option<String>,
}
fn make_gitignore_content(args: GitIgnoreModuleArgs) -> String {
[
args.env.then_some(ENV_PART),
args.direnv.then_some(DIRENV_PART),
args.rust.then_some(RUST_PART),
args.nodejs.then_some(NODEJS_PART),
args.custom.map(|c| format!("# custom\n{}", c)).as_deref(),
]
.iter()
.flatten()
.cloned()
.collect::<Vec<_>>()
.join("\n")
.to_string()
}
pub struct GitIgnoreModule;
impl Module for GitIgnoreModule {
type ModuleArgs = GitIgnoreModuleArgs;
fn cook(&self, args: Self::ModuleArgs) -> HashMap<String, String> {
HashMap::from([(String::from(".gitignore"), make_gitignore_content(args))])
}
}