Merged PR 5667: Add function to create a new nuget.config file (#8170)

Add function to create a new nuget.config file (#8170)

Add a function to create a NuGet.config file to consume packages  from the specified feed URL using the specified credentials.

  - Also, exclude the `nuget.config` file under `src/Modules` from being clean by `Start-PSBuild -clean`

# Conflicts:
#	build.psm1
This commit is contained in:
Travis Plunk
2018-11-06 01:26:46 +00:00
committed by Aditya Patwardhan
parent 68557bce6a
commit ea3c0168a3
+104 -1
View File
@@ -472,7 +472,8 @@ function Start-PSBuild {
Push-Location $PSScriptRoot
try {
# Excluded sqlite3 folder is due to this Roslyn issue: https://github.com/dotnet/roslyn/issues/23060
git clean -fdx --exclude .vs/PowerShell/v15/Server/sqlite3
# Excluded src/Modules/nuget.config as this is required for release build.
git clean -fdx --exclude .vs/PowerShell/v15/Server/sqlite3 --exclude src/Modules/nuget.config
} finally {
Pop-Location
}
@@ -3243,3 +3244,105 @@ $script:RESX_TEMPLATE = @'
{0}
</root>
'@
function New-TestPackage
{
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string] $Destination
)
if (Test-Path $Destination -PathType Leaf)
{
throw "Destination: '$Destination' is not a directory or does not exist."
}
else
{
$null = New-Item -Path $Destination -ItemType Directory -Force
Write-Verbose -Message "Creating destination folder: $Destination"
}
$rootFolder = $env:TEMP
if (-not $rootFolder -and $env:TF_BUILD) {
$rootFolder = $env:AGENT_WORKFOLDER
}
Write-Verbose -Verbose "RootFolder: $rootFolder"
$packageRoot = Join-Path $rootFolder ('TestPackage-' + (new-guid))
$null = New-Item -ItemType Directory -Path $packageRoot -Force
$packagePath = Join-Path $Destination "TestPackage.zip"
Write-Verbose -Verbose "PackagePath: $packagePath"
# Build test tools so they are placed in appropriate folders under 'test' then copy to package root.
$null = Publish-PSTestTools
$powerShellTestRoot = Join-Path $PSScriptRoot 'test'
Copy-Item $powerShellTestRoot -Recurse -Destination $packageRoot -Force
Write-Verbose -Message "Copied test directory"
# Copy assests folder to package root for wix related tests.
$assetsPath = Join-Path $PSScriptRoot 'assets'
Copy-Item $assetsPath -Recurse -Destination $packageRoot -Force
Write-Verbose -Message "Copied assests directory"
# Create expected folder structure for resx files in package root.
$srcRootForResx = New-Item -Path "$packageRoot/src" -Force -ItemType Directory
$resourceDirectories = Get-ChildItem -Recurse "$PSScriptRoot/src" -Directory -Filter 'resources'
$resourceDirectories | ForEach-Object {
$directoryFullName = $_.FullName
$partToRemove = Join-Path $PSScriptRoot "src"
$assemblyPart = $directoryFullName.Replace($partToRemove, '')
$assemblyPart = $assemblyPart.TrimStart([io.path]::DirectorySeparatorChar)
$resxDestPath = Join-Path $srcRootForResx $assemblyPart
$null = New-Item -Path $resxDestPath -Force -ItemType Directory
Write-Verbose -Message "Created resx directory : $resxDestPath"
Copy-Item -Path "$directoryFullName\*" -Recurse $resxDestPath -Force
}
Add-Type -AssemblyName System.IO.Compression.FileSystem
if(Test-Path $packagePath)
{
Remove-Item -Path $packagePath -Force
}
[System.IO.Compression.ZipFile]::CreateFromDirectory($packageRoot, $packagePath)
}
function New-NugetConfigFile
{
param(
[Parameter(Mandatory=$true)] [string] $NugetFeedUrl,
[Parameter(Mandatory=$true)] [string] $FeedName,
[Parameter(Mandatory=$true)] [string] $UserName,
[Parameter(Mandatory=$true)] [string] $ClearTextPAT,
[Parameter(Mandatory=$true)] [string] $Destination
)
$nugetConfigTemplate = @'
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="[FEEDNAME]" value="[FEED]" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
<packageSourceCredentials>
<[FEEDNAME]>
<add key="Username" value="[USERNAME]" />
<add key="ClearTextPassword" value="[PASSWORD]" />
</[FEEDNAME]>
</packageSourceCredentials>
</configuration>
'@
$content = $nugetConfigTemplate.Replace('[FEED]', $NugetFeedUrl).Replace('[FEEDNAME]', $FeedName).Replace('[USERNAME]', $UserName).Replace('[PASSWORD]', $ClearTextPAT)
Set-Content -Path (Join-Path $Destination 'nuget.config') -Value $content -Force
}