Last active
October 23, 2015 03:28
-
-
Save sakapon/1755bd87accc9dffa2dd to your computer and use it in GitHub Desktop.
BuildSample / Increment Version (C# in PowerShell)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$msbuild = "C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe" | |
.\IncrementVersion-cs.ps1 ..\EmptyConsole | |
$slnFilePath = "..\BuildSample.sln" | |
Invoke-Expression ([string]::Format("{0} {1} /p:Configuration=Release /t:Clean", $msbuild, $slnFilePath)) | |
Invoke-Expression ([string]::Format("{0} {1} /p:Configuration=Release /t:Rebuild", $msbuild, $slnFilePath)) | |
.\CreateZipForAssembly.ps1 ..\EmptyConsole\bin\Release\EmptyConsole.exe ..\Downloads | |
explorer ..\Downloads |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$source = @" | |
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
public static class Program | |
{ | |
public static int Main(string[] args) | |
{ | |
if (args.Length < 1) return 100; | |
IncrementVersion(args[0]); | |
return 0; | |
} | |
public static void IncrementVersion(string projDirPath) | |
{ | |
var assemblyInfoPath = Directory.EnumerateFiles(projDirPath, "AssemblyInfo.cs", SearchOption.AllDirectories).First(); | |
var contents = File.ReadLines(assemblyInfoPath, Encoding.UTF8) | |
.Select(IncrementLine) | |
.ToArray(); | |
File.WriteAllLines(assemblyInfoPath, contents, Encoding.UTF8); | |
} | |
static string IncrementLine(string line) | |
{ | |
if (line.StartsWith("//")) return line; | |
var match = Regex.Match(line, @"Assembly(File)?Version\(""([0-9\.]+)""\)"); | |
if (!match.Success) return line; | |
var oldVersion = match.Groups[2].Value; | |
var newVersion = IncrementBuildNumber(oldVersion); | |
return line.Replace(oldVersion, newVersion); | |
} | |
static string IncrementBuildNumber(string version) | |
{ | |
return Regex.Replace(version, @"^(\d+\.\d+\.)(\d+)((\.\d+)?)$", m => m.Groups[1].Value + IncrementNumber(m.Groups[2].Value) + m.Groups[3].Value); | |
} | |
static string IncrementNumber(string i) | |
{ | |
return (int.Parse(i) + 1).ToString(); | |
} | |
} | |
"@ | |
Add-Type -TypeDefinition $source -Language CSharp | |
# $Args[0]: project directory path | |
[Program]::Main($Args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment