Skip to content

Commit 4afe2f5

Browse files
Condensed the code for this cscript
1 parent a181b7d commit 4afe2f5

File tree

1 file changed

+28
-54
lines changed

1 file changed

+28
-54
lines changed

createpowershellmodule.ps1

Lines changed: 28 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,38 @@ param (
55
[string]$Imports,
66
[string]$Debug = 'false'
77
)
8-
try
9-
{
8+
9+
try {
1010
Write-Host "::group::Starting the Create PowerShell Module task..."
1111
Write-Host "::group::Setting up variables"
1212

13-
[bool]$Debug = [System.Convert]::ToBoolean($Debug)
14-
15-
if ([string]::IsNullOrEmpty($Source))
16-
{
17-
$sourcePath = "$($env:GITHUB_WORKSPACE)"
18-
}
19-
else
20-
{
21-
$sourcePath = "$($env:GITHUB_WORKSPACE)\$($Source)"
22-
}
13+
$Debug = [System.Convert]::ToBoolean($Debug)
2314

24-
if ([string]::IsNullOrEmpty($Output))
25-
{
26-
$outputPath = "$($env:GITHUB_WORKSPACE)\output"
27-
}
28-
else
29-
{
30-
$outputPath = "$($env:GITHUB_WORKSPACE)\$($Output)"
31-
}
15+
$sourcePath = if ([string]::IsNullOrEmpty($Source)) { $env:GITHUB_WORKSPACE } else { Join-Path $env:GITHUB_WORKSPACE $Source }
16+
$outputPath = if ([string]::IsNullOrEmpty($Output)) { Join-Path $env:GITHUB_WORKSPACE "output" } else { Join-Path $env:GITHUB_WORKSPACE $Output }
3217

33-
$Module = Get-ChildItem -Path $SourcePath -Filter "$($ModuleName).psd1" -Recurse
18+
$Module = Get-ChildItem -Path $sourcePath -Filter "$ModuleName.psd1" -Recurse
3419
$ModuleRoot = $Module.Directory.FullName
35-
$Destination = "$($outputPath)\$($ModuleName)"
36-
$modulePath = "$($Destination)\$($ModuleName).psm1"
20+
$Destination = Join-Path $outputPath $ModuleName
21+
$modulePath = Join-Path $Destination "$ModuleName.psm1"
3722

38-
if ($Debug)
39-
{
40-
Write-Host "ModuleName : $($ModuleName)"
41-
Write-Host "SourcePath : $($sourcePath)"
42-
Write-Host "OutputPath : $($outputPath)"
43-
Write-Host "Destination : $($Destination)"
44-
Write-Host "ManifestPath : $($ManifestPath)"
45-
Write-Host "ModuleRoot : $($ModuleRoot)"
46-
Write-Host "Imports : $($imports)"
23+
if ($Debug) {
24+
Write-Host "ModuleName : $ModuleName"
25+
Write-Host "SourcePath : $sourcePath"
26+
Write-Host "OutputPath : $outputPath"
27+
Write-Host "Destination : $Destination"
28+
Write-Host "ModuleRoot : $ModuleRoot"
29+
Write-Host "Imports : $Imports"
4730
}
4831

4932
$stringbuilder = [System.Text.StringBuilder]::new()
50-
$importFolders = $imports.Split(',')
33+
$importFolders = $Imports.Split(',')
5134

5235
Write-Host "::endgroup::"
5336

5437
Write-Host "::group::Copying Manifest"
5538

56-
if ($Debug)
57-
{
39+
if ($Debug) {
5840
Write-Host "Testing Output"
5941
}
6042

@@ -63,42 +45,34 @@ try
6345
}
6446

6547
Write-Host "::group::Processing import folders..."
66-
foreach ($importFolder in $importFolders)
67-
{
68-
Write-Host "Importing from [$($ModuleRoot)\$($importFolder)]"
69-
if ($Debug)
70-
{
48+
foreach ($importFolder in $importFolders) {
49+
Write-Host "Importing from [$ModuleRoot\$importFolder]"
50+
if ($Debug) {
7151
Write-Host "Testing ImportFolder"
7252
}
73-
if (Test-Path "$ModuleRoot\$importFolder")
74-
{
75-
$fileList = Get-ChildItem "$($ModuleRoot)\$($importFolder)\*.ps1" -Exclude "*.Tests.ps1"
76-
foreach ($file in $fileList)
77-
{
53+
if (Test-Path -Path (Join-Path $ModuleRoot $importFolder)) {
54+
$fileList = Get-ChildItem -Path (Join-Path $ModuleRoot $importFolder) -Filter "*.ps1" -Exclude "*.Tests.ps1"
55+
foreach ($file in $fileList) {
7856
Write-Host " Importing [.$($file.BaseName)]"
79-
if ($Debug)
80-
{
57+
if ($Debug) {
8158
Write-Host "Reading file: $file.FullName"
8259
}
8360
$stringbuilder.AppendLine("# .$($file.BaseName)") | Out-Null
8461
$stringbuilder.AppendLine([System.IO.File]::ReadAllText($file.FullName)) | Out-Null
8562
}
86-
}
87-
else
88-
{
63+
} else {
8964
Write-Host "##[warning]Folder $importFolder not found at $ModuleRoot"
9065
}
9166
}
9267
Write-Host "::endgroup::"
9368

94-
Write-Host "::group::Creating module [$($modulePath)]"
69+
Write-Host "::group::Creating module [$modulePath]"
9570
Set-Content -Path $modulePath -Value $stringbuilder.ToString()
9671
Write-Host "BuildModule task completed successfully."
9772
Write-Host "::endgroup::"
9873
Write-Host "::endgroup::"
9974
}
100-
catch
101-
{
75+
catch {
10276
Write-Host "##[error]An error occurred: $($_.Exception.Message)"
10377
exit 1
104-
}
78+
}

0 commit comments

Comments
 (0)