Skip to content

Commit 0999201

Browse files
authored
Merge pull request #5491 from markcowl/cmp-managed
Update module PSM1 creation tools to skip modules with a pre-created …
2 parents f870464 + c534de6 commit 0999201

File tree

6 files changed

+49
-48
lines changed

6 files changed

+49
-48
lines changed

src/ResourceManager/Compute.ManagedService/AzureRM.Compute.ManagedService.psd1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
@{
1010

1111
# Script module or binary module file associated with this manifest.
12-
RootModule = '.\ConvertTo-AzureRmVhd.psm1'
12+
RootModule = '.\AzureRM.Compute.ManagedService.psm1'
1313

1414
# Version number of this module.
1515
ModuleVersion = '0.1.0'
@@ -66,13 +66,13 @@ TypesToProcess = @()
6666
FormatsToProcess = @()
6767

6868
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
69-
NestedModules = @('.\Microsoft.Azure.Commands.Compute.ManagedService.dll')
69+
NestedModules = @()
7070

7171
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
72-
FunctionsToExport = @()
72+
FunctionsToExport = 'ConvertTo-AzureRmVhd'
7373

7474
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
75-
CmdletsToExport = @('ConvertTo-AzureRmVhd')
75+
CmdletsToExport = 'ConvertTo-AzureRmVhd'
7676

7777
# Variables to export from this module
7878
# VariablesToExport = @()

src/ResourceManager/Compute.ManagedService/Commands.Compute.ManagedService/ConvertTo-AzureRmVhd.psm1 renamed to src/ResourceManager/Compute.ManagedService/Commands.Compute.ManagedService/AzureRM.Compute.ManagedService.psm1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ This module requires Hyper-V version 1.1. Please follow the instructions below t
1212
1) https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/quick-start/enable-hyper-v
1313
2) https://docs.microsoft.com/en-us/windows-server/virtualization/hyper-v/get-started/install-the-hyper-v-role-on-windows-server
1414
"@;
15-
Write-Error $instructions -ErrorAction Stop;
15+
Write-Error $instructions -ErrorAction Stop
1616
}
1717
else
1818
{
19-
. $PSScriptRoot\Tools\ConvertTo-AzureRmVhd.ps1;
19+
Import-Module Hyper-V -MinimumVersion 1.1 -Scope Global
20+
. $PSScriptRoot\Tools\ConvertTo-AzureRmVhd.ps1
2021
}
21-

src/ResourceManager/Compute.ManagedService/Commands.Compute.ManagedService/Commands.Compute.ManagedService.csproj

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,13 @@
9696
<Project>{3819d8a7-c62c-4c47-8ddd-0332d9ce1252}</Project>
9797
<Name>Commands.ResourceManager.Common</Name>
9898
</ProjectReference>
99-
<ProjectReference Include="..\..\Compute\Commands.Compute\Commands.Compute.csproj">
100-
<Project>{52643bd5-6378-49bd-9f6e-dac9dd8a867b}</Project>
101-
<Name>Commands.Compute</Name>
102-
</ProjectReference>
10399
</ItemGroup>
104100
<ItemGroup>
105101
<None Include="..\AzureRM.Compute.ManagedService.psd1">
106102
<Link>AzureRM.Compute.ManagedService.psd1</Link>
107103
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
108104
</None>
109-
<None Include="ConvertTo-AzureRmVhd.psm1">
105+
<None Include="AzureRM.Compute.ManagedService.psm1">
110106
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
111107
</None>
112108
<None Include="Tools\ConvertTo-AzureRmVhd.ps1">

src/ResourceManager/Compute.ManagedService/Commands.Compute.ManagedService/Tools/ConvertTo-AzureRmVhd.ps1

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,6 @@ function ConvertTo-AzureRmVhd
3838
[switch]$AsJob
3939
)
4040

41-
$module = Get-Module Hyper-V;
42-
if ($module -ne $null -and $module.Version.ToString().CompareTo("1.1") -lt 0)
43-
{
44-
$instructions =
45-
@"
46-
This module requires Hyper-V version 1.1. Please follow the instructions below to enable Hyper-V on your Windows System:
47-
1) https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/quick-start/enable-hyper-v
48-
2) https://docs.microsoft.com/en-us/windows-server/virtualization/hyper-v/get-started/install-the-hyper-v-role-on-windows-server
49-
"@;
50-
Write-Error $instructions -ErrorAction Stop;
51-
}
52-
elseif ($module -eq $null)
53-
{
54-
Import-Module Hyper-V -MinimumVersion 1.1 -Scope Global;
55-
}
56-
5741
if ($AsJob)
5842
{
5943
Start-Job -ScriptBlock ${function:ExecuteCmdlet} -Argumentlist $HyperVVMName,$ExportPath,$HyperVServer,$Force;
@@ -158,7 +142,7 @@ function ExecuteCmdlet($HyperVVMName, $ExportPath, $HyperVServer, $Force)
158142
}
159143
}
160144

161-
if ($PSCmdlet.ShouldProcess($Name, "Converting a Hyper-V VM to Azure supported VHD file(s)..."))
145+
if ($PSCmdlet.ShouldProcess($HyperVVMName, "Converting a Hyper-V VM to Azure supported VHD file(s)..."))
162146
{
163147
$vhdFiles = Export-DiskFiles $HyperVServer $HyperVVMName $ExportPath;
164148

tools/PublishModules.ps1

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ function Update-NugetPackage
160160
}
161161
}
162162

163+
<#
164+
.DESCRIPTION
165+
Creates a module Nuget Package with correct Nuget metadata and module manifest metadata in two parts.
166+
Part 1: Uses Publish-Module to create a .nupkg file with the correct metadata for module installation,
167+
Part 2: Updates the package contents to make module suitable for Import inside PowerShell.
168+
In part 1, psd1 dependencies are used to determine the dependencies in the Nuget package
169+
In part 2, Alters psd1 settings to allow fine-grained control of assembly loading when module is imported
170+
through a generated psm1 file. If a module manifest already has a psm1 module definition in its RootModule property
171+
then the Part 2 processing is skipped.
172+
#>
163173
function Change-RMModule
164174
{
165175
[CmdletBinding()]
@@ -179,6 +189,11 @@ function Change-RMModule
179189
$file = Get-Item $moduleSourcePath
180190
Import-LocalizedData -BindingVariable ModuleMetadata -BaseDirectory $file.DirectoryName -FileName $file.Name
181191
$toss = Publish-Module -Path $Path -Repository $TempRepo -Force
192+
# Create a psm1 and alter psd1 dependencies to allow fine-grained control over assembly loading. Opt out by definitng a RootModule.
193+
if ($ModuleMetadata.RootModule)
194+
{
195+
return
196+
}
182197
Write-Output "Changing to directory for module modifications $TempRepoPath"
183198
$moduleVersion = $ModuleMetadata.ModuleVersion.ToString()
184199
if ($ModuleMetadata.PrivateData.PSData.Prerelease -ne $null)
@@ -189,25 +204,25 @@ function Change-RMModule
189204
pushd $TempRepoPath
190205
try
191206
{
192-
$nupkgPath = Join-Path -Path . -ChildPath ($moduleName + "." + $moduleVersion + ".nupkg")
193-
$zipPath = Join-Path -Path . -ChildPath ($moduleName + "." + $moduleVersion + ".zip")
194-
$dirPath = Join-Path -Path . -ChildPath $moduleName
195-
$unzippedManifest = Join-Path -Path $dirPath -ChildPath ($moduleName + ".psd1")
196-
197-
if (!(Test-Path -Path $nupkgPath))
198-
{
199-
throw "Module at $nupkgPath in $TempRepoPath does not exist"
200-
}
201-
Write-Output "Renaming package $nupkgPath to zip archive $zipPath"
202-
ren $nupkgPath $zipPath
203-
Write-Output "Expanding $zipPath"
204-
Expand-Archive $zipPath -DestinationPath $dirPath
205-
Write-Output "Adding PSM1 dependency to $unzippedManifest"
206-
Write-Output "Removing module manifest dependencies for $unzippedManifest"
207-
Remove-ModuleDependencies -Path $unzippedManifest
208-
Remove-Item -Path $zipPath -Force
209-
Write-Output "Repackaging $dirPath"
210-
Update-NugetPackage -BasePath $TempRepoPath -ModuleName $moduleName -DirPath $dirPath -NugetExe $NugetExe
207+
$nupkgPath = Join-Path -Path . -ChildPath ($moduleName + "." + $moduleVersion + ".nupkg")
208+
$zipPath = Join-Path -Path . -ChildPath ($moduleName + "." + $moduleVersion + ".zip")
209+
$dirPath = Join-Path -Path . -ChildPath $moduleName
210+
$unzippedManifest = Join-Path -Path $dirPath -ChildPath ($moduleName + ".psd1")
211+
212+
if (!(Test-Path -Path $nupkgPath))
213+
{
214+
throw "Module at $nupkgPath in $TempRepoPath does not exist"
215+
}
216+
Write-Output "Renaming package $nupkgPath to zip archive $zipPath"
217+
ren $nupkgPath $zipPath
218+
Write-Output "Expanding $zipPath"
219+
Expand-Archive $zipPath -DestinationPath $dirPath
220+
Write-Output "Adding PSM1 dependency to $unzippedManifest"
221+
Write-Output "Removing module manifest dependencies for $unzippedManifest"
222+
Remove-ModuleDependencies -Path $unzippedManifest
223+
Remove-Item -Path $zipPath -Force
224+
Write-Output "Repackaging $dirPath"
225+
Update-NugetPackage -BasePath $TempRepoPath -ModuleName $moduleName -DirPath $dirPath -NugetExe $NugetExe
211226
}
212227
finally
213228
{

tools/UpdateModules.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ function Create-ModulePsm1
3737
$manifestPath = Join-Path -Path $ModulePath -ChildPath $moduleName
3838
$file = Get-Item $manifestPath
3939
Import-LocalizedData -BindingVariable ModuleMetadata -BaseDirectory $file.DirectoryName -FileName $file.Name
40+
if ($ModuleMetadata.RootModule)
41+
{
42+
# Do not create a psm1 file if the RootModule dependency already has one.
43+
return
44+
}
45+
4046
$templateOutputPath = $manifestPath -replace ".psd1", ".psm1"
4147
[string]$importedModules
4248
if ($ModuleMetadata.RequiredModules -ne $null)

0 commit comments

Comments
 (0)