Skip to content

Commit cee760b

Browse files
authored
[ENG] Fix namespace collision between src and generated submodule (#28196)
1 parent 5c45b3b commit cee760b

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/Az.autorest.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
<LangVersion>7.1</LangVersion>
1414
<TargetFramework>netstandard2.0</TargetFramework>
1515
<AssemblyName>Az.$(PsModuleName).private</AssemblyName>
16-
<RootNamespace>$(AzAssemblyPrefix)$(PsModuleName)</RootNamespace>
1716
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
1817
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
1918
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>

tools/BuildScripts/BuildScripts.psm1

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,33 @@ function Update-GeneratedSubModule {
178178
if ($csprojName -match "^Az\.(?<cSubModuleName>\w+)\.csproj$") {
179179
$cSubModuleNameTrimmed = $Matches["cSubModuleName"]
180180
}
181+
182+
# Get namespace from csproj file
183+
$csprojPath = Join-Path $SourceDirectory $csprojName
184+
Write-Host "Reading namespace from csproj: $csprojPath" -ForegroundColor DarkGreen
185+
$namespace = $null
186+
if (Test-Path $csprojPath) {
187+
try {
188+
[xml]$csprojContent = Get-Content $csprojPath
189+
$rootNamespaceNode = $csprojContent.Project.PropertyGroup.RootNamespace
190+
if ($rootNamespaceNode) {
191+
$namespace = "$rootNamespaceNode".Trim()
192+
Write-Host "Loading namespace from csproj: $namespace retrieved" -ForegroundColor Green
193+
}
194+
else {
195+
Write-Warning "RootNamespace not found in csproj file"
196+
}
197+
}
198+
catch {
199+
Write-Warning "Failed to read namespace from csproj: $($_.Exception.Message)"
200+
}
201+
}
202+
else {
203+
Write-Warning "Csproj file not found at path: $csprojPath"
204+
}
205+
181206
# regenerate csproj
182-
New-GeneratedFileFromTemplate -TemplateName 'Az.ModuleName.csproj' -GeneratedFileName $csprojName -GeneratedDirectory $GeneratedDirectory -ModuleRootName $ModuleRootName -SubModuleName $cSubModuleNameTrimmed -SubModuleNameFull $SubModuleName
207+
New-GeneratedFileFromTemplate -TemplateName 'Az.ModuleName.csproj' -GeneratedFileName $csprojName -GeneratedDirectory $GeneratedDirectory -ModuleRootName $ModuleRootName -SubModuleName $cSubModuleNameTrimmed -SubModuleNameFull $SubModuleName -Namespace $namespace
183208

184209
# revert guid in psd1 so that no conflict in updating this file
185210
if ($guid) {
@@ -209,7 +234,9 @@ function New-GeneratedFileFromTemplate {
209234
[string]
210235
$SubModuleName,
211236
[string]
212-
$SubModuleNameFull
237+
$SubModuleNameFull,
238+
[string]
239+
$Namespace
213240
)
214241
$TemplatePath = Join-Path $PSScriptRoot "Templates"
215242
$templateFile = Join-Path $TemplatePath $TemplateName
@@ -228,6 +255,9 @@ function New-GeneratedFileFromTemplate {
228255
$templateFile = $templateFile -replace '{ModuleFolderPlaceHolder}', "$SubModuleName.Autorest"
229256
}
230257
$templateFile = $templateFile -replace '{RootModuleNamePlaceHolder}', $ModuleRootName
258+
if ($Namespace) {
259+
$templateFile = $templateFile -replace '{NamespacePlaceHolder}', $Namespace
260+
}
231261
Write-Host "Copying template: $TemplateName." -ForegroundColor Yellow
232262
$templateFile | Set-Content $GeneratedFile -force
233263
}

tools/BuildScripts/Templates/Az.ModuleName.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PsModuleName>{ModuleNamePlaceHolder}</PsModuleName>
44
<PsRootModuleName>{RootModuleNamePlaceHolder}</PsRootModuleName>
55
<PsModuleFolder>{ModuleFolderPlaceHolder}</PsModuleFolder>
6+
<RootNamespace>{NamespacePlaceHolder}</RootNamespace>
67
</PropertyGroup>
78

89
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory).., build.proj))\src\Az.autorest.props" />

0 commit comments

Comments
 (0)