Skip to content

Commit 07a043e

Browse files
Refactoring module architecture
1 parent bc846c4 commit 07a043e

21 files changed

+282
-98
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ sh.exe.stackdump
2525

2626
# ignore the TestResults
2727
TestResults/*
28+
29+
# ignore the build & publishing Directories
30+
publish/*
31+
Templates_Build/*
Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1-
$script:PSModuleRoot = $PSScriptRoot
2-
$script:PSModuleVersion = (Import-PowerShellDataFile -Path "$($script:PSModuleRoot)\PSModuleDevelopment.psd1").ModuleVersion
1+
$script:ModuleRoot = $PSScriptRoot
2+
$script:ModuleVersion = (Import-PowerShellDataFile -Path "$($script:ModuleRoot)\PSModuleDevelopment.psd1").ModuleVersion
33

4-
$script:doDotSource = $false
5-
if (Get-PSFConfigValue -FullName PSModuleDevelopment.Import.DoDotSource) { $script:doDotSource = $true }
4+
# Detect whether at some level dotsourcing was enforced
5+
$script:doDotSource = Get-PSFConfigValue -FullName PSModuleDevelopment.Import.DoDotSource -Fallback $false
6+
if ($PSModuleDevelopment_dotsourcemodule) { $script:doDotSource = $true }
67

7-
#region Helper function
8-
function Import-PSMDFile
8+
<#
9+
Note on Resolve-Path:
10+
All paths are sent through Resolve-Path/Resolve-PSFPath in order to convert them to the correct path separator.
11+
This allows ignoring path separators throughout the import sequence, which could otherwise cause trouble depending on OS.
12+
Resolve-Path can only be used for paths that already exist, Resolve-PSFPath can accept that the last leaf my not exist.
13+
This is important when testing for paths.
14+
#>
15+
16+
# Detect whether at some level loading individual module files, rather than the compiled module was enforced
17+
$importIndividualFiles = Get-PSFConfigValue -FullName PSModuleDevelopment.Import.IndividualFiles -Fallback $false
18+
if ($PSModuleDevelopment_importIndividualFiles) { $importIndividualFiles = $true }
19+
if (Test-Path (Resolve-PSFPath -Path "$($script:ModuleRoot)\..\.git" -SingleItem -NewChild)) { $importIndividualFiles = $true }
20+
if ("<was not compiled>" -eq '<was not compiled>') { $importIndividualFiles = $true }
21+
22+
function Import-ModuleFile
923
{
1024
<#
1125
.SYNOPSIS
@@ -14,44 +28,54 @@ function Import-PSMDFile
1428
.DESCRIPTION
1529
This helper function is used during module initialization.
1630
It should always be dotsourced itself, in order to proper function.
31+
32+
This provides a central location to react to files being imported, if later desired
1733
1834
.PARAMETER Path
1935
The path to the file to load
2036
2137
.EXAMPLE
22-
PS C:\> . Import-PSMDFile -File $function.FullName
38+
PS C:\> . Import-ModuleFile -File $function.FullName
2339
2440
Imports the file stored in $function according to import policy
2541
#>
2642
[CmdletBinding()]
27-
Param (
43+
param (
2844
[string]
2945
$Path
3046
)
3147

3248
$resolvedPath = $ExecutionContext.SessionState.Path.GetResolvedPSPathFromPSPath($Path).ProviderPath
33-
if ($script:doDotSource) { . $resolvedPath }
49+
if ($doDotSource) { . $resolvedPath }
3450
else { $ExecutionContext.InvokeCommand.InvokeScript($false, ([scriptblock]::Create([io.file]::ReadAllText($resolvedPath))), $null, $null) }
3551
}
36-
#endregion Helper function
37-
3852

39-
# Perform Actions before loading the rest of the content
40-
. Import-PSMDFile -Path "$PSModuleRoot\internal\scripts\preload.ps1"
41-
42-
#region Load internal functions
43-
foreach ($function in (Get-ChildItem "$PSModuleRoot\internal\functions" -Recurse -File -Filter "*.ps1"))
53+
#region Load individual files
54+
if ($importIndividualFiles)
4455
{
45-
. Import-PSMDFile -Path $function.FullName
46-
}
47-
#endregion Load internal functions
48-
49-
#region Load functions
50-
foreach ($function in (Get-ChildItem "$PSModuleRoot\functions" -Recurse -File -Filter "*.ps1"))
51-
{
52-
. Import-PSMDFile -Path $function.FullName
56+
# Execute Preimport actions
57+
. Import-ModuleFile -Path "$ModuleRoot\internal\scripts\preimport.ps1"
58+
59+
# Import all internal functions
60+
foreach ($function in (Get-ChildItem "$ModuleRoot\internal\functions" -Filter "*.ps1" -Recurse -ErrorAction Ignore))
61+
{
62+
. Import-ModuleFile -Path $function.FullName
63+
}
64+
65+
# Import all public functions
66+
foreach ($function in (Get-ChildItem "$ModuleRoot\functions" -Filter "*.ps1" -Recurse -ErrorAction Ignore))
67+
{
68+
. Import-ModuleFile -Path $function.FullName
69+
}
70+
71+
# Execute Postimport actions
72+
. Import-ModuleFile -Path "$ModuleRoot\internal\scripts\postimport.ps1"
73+
74+
# End it here, do not load compiled code below
75+
return
5376
}
54-
#endregion Load functions
77+
#endregion Load individual files
5578

56-
# Perform Actions after loading the module contents
57-
. Import-PSMDFile -Path "$PSModuleRoot\internal\scripts\postload.ps1"
79+
#region Load compiled code
80+
"<compile code into here>"
81+
#endregion Load compiled code

PSModuleDevelopment/changelog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# Changelog
22
##
33
- New: Export-PSMDString - Parses strings from modules using the PSFramework localization feature.
4+
- Upd: Template PSFProject - Adding `-IncludAZ` switch parameter to `vsts-packageFunction.ps1`, making the template include the AZ module as managed dependency.
5+
- Upd: Refactored module structure to comply with current Fred Reference Architecture
6+
- Fix: Template PSFProject - Publish Folder created during build is created using `-Force`
7+
- Fix: Template PSFProject - Cleaning up Azure Function conversion
8+
- Fix: Template PSFTests - Encoding test no longer fails on core (#104)
9+
- Fix: Template PSFTests - Referenced DLLs from GAC will fail as path cannot be found (#100)
10+
- Fix: Template Module - RootModule | 3-element version | Module Import from UNC path
11+
- Fix: Template-System - Bad default template store path on linux or mac. (#106)
412

513
## 2.2.6.72 (May 27th, 2019)
614
- New: Template AzureFunction - Creates a basic azure function scaffold

PSModuleDevelopment/functions/moduledebug/Import-PSMDModuleDebug.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
{
3838
[System.Management.Automation.ScriptBlock]::Create($____module.PreImportAction).Invoke()
3939
}
40-
Import-Module -Name $____module.Name
40+
Import-Module -Name $____module.Name -Scope Global
4141
if ($____module.PostImportAction)
4242
{
4343
[System.Management.Automation.ScriptBlock]::Create($____module.PostImportAction).Invoke()
Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
# The parameter identifier used to detect and insert parameters
1+
if ($IsLinux -or $IsMacOs)
2+
{
3+
# Defaults to the first value in $Env:XDG_CONFIG_DIRS on Linux or MacOS (or $HOME/.local/share/)
4+
$fileUserShared = @($Env:XDG_CONFIG_DIRS -split ([IO.Path]::PathSeparator))[0]
5+
if (-not $fileUserShared) { $fileUserShared = Join-Path $HOME .local/share/ }
6+
7+
$path_FileUserShared = Join-Path (Join-Path $fileUserShared $psVersionName) "PSFramework/"
8+
}
9+
else
10+
{
11+
# Defaults to $Env:AppData on Windows
12+
$path_FileUserShared = Join-Path $Env:AppData "$psVersionName\PSFramework\Config"
13+
if (-not $Env:AppData) { $path_FileUserShared = Join-Path ([Environment]::GetFolderPath("ApplicationData")) "$psVersionName\PSFramework\Config" }
14+
}
15+
16+
# The parameter identifier used to detect and insert parameters
217
Set-PSFConfig -Module 'PSModuleDevelopment' -Name 'Template.Identifier' -Value 'þ' -Initialize -Validation 'string' -Description "The identifier used by the template system to detect and insert variables / scriptblock values"
318

419
# The default values for common parameters
@@ -9,8 +24,8 @@ Set-PSFConfig -Module 'PSModuleDevelopment' -Name 'Template.ParameterDefault.Com
924
Set-PSFConfig -Module 'PSModuleDevelopment' -Name 'Template.BinaryExtensions' -Value @('.dll', '.exe', '.pdf', '.doc', '.docx', '.xls', '.xlsx') -Initialize -Description "When creating a template, files with these extensions will be included as raw bytes and not interpreted for parameter insertion."
1025

1126
# Define the default store. To add more stores, just add a similar setting with a different last name segment
12-
Set-PSFConfig -Module 'PSModuleDevelopment' -Name 'Template.Store.Default' -Value "$env:APPDATA\WindowsPowerShell\PSModuleDevelopment\Templates" -Initialize -Validation "string" -Description "Path to the default directory where PSModuleDevelopment will store its templates. You can add additional stores by creating the same setting again, only changing the last name segment to a new name and configuring a separate path."
13-
Set-PSFConfig -Module 'PSModuleDevelopment' -Name 'Template.Store.PSModuleDevelopment' -Value "$script:PSModuleRoot\internal\templates" -Initialize -Validation "string" -Description "Path to the templates shipped in PSModuleDevelopment"
27+
Set-PSFConfig -Module 'PSModuleDevelopment' -Name 'Template.Store.Default' -Value "$path_FileUserShared\WindowsPowerShell\PSModuleDevelopment\Templates" -Initialize -Validation "string" -Description "Path to the default directory where PSModuleDevelopment will store its templates. You can add additional stores by creating the same setting again, only changing the last name segment to a new name and configuring a separate path."
28+
Set-PSFConfig -Module 'PSModuleDevelopment' -Name 'Template.Store.PSModuleDevelopment' -Value "$script:ModuleRoot\internal\templates" -Initialize -Validation "string" -Description "Path to the templates shipped in PSModuleDevelopment"
1429

1530
# Define the default path to create from templates in
1631
Set-PSFConfig -Module 'PSModuleDevelopment' -Name 'Template.OutPath' -Value '.' -Initialize -Validation 'string' -Description "The path where new files & projects should be created from templates by default."
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Set-PSFConfig -Module PSModuleDevelopment -Name 'Wix.profile.path' -Value "$env:APPDATA\WindowsPowerShell\PSModuleDevelopment\Wix" -Initialize -Validation "string" -Handler { } -Description "The path where the wix commands store and look for msi building profiles by default."
2-
Set-PSFConfig -Module PSModuleDevelopment -Name 'Wix.profile.default' -Value " " -Initialize -Validation "string" -Handler { } -Description "The default profile to build. If this is specified, 'Invoke-PSMDWixBuild' will build this profile when nothing else is specified."
1+
#Set-PSFConfig -Module PSModuleDevelopment -Name 'Wix.profile.path' -Value "$env:APPDATA\WindowsPowerShell\PSModuleDevelopment\Wix" -Initialize -Validation "string" -Handler { } -Description "The path where the wix commands store and look for msi building profiles by default."
2+
#Set-PSFConfig -Module PSModuleDevelopment -Name 'Wix.profile.default' -Value " " -Initialize -Validation "string" -Handler { } -Description "The default profile to build. If this is specified, 'Invoke-PSMDWixBuild' will build this profile when nothing else is specified."

PSModuleDevelopment/internal/scripts/preload.ps1 renamed to PSModuleDevelopment/internal/scripts/initialize.ps1

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,4 @@
1-
2-
#region Configuration
3-
<#
4-
Usually configuration is imported after most of the module has been imported.
5-
This module is an exception to this, since a significant amount of its tasks are performed on import.
6-
#>
7-
8-
foreach ($file in (Get-ChildItem -Path "$script:PSModuleRoot\internal\configurations"))
9-
{
10-
. Import-PSMDFile -Path $file.FullName
11-
}
12-
#endregion Configuration
13-
14-
# Load strings
15-
. Import-PSMDFile -Path "$script:PSModuleRoot\internal\scripts\strings.ps1"
16-
17-
#region Ensure Config path exists
1+
#region Ensure Config path exists
182

193
# If there is no global override for the config path, use module default path
204
# Note: Generally, you shouldn't enforce checking on the global scope for variables. Since importing modules however is a global action, this is an exception

PSModuleDevelopment/internal/scripts/license.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
New-PSFLicense -Product 'PSModuleDevelopment' -Manufacturer 'Friedrich Weinmann' -ProductVersion $script:PSModuleVersion -ProductType Module -Name MIT -Version "1.0.0.0" -Date (Get-Date -Year 2017 -Month 04 -Day 27 -Hour 0 -Minute 0 -Second 0) -Text @"
1+
New-PSFLicense -Product 'PSModuleDevelopment' -Manufacturer 'Friedrich Weinmann' -ProductVersion $script:ModuleVersion -ProductType Module -Name MIT -Version "1.0.0.0" -Date (Get-Date "2017-04-27") -Text @"
22
Copyright (c) 2017 Friedrich Weinmann
33
44
Permission is hereby granted, free of charge, to any person obtaining a copy
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
$__modules = Get-PSMDModuleDebug | Sort-Object Priority
2+
3+
foreach ($__module in $__modules)
4+
{
5+
if ($__module.AutoImport)
6+
{
7+
try { . Import-PSMDModuleDebug -Name $__module.Name -ErrorAction Stop }
8+
catch { Write-PSFMessage -Level Warning -Message "Failed to import Module: $($__module.Name)" -Tag import -ErrorRecord $_ -Target $__module.Name }
9+
}
10+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Add all things you want to run after importing the main code
2+
3+
# Load Scriptblocks
4+
foreach ($file in (Get-ChildItem "$($script:ModuleRoot)\internal\scriptblocks\*.ps1" -ErrorAction Ignore))
5+
{
6+
. Import-ModuleFile -Path $file.FullName
7+
}
8+
9+
# Load Tab Expansion
10+
foreach ($file in (Get-ChildItem "$($script:ModuleRoot)\internal\tepp\*.tepp.ps1" -ErrorAction Ignore))
11+
{
12+
. Import-ModuleFile -Path $file.FullName
13+
}
14+
15+
# Load Tab Expansion Assignment
16+
. Import-ModuleFile -Path "$($script:ModuleRoot)\internal\tepp\assignment.ps1"
17+
18+
# Load Maintenance tasks
19+
foreach ($file in (Get-ChildItem "$($script:ModuleRoot)\internal\maintenance\*.ps1" -ErrorAction Ignore))
20+
{
21+
. Import-ModuleFile -Path $file.FullName
22+
}
23+
24+
# Load License
25+
. Import-ModuleFile -Path "$($script:ModuleRoot)\internal\scripts\license.ps1"
26+
27+
# Load Modules set for debug-import on PSMD import
28+
. Import-ModuleFile -Path "$($script:ModuleRoot)\internal\scripts\moduledebug.ps1"

0 commit comments

Comments
 (0)