Transfer version information into your code and build environment.
In a rush to patch a hot issue developers may deploy | |
a dirty codebase build. Dirty in a sense that the build | |
happens while there are changes to the code which are | |
not committed to the repository, even if local. If that | |
dirty build goes to production while source code gets | |
more changes before committing, one will never have | |
certainty why that production build behaves a certain | |
way should a problem arise. | |
A full write-up on files in this solution is at: | |
http://blog.didenko.com/2013/11/version-inventory.html |
powershell.exe -ExecutionPolicy RemoteSigned -File "$(SolutionDir)Util\Build\version_info.ps1" server "$(ProjectDir)\" "$(SolutionDir)\" |
<?xml version="1.0" encoding="utf-8"?> | |
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
<ImportGroup Label="PropertySheets" /> | |
<PropertyGroup Label="UserMacros" /> | |
<PropertyGroup /> | |
<ItemDefinitionGroup> | |
<PreBuildEvent> | |
<Command>powershell.exe -ExecutionPolicy RemoteSigned -File "$(SolutionDir)Util\Build\version_info.ps1" server "$(ProjectDir)\" "$(SolutionDir)\"</Command> | |
</PreBuildEvent> | |
</ItemDefinitionGroup> | |
<ItemGroup /> | |
</Project> |
Param ( | |
[String]$Namespace, | |
[String]$Project, | |
[String]$GitRoot, | |
[String]$HeaderFile="version.h", | |
[String]$VerPrefix="https://github.com/<USER>/<REPO>/commit/" | |
) | |
Push-Location -LiteralPath $GitRoot | |
$VerFileHead = "`#pragma once`n`#include <string>`n`nnamespace $Namespace {`n" | |
$VerFileTail = "}`n" | |
$VerBy = (git log -n 1 --format=format:" const std::string VerAuthor=`\`"%an `<%ae`>`\`";%n") | Out-String | |
$VerUrl = (git log -n 1 --format=format:" const std::string VerUrl=`\`"$VerPrefix%H`\`";%n") | Out-String | |
$VerDate = (git log -n 1 --format=format:" const std::string VerDate=`\`"%ai`\`";%n") | Out-String | |
$VerSubj = (git log -n 1 --format=format:" const std::string VerSubj=`\`"%f`\`";%n") | Out-String | |
$VerChgs = ((git ls-files --exclude-standard -d -m -o -k) | Measure-Object -Line).Lines | |
if ($VerChgs -gt 0) { | |
$VerDirty = " const bool VerDirty=true;`n" | |
} else { | |
$VerDirty = " const bool VerDirty=false;`n" | |
} | |
"Written $Project\" + ( | |
New-Item -Force -Path "$Project" -Name "$HeaderFile" -ItemType "file" -Value "$VerFileHead$VerUrl$VerDate$VerSubj$VerBy$VerDirty$VerFileTail" | |
).Name + " as:" | |
"" | |
Get-Content "$Project\$HeaderFile" | |
"" | |
Pop-Location |
#pragma once | |
#include <string> | |
namespace server { | |
const std::string VerUrl="https://github.com/<USER>/<REPO>/commit/<SHA_WILL_BE_HERE>"; | |
const std::string VerDate="2013-10-22 15:00:00 -0700"; | |
const std::string VerSubj="properly-tested-commit-enforcement"; | |
const std::string VerAuthor="The Developer <dev@company.com>"; | |
const bool VerDirty=true; | |
} |
#include "version.h" | |
void Server::versionLogAndVet() { | |
Log::Info("Version Date: ", server::VerDate); | |
Log::Info("Version URL: ", server::VerUrl); | |
Log::Info("Version Info: ", server::VerSubj); | |
Log::Info("Version Author: ", server::VerAuthor); | |
Log::Info("Version Repo Clean: ", (server::VerDirty? "NO, it is DIRTY" : "yes" )); | |
#if defined(_DEBUG) | |
Log::Info("Build configuration: DEBUG"); | |
#elif defined(LOGGED) | |
Log::Info("Build configuration: LOGGED"); | |
#else | |
Log::Info("Build configuration: RELEASE"); | |
#endif | |
#if ! defined(_DEBUG) | |
if (server::VerDirty) | |
{ | |
Log::Error("Must NOT run production code build from a dirty repository. Server process STOPPED."); | |
std::cerr << std::endl; | |
std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl; | |
std::cerr << "Must NOT run production code build from a dirty repository." << std::endl; | |
std::cerr << "STOPPED. Press Enter key to exit or close the window." << std::endl; | |
std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl; | |
std::cerr << std::endl; | |
exit(1); | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment