Skip to content

Commit

Permalink
updated psake
Browse files Browse the repository at this point in the history
  • Loading branch information
davidegironi committed Jan 30, 2021
1 parent 347b333 commit 0d86d43
Show file tree
Hide file tree
Showing 37 changed files with 4,820 additions and 773 deletions.
2,265 changes: 2,265 additions & 0 deletions _DevTools/Tools/PSake/en-US/psake.psm1-help.xml.old

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions _DevTools/Tools/PSake/private/CleanupEnvironment.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function CleanupEnvironment {
if ($psake.context.Count -gt 0) {
$currentContext = $psake.context.Peek()
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '')]
$env:PATH = $currentContext.originalEnvPath
Set-Location $currentContext.originalDirectory
$global:ErrorActionPreference = $currentContext.originalErrorActionPreference
$psake.LoadedTaskModules = @{}
$psake.ReferenceTasks = @{}
[void] $psake.context.Pop()
}
}
202 changes: 202 additions & 0 deletions _DevTools/Tools/PSake/private/ConfigureBuildEnvironment.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@

function ConfigureBuildEnvironment {
if (!(Test-Path Variable:\IsWindows) -or $IsWindows) {
$framework = $psake.context.peek().config.framework
if ($framework -cmatch '^((?:\d+\.\d+)(?:\.\d+){0,1})(x86|x64){0,1}$') {
$versionPart = $matches[1]
$bitnessPart = $matches[2]
}
else {
throw ($msgs.error_invalid_framework -f $framework)
}
$versions = $null
$buildToolsVersions = $null
switch ($versionPart) {
'1.0' {
$versions = @('v1.0.3705')
}
'1.1' {
$versions = @('v1.1.4322')
}
'1.1.0' {
$versions = @()
}
'2.0' {
$versions = @('v2.0.50727')
}
'2.0.0' {
$versions = @()
}
'3.0' {
$versions = @('v2.0.50727')
}
'3.5' {
$versions = @('v3.5', 'v2.0.50727')
}
'4.0' {
$versions = @('v4.0.30319')
}
{($_ -eq '4.5') -or ($_ -eq '4.5.1') -or ($_ -eq '4.5.2')} {
$versions = @('v4.0.30319')
$buildToolsVersions = @('16.0', '15.0', '14.0', '12.0')
}
{($_ -eq '4.6') -or ($_ -eq '4.6.1') -or ($_ -eq '4.6.2')} {
$versions = @('v4.0.30319')
$buildToolsVersions = @('16.0', '15.0', '14.0')
}
{($_ -eq '4.7') -or ($_ -eq '4.7.1') -or ($_ -eq '4.7.2')} {
$versions = @('v4.0.30319')
$buildToolsVersions = @('16.0', '15.0')
}
'4.8' {
$versions = @('v4.0.30319')
$buildToolsVersions = @('16.0', '15.0')
}

default {
throw ($msgs.error_unknown_framework -f $versionPart, $framework)
}
}

$bitness = 'Framework'
if ($versionPart -ne '1.0' -and $versionPart -ne '1.1') {
switch ($bitnessPart) {
'x86' {
$bitness = 'Framework'
$buildToolsKey = 'MSBuildToolsPath32'
}
'x64' {
$bitness = 'Framework64'
$buildToolsKey = 'MSBuildToolsPath'
}
{ [string]::IsNullOrEmpty($_) } {
$ptrSize = [System.IntPtr]::Size
switch ($ptrSize) {
4 {
$bitness = 'Framework'
$buildToolsKey = 'MSBuildToolsPath32'
}
8 {
$bitness = 'Framework64'
$buildToolsKey = 'MSBuildToolsPath'
}
default {
throw ($msgs.error_unknown_pointersize -f $ptrSize)
}
}
}
default {
throw ($msgs.error_unknown_bitnesspart -f $bitnessPart, $framework)
}
}
}

$frameworkDirs = @()
if ($null -ne $buildToolsVersions) {
foreach($ver in $buildToolsVersions) {
if ($ver -eq "15.0") {
if ($null -eq (Get-Module -Name VSSetup)) {
if ($null -eq (Get-Module -Name VSSetup -ListAvailable)) {
WriteColoredOutput ($msgs.warning_missing_vsssetup_module -f $ver) -foregroundcolor Yellow
continue
}

Import-Module VSSetup
}

# borrowed from nightroman https://github.com/nightroman/Invoke-Build
if ($vsInstances = Get-VSSetupInstance) {
$vs = @($vsInstances | Select-VSSetupInstance -Version '[15.0, 16.0)' -Require Microsoft.Component.MSBuild)
if ($vs) {
if ($buildToolsKey -eq 'MSBuildToolsPath32') {
$frameworkDirs += Join-Path ($vs[0].InstallationPath) MSBuild\15.0\Bin
}
else {
$frameworkDirs += Join-Path ($vs[0].InstallationPath) MSBuild\15.0\Bin\amd64
}
}

$vs = @($vsInstances | Select-VSSetupInstance -Version '[15.0, 16.0)' -Product Microsoft.VisualStudio.Product.BuildTools)
if ($vs) {
if ($buildToolsKey -eq 'MSBuildToolsPath32') {
$frameworkDirs += Join-Path ($vs[0].InstallationPath) MSBuild\15.0\Bin
}
else {
$frameworkDirs += Join-Path ($vs[0].InstallationPath) MSBuild\15.0\Bin\amd64
}
}
}
else {
if (!($root = ${env:ProgramFiles(x86)})) {$root = $env:ProgramFiles}
if (Test-Path -LiteralPath "$root\Microsoft Visual Studio\2017") {
if ($buildToolsKey -eq 'MSBuildToolsPath32') {
$rp = @(Resolve-Path "$root\Microsoft Visual Studio\2017\*\MSBuild\15.0\Bin" -ErrorAction SilentlyContinue)
}
else {
$rp = @(Resolve-Path "$root\Microsoft Visual Studio\2017\*\MSBuild\15.0\Bin\amd64" -ErrorAction SilentlyContinue)
}

if ($rp) {
$frameworkDirs += $rp[-1].ProviderPath
}
}
}
}
elseif ($ver -eq "16.0") {
if ($null -eq (Get-Module -Name VSSetup)) {
if ($null -eq (Get-Module -Name VSSetup -ListAvailable)) {
WriteColoredOutput ($msgs.warning_missing_vsssetup_module -f $ver) -foregroundcolor Yellow
continue
}

Import-Module VSSetup
}

# borrowed from nightroman https://github.com/nightroman/Invoke-Build
if ($vsInstances = Get-VSSetupInstance) {
$vs = @($vsInstances | Select-VSSetupInstance -Version '[16.0,)' -Require Microsoft.Component.MSBuild)
if ($vs) {
$frameworkDirs += Join-Path ($vs[0].InstallationPath) MSBuild\Current\Bin
}

$vs = @($vsInstances | Select-VSSetupInstance -Version '[16.0,)' -Product Microsoft.VisualStudio.Product.BuildTools)
if ($vs) {
$frameworkDirs += Join-Path ($vs[0].InstallationPath) MSBuild\Current\Bin
}
}
else {
if (!($root = ${env:ProgramFiles(x86)})) {$root = $env:ProgramFiles}
if (Test-Path -LiteralPath "$root\Microsoft Visual Studio\2019") {
$rp = @(Resolve-Path "$root\Microsoft Visual Studio\2019\*\MSBuild\Current\Bin" -ErrorAction SilentlyContinue)
if ($rp) {
$frameworkDirs += $rp[-1].ProviderPath
}
}
}
}
elseif (Test-Path "HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions\$ver") {
$frameworkDirs += (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions\$ver" -Name $buildToolsKey).$buildToolsKey
}
}
}

$frameworkDirs = $frameworkDirs + @($versions | ForEach-Object { "$env:windir\Microsoft.NET\$bitness\$_\" })
for ($i = 0; $i -lt $frameworkDirs.Count; $i++) {
$dir = $frameworkDirs[$i]
if ($dir -Match "\$\(Registry:HKEY_LOCAL_MACHINE(.*?)@(.*)\)") {
$key = "HKLM:" + $matches[1]
$name = $matches[2]
$dir = (Get-ItemProperty -Path $key -Name $name).$name
$frameworkDirs[$i] = $dir
}
}

$frameworkDirs | ForEach-Object { Assert (test-path $_ -pathType Container) ($msgs.error_no_framework_install_dir_found -f $_)}

$env:PATH = ($frameworkDirs -join ";") + ";$env:PATH"
}

# if any error occurs in a PS function then "stop" processing immediately
# this does not effect any external programs that return a non-zero exit code
$global:ErrorActionPreference = "Stop"
}
28 changes: 28 additions & 0 deletions _DevTools/Tools/PSake/private/CreateConfigurationForNewContext.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function CreateConfigurationForNewContext {
param(
[string] $buildFile,
[string] $framework
)

$previousConfig = GetCurrentConfigurationOrDefault

$config = new-object psobject -property @{
buildFileName = $previousConfig.buildFileName;
framework = $previousConfig.framework;
taskNameFormat = $previousConfig.taskNameFormat;
verboseError = $previousConfig.verboseError;
coloredOutput = $previousConfig.coloredOutput;
modules = $previousConfig.modules;
moduleScope = $previousConfig.moduleScope;
}

if ($framework) {
$config.framework = $framework;
}

if ($buildFile) {
$config.buildFileName = $buildFile;
}

return $config
}
58 changes: 58 additions & 0 deletions _DevTools/Tools/PSake/private/ExecuteInBuildFileScope.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
function ExecuteInBuildFileScope {
param([string]$buildFile, $module, [scriptblock]$sb)

# Execute the build file to set up the tasks and defaults
Assert (test-path $buildFile -pathType Leaf) ($msgs.error_build_file_not_found -f $buildFile)

$psake.build_script_file = get-item $buildFile
$psake.build_script_dir = $psake.build_script_file.DirectoryName
$psake.build_success = $false

# Create a new psake context
$psake.context.push(
@{
"buildSetupScriptBlock" = {}
"buildTearDownScriptBlock" = {}
"taskSetupScriptBlock" = {}
"taskTearDownScriptBlock" = {}
"executedTasks" = new-object System.Collections.Stack
"callStack" = new-object System.Collections.Stack
"originalEnvPath" = $env:PATH
"originalDirectory" = get-location
"originalErrorActionPreference" = $global:ErrorActionPreference
"tasks" = @{}
"aliases" = @{}
"properties" = new-object System.Collections.Stack
"includes" = new-object System.Collections.Queue
"config" = CreateConfigurationForNewContext $buildFile $framework
}
)

# Load in the psake configuration (or default)
LoadConfiguration $psake.build_script_dir

set-location $psake.build_script_dir

# Import any modules declared in the build script
LoadModules

$frameworkOldValue = $framework

. $psake.build_script_file.FullName

$currentContext = $psake.context.Peek()

if ($framework -ne $frameworkOldValue) {
writecoloredoutput $msgs.warning_deprecated_framework_variable -foregroundcolor Yellow
$currentContext.config.framework = $framework
}

ConfigureBuildEnvironment

while ($currentContext.includes.Count -gt 0) {
$includeFilename = $currentContext.includes.Dequeue()
. $includeFilename
}

& $sb $currentContext $module
}
26 changes: 26 additions & 0 deletions _DevTools/Tools/PSake/private/FormatErrorMessage.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function FormatErrorMessage
{
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline=$true)]
$ErrorRecord = $Error[0]
)

$currentConfig = GetCurrentConfigurationOrDefault
if ($currentConfig.verboseError) {
$error_message = "{0}: An Error Occurred. See Error Details Below: $($script:nl)" -f (Get-Date)
$error_message += ("-" * 70) + $script:nl
$error_message += "Error: {0}$($script:nl)" -f (ResolveError $ErrorRecord -Short)
$error_message += ("-" * 70) + $script:nl
$error_message += ResolveError $ErrorRecord
$error_message += ("-" * 70) + $script:nl
$error_message += "Script Variables" + $script:nl
$error_message += ("-" * 70) + $script:nl
$error_message += get-variable -scope script | format-table | out-string
} else {
# ($_ | Out-String) gets error messages with source information included.
$error_message = "Error: {0}: $($script:nl){1}" -f (Get-Date), (ResolveError $ErrorRecord -Short)
}

$error_message
}
17 changes: 17 additions & 0 deletions _DevTools/Tools/PSake/private/Get-DefaultBuildFile.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Attempt to find the default build file given the config_default of
# buildFileName and legacyBuildFileName. If neither exist optionally
# return the buildFileName or $null
function Get-DefaultBuildFile {
param(
[boolean] $UseDefaultIfNoneExist = $true
)

if (test-path $psake.config_default.buildFileName -pathType Leaf) {
Write-Output $psake.config_default.buildFileName
} elseif (test-path $psake.config_default.legacyBuildFileName -pathType Leaf) {
Write-Warning "The default configuration file of default.ps1 is deprecated. Please use psakefile.ps1"
Write-Output $psake.config_default.legacyBuildFileName
} elseif ($UseDefaultIfNoneExist) {
Write-Output $psake.config_default.buildFileName
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function GetCurrentConfigurationOrDefault() {
if ($psake.context.count -gt 0) {
return $psake.context.peek().config
} else {
return $psake.config_default
}
}
15 changes: 15 additions & 0 deletions _DevTools/Tools/PSake/private/GetTasksFromContext.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function GetTasksFromContext($currentContext) {

$docs = $currentContext.tasks.Keys | foreach-object {

$task = $currentContext.tasks.$_
new-object PSObject -property @{
Name = $task.Name;
Alias = $task.Alias;
Description = $task.Description;
DependsOn = $task.DependsOn;
}
}

return $docs
}
Loading

0 comments on commit 0d86d43

Please sign in to comment.