Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[Bb]in/
[Oo]bj/
[Ww]orking*/
Build/runbuild.txt
Build/nuget.exe
Doc/doc.shfbproj_*
TestResults/
AppPackages/
*.suo
*.user
*.userprefs
_ReSharper.*
*.ReSharper.user
*.resharper.user
.vs/
*.lock.json
*.nuget.props
*.nuget.targets
*.orig
.DS_Store
236 changes: 236 additions & 0 deletions build/build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
properties {

$majorVersion = "1.0"
$majorWithReleaseVersion = "1.0.10"
$version = Get-Version $majorWithReleaseVersion
$baseDir = resolve-path ..
$sourceDir = "$baseDir\Src"
$signAssemblies = $false
$packageId = "SqlSharpener"
$treatWarningsAsErrors = $false
$buildNuGet = $true
$workingName = if ($workingName) {$workingName} else {"Working"}
$workingDir = "$baseDir\$workingName"
$workingSourceDir = "$workingDir\Src"
$builds = @(
@{Name = "SqlSharpener"; TestsName = "SqlSharpener.Tests"; BuildFunction = "MSBuildBuild"; TestsFunction = "NUnitTests"; Constants="NET40"; FinalDir="net40"; NuGetDir = "net40"; Framework="net-4.0"}
)
}

task default -depends Test

# Ensure a clean working directory
task Clean {
Write-Host "Setting location to $baseDir"
Set-Location $baseDir

if (Test-Path -path $workingDir)
{
Write-Host "Deleting existing working directory $workingDir"

Execute-Command -command { del $workingDir -Recurse -Force }
}

Write-Host "Creating working directory $workingDir"
New-Item -Path $workingDir -ItemType Directory
}

# Build each solution, optionally signed
task Build -depends Clean {
Write-Host "Copying source to working source directory $workingSourceDir"
robocopy $sourceDir $workingSourceDir /MIR /NP /XD bin obj TestResults AppPackages $packageDirs .vs artifacts /XF *.suo *.user *.lock.json | Out-Default

Write-Host -ForegroundColor Green "Updating assembly version"
Write-Host
Update-AssemblyInfoFiles $workingSourceDir ($majorVersion + '.0.0') $version

foreach ($build in $builds)
{
$name = $build.Name
if ($name -ne $null)
{
Write-Host -ForegroundColor Green "Building " $name

& $build.BuildFunction $build
}
}
}

# Optional build documentation, add files to final zip
task Package -depends Build {
foreach ($build in $builds)
{
$name = $build.TestsName
$finalDir = $build.FinalDir

robocopy "$workingSourceDir\SqlSharpener\bin\Release\$finalDir" $workingDir\Package\Bin\$finalDir *.dll *.pdb *.xml /NFL /NDL /NJS /NC /NS /NP /XO /XF *.CodeAnalysisLog.xml | Out-Default
}

if ($buildNuGet)
{
$nugetVersion = $majorWithReleaseVersion
if ($nugetPrelease -ne $null)
{
$nugetVersion = $nugetVersion + "-" + $nugetPrelease
}

New-Item -Path $workingDir\NuGet -ItemType Directory

$nuspecPath = "$workingDir\NuGet\SqlSharpener.nuspec"
Copy-Item -Path "$baseDir\dist\SqlSharpener.nuspec" -Destination $nuspecPath -recurse

Write-Host "Updating nuspec file at $nuspecPath" -ForegroundColor Green
Write-Host

$xml = [xml](Get-Content $nuspecPath)
Edit-XmlNodes -doc $xml -xpath "//*[local-name() = 'id']" -value $packageId
Edit-XmlNodes -doc $xml -xpath "//*[local-name() = 'version']" -value $nugetVersion

Write-Host $xml.OuterXml

$xml.save($nuspecPath)

foreach ($build in $builds)
{
if ($build.NuGetDir)
{
$name = $build.TestsName
$finalDir = $build.FinalDir
$frameworkDirs = $build.NuGetDir.Split(",")

foreach ($frameworkDir in $frameworkDirs)
{
robocopy "$workingSourceDir\SqlSharpener\bin\Release\$finalDir" $workingDir\NuGet\tools *.dll *.pdb *.xml /NFL /NDL /NJS /NC /NS /NP /XO /XF *.CodeAnalysisLog.xml | Out-Default
}
}
}

#robocopy $workingSourceDir $workingDir\NuGet\src *.cs /S /NFL /NDL /NJS /NC /NS /NP /XD Newtonsoft.Json.Tests Newtonsoft.Json.TestConsole obj .vs artifacts | Out-Default

Write-Host "Building NuGet package with ID $packageId and version $nugetVersion" -ForegroundColor Green
Write-Host

exec { .\Tools\NuGet\NuGet.exe pack $nuspecPath -Symbols }
move -Path .\*.nupkg -Destination $workingDir\NuGet
}
}

# Unzip package to a location
task Deploy -depends Package {
}


# Run tests on deployed files
task Test -depends Deploy {
}

function MSBuildBuild($build)
{
$name = $build.Name
$finalDir = $build.FinalDir

Write-Host
Write-Host "Restoring $workingSourceDir\$name.sln" -ForegroundColor Green
[Environment]::SetEnvironmentVariable("EnableNuGetPackageRestore", "true", "Process")
exec { .\Tools\NuGet\NuGet.exe update -self }
exec { .\Tools\NuGet\NuGet.exe restore "$workingSourceDir\$name.sln" -verbosity detailed | Out-Default } "Error restoring $name"

$constants = Get-Constants $build.Constants $signAssemblies

Write-Host
Write-Host "Building $workingSourceDir\$name.sln" -ForegroundColor Green
exec { msbuild "/t:Clean;Rebuild" /p:Configuration=Release "/p:CopyNuGetImplementations=true" "/p:Platform=Any CPU" "/p:PlatformTarget=AnyCPU" /p:OutputPath=bin\Release\$finalDir\ "/p:SignAssembly=$signAssemblies" "/p:TreatWarningsAsErrors=$treatWarningsAsErrors" "/p:VisualStudioVersion=14.0" /p:DefineConstants=`"$constants`" "$workingSourceDir\$name.sln" | Out-Default } "Error building $name"
}

function Get-Constants($constants, $includeSigned)
{
$signed = switch($includeSigned) { $true { ";SIGNED" } default { "" } }

return "CODE_ANALYSIS;TRACE;$constants$signed"
}

function Get-Version($majorVersion)
{
$now = [DateTime]::Now

$year = $now.Year - 2000
$month = $now.Month
$totalMonthsSince2000 = ($year * 12) + $month
$day = $now.Day
$minor = "{0}{1:00}" -f $totalMonthsSince2000, $day

$hour = $now.Hour
$minute = $now.Minute
$revision = "{0:00}{1:00}" -f $hour, $minute

return $majorVersion + "." + $minor
}

function Update-AssemblyInfoFiles ([string] $workingSourceDir, [string] $assemblyVersionNumber, [string] $fileVersionNumber)
{
$assemblyVersionPattern = 'AssemblyVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)'
$fileVersionPattern = 'AssemblyFileVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)'
$assemblyVersion = 'AssemblyVersion("' + $assemblyVersionNumber + '")';
$fileVersion = 'AssemblyFileVersion("' + $fileVersionNumber + '")';

Get-ChildItem -Path $workingSourceDir -r -filter AssemblyInfo.cs | ForEach-Object {

$filename = $_.Directory.ToString() + '\' + $_.Name
Write-Host $filename
$filename + ' -> ' + $version

(Get-Content $filename) | ForEach-Object {
% {$_ -replace $assemblyVersionPattern, $assemblyVersion } |
% {$_ -replace $fileVersionPattern, $fileVersion }
} | Set-Content $filename
}
}


function Edit-XmlNodes {
param (
[xml] $doc,
[string] $xpath = $(throw "xpath is a required parameter"),
[string] $value = $(throw "value is a required parameter")
)

$nodes = $doc.SelectNodes($xpath)
$count = $nodes.Count

Write-Host "Found $count nodes with path '$xpath'"

foreach ($node in $nodes) {
if ($node -ne $null) {
if ($node.NodeType -eq "Element")
{
$node.InnerXml = $value
}
else
{
$node.Value = $value
}
}
}
}

function Execute-Command($command) {
$currentRetry = 0
$success = $false
do {
try
{
& $command
$success = $true
}
catch [System.Exception]
{
if ($currentRetry -gt 5) {
throw $_.Exception.ToString()
} else {
write-host "Retry $currentRetry"
Start-Sleep -s 1
}
$currentRetry = $currentRetry + 1
}
} while (!$success)
}
5 changes: 5 additions & 0 deletions build/runbuild.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cls
powershell -Command "& { [Console]::WindowWidth = 150; [Console]::WindowHeight = 50; Start-Transcript %~dp0runbuild.txt; Import-Module %~dp0..\Tools\PSake\psake.psm1; Invoke-psake %~dp0..\Build\build.ps1 %*; Stop-Transcript; exit !($psake.build_success); }"

ECHO %ERRORLEVEL%
EXIT /B %ERRORLEVEL%
Binary file added tools/NuGet/NuGet.exe
Binary file not shown.
53 changes: 53 additions & 0 deletions tools/PSake/psake.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Helper script for those who want to run psake without importing the module.
# Example run from PowerShell:
# .\psake.ps1 "default.ps1" "BuildHelloWord" "4.0"

# Must match parameter definitions for psake.psm1/invoke-psake
# otherwise named parameter binding fails
param(
[Parameter(Position=0,Mandatory=0)]
[string]$buildFile,
[Parameter(Position=1,Mandatory=0)]
[string[]]$taskList = @(),
[Parameter(Position=2,Mandatory=0)]
[string]$framework,
[Parameter(Position=3,Mandatory=0)]
[switch]$docs = $false,
[Parameter(Position=4,Mandatory=0)]
[System.Collections.Hashtable]$parameters = @{},
[Parameter(Position=5, Mandatory=0)]
[System.Collections.Hashtable]$properties = @{},
[Parameter(Position=6, Mandatory=0)]
[alias("init")]
[scriptblock]$initialization = {},
[Parameter(Position=7, Mandatory=0)]
[switch]$nologo = $false,
[Parameter(Position=8, Mandatory=0)]
[switch]$help = $false,
[Parameter(Position=9, Mandatory=0)]
[string]$scriptPath,
[Parameter(Position=10,Mandatory=0)]
[switch]$detailedDocs = $false
)

# setting $scriptPath here, not as default argument, to support calling as "powershell -File psake.ps1"
if (!$scriptPath) {
$scriptPath = $(Split-Path -parent $MyInvocation.MyCommand.path)
}

# '[p]sake' is the same as 'psake' but $Error is not polluted
remove-module [p]sake
import-module (join-path $scriptPath psake.psm1)
if ($help) {
Get-Help Invoke-psake -full
return
}

if ($buildFile -and (-not(test-path $buildFile))) {
$absoluteBuildFile = (join-path $scriptPath $buildFile)
if (test-path $absoluteBuildFile) {
$buildFile = $absoluteBuildFile
}
}

Invoke-psake $buildFile $taskList $framework $docs $parameters $properties $initialization $nologo $detailedDocs
31 changes: 31 additions & 0 deletions tools/PSake/psake.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@{
ModuleToProcess = 'psake.psm1'
ModuleVersion = '4.6.0'
GUID = 'cfb53216-072f-4a46-8975-ff7e6bda05a5'
Author = 'James Kovacs'
Copyright = 'Copyright (c) 2012-16 James Kovacs, Damian Hickey and Contributors'
PowerShellVersion = '2.0'
Description = 'psake is a build automation tool written in PowerShell.'
FunctionsToExport = @('Invoke-psake',
'Invoke-Task',
'Get-PSakeScriptTasks',
'Task',
'Properties',
'Include',
'FormatTaskName',
'TaskSetup',
'TaskTearDown',
'Framework',
'Assert',
'Exec')
VariablesToExport = 'psake'

PrivateData = @{
PSData = @{
LicenseUri = 'https://github.com/psake/psake/blob/master/license.txt'
ProjectUri = 'https://github.com/psake/psake'
Tags = @('Build', 'Task')
IconUri = 'https://raw.githubusercontent.com/psake/graphics/master/png/psake-single-icon-teal-bg-256x256.png'
}
}
}
Loading