Skip to content

Commit

Permalink
Set version properties on task setup, modify clean task #30
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonZiminSaritasa committed May 16, 2017
1 parent 7a808d6 commit 70e46a5
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 31 deletions.
48 changes: 28 additions & 20 deletions default.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ if ($PSVersionTable.PSVersion.Major -lt 3)
throw "PowerShell 3 is required.`nhttp://www.microsoft.com/en-us/download/details.aspx?id=40855"
}

. .\Scripts\Saritasa.PsakeExtensions.ps1
. .\scripts\Saritasa.PsakeTasks.ps1

. .\scripts\BuildTasks.ps1
Expand All @@ -17,13 +18,17 @@ if ($PSVersionTable.PSVersion.Major -lt 3)

Properties `
{
$Version = '0.1.0'
$SemVer = $env:SemVer
$MajorMinorPatch = $env:MajorMinorPatch
$LibDirectory = './build/lib'
$Configuration = 'Release'
}

# Global variable.
$script:Version = '0.0.0'
# For samples.
$ServerHost = $null
$SiteName = $null
$DeployUsername = $null
$DeployPassword = $null
}

$packages = @(
'Saritasa.Tools.Common' # common
Expand All @@ -32,7 +37,7 @@ $packages = @(
'Saritasa.Tools.EfCore1' # efcore1
'Saritasa.Tools.Emails' # emails
'Saritasa.Tools.Messages' # messages
'Saritasa.Tools.Messages.Abstractions' # messages-abstractiona
'Saritasa.Tools.Messages.Abstractions' # messages-abstractions
'Saritasa.Tools.Misc' # misc
'Saritasa.Tools.Mvc5' # mvc5
'Saritasa.Tools.NLog4' # nlog4
Expand All @@ -52,19 +57,7 @@ Task pack -description 'Build the library, test it and prepare nuget packages' `
}
}

Task clean -description 'Clean solution' `
{
Remove-Item $LibDirectory -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item './Saritasa.*.nupkg' -ErrorAction SilentlyContinue
Remove-Item './src/*.suo' -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item './src/Saritasa.Tools/bin' -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item './src/Saritasa.Tools/obj' -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item './src/StyleCop.Cache' -Force -ErrorAction SilentlyContinue
Remove-Item './docs/_build' -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item './docs/conf.py' -ErrorAction SilentlyContinue
}

Task docs -depends get-version -description 'Compile and open documentation' `
Task docs -description 'Compile and open documentation' `
{
CompileDocs
Invoke-Item './docs/_build/html/index.html'
Expand All @@ -73,11 +66,26 @@ Task docs -depends get-version -description 'Compile and open documentation' `
function CompileDocs
{
Copy-Item "$docsRoot\conf.py.template" "$docsRoot\conf.py"
(Get-Content "$docsRoot\conf.py").Replace('VX.VY', $Version) | Set-Content "$docsRoot\conf.py"
(Get-Content "$docsRoot\conf.py").Replace('VX.VY', $MajorMinorPatch) | Set-Content "$docsRoot\conf.py"

python -m sphinx.__init__ -b html -d "$docsRoot\_build\doctrees" $docsRoot "$docsRoot\_build\html"
if ($LASTEXITCODE)
{
throw 'Cannot compile documentation.'
}
}

TaskSetup `
{
if (!$SemVer)
{
# 1.2.3-beta.1
Expand-PsakeConfiguration @{ SemVer = Exec { GitVersion.exe /showvariable SemVer } }
}

if (!$MajorMinorPatch)
{
# 1.2.3
Expand-PsakeConfiguration @{ MajorMinorPatch = Exec { GitVersion.exe /showvariable MajorMinorPatch } }
}
}
18 changes: 13 additions & 5 deletions scripts/BuildTasks.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Properties `
{
$Configuration = $null
}

$root = $PSScriptRoot
$src = "$root\..\src"
$samples = "$root\..\samples"
Expand All @@ -17,14 +22,10 @@ Task pre-build `
Exec { GitVersion.exe /updateassemblyinfo }
}

Task get-version `
{
$script:Version = Exec { GitVersion.exe /showvariable MajorMinorPatch }
}

Task build-samples -depends build-zergrushco, build-boringwarehouse

Task build-zergrushco -depends pre-build `
-requiredVariables @('Configuration') `
{
Invoke-SolutionBuild "$samples\ZergRushCo.Todosya\ZergRushCo.Todosya.sln" -Configuration $Configuration

Expand All @@ -49,11 +50,18 @@ Task build-zergrushco -depends pre-build `
}

Task build-boringwarehouse -depends pre-build `
-requiredVariables @('Configuration') `
{
Invoke-SolutionBuild "$samples\Saritasa.BoringWarehouse\Saritasa.BoringWarehouse.sln" -Configuration $Configuration
}

Task build-saritasatools -depends pre-build `
-requiredVariables @('Configuration') `
{
Invoke-SolutionBuild "$src\..\Saritasa.Tools.sln" -Configuration $Configuration
}

Task clean -description '* Clean repository.' `
{
Exec { git clean -xdf -e packages/ -e node_modules/ -e bower_components/ -e nuget.exe }
}
19 changes: 13 additions & 6 deletions scripts/DockerTasks.ps1
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
Properties `
{
$Configuration = $null
$SemVer = $null
}

$root = $PSScriptRoot
$workspace = Resolve-Path "$root\.."

Task docker-zergrushco -depends package-zergrushco `
-requiredVariables @('SemVer') `
{
$dockerContext = "$samples\ZergRushCo.Todosya\Docker"
$version = Exec { GitVersion.exe /showvariable SemVer }

Exec { docker build -t zerg/web:latest -t "zerg/web:$version" -f "$dockerContext\Dockerfile.web" $workspace }
Exec { docker build -t zerg/db:latest -t "zerg/db:$version" -f "$dockerContext\Dockerfile.db" $dockerContext }
Exec { docker build -t zerg/web:latest -t "zerg/web:$SemVer" -f "$dockerContext\Dockerfile.web" $workspace }
Exec { docker build -t zerg/db:latest -t "zerg/db:$SemVer" -f "$dockerContext\Dockerfile.db" $dockerContext }
}

Task docker-boringwarehouse -depends package-boringwarehouse `
-requiredVariables @('SemVer') `
{
$dockerContext = "$samples\Saritasa.BoringWarehouse\Docker"
$version = Exec { GitVersion.exe /showvariable SemVer }

Exec { docker build -t bw/web:latest -t "bw/web:$version" -f "$dockerContext\Dockerfile.web" $workspace }
Exec { docker build -t bw/db:latest -t "bw/db:$version" -f "$dockerContext\Dockerfile.db" $dockerContext }
Exec { docker build -t bw/web:latest -t "bw/web:$SemVer" -f "$dockerContext\Dockerfile.web" $workspace }
Exec { docker build -t bw/db:latest -t "bw/db:$SemVer" -f "$dockerContext\Dockerfile.db" $dockerContext }
}

# Docker images should be built before run (docker-boringwarehouse task).
Task run-boringwarehouse-tests `
-requiredVariables @('Configuration') `
{
# Recreate and containers.
Exec { docker-compose -f "$samples\Saritasa.BoringWarehouse\docker-compose.yml" up --force-recreate -d db }
Expand Down
2 changes: 2 additions & 0 deletions scripts/PublishTasks.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Task pre-publish -description 'Set common publish settings for all deployments.'
}

Task package-zergrushco -depends build-zergrushco `
-requiredVariables @('Configuration') `
{
$packagePath = "$workspace\ZergRushCo.zip"
Invoke-PackageBuild -ProjectPath "$samples\ZergRushCo.Todosya\ZergRushCo.Todosya.Web\ZergRushCo.Todosya.Web.csproj" `
Expand All @@ -31,6 +32,7 @@ Task package-zergrushco -depends build-zergrushco `
}

Task package-boringwarehouse -depends pre-build `
-requiredVariables @('Configuration') `
{
$packagePath = "$workspace\BoringWarehouse.zip"
Invoke-PackageBuild -ProjectPath "$samples\Saritasa.BoringWarehouse\Saritasa.BoringWarehouse.Web\Saritasa.BoringWarehouse.Web.csproj" `
Expand Down
118 changes: 118 additions & 0 deletions scripts/Saritasa.PsakeExtensions.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<#PSScriptInfo
.VERSION 1.0.0
.GUID a55519ec-c877-4480-8496-6c87ca097332
.AUTHOR Anton Zimin
.COMPANYNAME Saritasa
.COPYRIGHT (c) 2016 Saritasa. All rights reserved.
.TAGS Psake
.LICENSEURI https://raw.githubusercontent.com/Saritasa/PSGallery/master/LICENSE
.PROJECTURI https://github.com/Saritasa/PSGallery
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
.SYNOPSIS
The script is intended for dot-sourcing into default.ps1 (main file for Psake tasks). It allows to override Psake properties from another file.
.DESCRIPTION
Dot source the script:
. .\Saritasa.PsakeExtensions.ps1
#>

<#
.SYNOPSIS
Checks file existence and dot-sources it.
.DESCRIPTION
The file should contain Expand-PsakeConfiguration call.
Call Import-PsakeConfigurationFile from TaskSetup block.
.EXAMPLE
# default.ps1
TaskSetup `
{
Import-PsakeConfigurationFile ".\Config.$configuration.ps1"
}
# Config.Debug.ps1
Expand-PsakeConfiguration `
@{
serverHost = 'dev.example.com'
}
#>
function Import-PsakeConfigurationFile
{
param
(
[Parameter(Mandatory = $true)]
[string] $Path
)

$fileName = Resolve-Path $Path
if (Test-Path $fileName)
{
. $fileName
}
}

<#
.SYNOPSIS
Extends Psake properties from the hashtable.
#>
function Expand-PsakeConfiguration
{
param
(
[Parameter(Mandatory = $true)]
[hashtable]
$NewConfiguration
)

for ($i = 1; $i -lt 20; $i++)
{
try
{
Get-Variable -Name 'properties' -Scope $i -ErrorAction SilentlyContinue | Out-Null
}
catch
{
break
}
}
$maxScope = $i - 1
$cmdLineScope = $maxScope - 2
$propertiesScope = $cmdLineScope - 3

# Override properties from passed hashtable.
foreach ($key in $NewConfiguration.Keys)
{
Write-Debug "Set1: $key = $($NewConfiguration.$key)"
Set-Variable -Name $key -Value $NewConfiguration.$key -Scope $propertiesScope | Out-Null
}

# Override properties from command line arguments.
$properties = (Get-Variable 'properties' -Scope $cmdLineScope).Value
foreach ($key in $properties.keys)
{
Write-Debug "Set2: $key"
if (Test-Path "variable:\$key")
{
Write-Debug "Set3: $key = $($properties.$key)"
Set-Variable -Name $key -Value $properties.$key -Scope $propertiesScope | Out-Null
}
}
}

0 comments on commit 70e46a5

Please sign in to comment.