Skip to content

Commit 5d2d3b2

Browse files
committed
Add cake to automaticly set version number on build
1 parent c38f0e8 commit 5d2d3b2

File tree

4 files changed

+264
-3
lines changed

4 files changed

+264
-3
lines changed

BencodeNET.sln

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26228.10
4+
VisualStudioVersion = 15.0.26430.16
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{65E984B8-653C-4BCD-AE37-4E21497B5B87}"
77
ProjectSection(SolutionItems) = preProject
88
.gitattributes = .gitattributes
99
.gitignore = .gitignore
10+
build.cake = build.cake
11+
build.ps1 = build.ps1
1012
LICENSE.md = LICENSE.md
1113
README.md = README.md
1214
EndProjectSection

BencodeNET/BencodeNET.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<NetStandardImplicitPackageVersion Condition=" '$(TargetFramework)' == 'netstandard1.3' ">1.6.0</NetStandardImplicitPackageVersion>
66
<GenerateDocumentationFile>True</GenerateDocumentationFile>
77
<PackageId>BencodeNET</PackageId>
8-
<Version>2.2.3</Version>
8+
<Version>2.2.0</Version>
99
<Authors>Søren Kruse</Authors>
1010
<Company />
1111
<Product>BencodeNET</Product>
@@ -15,7 +15,7 @@
1515
<PackageIconUrl>https://raw.githubusercontent.com/Krusen/BencodeNET/master/Assets/icon.png</PackageIconUrl>
1616
<RepositoryUrl>https://github.com/Krusen/BencodeNET</RepositoryUrl>
1717
<RepositoryType>git</RepositoryType>
18-
<PackageReleaseNotes>Save original info hash of parsed torrents</PackageReleaseNotes>
18+
<PackageReleaseNotes></PackageReleaseNotes>
1919
<PackageTags>bencode;torrent;torrents</PackageTags>
2020
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
2121
<IncludeSource>True</IncludeSource>

build.cake

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var target = Argument("target", "Default");
2+
3+
Task("Set-Build-Version")
4+
.Does(() =>
5+
{
6+
var projectFile = "./BencodeNET/BencodeNET.csproj";
7+
var versionPeekXpath = "/Project/PropertyGroup/Version/text()";
8+
var versionPokeXpath = "/Project/PropertyGroup/Version";
9+
10+
var version = XmlPeek(projectFile, versionPeekXpath);
11+
var parts = version.Split('.');
12+
13+
var buildNumber = 0;
14+
15+
if (BuildSystem.IsRunningOnAppVeyor)
16+
{
17+
buildNumber = AppVeyor.Environment.Build.Number;
18+
}
19+
20+
version = string.Join(".", parts[0], parts[1], buildNumber);
21+
22+
if (BuildSystem.IsRunningOnAppVeyor)
23+
{
24+
AppVeyor.UpdateBuildVersion(version);
25+
Information("Updated AppVeyor build version to " + version);
26+
}
27+
28+
XmlPoke(projectFile, versionPokeXpath, version);
29+
Information("Set project version to " + version);
30+
});
31+
32+
Task("Default")
33+
.IsDependentOn("Set-Build-Version");
34+
35+
RunTarget(target);

build.ps1

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
##########################################################################
2+
# This is the Cake bootstrapper script for PowerShell.
3+
# This file was downloaded from https://github.com/cake-build/resources
4+
# Feel free to change this file to fit your needs.
5+
##########################################################################
6+
7+
<#
8+
.SYNOPSIS
9+
This is a Powershell script to bootstrap a Cake build.
10+
.DESCRIPTION
11+
This Powershell script will download NuGet if missing, restore NuGet tools (including Cake)
12+
and execute your Cake build script with the parameters you provide.
13+
.PARAMETER Script
14+
The build script to execute.
15+
.PARAMETER Target
16+
The build script target to run.
17+
.PARAMETER Configuration
18+
The build configuration to use.
19+
.PARAMETER Verbosity
20+
Specifies the amount of information to be displayed.
21+
.PARAMETER Experimental
22+
Tells Cake to use the latest Roslyn release.
23+
.PARAMETER WhatIf
24+
Performs a dry run of the build script.
25+
No tasks will be executed.
26+
.PARAMETER Mono
27+
Tells Cake to use the Mono scripting engine.
28+
.PARAMETER SkipToolPackageRestore
29+
Skips restoring of packages.
30+
.PARAMETER ScriptArgs
31+
Remaining arguments are added here.
32+
.LINK
33+
http://cakebuild.net
34+
#>
35+
36+
[CmdletBinding()]
37+
Param(
38+
[string]$Script = "build.cake",
39+
[string]$Target = "Default",
40+
[ValidateSet("Release", "Debug")]
41+
[string]$Configuration = "Release",
42+
[ValidateSet("Quiet", "Minimal", "Normal", "Verbose", "Diagnostic")]
43+
[string]$Verbosity = "Verbose",
44+
[switch]$Experimental,
45+
[Alias("DryRun","Noop")]
46+
[switch]$WhatIf,
47+
[switch]$Mono,
48+
[switch]$SkipToolPackageRestore,
49+
[Parameter(Position=0,Mandatory=$false,ValueFromRemainingArguments=$true)]
50+
[string[]]$ScriptArgs
51+
)
52+
53+
[Reflection.Assembly]::LoadWithPartialName("System.Security") | Out-Null
54+
function MD5HashFile([string] $filePath)
55+
{
56+
if ([string]::IsNullOrEmpty($filePath) -or !(Test-Path $filePath -PathType Leaf))
57+
{
58+
return $null
59+
}
60+
61+
[System.IO.Stream] $file = $null;
62+
[System.Security.Cryptography.MD5] $md5 = $null;
63+
try
64+
{
65+
$md5 = [System.Security.Cryptography.MD5]::Create()
66+
$file = [System.IO.File]::OpenRead($filePath)
67+
return [System.BitConverter]::ToString($md5.ComputeHash($file))
68+
}
69+
finally
70+
{
71+
if ($file -ne $null)
72+
{
73+
$file.Dispose()
74+
}
75+
}
76+
}
77+
78+
Write-Host "Preparing to run build script..."
79+
80+
if(!$PSScriptRoot){
81+
$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
82+
}
83+
84+
$TOOLS_DIR = Join-Path $PSScriptRoot "tools"
85+
$ADDINS_DIR = Join-Path $TOOLS_DIR "Addins"
86+
$MODULES_DIR = Join-Path $TOOLS_DIR "Modules"
87+
$NUGET_EXE = Join-Path $TOOLS_DIR "nuget.exe"
88+
$CAKE_EXE = Join-Path $TOOLS_DIR "Cake/Cake.exe"
89+
$NUGET_URL = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"
90+
$PACKAGES_CONFIG = Join-Path $TOOLS_DIR "packages.config"
91+
$PACKAGES_CONFIG_MD5 = Join-Path $TOOLS_DIR "packages.config.md5sum"
92+
$ADDINS_PACKAGES_CONFIG = Join-Path $ADDINS_DIR "packages.config"
93+
$MODULES_PACKAGES_CONFIG = Join-Path $MODULES_DIR "packages.config"
94+
95+
# Should we use mono?
96+
$UseMono = "";
97+
if($Mono.IsPresent) {
98+
Write-Verbose -Message "Using the Mono based scripting engine."
99+
$UseMono = "-mono"
100+
}
101+
102+
# Should we use the new Roslyn?
103+
# $UseExperimental = "";
104+
$UseExperimental = "-experimental"; # Always use experimental
105+
if($Experimental.IsPresent -and !($Mono.IsPresent)) {
106+
Write-Verbose -Message "Using experimental version of Roslyn."
107+
$UseExperimental = "-experimental"
108+
}
109+
110+
# Is this a dry run?
111+
$UseDryRun = "";
112+
if($WhatIf.IsPresent) {
113+
$UseDryRun = "-dryrun"
114+
}
115+
116+
# Make sure tools folder exists
117+
if ((Test-Path $PSScriptRoot) -and !(Test-Path $TOOLS_DIR)) {
118+
Write-Verbose -Message "Creating tools directory..."
119+
New-Item -Path $TOOLS_DIR -Type directory | out-null
120+
}
121+
122+
# Make sure that packages.config exist.
123+
if (!(Test-Path $PACKAGES_CONFIG)) {
124+
Write-Verbose -Message "Downloading packages.config..."
125+
try { (New-Object System.Net.WebClient).DownloadFile("http://cakebuild.net/download/bootstrapper/packages", $PACKAGES_CONFIG) } catch {
126+
Throw "Could not download packages.config."
127+
}
128+
}
129+
130+
# Try find NuGet.exe in path if not exists
131+
if (!(Test-Path $NUGET_EXE)) {
132+
Write-Verbose -Message "Trying to find nuget.exe in PATH..."
133+
$existingPaths = $Env:Path -Split ';' | Where-Object { (![string]::IsNullOrEmpty($_)) -and (Test-Path $_ -PathType Container) }
134+
$NUGET_EXE_IN_PATH = Get-ChildItem -Path $existingPaths -Filter "nuget.exe" | Select -First 1
135+
if ($NUGET_EXE_IN_PATH -ne $null -and (Test-Path $NUGET_EXE_IN_PATH.FullName)) {
136+
Write-Verbose -Message "Found in PATH at $($NUGET_EXE_IN_PATH.FullName)."
137+
$NUGET_EXE = $NUGET_EXE_IN_PATH.FullName
138+
}
139+
}
140+
141+
# Try download NuGet.exe if not exists
142+
if (!(Test-Path $NUGET_EXE)) {
143+
Write-Verbose -Message "Downloading NuGet.exe..."
144+
try {
145+
(New-Object System.Net.WebClient).DownloadFile($NUGET_URL, $NUGET_EXE)
146+
} catch {
147+
Throw "Could not download NuGet.exe."
148+
}
149+
}
150+
151+
# Save nuget.exe path to environment to be available to child processed
152+
$ENV:NUGET_EXE = $NUGET_EXE
153+
154+
# Restore tools from NuGet?
155+
if(-Not $SkipToolPackageRestore.IsPresent) {
156+
Push-Location
157+
Set-Location $TOOLS_DIR
158+
159+
# Check for changes in packages.config and remove installed tools if true.
160+
[string] $md5Hash = MD5HashFile($PACKAGES_CONFIG)
161+
if((!(Test-Path $PACKAGES_CONFIG_MD5)) -Or
162+
($md5Hash -ne (Get-Content $PACKAGES_CONFIG_MD5 ))) {
163+
Write-Verbose -Message "Missing or changed package.config hash..."
164+
Remove-Item * -Recurse -Exclude packages.config,nuget.exe
165+
}
166+
167+
Write-Verbose -Message "Restoring tools from NuGet..."
168+
$NuGetOutput = Invoke-Expression "&`"$NUGET_EXE`" install -ExcludeVersion -OutputDirectory `"$TOOLS_DIR`""
169+
170+
if ($LASTEXITCODE -ne 0) {
171+
Throw "An error occured while restoring NuGet tools."
172+
}
173+
else
174+
{
175+
$md5Hash | Out-File $PACKAGES_CONFIG_MD5 -Encoding "ASCII"
176+
}
177+
Write-Verbose -Message ($NuGetOutput | out-string)
178+
179+
Pop-Location
180+
}
181+
182+
# Restore addins from NuGet
183+
if (Test-Path $ADDINS_PACKAGES_CONFIG) {
184+
Push-Location
185+
Set-Location $ADDINS_DIR
186+
187+
Write-Verbose -Message "Restoring addins from NuGet..."
188+
$NuGetOutput = Invoke-Expression "&`"$NUGET_EXE`" install -ExcludeVersion -OutputDirectory `"$ADDINS_DIR`""
189+
190+
if ($LASTEXITCODE -ne 0) {
191+
Throw "An error occured while restoring NuGet addins."
192+
}
193+
194+
Write-Verbose -Message ($NuGetOutput | out-string)
195+
196+
Pop-Location
197+
}
198+
199+
# Restore modules from NuGet
200+
if (Test-Path $MODULES_PACKAGES_CONFIG) {
201+
Push-Location
202+
Set-Location $MODULES_DIR
203+
204+
Write-Verbose -Message "Restoring modules from NuGet..."
205+
$NuGetOutput = Invoke-Expression "&`"$NUGET_EXE`" install -ExcludeVersion -OutputDirectory `"$MODULES_DIR`""
206+
207+
if ($LASTEXITCODE -ne 0) {
208+
Throw "An error occured while restoring NuGet modules."
209+
}
210+
211+
Write-Verbose -Message ($NuGetOutput | out-string)
212+
213+
Pop-Location
214+
}
215+
216+
# Make sure that Cake has been installed.
217+
if (!(Test-Path $CAKE_EXE)) {
218+
Throw "Could not find Cake.exe at $CAKE_EXE"
219+
}
220+
221+
# Start Cake
222+
Write-Host "Running build script..."
223+
Invoke-Expression "& `"$CAKE_EXE`" `"$Script`" -target=`"$Target`" -configuration=`"$Configuration`" -verbosity=`"$Verbosity`" $UseMono $UseDryRun $UseExperimental $ScriptArgs"
224+
exit $LASTEXITCODE

0 commit comments

Comments
 (0)