From 5acec55a4ce4e0f6403dc67d82b1719894da450c Mon Sep 17 00:00:00 2001 From: Jack Bennett Date: Thu, 9 May 2024 11:55:44 +0100 Subject: [PATCH] Add version.sh script to make version number from Git info --- .cargo/config.toml | 5 +++++ .editorconfig | 3 +++ Cargo.toml | 1 + Makefile | 5 ++--- src/cli.rs | 15 +++++++++------ util/version.sh | 41 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 .cargo/config.toml create mode 100755 util/version.sh diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..de0b841 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,5 @@ +# putting default env var value here so that `cargo check` doesn't underline calls to `std::env!` as errors: +# https://github.com/rust-lang/rust-analyzer/issues/13751 + +[env] +DEVINITVERS = "latest" diff --git a/.editorconfig b/.editorconfig index 3c44241..2fe0ce0 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,3 +7,6 @@ end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true + +[Makefile] +indent_style = tab diff --git a/Cargo.toml b/Cargo.toml index 99dcff8..cc67268 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [package] name = "devinit" +description = "Quickly initialise files and folders with preconfigured boilerplate templates" edition = "2018" diff --git a/Makefile b/Makefile index 0e5f4e5..bd786f4 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,7 @@ BUILD_DIR := build SRC_DIR := . -# PROJECT_VERS := $(shell "$(SRC_DIR)/util/version.sh" --short) -# PROJECT_VERS_LONG := $(shell "$(SRC_DIR)/util/version.sh" --long) +PROJECT_VERS := $(shell "$(SRC_DIR)/util/version.sh" --short) CARGO := cargo CARGOCHAN := +nightly @@ -41,12 +40,12 @@ all: devinit $(BUILD_DIR): mkdir -p $(BUILD_DIR) - # # Compile the devinit executable # devinit: $(CARGO_TOML) | validate_cargo + DEVINITVERS=$(PROJECT_VERS) \ $(CARGO) $(CARGOCHAN) build $(CARGOFLAGS) --manifest-path=$(CARGO_TOML) --target-dir=$(BUILD_DIR)/_$@ --out-dir=$(BUILD_DIR) diff --git a/src/cli.rs b/src/cli.rs index dbc72bd..87c12ee 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -8,7 +8,7 @@ use clap::{Args, Parser, Subcommand}; #[derive(Parser, Debug)] -#[command(about, long_about = None)] +#[command(version = env!("DEVINITVERS"), about, long_about = None)] pub struct Cli { #[command(subcommand)] pub subcommand: CommandVariant, @@ -22,25 +22,28 @@ pub enum CommandVariant { Project(ProjectArgs), } -/// Compile TeX input into PDF output +/// Initialise a file with a specified template profile #[derive(Args, Debug)] pub struct FileArgs { + /// Path to output file (will be created if necessary) + pub file: String, + #[command(flatten)] pub com: CommonArgGroup, } -/// Persistently watch folder or file for changes and recompile +/// Initialise a new folder with a specified project template profile #[derive(Args, Debug)] pub struct ProjectArgs { + /// Path to output directory (will be created if necessary) + pub folder: String, + #[command(flatten)] pub com: CommonArgGroup, } #[derive(Args, Debug)] pub struct CommonArgGroup { - /// Path to output location - pub output: String, - /// Print verbose output #[arg(short, long, action)] pub verbose: bool, diff --git a/util/version.sh b/util/version.sh new file mode 100755 index 0000000..839de24 --- /dev/null +++ b/util/version.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash + +function getverslong { + echo "$($GIT describe --abbrev=4 --always --tags --dirty 2>/dev/null)" +} +function getversshort { + s="$($GIT describe --abbrev=4 --always --tags --dirty 2>/dev/null)" + echo "${s%-g*}" +} + +GIT="$(command -v git)" + +OUT= +NO_V= + +for arg in "$@"; do + case "$arg" in + --long) + OUT="$(getverslong)" + ;; + --short) + OUT="$(getversshort)" + if [ "$?" != 0 ]; then + OUT="v0.0.0" + fi + ;; + --no-v) + NO_V=true + ;; + esac +done + +if [ ! -n "$OUT" ]; then + OUT="$(getverslong)" +fi + +if [ -n "$NO_V" ]; then + OUT="${OUT/v}" +fi + +echo $OUT