Skip to content

Commit

Permalink
v0.1.14
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Oct 21, 2023
1 parent b85e724 commit 6105337
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
["package"]
name = "amble"
description = "First class, scalable rust project generator with batteries included."
version = "0.1.13"
version = "0.1.14"
edition = "2021"
license = "MIT"
authors = ["refcell"]
Expand Down Expand Up @@ -32,6 +32,7 @@ tokio = { version = "1.11.0", features = ["full"] }
serde_json = "1.0.107"
reqwest = { version = "0.11.22", features = ["blocking", "json"] }
chrono = "0.4.31"
gitignores = "2.3.2"

[dev-dependencies]
tempfile = "3.8.0"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ Options:
-l, --lib Builds a cargo library project
--full Full generates a full project structure including license, ci, gitignore, etc
--license Adds an MIT License to the project. The MIT License type can be overridden with the `--with-license` flag
--gitignore Adds a Gitignore file to the project
--with-license <WITH_LICENSE> License Override. This will override the default MIT License. The license type must be a valid SPDX license identifier
-h, --help Print help
-V, --version Print version
Expand Down
10 changes: 10 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ pub struct Args {
#[arg(long)]
license: bool,

/// Adds a Gitignore file to the project.
#[arg(long)]
gitignore: bool,

/// License Override.
/// This will override the default MIT License.
/// The license type must be a valid SPDX license identifier.
Expand Down Expand Up @@ -85,13 +89,15 @@ pub fn run() -> Result<()> {
lib,
mut license,
with_license,
mut gitignore,
full,
} = Args::parse();
let project_dir_path = std::path::Path::new(&project_dir);

if full {
with_ci = true;
license = true;
gitignore = true;
}

crate::telemetry::init_tracing_subscriber(v)?;
Expand Down Expand Up @@ -133,6 +139,10 @@ pub fn run() -> Result<()> {
crate::license::create(project_dir_path, license_type, dry_run, Some(&mut builder))?;
}

if gitignore {
crate::gitignore::create(project_dir_path, dry_run, Some(&mut builder))?;
}

if !bin && !lib {
crate::root::create(
project_dir_path,
Expand Down
44 changes: 44 additions & 0 deletions src/gitignore.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use eyre::Result;
use ptree::TreeBuilder;
use std::io::Write;
use std::path::Path;
use tracing::instrument;

/// Creates a new gitignore file in the given directory.
#[instrument(name = "gitignore", skip(dir, dry, tree))]
pub(crate) fn create(dir: &Path, dry: bool, tree: Option<&mut TreeBuilder>) -> Result<()> {
tracing::info!("Creating a .gitignore file");

// Create the directory if it doesn't exist.
if !dry {
tracing::debug!("Creating directory {:?}", dir);
std::fs::create_dir_all(dir)?;
}

if !dry {
tracing::debug!("Writing .gitignore to {:?}", dir.join(".gitignore"));
let mut file = std::fs::File::create(dir.join(".gitignore"))?;
let rust_gitignore = gitignores::Root::Rust.to_string();
file.write_all(rust_gitignore.as_bytes())?;
}

tree.map(|t| t.add_empty_child(".gitignore".to_string()));
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
use tempfile::tempdir;

#[test]
fn test_create_gitignore() {
let dir = tempdir().unwrap();
let dir_path_buf = dir.path().to_path_buf();
let package_dir = dir_path_buf.join("example");
create(&package_dir, false, None).unwrap();

assert!(package_dir.exists());
assert!(package_dir.join(".gitignore").exists());
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub mod cli;
pub(crate) mod bins;
pub(crate) mod cargo;
pub(crate) mod ci;
pub(crate) mod gitignore;
pub(crate) mod libs;
pub(crate) mod license;
pub(crate) mod root;
Expand Down

0 comments on commit 6105337

Please sign in to comment.