Skip to content

Instantly share code, notes, and snippets.

@gpduck
Created April 12, 2017 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gpduck/bea1f64d1c939f964fa4317b1a55f73e to your computer and use it in GitHub Desktop.
Save gpduck/bea1f64d1c939f964fa4317b1a55f73e to your computer and use it in GitHub Desktop.
PSake Build Script
Properties {
if(!$OutDir) {
$OutDir = "out"
}
if(!$ProjectDir) {
$ProjectDir = $PSake.build_script_dir
}
if(!$TargetDir) {
$TargetDir = Join-Path -Path $ProjectDir -ChildPath $OutDir
}
if(!$ProjectName) {
$ProjectName = Split-Path -Path $ProjectDir -Leaf
}
if(!$NuSpecFile) {
$NuSpecFile = Join-Path -Path $ProjectDir -ChildPath "$ProjectName.nuspec"
}
if(!$BasePath) {
$BasePath = $ProjectDir
}
if(!$NugetServer) {
$NugetServer = "http://mynugetserver/nuget"
}
}
Task default -Depends Pack,Publish
Task Build -Depends VersionModule
function GenerateVersion {
param($NuSpecFile)
$NuSpecXml = [Xml](Get-Content $NuSpecFile)
$NuSpecVersion = [Version]($NuSpecXml.Package.Metadata.Version)
$VersionDate = [int][Datetime]::Now.ToString("yyyyMMdd")
New-Object System.Version($NuSpecVersion.Major, $NuSpecVersion.Minor, $VersionDate, $BuildNumber)
}
Task VersionModule {
$OutputVersion = GenerateVersion -NuSpecFile $NuSpecFile
$ModuleFile = Join-Path -Path $ProjectDir -ChildPath "$ProjectName.psd1"
(Get-Item $ModuleFile).IsReadOnly = $false
(Get-Content $ModuleFile) -replace "ModuleVersion ?= ?['`"](\d+\.?){2,4}['`"]", "ModuleVersion = '$OutputVersion'" | Set-Content $ModuleFile
}
Task Pack -Depends Build {
ipmo ps-get
Assert (Test-Path $NuSpecFile) -FailureMessage "$NuSpecFile does not exist"
if(!(Test-Path $TargetDir)) {
mkdir $TargetDir > $null
}
$OutputVersion = GenerateVersion -NuSpecFile $NuSpecFile
exec { nuget pack $NuSpecFile -OutputDirectory $TargetDir -BasePath $BasePath -NoPackageAnalysis -NonInteractive -Version ($OutputVersion.ToString())}
$OutputFileName = "$ProjectName.$($OutputVersion.ToString()).nupkg"
$Script:OutputFilePath = Join-Path -Path $TargetDir -ChildPath $OutputFileName
}
Task Publish -Depends Pack {
ipmo ps-get
exec { nuget push $Script:OutputFilePath -Source $NugetServer -NonInteractive }
}
Task Clean {
if( (Test-Path $TargetDir) ) {
del $TargetDir -Recurse
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment