@@ -66,15 +66,42 @@ function New-Bootstrap {
6666
6767 # Get starter module
6868 $starterModulePath = " "
69+ $starterRootModuleFolder = " "
70+ $starterRootModuleFolderPath = " "
71+ $starterFoldersToRetain = @ ()
6972
7073 if ($hasStarter ) {
7174 if ($inputConfig.starter_module_name -eq " " ) {
7275 $inputConfig.starter_module_name = Request-SpecialInput - type " starter" - starterConfig $starterConfig
7376 }
7477
78+ $chosenStarterConfig = $starterConfig.starter_modules .$ ($inputConfig.starter_module_name )
79+
7580 Write-Verbose " Selected Starter: $ ( $inputConfig.starter_module_name ) )"
76- $starterModulePath = (Resolve-Path (Join-Path - Path $starterPath - ChildPath $starterConfig.starter_modules .$ ($inputConfig.starter_module_name ).location)).Path
81+ $starterModulePath = (Resolve-Path (Join-Path - Path $starterPath - ChildPath $chosenStarterConfig.location )).Path
82+ $starterRootModuleFolderPath = $starterModulePath
7783 Write-Verbose " Starter Module Path: $starterModulePath "
84+
85+ if ($chosenStarterConfig.PSObject.Properties.Name -contains " additional_retained_folders" ) {
86+ $starterFoldersToRetain = $chosenStarterConfig.additional_retained_folders
87+ Write-Verbose " Starter Additional folders to retain: $ ( $starterFoldersToRetain -join " ," ) "
88+ }
89+
90+ if ($chosenStarterConfig.PSObject.Properties.Name -contains " root_module_folder" ) {
91+ $starterRootModuleFolder = $chosenStarterConfig.root_module_folder
92+
93+ # Retain the root module folder
94+ $starterFoldersToRetain += $starterRootModuleFolder
95+
96+ # Add the root module folder to bootstrap input config
97+ $inputConfig | Add-Member - NotePropertyName " root_module_folder_relative_path" - NotePropertyValue $starterRootModuleFolder
98+
99+ # Set the starter root module folder full path
100+ $starterRootModuleFolderPath = Join-Path - Path $starterModulePath - ChildPath $starterRootModuleFolder
101+
102+ Write-Verbose " Starter root module folder: $starterRootModuleFolder "
103+ Write-Verbose " Starter final folders to retain: $ ( $starterFoldersToRetain -join " ," ) "
104+ }
78105 }
79106
80107 # Getting configuration for the bootstrap module user input
@@ -92,7 +119,7 @@ function New-Bootstrap {
92119 if ($hasStarter ) {
93120 Write-Verbose " Getting the starter configuration..."
94121 if ($iac -eq " terraform" ) {
95- $terraformFiles = Get-ChildItem - Path $starterModulePath - Filter " *.tf" - File
122+ $terraformFiles = Get-ChildItem - Path $starterRootModuleFolderPath - Filter " *.tf" - File
96123 foreach ($terraformFile in $terraformFiles ) {
97124 $starterParameters = Convert-HCLVariablesToInputConfig - targetVariableFile $terraformFile.FullName - hclParserToolPath $hclParserToolPath - validators $validationConfig - appendToObject $starterParameters
98125 }
@@ -140,13 +167,26 @@ function New-Bootstrap {
140167 # Creating the tfvars files for the bootstrap and starter module
141168 $tfVarsFileName = " terraform.tfvars.json"
142169 $bootstrapTfvarsPath = Join-Path - Path $bootstrapModulePath - ChildPath $tfVarsFileName
143- $starterTfvarsPath = Join-Path - Path $starterModulePath - ChildPath " terraform.tfvars.json"
170+ $starterTfvarsPath = Join-Path - Path $starterRootModuleFolderPath - ChildPath " terraform.tfvars.json"
144171 $starterBicepVarsPath = Join-Path - Path $starterModulePath - ChildPath " parameters.json"
145172
146173 # Write the tfvars file for the bootstrap and starter module
147174 Write-TfvarsJsonFile - tfvarsFilePath $bootstrapTfvarsPath - configuration $bootstrapConfiguration
148175
149176 if ($iac -eq " terraform" ) {
177+ if ($starterFoldersToRetain.Length -gt 0 ) {
178+ Write-Verbose " Removing unwanted folders from the starter module..."
179+ $folders = Get-ChildItem - Path $starterModulePath - Directory
180+ foreach ($folder in $folders ) {
181+ if ($starterFoldersToRetain -notcontains $folder.Name ) {
182+ Write-Verbose " Removing folder: $ ( $folder.FullName ) "
183+ Remove-Item - Path $folder.FullName - Recurse - Force
184+ } else {
185+ Write-Verbose " Retaining folder: $ ( $folder.FullName ) "
186+ Remove-TerraformMetaFileSet - path $folder.FullName - writeVerboseLogs:$writeVerboseLogs.IsPresent
187+ }
188+ }
189+ }
150190 Remove-TerraformMetaFileSet - path $starterModulePath - writeVerboseLogs:$writeVerboseLogs.IsPresent
151191 Write-TfvarsJsonFile - tfvarsFilePath $starterTfvarsPath - configuration $starterConfiguration
152192 }
0 commit comments