Add Git-derived version reporting

This commit is contained in:
deeaitch committed 2026-08-28 01:30:44 -04:00
1 parent 7026e246bb
commit 571a820807
3 files changed
+59 -4

No files matched your search

+27 -1
View File
@@ -1,11 +1,34 @@
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
project(kindle2notes LANGUAGES CXX) project(kindle2notes VERSION 1.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_EXTENSIONS OFF)
set(KINDLE2NOTES_VERSION "${PROJECT_VERSION}")
find_package(Git QUIET)
if(Git_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} describe --tags --long --dirty --always
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE KINDLE2NOTES_GIT_RESULT
OUTPUT_VARIABLE KINDLE2NOTES_GIT_DESCRIBE
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
if(KINDLE2NOTES_GIT_RESULT EQUAL 0 AND NOT KINDLE2NOTES_GIT_DESCRIBE STREQUAL "")
if(KINDLE2NOTES_GIT_DESCRIBE MATCHES "^(.+)-0-g[0-9a-f]+$")
set(KINDLE2NOTES_VERSION "${CMAKE_MATCH_1}")
else()
set(KINDLE2NOTES_VERSION "${KINDLE2NOTES_GIT_DESCRIBE}")
endif()
endif()
endif()
message(STATUS "kindle2notes version: ${KINDLE2NOTES_VERSION}")
function(kindle2notes_enable_warnings target) function(kindle2notes_enable_warnings target)
if(MSVC) if(MSVC)
target_compile_options(${target} PRIVATE /W4) target_compile_options(${target} PRIVATE /W4)
@@ -29,3 +52,6 @@ add_executable(kindle2notes
typefactory/typefactory.h typefactory/typefactory.h
) )
kindle2notes_enable_warnings(kindle2notes) kindle2notes_enable_warnings(kindle2notes)
target_compile_definitions(kindle2notes PRIVATE
KINDLE2NOTES_VERSION="${KINDLE2NOTES_VERSION}"
)
+13
View File
@@ -131,6 +131,12 @@ The resulting executable is:
build/kindle2notes build/kindle2notes
``` ```
When the source is built from Git, CMake obtains the version from
`git describe`. A build made exactly from a release tag reports only that tag
(for example, `1.0`). Later commits include their distance from the tag and
the abbreviated commit hash (for example, `1.0-3-gabc1234`). A modified
working tree also receives the `-dirty` suffix.
## Usage ## Usage
Generate LaTeX: Generate LaTeX:
@@ -175,11 +181,18 @@ Short options are also available:
./build/kindle2notes -i input.txt -o notes.md -t md ./build/kindle2notes -i input.txt -o notes.md -t md
``` ```
Show the version compiled into the executable:
```bash
./build/kindle2notes --version
```
| Argument | Short form | Description | | Argument | Short form | Description |
| --- | --- | --- | | --- | --- | --- |
| `--input` | `-i` | Path to the Kindle clippings file | | `--input` | `-i` | Path to the Kindle clippings file |
| `--output` | `-o` | Path to the generated file | | `--output` | `-o` | Path to the generated file |
| `--type` | `-t` | Output format: `latex`, `md`, `org`, or `adoc` | | `--type` | `-t` | Output format: `latex`, `md`, `org`, or `adoc` |
| `--version` | `-v` | Show the compiled version and exit |
| `--help` | `-h` | Show usage information | | `--help` | `-h` | Show usage information |
If an unsupported output type is requested, the program prints the list of If an unsupported output type is requested, the program prints the list of
+19 -3
View File
@@ -9,6 +9,10 @@
#include "convertorg.h" #include "convertorg.h"
#include "typefactory.h" #include "typefactory.h"
#ifndef KINDLE2NOTES_VERSION
#define KINDLE2NOTES_VERSION "unknown"
#endif
using ConverterFactory = TypeFactory<std::string, Converter, std::string>; using ConverterFactory = TypeFactory<std::string, Converter, std::string>;
static constexpr std::array<const char *, 4> kSupportedTypes = { static constexpr std::array<const char *, 4> kSupportedTypes = {
@@ -19,11 +23,13 @@ struct CliArgs {
std::string input; std::string input;
std::string output; std::string output;
std::string type; std::string type;
bool version = false;
}; };
static void print_usage (const char *argv0) { static void print_usage (const char *argv0) {
std::cerr << "Usage: " << argv0 std::cerr << "Usage: " << argv0
<< " --input <file> --output <file> --type <type>\n"; << " --input <file> --output <file> --type <type>\n"
<< " " << argv0 << " --version\n";
} }
static void print_supported_types() { static void print_supported_types() {
@@ -47,12 +53,13 @@ static bool parse_args (int argc, char **argv, CliArgs &out) {
{"input", required_argument, nullptr, 'i'}, {"input", required_argument, nullptr, 'i'},
{"output", required_argument, nullptr, 'o'}, {"output", required_argument, nullptr, 'o'},
{"type", required_argument, nullptr, 't'}, {"type", required_argument, nullptr, 't'},
{"version", no_argument, nullptr, 'v'},
{"help", no_argument, nullptr, 'h'}, {"help", no_argument, nullptr, 'h'},
{nullptr, 0, nullptr, 0 } {nullptr, 0, nullptr, 0 }
}; };
int c = 0; int c = 0;
while ((c = ::getopt_long (argc, argv, "i:o:t:h", long_opts, nullptr)) != -1) { while ((c = ::getopt_long (argc, argv, "i:o:t:vh", long_opts, nullptr)) != -1) {
switch (c) { switch (c) {
case 'i': case 'i':
out.input = optarg; out.input = optarg;
@@ -63,6 +70,9 @@ static bool parse_args (int argc, char **argv, CliArgs &out) {
case 't': case 't':
out.type = optarg; out.type = optarg;
break; break;
case 'v':
out.version = true;
break;
case 'h': case 'h':
print_usage (argv[0]); print_usage (argv[0]);
return false; return false;
@@ -72,7 +82,8 @@ static bool parse_args (int argc, char **argv, CliArgs &out) {
} }
} }
if (out.input.empty() || out.output.empty() || out.type.empty()) { if (!out.version &&
(out.input.empty() || out.output.empty() || out.type.empty())) {
print_usage (argv[0]); print_usage (argv[0]);
return false; return false;
} }
@@ -85,6 +96,11 @@ int main (int argc, char **argv) {
if (!parse_args (argc, argv, args)) if (!parse_args (argc, argv, args))
return 1; return 1;
if (args.version) {
std::cout << "kindle2notes " << KINDLE2NOTES_VERSION << '\n';
return 0;
}
const ConverterFactory factory = build_converter_factory(); const ConverterFactory factory = build_converter_factory();
if (!factory.contains (args.type)) { if (!factory.contains (args.type)) {
std::cerr << "Unsupported output type: " << args.type << '\n'; std::cerr << "Unsupported output type: " << args.type << '\n';