Skip to content

Context aware Invoke-PSMDTemplate? #161

Description

@trondr

First of all. Thanks for a template system that feels right! Now to the question. Is it feasible to make Invoke-PSMDTemplate command aware of where in the folder structure the command is run? I would like to be able to override default values set higher up in the folder structure. I could not find this feature already implemented, but I might have overlooked it? If it is not already available, an proposed implementation could be the following inserted on line 143 of Invoke-PSMDTemplate.ps1. This implementation is based on static key, value pairs. An improved version could maybe utilize the same dynamic mechanisms used by PSMDTemplate.ps1?

        # Import context aware configuration parameters by processing PSMDConfig.ps1 in all parent directories 
        # all the way to the top from current directory. The config file in the current directory have priority over config files 
        # further up the tree. Each config file have a dictionary defined that stores key value pairs.
        #Example: 
        #   Top Directory: PSMDConfig.ps1:
        #   $PSMDConfig = @{
        #       "Author" = "Ola Hansen"
        #       "Company" = "work.com"
        #   }
        #   Parent Directory: PSMDConfig.ps1:
        #   $PSMDConfig = @{
        #       "Company" = "customer.com"
        #       "NameSpace" = "App1"
        #   }
        #   Current Directory: PSMDConfig.ps1:
        #   $PSMDConfig = @{            
        #       "NameSpace" = "App1.Library"
        #   }
        #   Resultant config for current directory after processing the PSMDConfig.ps1 files further up:
        #   $PSMDConfig = @{
        #       "Author" = "Ola Hansen"
        #       "Company" = "customer.com"
        #       "NameSpace" = "App1.Library"
        #   }
        #  The resultant config will be will be added to 'Template.ParameterDefault.*' with the Set-PSFConfig command.
        function Get-ParentDirectory
        {
            param(
                [System.IO.DirectoryInfo]
                $Directory
            )
            if($null -ne $Directory.Parent)
            {
                Write-Output -InputObject $Directory.FullName
                Get-ParentDirectory -Directory $($Directory.Parent)
            }
        }        
        $currentDirectory = [System.IO.DirectoryInfo]::new((Get-Location).Path)
        Get-ParentDirectory -Directory $currentDirectory | Sort-Object | ForEach-Object{
            $folderPath = $_
            $psmdConfigPath = [System.IO.Path]::Combine($folderPath,"PSMDConfig.ps1")
            $psmdConfigPath
        } | Where-Object { Test-Path -Path $_} | Foreach-Object {
            $psmdConfigPath = $_
            . $psmdConfigPath 
            #$PSMDConfig variable is now set, run through it and overwrite coresponding values in Template.ParameterDefault.*
            foreach ($key in $PSMDConfig.Keys) {                
                Set-PSFConfig -Module 'PSModuleDevelopment' -Name "Template.ParameterDefault.$($key)" -Value $($PSMDConfig[$key])
            }
        }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions