From 2258a914fa87e103964527b58efb15fa3692fa82 Mon Sep 17 00:00:00 2001 From: Michael Adler Date: Mon, 2 Sep 2024 22:10:53 +0200 Subject: [PATCH] feat: embed version from git --- .github/workflows/ci.yml | 24 +++++---------- Cargo.lock | 23 +++++++++++++- Cargo.toml | 26 +++++++++++----- build.rs | 23 +++++++------- share/_notmuch-mailmover | 41 ------------------------- share/_notmuch-mailmover.ps1 | 40 ------------------------- share/notmuch-mailmover.1 | 32 -------------------- share/notmuch-mailmover.bash | 58 ------------------------------------ share/notmuch-mailmover.elv | 34 --------------------- share/notmuch-mailmover.fish | 5 ---- src/{ => lib}/action.rs | 0 src/{ => lib}/cli.rs | 14 +++++++-- src/{ => lib}/config.rs | 0 src/{ => lib}/engine.rs | 0 src/lib/mod.rs | 5 ++++ src/{ => lib}/repo.rs | 0 src/main.rs | 13 +++----- 17 files changed, 79 insertions(+), 259 deletions(-) delete mode 100644 share/_notmuch-mailmover delete mode 100644 share/_notmuch-mailmover.ps1 delete mode 100644 share/notmuch-mailmover.1 delete mode 100644 share/notmuch-mailmover.bash delete mode 100644 share/notmuch-mailmover.elv delete mode 100644 share/notmuch-mailmover.fish rename src/{ => lib}/action.rs (100%) rename src/{ => lib}/cli.rs (68%) rename src/{ => lib}/config.rs (100%) rename src/{ => lib}/engine.rs (100%) create mode 100644 src/lib/mod.rs rename src/{ => lib}/repo.rs (100%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1302937..8b166d0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,11 +6,18 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + with: + fetch-depth: 0 # needed to embed version information - uses: actions-rust-lang/setup-rust-toolchain@v1 - name: Install deps run: sudo apt-get update -q && sudo apt-get install -y libnotmuch-dev - - run: cargo build + - run: cargo build --release - run: cargo test --all-features + - uses: actions/upload-artifact@v4 + with: + name: notmuch-mailmover + path: | + target/release/notmuch-mailmover format: name: cargo fmt @@ -33,21 +40,6 @@ jobs: components: clippy - run: cargo clippy - generate: - name: generate - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: actions-rust-lang/setup-rust-toolchain@v1 - - name: Install deps - run: sudo apt-get update -q && sudo apt-get install -y libnotmuch-dev - - name: Generate files - run: cargo build - env: - GEN_ARTIFACTS: share - - name: Check if files have changed - run: git diff --exit-code - coverage: runs-on: ubuntu-latest env: diff --git a/Cargo.lock b/Cargo.lock index de5962b..3b3c544 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -275,6 +275,26 @@ dependencies = [ "wasi", ] +[[package]] +name = "git-version" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad568aa3db0fcbc81f2f116137f263d7304f512a1209b35b85150d3ef88ad19" +dependencies = [ + "git-version-macro", +] + +[[package]] +name = "git-version-macro" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53010ccb100b96a67bc32c0175f0ed1426b31b655d562898e57325f81c023ac0" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.77", +] + [[package]] name = "hashbrown" version = "0.14.5" @@ -361,7 +381,7 @@ dependencies = [ [[package]] name = "notmuch-mailmover" -version = "0.2.0" +version = "0.3.0" dependencies = [ "anyhow", "clap", @@ -369,6 +389,7 @@ dependencies = [ "clap_mangen", "directories", "env_logger", + "git-version", "log", "notmuch", "regex", diff --git a/Cargo.toml b/Cargo.toml index 0eea6de..18f3d68 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,25 @@ [package] name = "notmuch-mailmover" -version = "0.2.0" +version = "0.3.0" authors = ["Michael Adler "] description = "Move notmuch tagged mails into Maildir folders" homepage = "https://github.com/michaeladler/notmuch-mailmover" license = "Apache-2.0" edition = "2021" +[lib] +name = "nm_mailmover" +path = "src/lib/mod.rs" + +[[bin]] +name = "notmuch-mailmover" +path = "src/main.rs" + +[profile.release] +lto = true +panic = "abort" +codegen-units = 1 + [dependencies] uuid = { version = "1.10.0", features = ["v4"] } anyhow = "1.0.82" @@ -18,16 +31,13 @@ serde_yaml = "0.9.34" directories = "5.0.1" shellexpand = "3.0.0" clap = { version = "4.5.13", features = [ "derive", "cargo" ] } - -[dev-dependencies] -regex = "1.10.6" +git-version = "0.3.9" [build-dependencies] clap = { version = "4.5.13", features = [ "derive", "cargo" ] } clap_complete = "4.5.13" clap_mangen = "0.2.23" +git-version = "0.3.9" -[profile.release] -lto = true -panic = "abort" -codegen-units = 1 +[dev-dependencies] +regex = "1.10.6" diff --git a/build.rs b/build.rs index a1ad214..a11ae93 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,4 @@ use std::{ - env, fs::{create_dir_all, File}, path::Path, }; @@ -8,22 +7,20 @@ use clap::CommandFactory; use clap_complete::{generate_to, Shell}; use clap_mangen::Man; -include!("src/cli.rs"); +include!("src/lib/cli.rs"); fn main() { - println!("cargo:rerun-if-env-changed=GEN_ARTIFACTS"); + println!("cargo::rerun-if-changed=src/lib/cli.rs"); - if let Some(dir) = env::var_os("GEN_ARTIFACTS") { - let out = &Path::new(&dir); - create_dir_all(out).unwrap(); - let cmd = &mut Cli::command(); + let out = &Path::new("share"); + create_dir_all(out).unwrap(); + let cmd = &mut Cli::command(); - Man::new(cmd.clone()) - .render(&mut File::create(out.join("notmuch-mailmover.1")).unwrap()) - .unwrap(); + Man::new(cmd.clone()) + .render(&mut File::create(out.join("notmuch-mailmover.1")).unwrap()) + .unwrap(); - for shell in Shell::value_variants() { - generate_to(*shell, cmd, "notmuch-mailmover", out).unwrap(); - } + for shell in Shell::value_variants() { + generate_to(*shell, cmd, "notmuch-mailmover", out).unwrap(); } } diff --git a/share/_notmuch-mailmover b/share/_notmuch-mailmover deleted file mode 100644 index a1f6b81..0000000 --- a/share/_notmuch-mailmover +++ /dev/null @@ -1,41 +0,0 @@ -#compdef notmuch-mailmover - -autoload -U is-at-least - -_notmuch-mailmover() { - typeset -A opt_args - typeset -a _arguments_options - local ret=1 - - if is-at-least 5.2; then - _arguments_options=(-s -S -C) - else - _arguments_options=(-s -C) - fi - - local context curcontext="$curcontext" state line - _arguments "${_arguments_options[@]}" : \ -'-c+[Use the provided config file instead of the default]:FILE:_files' \ -'--config=[Use the provided config file instead of the default]:FILE:_files' \ -'-l+[Configure the log level]:LOG_LEVEL:(trace debug info warn error)' \ -'--log-level=[Configure the log level]:LOG_LEVEL:(trace debug info warn error)' \ -'-d[Enable dry-run mode, i.e. no files are being moved]' \ -'--dry-run[Enable dry-run mode, i.e. no files are being moved]' \ -'-h[Print help]' \ -'--help[Print help]' \ -'-V[Print version]' \ -'--version[Print version]' \ -&& ret=0 -} - -(( $+functions[_notmuch-mailmover_commands] )) || -_notmuch-mailmover_commands() { - local commands; commands=() - _describe -t commands 'notmuch-mailmover commands' commands "$@" -} - -if [ "$funcstack[1]" = "_notmuch-mailmover" ]; then - _notmuch-mailmover "$@" -else - compdef _notmuch-mailmover notmuch-mailmover -fi diff --git a/share/_notmuch-mailmover.ps1 b/share/_notmuch-mailmover.ps1 deleted file mode 100644 index 3626a31..0000000 --- a/share/_notmuch-mailmover.ps1 +++ /dev/null @@ -1,40 +0,0 @@ - -using namespace System.Management.Automation -using namespace System.Management.Automation.Language - -Register-ArgumentCompleter -Native -CommandName 'notmuch-mailmover' -ScriptBlock { - param($wordToComplete, $commandAst, $cursorPosition) - - $commandElements = $commandAst.CommandElements - $command = @( - 'notmuch-mailmover' - for ($i = 1; $i -lt $commandElements.Count; $i++) { - $element = $commandElements[$i] - if ($element -isnot [StringConstantExpressionAst] -or - $element.StringConstantType -ne [StringConstantType]::BareWord -or - $element.Value.StartsWith('-') -or - $element.Value -eq $wordToComplete) { - break - } - $element.Value - }) -join ';' - - $completions = @(switch ($command) { - 'notmuch-mailmover' { - [CompletionResult]::new('-c', '-c', [CompletionResultType]::ParameterName, 'Use the provided config file instead of the default') - [CompletionResult]::new('--config', '--config', [CompletionResultType]::ParameterName, 'Use the provided config file instead of the default') - [CompletionResult]::new('-l', '-l', [CompletionResultType]::ParameterName, 'Configure the log level') - [CompletionResult]::new('--log-level', '--log-level', [CompletionResultType]::ParameterName, 'Configure the log level') - [CompletionResult]::new('-d', '-d', [CompletionResultType]::ParameterName, 'Enable dry-run mode, i.e. no files are being moved') - [CompletionResult]::new('--dry-run', '--dry-run', [CompletionResultType]::ParameterName, 'Enable dry-run mode, i.e. no files are being moved') - [CompletionResult]::new('-h', '-h', [CompletionResultType]::ParameterName, 'Print help') - [CompletionResult]::new('--help', '--help', [CompletionResultType]::ParameterName, 'Print help') - [CompletionResult]::new('-V', '-V ', [CompletionResultType]::ParameterName, 'Print version') - [CompletionResult]::new('--version', '--version', [CompletionResultType]::ParameterName, 'Print version') - break - } - }) - - $completions.Where{ $_.CompletionText -like "$wordToComplete*" } | - Sort-Object -Property ListItemText -} diff --git a/share/notmuch-mailmover.1 b/share/notmuch-mailmover.1 deleted file mode 100644 index 912277f..0000000 --- a/share/notmuch-mailmover.1 +++ /dev/null @@ -1,32 +0,0 @@ -.ie \n(.g .ds Aq \(aq -.el .ds Aq ' -.TH notmuch-mailmover 1 "notmuch-mailmover 0.2.0" -.SH NAME -notmuch\-mailmover -.SH SYNOPSIS -\fBnotmuch\-mailmover\fR [\fB\-c\fR|\fB\-\-config\fR] [\fB\-l\fR|\fB\-\-log\-level\fR] [\fB\-d\fR|\fB\-\-dry\-run\fR] [\fB\-h\fR|\fB\-\-help\fR] [\fB\-V\fR|\fB\-\-version\fR] -.SH DESCRIPTION -.SH OPTIONS -.TP -\fB\-c\fR, \fB\-\-config\fR=\fIFILE\fR -Use the provided config file instead of the default -.TP -\fB\-l\fR, \fB\-\-log\-level\fR=\fILOG_LEVEL\fR [default: info] -Configure the log level -.br - -.br -[\fIpossible values: \fRtrace, debug, info, warn, error] -.TP -\fB\-d\fR, \fB\-\-dry\-run\fR -Enable dry\-run mode, i.e. no files are being moved -.TP -\fB\-h\fR, \fB\-\-help\fR -Print help -.TP -\fB\-V\fR, \fB\-\-version\fR -Print version -.SH VERSION -v0.2.0 -.SH AUTHORS -Michael Adler diff --git a/share/notmuch-mailmover.bash b/share/notmuch-mailmover.bash deleted file mode 100644 index e756e0e..0000000 --- a/share/notmuch-mailmover.bash +++ /dev/null @@ -1,58 +0,0 @@ -_notmuch-mailmover() { - local i cur prev opts cmd - COMPREPLY=() - cur="${COMP_WORDS[COMP_CWORD]}" - prev="${COMP_WORDS[COMP_CWORD-1]}" - cmd="" - opts="" - - for i in ${COMP_WORDS[@]} - do - case "${cmd},${i}" in - ",$1") - cmd="notmuch__mailmover" - ;; - *) - ;; - esac - done - - case "${cmd}" in - notmuch__mailmover) - opts="-c -l -d -h -V --config --log-level --dry-run --help --version" - if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then - COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) - return 0 - fi - case "${prev}" in - --config) - COMPREPLY=($(compgen -f "${cur}")) - return 0 - ;; - -c) - COMPREPLY=($(compgen -f "${cur}")) - return 0 - ;; - --log-level) - COMPREPLY=($(compgen -W "trace debug info warn error" -- "${cur}")) - return 0 - ;; - -l) - COMPREPLY=($(compgen -W "trace debug info warn error" -- "${cur}")) - return 0 - ;; - *) - COMPREPLY=() - ;; - esac - COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) - return 0 - ;; - esac -} - -if [[ "${BASH_VERSINFO[0]}" -eq 4 && "${BASH_VERSINFO[1]}" -ge 4 || "${BASH_VERSINFO[0]}" -gt 4 ]]; then - complete -F _notmuch-mailmover -o nosort -o bashdefault -o default notmuch-mailmover -else - complete -F _notmuch-mailmover -o bashdefault -o default notmuch-mailmover -fi diff --git a/share/notmuch-mailmover.elv b/share/notmuch-mailmover.elv deleted file mode 100644 index 64dde34..0000000 --- a/share/notmuch-mailmover.elv +++ /dev/null @@ -1,34 +0,0 @@ - -use builtin; -use str; - -set edit:completion:arg-completer[notmuch-mailmover] = {|@words| - fn spaces {|n| - builtin:repeat $n ' ' | str:join '' - } - fn cand {|text desc| - edit:complex-candidate $text &display=$text' '(spaces (- 14 (wcswidth $text)))$desc - } - var command = 'notmuch-mailmover' - for word $words[1..-1] { - if (str:has-prefix $word '-') { - break - } - set command = $command';'$word - } - var completions = [ - &'notmuch-mailmover'= { - cand -c 'Use the provided config file instead of the default' - cand --config 'Use the provided config file instead of the default' - cand -l 'Configure the log level' - cand --log-level 'Configure the log level' - cand -d 'Enable dry-run mode, i.e. no files are being moved' - cand --dry-run 'Enable dry-run mode, i.e. no files are being moved' - cand -h 'Print help' - cand --help 'Print help' - cand -V 'Print version' - cand --version 'Print version' - } - ] - $completions[$command] -} diff --git a/share/notmuch-mailmover.fish b/share/notmuch-mailmover.fish deleted file mode 100644 index 38e59be..0000000 --- a/share/notmuch-mailmover.fish +++ /dev/null @@ -1,5 +0,0 @@ -complete -c notmuch-mailmover -s c -l config -d 'Use the provided config file instead of the default' -r -F -complete -c notmuch-mailmover -s l -l log-level -d 'Configure the log level' -r -f -a "{trace\t'',debug\t'',info\t'',warn\t'',error\t''}" -complete -c notmuch-mailmover -s d -l dry-run -d 'Enable dry-run mode, i.e. no files are being moved' -complete -c notmuch-mailmover -s h -l help -d 'Print help' -complete -c notmuch-mailmover -s V -l version -d 'Print version' diff --git a/src/action.rs b/src/lib/action.rs similarity index 100% rename from src/action.rs rename to src/lib/action.rs diff --git a/src/cli.rs b/src/lib/cli.rs similarity index 68% rename from src/cli.rs rename to src/lib/cli.rs index 265efee..786ec06 100644 --- a/src/cli.rs +++ b/src/lib/cli.rs @@ -1,11 +1,21 @@ use std::path::PathBuf; -use clap::{crate_authors, crate_version, Parser, ValueEnum}; +use clap::{crate_authors, Parser, ValueEnum}; + +use git_version::git_version; + +pub const VERSION: &str = git_version!( + cargo_prefix = "", + prefix = "", + // Note that on the CLI, the v* needs to be in single quotes + // When passed here though there seems to be some magic quoting that happens. + args = ["--always", "--dirty=-dirty", "--match=v*", "--tags"] +); #[derive(Parser)] #[clap( name = "notmuch-mailmover", - version = crate_version!(), + version = VERSION, author = crate_authors!(), )] pub struct Cli { diff --git a/src/config.rs b/src/lib/config.rs similarity index 100% rename from src/config.rs rename to src/lib/config.rs diff --git a/src/engine.rs b/src/lib/engine.rs similarity index 100% rename from src/engine.rs rename to src/lib/engine.rs diff --git a/src/lib/mod.rs b/src/lib/mod.rs new file mode 100644 index 0000000..5e8a72d --- /dev/null +++ b/src/lib/mod.rs @@ -0,0 +1,5 @@ +pub mod action; +pub mod cli; +pub mod config; +pub mod engine; +pub mod repo; diff --git a/src/repo.rs b/src/lib/repo.rs similarity index 100% rename from src/repo.rs rename to src/lib/repo.rs diff --git a/src/main.rs b/src/main.rs index 5dae8eb..e7b16e0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,10 @@ -mod action; -mod cli; -mod config; -mod engine; -mod repo; - -use clap::Parser; -use std::time::Instant; - use anyhow::Result; +use clap::Parser; use env_logger::Env; use log::{debug, info}; +use std::time::Instant; + +use nm_mailmover::{action, cli, config, engine}; fn main() -> Result<()> { let opts = cli::Cli::parse();