Skip to content

Commit

Permalink
Automatically generate the version based on the git tag
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-carlborg committed Mar 9, 2019
1 parent d574614 commit 116b78e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -10,4 +10,5 @@ dvm/dvm/dvm
bin/dvm
*.deps
.dub
bin/*
bin/*
tmp
12 changes: 10 additions & 2 deletions dub.json
Expand Up @@ -12,7 +12,7 @@
"targetPath": "bin",
"sourcePaths": ["dvm"],
"importPaths": ["dvm"],
"stringImportPaths": ["resources"],
"stringImportPaths": ["resources", "tmp"],

"excludedSourceFiles-posix": [
"dvm/util/DvmRegistry.d",
Expand All @@ -25,5 +25,13 @@

"dependencies": {
"mambo": "~>0.0.9"
}
},

"preGenerateCommands-posix": [
"$$DC -run $PACKAGE_DIR/tools/generate_version.d"
],

"preGenerateCommands-windows": [
"%DC% -run $PACKAGE_DIR/tools/generate_version.d"
]
}
5 changes: 4 additions & 1 deletion dvm/dvm/Version.d
Expand Up @@ -6,4 +6,7 @@
*/
module dvm.dvm.Version;

enum Version = "0.4.5";
import std.range : drop;
import std.string : strip;

enum Version = import("version").strip.drop(1);
36 changes: 36 additions & 0 deletions tools/generate_version.d
@@ -0,0 +1,36 @@
import std.file : exists, mkdirRecurse, readText, write;
import std.path : buildPath;
import std.process : execute, env = environment;
import std.string : strip;

enum outputDirectory = "tmp";
enum versionFile = "version";

void main()
{
const outputDirectory = env.get("DUB_PACKAGE_DIR", ".").buildPath("tmp");

mkdirRecurse(outputDirectory);

outputDirectory
.buildPath("version")
.updateIfChanged(generateVersion);
}

string generateVersion()
{
const result = execute(["git", "describe", "--dirty", "--tags", "--always"]);

if (result.status != 0)
throw new Exception("Failed to execute 'git describe'");

return result.output.strip;
}

void updateIfChanged(const string path, const string content)
{
const existingContent = path.exists ? path.readText : "";

if (content != existingContent)
write(path, content);
}

0 comments on commit 116b78e

Please sign in to comment.