Skip to content

Commit

Permalink
Add version.sh script to make version number from Git info
Browse files Browse the repository at this point in the history
  • Loading branch information
kosude committed May 9, 2024
1 parent 28146cb commit 5acec55
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[Makefile]
indent_style = tab
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "devinit"
description = "Quickly initialise files and folders with preconfigured boilerplate templates"

edition = "2018"

Expand Down
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)


Expand Down
15 changes: 9 additions & 6 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
41 changes: 41 additions & 0 deletions util/version.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5acec55

Please sign in to comment.