Skip to content

Commit 180c27c

Browse files
Merge pull request #20 from Neko-Box-Coder/Version
Adding version to CMake and interface
2 parents ceeae42 + 21ce38e commit 180c27c

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

CMakeLists.txt

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,54 @@ set(CMAKE_CXX_STANDARD 11)
1010
option(RUNCPP2_UPDATE_DEFAULT_YAMLS "Update default yaml files" OFF)
1111
option(RUNCPP2_BUILD_TESTS "Build runcpp2 tests" OFF)
1212

13+
# =========================================================================
14+
# Retrieving Version String
15+
# =========================================================================
16+
17+
function(get_version_from_git OUTPUT_VARIABLE)
18+
# Get the latest tag
19+
execute_process(
20+
COMMAND git describe --tags --abbrev=0
21+
OUTPUT_VARIABLE GIT_TAG
22+
OUTPUT_STRIP_TRAILING_WHITESPACE
23+
RESULT_VARIABLE GIT_TAG_RESULT
24+
ERROR_QUIET
25+
)
26+
27+
# Get the current commit hash
28+
execute_process(
29+
COMMAND git rev-parse --short HEAD
30+
OUTPUT_VARIABLE GIT_COMMIT_HASH
31+
OUTPUT_STRIP_TRAILING_WHITESPACE
32+
ERROR_QUIET
33+
)
34+
35+
if(GIT_TAG_RESULT EQUAL 0)
36+
# Check if the tag is on the current commit
37+
execute_process(
38+
COMMAND git describe --tags --exact-match
39+
RESULT_VARIABLE TAG_ON_COMMIT
40+
OUTPUT_QUIET
41+
ERROR_QUIET
42+
)
43+
44+
if(TAG_ON_COMMIT EQUAL 0)
45+
set(VERSION ${GIT_TAG})
46+
else()
47+
set(VERSION "${GIT_TAG}-${GIT_COMMIT_HASH}")
48+
endif()
49+
else()
50+
# No tags found, use v0.0.0 with commit hash
51+
set(VERSION "v0.0.0-${GIT_COMMIT_HASH}")
52+
endif()
53+
54+
set(${OUTPUT_VARIABLE} ${VERSION} PARENT_SCOPE)
55+
endfunction()
56+
57+
# Call the function and store the result
58+
get_version_from_git(RUNCPP2_PROJECT_VERSION)
59+
message("RUNCPP2_PROJECT_VERSION: ${RUNCPP2_PROJECT_VERSION}")
60+
1361
# =========================================================================
1462
# External Dependencies
1563
# =========================================================================
@@ -122,7 +170,8 @@ endif()
122170

123171
target_compile_options(runcpp2 PRIVATE ${STANDARD_COMPILE_FLAGS})
124172

125-
173+
# Define the version macro
174+
target_compile_definitions(runcpp2 PUBLIC RUNCPP2_VERSION="${RUNCPP2_PROJECT_VERSION}")
126175

127176
# TODO: Maybe move this to the UnitTests sub folder
128177
# Unit Tests
@@ -142,3 +191,4 @@ endif()
142191

143192

144193

194+

Include/runcpp2/runcpp2.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace runcpp2
2222
SCRIPT_TEMPLATE,
2323
WATCH,
2424
BUILD,
25+
VERSION,
2526
COUNT
2627
};
2728

Src/runcpp2/main.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ int main(int argc, char* argv[])
174174
std::unordered_map<runcpp2::CmdOptions, std::string> currentOptions;
175175

176176
{
177-
static_assert(static_cast<int>(runcpp2::CmdOptions::COUNT) == 11, "Update this");
177+
static_assert(static_cast<int>(runcpp2::CmdOptions::COUNT) == 12, "Update this");
178178
std::unordered_map<std::string, runcpp2::OptionInfo> longOptionsMap =
179179
{
180180
{
@@ -216,10 +216,14 @@ int main(int argc, char* argv[])
216216
{
217217
"--build",
218218
runcpp2::OptionInfo(runcpp2::CmdOptions::BUILD, false)
219+
},
220+
{
221+
"--version",
222+
runcpp2::OptionInfo(runcpp2::CmdOptions::VERSION, false)
219223
}
220224
};
221225

222-
static_assert(static_cast<int>(runcpp2::CmdOptions::COUNT) == 11, "Update this");
226+
static_assert(static_cast<int>(runcpp2::CmdOptions::COUNT) == 12, "Update this");
223227
std::unordered_map<std::string, const runcpp2::OptionInfo&> shortOptionsMap =
224228
{
225229
{"-r", longOptionsMap.at("--reset-cache")},
@@ -231,7 +235,8 @@ int main(int argc, char* argv[])
231235
{"-s", longOptionsMap.at("--show-config-path")},
232236
{"-t", longOptionsMap.at("--create-script-template")},
233237
{"-w", longOptionsMap.at("--watch")},
234-
{"-b", longOptionsMap.at("--build")}
238+
{"-b", longOptionsMap.at("--build")},
239+
{"-v", longOptionsMap.at("--version")}
235240
};
236241

237242
currentArgIndex = ParseArgs(longOptionsMap, shortOptionsMap, currentOptions, argc, argv);
@@ -248,7 +253,7 @@ int main(int argc, char* argv[])
248253
//Help message
249254
if(currentOptions.count(runcpp2::CmdOptions::HELP))
250255
{
251-
static_assert(static_cast<int>(runcpp2::CmdOptions::COUNT) == 11, "Update this");
256+
static_assert(static_cast<int>(runcpp2::CmdOptions::COUNT) == 12, "Update this");
252257
ssLOG_BASE("Usage: runcpp2 [options] [input_file]");
253258
ssLOG_BASE("Options:");
254259
ssLOG_BASE(" -r, --[r]eset-cache Deletes all cache and build everything from scratch");
@@ -261,6 +266,7 @@ int main(int argc, char* argv[])
261266
ssLOG_BASE(" -t, --create-script-[t]emplate <file> Creates/prepend runcpp2 script info template");
262267
ssLOG_BASE(" -w, --[w]atch Watch script changes and output any compiling errors");
263268
ssLOG_BASE(" -b, --[b]uild Build the script and copy output files to the working directory");
269+
ssLOG_BASE(" -v, --[v]ersion Show the version of runcpp2");
264270

265271
return 0;
266272
}
@@ -271,6 +277,13 @@ int main(int argc, char* argv[])
271277
ssLOG_BASE(runcpp2::GetConfigFilePath());
272278
return 0;
273279
}
280+
281+
// Check if the version flag is present
282+
if (currentOptions.count(runcpp2::CmdOptions::VERSION))
283+
{
284+
ssLOG_BASE("runcpp2 version " << RUNCPP2_VERSION);
285+
return 0;
286+
}
274287

275288
//Resetting user config
276289
if(currentOptions.count(runcpp2::CmdOptions::RESET_USER_CONFIG))
@@ -415,3 +428,4 @@ int main(int argc, char* argv[])
415428

416429
return result;
417430
}
431+

0 commit comments

Comments
 (0)