|
| 1 | +param |
| 2 | +( |
| 3 | + # Directory used to base all relative paths |
| 4 | + [Parameter(Mandatory = $false)] |
| 5 | + [string] $BaseDirectory = ".\", |
| 6 | + # |
| 7 | + [Parameter(Mandatory = $false)] |
| 8 | + [string] $OutputDirectory = ".\build\release\", |
| 9 | + # |
| 10 | + [Parameter(Mandatory = $false)] |
| 11 | + [string] $SourceDirectory = ".\src\powershell\", |
| 12 | + # |
| 13 | + [Parameter(Mandatory = $false)] |
| 14 | + [string] $ModuleManifestPath, |
| 15 | + # |
| 16 | + [Parameter(Mandatory = $false)] |
| 17 | + [string] $PackagesConfigPath = ".\packages.config", |
| 18 | + # |
| 19 | + [Parameter(Mandatory = $false)] |
| 20 | + [string] $PackagesDirectory = ".\build\packages", |
| 21 | + # |
| 22 | + [Parameter(Mandatory = $false)] |
| 23 | + [string] $LicensePath = ".\LICENSE", |
| 24 | + # |
| 25 | + [Parameter(Mandatory = $false)] |
| 26 | + [switch] $SkipMergingNestedModuleScripts, |
| 27 | + |
| 28 | + # If true, builds the module for production, otherwise builds a preview module that is installed with -AllowPrerelease |
| 29 | + [Parameter()] |
| 30 | + [switch]$ProductionBuild |
| 31 | +) |
| 32 | + |
| 33 | +## Initialize |
| 34 | +Import-Module "$PSScriptRoot\CommonFunctions.psm1" -Force -WarningAction SilentlyContinue -ErrorAction Stop |
| 35 | + |
| 36 | +## Increment the build number |
| 37 | +&$PSScriptRoot\Set-Version.ps1 -preview:(!$ProductionBuild) |
| 38 | + |
| 39 | +[System.IO.DirectoryInfo] $BaseDirectoryInfo = Get-PathInfo $BaseDirectory -InputPathType Directory -ErrorAction Stop |
| 40 | +[System.IO.DirectoryInfo] $OutputDirectoryInfo = Get-PathInfo $OutputDirectory -InputPathType Directory -DefaultDirectory $BaseDirectoryInfo.FullName -ErrorAction SilentlyContinue |
| 41 | +[System.IO.DirectoryInfo] $SourceDirectoryInfo = Get-PathInfo $SourceDirectory -InputPathType Directory -DefaultDirectory $BaseDirectoryInfo.FullName -ErrorAction Stop |
| 42 | +[System.IO.FileInfo] $ModuleManifestFileInfo = Get-PathInfo $ModuleManifestPath -DefaultDirectory $SourceDirectoryInfo.FullName -DefaultFilename "*.psd1" -ErrorAction Stop |
| 43 | +[System.IO.FileInfo] $PackagesConfigFileInfo = Get-PathInfo $PackagesConfigPath -DefaultDirectory $BaseDirectoryInfo.FullName -DefaultFilename "packages.config" -ErrorAction SilentlyContinue |
| 44 | +[System.IO.DirectoryInfo] $PackagesDirectoryInfo = Get-PathInfo $PackagesDirectory -InputPathType Directory -DefaultDirectory $BaseDirectoryInfo.FullName -ErrorAction SilentlyContinue |
| 45 | +[System.IO.FileInfo] $LicenseFileInfo = Get-PathInfo $LicensePath -DefaultDirectory $BaseDirectoryInfo.FullName -DefaultFilename "LICENSE" -ErrorAction SilentlyContinue |
| 46 | + |
| 47 | +## Read Module Manifest |
| 48 | +$ModuleManifest = Import-PowerShellDataFile $ModuleManifestFileInfo.FullName |
| 49 | +[System.IO.DirectoryInfo] $ModuleOutputDirectoryInfo = Join-Path $OutputDirectoryInfo.FullName (Join-Path $ModuleManifestFileInfo.BaseName $ModuleManifest['ModuleVersion']) |
| 50 | +[System.IO.FileInfo] $OutputModuleManifestFileInfo = Join-Path $ModuleOutputDirectoryInfo.FullName $ModuleManifestFileInfo.Name |
| 51 | + |
| 52 | +## Copy Source Module Code to Module Output Directory |
| 53 | +Assert-DirectoryExists $ModuleOutputDirectoryInfo -ErrorAction Stop | Out-Null |
| 54 | +Copy-Item ("{0}\*" -f $SourceDirectoryInfo.FullName) -Destination $ModuleOutputDirectoryInfo.FullName -Recurse -Force |
| 55 | + |
| 56 | +if (!$SkipMergingNestedModuleScripts) { |
| 57 | + [System.IO.FileInfo] $OutputRootModuleFileInfo = (Join-Path $ModuleOutputDirectoryInfo.FullName $ModuleManifest['RootModule']) |
| 58 | + &$PSScriptRoot\Merge-PSModuleNestedModuleScripts.ps1 -ModuleManifestPath $OutputModuleManifestFileInfo.FullName -OutputModulePath $OutputRootModuleFileInfo.FullName -MergeWithRootModule -RemoveNestedModuleScriptFiles |
| 59 | +} |
| 60 | +if ($LicenseFileInfo.Exists) { |
| 61 | + Copy-Item $LicenseFileInfo.FullName -Destination (Join-Path $ModuleOutputDirectoryInfo.FullName License.txt) -Force |
| 62 | +} |
| 63 | + |
| 64 | +if ($PackagesConfigFileInfo.Exists) { |
| 65 | + ## NuGet Restore |
| 66 | + &$PSScriptRoot\Restore-NugetPackages.ps1 -PackagesConfigPath $PackagesConfigFileInfo.FullName -OutputDirectory $PackagesDirectoryInfo.FullName |
| 67 | + |
| 68 | + ## Read Packages Configuration |
| 69 | + $xmlPackagesConfig = New-Object xml |
| 70 | + $xmlPackagesConfig.Load($PackagesConfigFileInfo.FullName) |
| 71 | + |
| 72 | + ## Copy Packages to Module Output Directory |
| 73 | + foreach ($package in $xmlPackagesConfig.packages.package) { |
| 74 | + [string[]] $targetFrameworks = $package.targetFramework |
| 75 | + if (!$targetFrameworks) { [string[]] $targetFrameworks = "net45", "netcoreapp2.1" } |
| 76 | + foreach ($targetFramework in $targetFrameworks) { |
| 77 | + [System.IO.DirectoryInfo] $PackageDirectory = Join-Path $PackagesDirectoryInfo.FullName ("{0}.{1}\lib\{2}" -f $package.id, $package.version, $targetFramework) |
| 78 | + [System.IO.DirectoryInfo] $PackageOutputDirectory = "{0}\{1}.{2}\{3}" -f $ModuleOutputDirectoryInfo.FullName, $package.id, $package.version, $targetFramework |
| 79 | + $PackageOutputDirectory |
| 80 | + Assert-DirectoryExists $PackageOutputDirectory -ErrorAction Stop | Out-Null |
| 81 | + Copy-Item ("{0}\*" -f $PackageDirectory) -Destination $PackageOutputDirectory.FullName -Recurse -Force |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +## Get Module Output FileList |
| 87 | +#$ModuleFileListFileInfo = Get-ChildItem $ModuleOutputDirectoryInfo.FullName -Recurse -File |
| 88 | +#$ModuleManifestOutputFileInfo = $ModuleFileListFileInfo | Where-Object Name -EQ $ModuleManifestFileInfo.Name |
| 89 | + |
| 90 | +## Update Module Manifest in Module Output Directory |
| 91 | +&$PSScriptRoot\Update-PSModuleManifest.ps1 -ModuleManifestPath $OutputModuleManifestFileInfo.FullName |
| 92 | +if (!$SkipMergingNestedModuleScripts) { |
| 93 | + &$PSScriptRoot\Add-PSModuleHeader.ps1 -ModuleManifestPath $OutputModuleManifestFileInfo.FullName |
| 94 | +} |
| 95 | + |
| 96 | +Write-Host "Module built successfully: $($OutputModuleManifestFileInfo.FullName)" -ForegroundColor Green |
| 97 | +## Sign Module |
| 98 | +#&$PSScriptRoot\Sign-PSModule.ps1 -ModuleManifestPath $OutputModuleManifestFileInfo.FullName | Format-Table Path, Status, StatusMessage |
0 commit comments