Skip to content

Commit

Permalink
Fixed self-building process (#5111)
Browse files Browse the repository at this point in the history
* Fixed self-building process

* fixed indents
  • Loading branch information
FriedrichWeinmann authored and wsmelton committed Feb 23, 2019
1 parent 5b387ff commit 0b4123a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
45 changes: 23 additions & 22 deletions bin/build-project.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ param (
[string]$ProjectPath = (Resolve-Path -Path (Join-Path -Path $PSModuleRoot -ChildPath 'bin\projects\dbatools\dbatools.sln')),
[ValidateSet('ps3', 'ps4', 'Release', 'Debug')]
[string]$MsbuildConfiguration = "Release",
[string]$MsbuildOptions = "",
[Parameter(HelpMessage = 'Target to run instead of build')]
[string]$MsbuildTarget = 'Build'
[string]$MsbuildTarget = 'Build',
[string]$DllRoot = $script:DllRoot,
[string]$LibraryBase = (Join-Path $PSModuleRoot "bin")
)

if (-not $PSBoundParameters.ContainsKey('MsbuildConfiguration')) {
Expand All @@ -24,53 +25,53 @@ if (-not $PSBoundParameters.ContainsKey('MsbuildConfiguration')) {
$_MsbuildConfiguration = $MsbuildConfiguration
}

function Get-MsBuildPath {
function Get-DotNetPath {
[CmdletBinding()]
[OutputType([string])]
param ()
process {
$rawPath = "$(Split-Path ([string].Assembly.Location))\msbuild.exe"
(Resolve-Path $rawPath).Path
if (Get-Command dotnet.exe -CommandType Application -ErrorAction SilentlyContinue) { return (Get-Command dotnet.exe -CommandType Application).Source }
"$($env:ProgramFiles)\dotnet\dotnet.exe"
}
}

$start = Get-Date
$msbuild = Get-MsBuildPath
$dotnet = Get-DotNetPath

if (-not (Test-Path $msbuild)) {
throw "msbuild not found, cannot compile library! Check your .NET installation health, then try again. Path checked: $msbuild"
if (-not (Test-Path $dotnet)) {
throw "dotnet application not found, cannot compile library! Download and install the dotnet SDK from https://dotnet.microsoft.com/download"
}

if ($env:APPVEYOR -eq 'True') {
$MsbuildOptions = $MsbuildOptions + '/logger:"C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll" '
$_MsbuildConfiguration = 'Debug'

if (-not (Test-Path "C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll")) {
throw "msbuild logger not found, cannot compile library! Check your .NET installation health, then try again. Path checked: $msbuild"
}
}

#$MsbuildOptions = $MsbuildOptions + '/logger:BinaryLogger,"{0}";"{1}" ' -f (Join-Path $PSScriptRoot 'StructuredLogger.dll'), (Resolve-Path '..\msbuild.bin.log').Path

if (-not (Test-Path $ProjectPath)) {
throw new-object 'System.IO.FileNotFoundException' 'Could not file project or solution', $ProjectPath
}

Write-Verbose -Message "Building the library with command $msbuild $ProjectPath /p:Configuration=$_MsbuildConfiguration $MsbuildOptions /t:$MsBuildTarget"
& $msbuild $ProjectPath "/p:Configuration=$_MsbuildConfiguration" $MsbuildOptions "/t:$MsBuildTarget"
Write-Verbose -Message "Building the library with command $dotnet build $ProjectPath -c $_MsbuildConfiguration"
& $dotnet build $ProjectPath -c $_MsbuildConfiguration

if ($MsbuildTarget -eq 'Build') {
if ($MsbuildTarget -eq 'Build')
{
if ($PSVersionTable.PSVersion.Major -ge 6) {
$dll = (Resolve-Path -Path "$libraryBase\netcoreapp2.1\dbatools.dll" -ErrorAction Ignore)
}
else {
$dll = (Resolve-Path -Path "$libraryBase\net452\dbatools.dll" -ErrorAction Ignore)
}
try {
Write-Verbose -Message "Found library, trying to copy & import"
if ($script:alwaysBuildLibrary) {
Move-Item -Path (Resolve-Path -Path "$PSModuleRoot\bin\dbatools.dll") -Destination $script:DllRoot -Force -ErrorAction Stop
Move-Item -Path $dll -Destination $DllRoot -Force -ErrorAction Stop
} else {
Copy-Item -Path (Resolve-Path -Path "$PSModuleRoot\bin\dbatools.dll") -Destination $script:DllRoot -Force -ErrorAction Stop
Copy-Item -Path $dll -Destination $DllRoot -Force -ErrorAction Stop
}
Add-Type -Path (Resolve-Path -Path "$PSModuleRoot\dbatools.dll") -ErrorAction Stop
Add-Type -Path (Resolve-Path -Path "$DllRoot\dbatools.dll") -ErrorAction Stop
} catch {
Write-Verbose -Message "Failed to copy & import, attempting to import straight from the module directory"
Add-Type -Path (Resolve-Path -Path "$PSModuleRoot\bin\dbatools.dll") -ErrorAction Stop
Add-Type -Path $dll -ErrorAction Stop
}
Write-Verbose -Message "Total duration: $((Get-Date) - $start)"
}
5 changes: 1 addition & 4 deletions bin/library.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,10 @@ if ($ImportLibrary) {
$hasCompiledDll = Test-Path -Path $dll -ErrorAction Stop
}

$reslibdll = Resolve-Path -Path "$libraryBase\dbatools.dll"

if ((-not $script:alwaysBuildLibrary) -and $hasCompiledDll -and ([System.Diagnostics.FileVersionInfo]::GetVersionInfo($reslibdll).FileVersion -eq $currentLibraryVersion)) {
if ((-not $script:alwaysBuildLibrary) -and $hasCompiledDll -and ([System.Diagnostics.FileVersionInfo]::GetVersionInfo($dll).FileVersion -eq $currentLibraryVersion)) {
$start = Get-Date

try {
$libraryBase = Resolve-Path -Path "$libraryBase\"
$script:DllRoot = Resolve-Path -Path $script:DllRoot
Write-Verbose -Message "Found library, trying to copy & import"
# this looks excessive but for some reason the explicit string to string is required
Expand Down

0 comments on commit 0b4123a

Please sign in to comment.