Init-TestRepository.ps1 (1883B)
1 # Copyright (c) Microsoft Corporation. 2 # Licensed under the MIT License. 3 [CmdletBinding()] 4 param( 5 [string]$ModulesPath, 6 7 [string]$RepositoryPath, 8 9 [string]$RepositoryName, 10 11 [switch]$Force 12 ) 13 14 if ([System.String]::IsNullOrEmpty($ModulesPath)) 15 { 16 $ModulesPath = Join-Path $PSScriptRoot "Modules" 17 } 18 19 if ([System.String]::IsNullOrEmpty($RepositoryPath)) 20 { 21 $RepositoryPath = Join-Path ([System.IO.Path]::GetTempPath()) (New-Guid) 22 } 23 24 if ([System.String]::IsNullOrEmpty($RepositoryName)) 25 { 26 $RepositoryName = "AppInstallerCLIE2ETestsRepo" 27 } 28 29 if ($Force) { 30 $null = New-Item -Path $RepositoryPath -ItemType Directory -Force 31 } else { 32 $null = New-Item -Path $RepositoryPath -ItemType Directory -ErrorAction Inquire 33 } 34 35 $Local:existingRepository = Get-PSRepository -Name $RepositoryName -ErrorAction Ignore 36 if ($Local:existingRepository) 37 { 38 if ($Force) 39 { 40 Unregister-PSRepository -Name $RepositoryName 41 } 42 else 43 { 44 throw "Repository named $RepositoryName is already registered. Use -Force to overwrite it." 45 } 46 } 47 48 $null = Register-PSRepository -Name $RepositoryName -SourceLocation $RepositoryPath -ScriptSourceLocation $RepositoryPath 49 50 $Local:allItems = Get-ChildItem $ModulesPath 51 52 $Local:progressActivity = "Publishing modules to $RepositoryPath" 53 Write-Progress -Activity $Local:progressActivity 54 55 [Int32]$Local:modulesPublished = 0 56 57 $Local:allItems | ForEach-Object -Process { 58 $Local:modulePath = $_.FullName 59 Write-Verbose "Publishing $Local:modulePath" 60 Publish-Module -Path $Local:modulePath -Repository $RepositoryName -Force 61 $Local:modulesPublished += 1 62 Write-Progress -Activity $Local:progressActivity -PercentComplete (($Local:modulesPublished * 100) / $Local:allItems.Count) 63 } 64 65 Write-Progress -Activity $Local:progressActivity -Completed