Skip to content

Commit c1dc410

Browse files
feat(Build-PSModule): ✨ Use extra script content in .psm1 files in built module (#10)
1 parent 1678f1f commit c1dc410

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

build.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
$BuildPSModule = @{
22
Name = 'PSModuleUtils'
3-
Version = '1.6.1'
3+
Version = '1.7.0-preview1'
44
Description = 'A module with helper functions to build and publish PowerShell modules to the PSGallery.'
55
Tags = ('PSEdition_Desktop', 'PSEdition_Core', 'Windows')
66
}

src/public/Build-PSModule.ps1

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ The description of the module.
2121
.PARAMETER Tags
2222
The tags for the module.
2323
24+
.PARAMETER LicenseUri
25+
The URL for the repo's license.
26+
2427
.PARAMETER SourceDirectory
2528
The source directory of the module. Should be a nested directory that doesn't contain and build scripts.
2629
@@ -50,6 +53,7 @@ function Build-PSModule {
5053
[String]$Version = '0.0.1',
5154
[String]$Description = 'A PowerShell module.',
5255
[String[]]$Tags = @('PSEdition_Desktop', 'PSEdition_Core', 'Windows'),
56+
[String]$LicenseUri = 'https://opensource.org/licenses/MIT',
5357
[String]$SourceDirectory = "$PWD/src",
5458
[String]$OutputDirectory = "$PWD/out",
5559
[Switch]$FixScriptAnalyzer
@@ -62,6 +66,7 @@ function Build-PSModule {
6266
Version = $Version
6367
Description = $Description
6468
Tags = $Tags
69+
LicenseUri = $LicenseUri
6570
SourceDirectory = $SourceDirectory
6671
OutputDirectory = $OutputDirectory
6772
FixScriptAnalyzer = $FixScriptAnalyzer
@@ -102,6 +107,15 @@ function Build-PSModule {
102107
$moduleContent += $functionContent
103108
}
104109

110+
$srcModuleContent = Get-Content -Path "$SourceDirectory\$Name.psm1" -Raw
111+
$startIndex = $srcModuleContent.IndexOf('Get-ChildItem')
112+
$subString = $srcModuleContent.Substring($startIndex)
113+
$braceIndex = $subString.IndexOf('}')
114+
$moduleScriptContent = $subString.Substring($braceIndex + 1)
115+
if ($moduleScriptContent) {
116+
$moduleContent += $moduleScriptContent
117+
}
118+
105119
$moduleContent | Set-Content -Path "$ModuleOutputDirectory/$name.psm1" -Force
106120
$null = New-Item -Path "$ModuleOutputDirectory/private" -ItemType Directory -Force
107121
Get-ChildItem -Path "$SourceDirectory/private" -Exclude '*.Tests.ps1' |
@@ -124,7 +138,7 @@ function Build-PSModule {
124138
else {
125139
( New-Guid ).Guid
126140
}
127-
$requiredModulesStatement = Get-Content -Path "$SourceDirectory\$Name.psm1" |
141+
$requiredModulesStatement = $srcModuleContent.Split("`n") |
128142
Where-Object -FilterScript { $_ -match '#Requires' }
129143
$requiredModules = (($requiredModulesStatement -split '-Modules ')[1] -split ',').Trim() |
130144
ForEach-Object {
@@ -137,21 +151,21 @@ function Build-PSModule {
137151
}
138152
$moduleVersion, $modulePrerelease = $Version -split '-', 2
139153
$newModuleManifest = @{
140-
Path = $manifestPath
141-
Author = (( & git log --format='%aN' -- . | Sort-Object -Unique ) -join ', ')
142-
CompanyName = $companyName
143-
Copyright = "(c) $( Get-Date -Format yyyy ) $companyName. All rights reserved."
144-
RootModule = "$Name.psm1"
145-
ModuleVersion = $moduleVersion
146-
Guid = $guid
147-
Description = $Description
148-
PowerShellVersion = 5.1
149-
FunctionsToExport = $functionNames
154+
Path = $manifestPath
155+
Author = (( & git log --format='%aN' -- . | Sort-Object -Unique ) -join ', ')
156+
CompanyName = $companyName
157+
Copyright = "(c) $( Get-Date -Format yyyy ) $companyName. All rights reserved."
158+
RootModule = "$Name.psm1"
159+
ModuleVersion = $moduleVersion
160+
Guid = $guid
161+
Description = $Description
162+
PowerShellVersion = 5.1
163+
FunctionsToExport = $functionNames
150164
CompatiblePSEditions = ('Desktop', 'Core')
151-
Tags = $Tags
152-
ProjectUri = $repoUrl
153-
LicenseUri = 'https://opensource.org/licenses/MIT'
154-
ReleaseNotes = ( git log -1 --pretty=%B )[0]
165+
Tags = $Tags
166+
ProjectUri = $repoUrl
167+
LicenseUri = $LicenseUri
168+
ReleaseNotes = ( git log -1 --pretty=%B )[0]
155169
}
156170
if ($requiredModules) {
157171
$newModuleManifest['RequiredModules'] = $requiredModules

0 commit comments

Comments
 (0)