Skip to content

Update build tools for new module defintion #2888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion build.proj
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,12 @@
<GetFrameworkSdkPath>
<Output TaskParameter="Path" PropertyName="WindowsSdkPath"/>
</GetFrameworkSdkPath>

<Exec Command="$(PowerShellCommand) -NonInteractive -NoLogo -NoProfile -Command &quot;. $(LibraryToolsFolder)\UpdateModules.ps1 $(Configuration) $(Scope) &quot;"/>
<!-- Copying shortcut to be signed -->
<Copy SourceFiles="$(LibraryRoot)tools\AzureRM\AzureRM.psd1"
DestinationFolder="$(LibrarySourceFolder)\Package\$(Configuration)" />
<Copy SourceFiles="$(LibraryRoot)tools\AzureRM\AzureRM.psm1"
DestinationFolder="$(LibrarySourceFolder)\Package\$(Configuration)" />

<ItemGroup>
<DelaySignedAssembliesToSign Include="$(LibrarySourceFolder)\Package\$(Configuration)\**\Microsoft*Azure*Commands*.dll" />
Expand Down Expand Up @@ -263,6 +265,8 @@
<!-- Copying signed shortcut back -->
<Copy SourceFiles="$(LibrarySourceFolder)\Package\$(Configuration)\AzureRM.psd1"
DestinationFolder="$(LibraryRoot)tools\AzureRM" />
<Copy SourceFiles="$(LibrarySourceFolder)\Package\$(Configuration)\AzureRM.psm1"
DestinationFolder="$(LibraryRoot)tools\AzureRM" />
</Target>

<Target Name="CodeSignInstaller">
Expand Down
Binary file added tools/AzureRM.Example.psm1
Binary file not shown.
285 changes: 237 additions & 48 deletions tools/PublishModules.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,208 @@ param(
[Parameter(Mandatory = $false, Position = 2)]
[string] $apiKey,
[Parameter(Mandatory = $false, Position = 3)]
[string] $repositoryLocation
[string] $repositoryLocation,
[Parameter(Mandatory = $false, Position = 4)]
[string] $nugetExe
)

function Get-TargetModules
{
[CmdletBinding()]
param
(
[string]$buildConfig,
[string]$Scope,
[bool]$PublishLocal
)

PROCESS
{
$targets = @()
$packageFolder = "$PSScriptRoot\..\src\Package"
$resourceManagerRootFolder = "$packageFolder\$buildConfig\ResourceManager\AzureResourceManager"
if (($Scope -eq 'All') -or $PublishLocal ) {
$targets += "$resourceManagerRootFolder\AzureRM.Profile"
}

if (($Scope -eq 'All') -or ($Scope -eq 'AzureStorage')) {
$targets += "$packageFolder\$buildConfig\Storage\Azure.Storage"
}

if (($Scope -eq 'All') -or ($Scope -eq 'ServiceManagement')) {
$targets += "$packageFolder\$buildConfig\ServiceManagement\Azure"
}

$resourceManagerModules = Get-ChildItem -Path $resourceManagerRootFolder -Directory
if ($Scope -eq 'All') {
foreach ($module in $resourceManagerModules) {
# filter out AzureRM.Profile which always gets published first
# And "Azure.Storage" which is built out as test dependencies
if (($module.Name -ne "AzureRM.Profile") -and ($module.Name -ne "Azure.Storage")) {
$targets += $module.FullName
}
}
} elseif (($Scope -ne 'AzureRM') -and ($Scope -ne "ServiceManagement") -and ($Scope -ne "AzureStorage")) {
$modulePath = Join-Path $resourceManagerRootFolder "AzureRM.$scope"
if (Test-Path $modulePath) {
$targets += $modulePath
} else {
Write-Error "Can not find module with name $scope to publish"
}
}

if (($Scope -eq 'All') -or ($Scope -eq 'AzureRM')) {
# Publish AzureRM module
$targets += "$PSScriptRoot\AzureRM"
}

Write-Output -InputObject $targets
}
}

function Make-StrictModuleDependencies
{
[CmdletBinding()]
param(
[string] $Path)

PROCESS
{
$manifest = Test-ModuleManifest -Path $Path
$newModules = @()
foreach ($module in $manifest.RequiredModules)
{
$newModules += (@{ModuleName = $module.Name; RequiredVersion= $module.Version})
}

if ($newModules.Count -gt 0)
{
Update-ModuleManifest -Path $Path -RequiredModules $newModules
}

}

}

function Add-PSM1Dependency
{
[CmdletBinding()]
param(
[string] $Path)

PROCESS
{
$file = Get-Item -Path $Path
$manifestFile = $file.Name
$psm1file = $manifestFile -replace ".psd1", ".psm1"
$manifest = Test-ModuleManifest -Path $Path
Update-ModuleManifest -Path $Path -RootModule $psm1file
}

}


function Remove-ModuleDependencies
{
[CmdletBinding()]
param(
[string] $Path)

PROCESS
{
$regex = New-Object System.Text.RegularExpressions.Regex "RequiredModules\s*=\s*@\([^\)]+\)"
$content = (Get-Content -Path $Path) -join "`r`n"
$text = $regex.Replace($content, "RequiredModules = @()")
$text | Out-File -FilePath $Path

}

}

function Change-RMModule
{
[CmdletBinding()]
param(
[string]$Path,
[string]$RepoLocation,
[string]$TempRepo,
[string]$TempRepoPath
)

PROCESS
{
$moduleName = (Get-Item -Path $Path).Name
$moduleManifest = $moduleName + ".psd1"
$moduleSourcePath = Join-Path -Path $Path -ChildPath $moduleManifest
$manifest = Make-StrictModuleDependencies $moduleSourcePath
$manifest = Test-ModuleManifest -Path $moduleSourcePath
$toss = Publish-Module -Path $Path -Repository $TempRepo
Write-Output "Changing to directory for module modifications $TempRepoPath"
pushd $TempRepoPath
try
{
$nupkgPath = Join-Path -Path . -ChildPath ($moduleName + "." + $manifest.Version.ToString() + ".nupkg")
$zipPath = Join-Path -Path . -ChildPath ($moduleName + "." + $manifest.Version.ToString() + ".zip")
$dirPath = Join-Path -Path . -ChildPath ($moduleName + "." + $manifest.Version.ToString())
$unzippedManifest = Join-Path -Path $dirPath -ChildPath ($moduleName + ".psd1")

if (!(Test-Path -Path $nupkgPath))
{
throw "Module at $nupkgPath in $TempRepoPath does not exist"
}
Write-Output "Renaming package $nupkgPath to nsip archive $zipPath"
ren $nupkgPath $zipPath
Write-Output "Expanding $zipPath"
Expand-Archive $zipPath
Write-Output "Adding PSM1 dependency to $unzippedManifest"
Add-PSM1Dependency -Path $unzippedManifest
Write-Output "Removing module manifest dependencies for $unzippedManifest"
Remove-ModuleDependencies -Path $unzippedManifest

Remove-Item -Path $zipPath -Force
Write-Output "Compressing $zipPath"
Compress-Archive (Join-Path -Path $dirPath -ChildPath "*") -DestinationPath $zipPath
Write-Output "Renaming package $zipPath to zip archive $nupkgPath"
ren $zipPath $nupkgPath
}
finally
{
popd
}
}
}

function Publish-RMModule
{
[CmdletBinding()]
param(
[string]$Path,
[string]$ApiKey,
[string]$TempRepoPath,
[string]$RepoLocation,
[string]$nugetExe
)

PROCESS
{
$moduleName = (Get-Item -Path $Path).Name
$moduleManifest = $moduleName + ".psd1"
$moduleSourcePath = Join-Path -Path $Path -ChildPath $moduleManifest
$manifest = Test-ModuleManifest -Path $moduleSourcePath
$nupkgPath = Join-Path -Path $TempRepoPath -ChildPath ($moduleName + "." + $manifest.Version.ToString() + ".nupkg")
if (!(Test-Path -Path $nupkgPath))
{
throw "Module at $nupkgPath in $TempRepoPath does not exist"
}

Write-Output "Pushing package $moduleName to nuget source $RepoLocation"
&$nugetExe push $nupkgPath $ApiKey -s $RepoLocation
Write-Output "Pushed package $moduleName to nuget source $RepoLocation"
}
}



if ([string]::IsNullOrEmpty($buildConfig))
{
Write-Verbose "Setting build configuration to 'Release'"
Expand All @@ -41,6 +240,12 @@ if ([string]::IsNullOrEmpty($scope))
$scope = 'All'
}

if ([string]::IsNullOrEmpty($nugetExe))
{
Write-Verbose "Use default nuget path"
$nugetExe = "$PSScriptRoot\nuget.exe"
}

Write-Host "Publishing $scope package(and its dependencies)"

$packageFolder = "$PSScriptRoot\..\src\Package"
Expand All @@ -53,56 +258,40 @@ if ($repo -ne $null) {
Register-PSRepository -Name $repoName -SourceLocation $repositoryLocation -PublishLocation $repositoryLocation/package -InstallationPolicy Trusted
}

$resourceManagerRootFolder = "$packageFolder\$buildConfig\ResourceManager\AzureResourceManager"
$publishToLocal = test-path $repositoryLocation
if (($scope -eq 'All') -or $publishToLocal ) {
# If we publish 'All' or to local folder, publish AzureRM.Profile first, becasue it is the common dependency
Write-Host "Publishing profile module"
Publish-Module -Path "$resourceManagerRootFolder\AzureRM.Profile" -NuGetApiKey $apiKey -Repository $repoName -Tags ("Azure")
Write-Host "Published profile module"
[string]$tempRepoPath = "$PSScriptRoot\..\src\package"
if ($publishToLocal)
{
$tempRepoPath = (Join-Path $repositoryLocation -ChildPath "package")
}
$tempRepoName = ([System.Guid]::NewGuid()).ToString()
Register-PSRepository -Name $tempRepoName -SourceLocation $tempRepoPath -PublishLocation $tempRepoPath -InstallationPolicy Trusted -PackageManagementProvider NuGet

if (($scope -eq 'All') -or ($scope -eq 'AzureStorage')) {
$modulePath = "$packageFolder\$buildConfig\Storage\Azure.Storage"
# Publish AzureStorage module
Write-Host "Publishing AzureStorage module from $modulePath"
Publish-Module -Path $modulePath -NuGetApiKey $apiKey -Repository $repoName -Tags ("Azure")
}

if (($scope -eq 'All') -or ($scope -eq 'ServiceManagement')) {
$modulePath = "$packageFolder\$buildConfig\ServiceManagement\Azure"
# Publish Azure module
Write-Host "Publishing ServiceManagement(aka Azure) module from $modulePath"
Publish-Module -Path $modulePath -NuGetApiKey $apiKey -Repository $repoName -Tags ("Azure")
}

$resourceManagerModules = Get-ChildItem -Path $resourceManagerRootFolder -Directory
if ($scope -eq 'All') {
foreach ($module in $resourceManagerModules) {
try {
$modulesInScope = Get-TargetModules -buildConfig $buildConfig -Scope $scope -PublishLocal $publishToLocal
foreach ($modulePath in $modulesInScope) {
# filter out AzureRM.Profile which always gets published first
# And "Azure.Storage" which is built out as test dependencies
if (($module.Name -ne "AzureRM.Profile") -and ($module.Name -ne "Azure.Storage")) {
$modulePath = $module.FullName
Write-Host "Publishing $module module from $modulePath"
Publish-Module -Path $modulePath -NuGetApiKey $apiKey -Repository $repoName -Tags ("Azure")
Write-Host "Published $module module"
}
}
} elseif ($scope -ne 'AzureRM') {
$modulePath = Join-Path $resourceManagerRootFolder "AzureRM.$scope"
if (Test-Path $modulePath) {
Write-Host "Publishing $scope module from $modulePath"
Publish-Module -Path $modulePath -NuGetApiKey $apiKey -Repository $repoName -Tags ("Azure")
Write-Host "Published $scope module"
} else {
Write-Error "Can not find module with name $scope to publish"
}
}
$module = Get-Item -Path $modulePath
Write-Host "Changing $module module from $modulePath"
Change-RMModule -Path $modulePath -RepoLocation $repositoryLocation -TempRepo $tempRepoName -TempRepoPath $tempRepoPath
Write-Host "Changed $module module"
}

if (!$publishToLocal)
{
foreach ($modulePath in $modulesInScope) {
# filter out AzureRM.Profile which always gets published first
# And "Azure.Storage" which is built out as test dependencies
$module = Get-Item -Path $modulePath
Write-Host "Pushing $module module from $modulePath"
Publish-RMModule -Path $modulePath -ApiKey $apiKey -TempRepoPath $tempRepoPath -RepoLocation $repositoryLocation -nugetExe $nugetExe
Write-Host "Pushed $module module"
}
}

if (($scope -eq 'All') -or ($scope -eq 'AzureRM')) {
# Publish AzureRM module
$modulePath = "$PSScriptRoot\AzureRM"
Write-Host "Publishing AzureRM module from $modulePath"
Publish-Module -Path $modulePath -NuGetApiKey $apiKey -Repository $repoName -Tags ("Azure")
Write-Host "Published Azure module"
}
}
finally
{
Unregister-PSRepository -Name $tempRepoName
}
Loading