PSPublishModule.psm1
function Add-Directory { [CmdletBinding()] param( [string] $Directory ) $exists = Test-Path -Path $Directory if ($exists -eq $false) { $null = New-Item -Path $Directory -ItemType Directory -Force #$Directory } } function Add-FilesWithFolders { [CmdletBinding()] param ($file, $FullProjectPath, $directory) $LinkPrivatePublicFiles = foreach ($dir in $directory) { if ($file -like "$dir*") { $file #Write-Verbose "Adding file to linking list of files $file" # Write-Color 'Adding file to ', 'linking list', ' of files ', $path -Color White, Yellow, White, Yellow } } $LinkPrivatePublicFiles } function Copy-File { [CmdletBinding()] param ( $Source, $Destination ) if ((Test-Path $Source) -and !(Test-Path $Destination)) { Copy-Item -Path $Source -Destination $Destination } } function Export-PSData { [cmdletbinding()] <# .Synopsis Exports property bags into a data file .Description Exports property bags and the first level of any other object into a ps data file (.psd1) .Link https://github.com/StartAutomating/Pipeworks Import-PSData .Example Get-Web -Url http://www.youtube.com/watch?v=xPRC3EDR_GU -AsMicrodata -ItemType http://schema.org/VideoObject | Export-PSData .\PipeworksQuickstart.video.psd1 #> [OutputType([IO.FileInfo])] param( # The data that will be exported [Parameter(Mandatory = $true, ValueFromPipeline = $true)] [PSObject[]] $InputObject, # The path to the data file [Parameter(Mandatory = $true, Position = 0)] [string] $DataFile ) begin { $AllObjects = New-Object Collections.ArrayList } process { $null = $AllObjects.AddRange($InputObject) } end { #region Convert to Hashtables and export $text = $AllObjects | Write-PowerShellHashtable $text | Set-Content -Path $DataFile Get-Item -Path $DataFile #endregion Convert to Hashtables and export } } function Find-EnumsList { [CmdletBinding()] param ( [string] $ProjectPath ) if ($PSEdition -eq 'Core') { $Enums = Get-ChildItem -Path $ProjectPath\Enums\*.ps1 -ErrorAction SilentlyContinue -FollowSymlink } else { $Enums = Get-ChildItem -Path $ProjectPath\Enums\*.ps1 -ErrorAction SilentlyContinue } $EnumsList = @( $Files = Foreach ($import in @($Enums)) { "'Enums\$($import.Name)'" } $Files -join ',' ) return [string] "@($EnumsList)" } function Find-RequiredModules { param( [string] $Name ) $Module = Get-Module -ListAvailable $Name -ErrorAction SilentlyContinue -Verbose:$false $AllModules = if ($Module) { [Array] $RequiredModules = $Module.RequiredModules.Name if ($null -ne $RequiredModules) { $null } $RequiredModules foreach ($_ in $RequiredModules) { Find-RequiredModules -Path $Path -Name $_ } } [Array] $ListModules = $AllModules | Where-Object { $null -ne $_ } if ($null -ne $ListModules) { [array]::Reverse($ListModules) } $CleanedModules = [System.Collections.Generic.List[string]]::new() foreach ($_ in $ListModules) { if ($CleanedModules -notcontains $_) { $CleanedModules.Add($_) } } # $CleanedModules.Add($Name) $CleanedModules } function Format-Code { [cmdletbinding()] param( [string] $FilePath, $FormatCode ) if ($FormatCode.Enabled) { if ($FormatCode.RemoveComments) { # Write-Verbose "Removing Comments" $Output = Write-TextWithTime -Text "[+] Removing Comments - $FilePath" { Remove-Comments -FilePath $FilePath } } else { $Output = Write-TextWithTime -Text "[+] Reading file content - $FilePath" { Get-Content -LiteralPath $FilePath -Raw } } if ($null -eq $FormatCode.FormatterSettings) { $FormatCode.FormatterSettings = $Script:FormatterSettings } $Data = Write-TextWithTime -Text "[+] Formatting file - $FilePath" { # Write-Verbose "Formatting - $FilePath" try { Invoke-Formatter -ScriptDefinition $Output -Settings $FormatCode.FormatterSettings -Verbose:$false } catch { $ErrorMessage = $_.Exception.Message #Write-Warning "Merge module on file $FilePath failed. Error: $ErrorMessage" Write-Error "Format-Code - Formatting on file $FilePath failed. Error: $ErrorMessage" Exit } } Write-TextWithTime -Text "[+] Saving file - $FilePath" { # Resave $Final = foreach ($O in $Data) { if ($O.Trim() -ne '') { $O.Trim() } } try { $Final | Out-File -LiteralPath $FilePath -NoNewline -Encoding utf8 } catch { $ErrorMessage = $_.Exception.Message #Write-Warning "Merge module on file $FilePath failed. Error: $ErrorMessage" Write-Text "[-] Format-Code - Resaving file $FilePath failed. Error: $ErrorMessage" -Color Red Exit } } } } function Format-PSD1 { [cmdletbinding()] param( [string] $PSD1FilePath, $FormatCode ) if ($FormatCode.Enabled) { $Output = Get-Content -LiteralPath $PSD1FilePath -Raw if ($FormatCode.RemoveComments) { Write-Verbose "Removing Comments - $PSD1FilePath" # Remove comments $Output = Remove-Comments -ScriptContent $Output } Write-Verbose "Formatting - $PSD1FilePath" if ($null -eq $FormatCode.FormatterSettings) { $FormatCode.FormatterSettings = $Script:FormatterSettings } $Output = Invoke-Formatter -ScriptDefinition $Output -Settings $FormatCode.FormatterSettings #$Output = foreach ($O in $Output) { # if ($O.Trim() -ne '') { # $O.Trim() # } #} $Output | Out-File -LiteralPath $PSD1FilePath -NoNewline } } function Format-UsingNamespace { [CmdletBinding()] param( [string] $FilePath, [string] $FilePathSave, [string] $FilePathUsing ) if ($FilePathSave -eq '') { $FilePathSave = $FilePath } if ($FilePath -ne '' -and (Test-Path -Path $FilePath) -and (Get-Item -LiteralPath $FilePath).Length -gt 0kb) { $FileStream = New-Object -TypeName IO.FileStream -ArgumentList ($FilePath), ([System.IO.FileMode]::Open), ([System.IO.FileAccess]::Read), ([System.IO.FileShare]::ReadWrite); $ReadFile = New-Object -TypeName System.IO.StreamReader -ArgumentList ($FileStream, [System.Text.Encoding]::UTF8, $true); # Read Lines $UsingNamespaces = [System.Collections.Generic.List[string]]::new() #$AddTypes = [System.Collections.Generic.List[string]]::new() $Content = while (!$ReadFile.EndOfStream) { $Line = $ReadFile.ReadLine() if ($Line -like 'using namespace*') { $UsingNamespaces.Add($Line) #} elseif ($Line -like '*Add-Type*') { #$AddTypes.Add($Line) } else { $Line } } $ReadFile.Close() $null = New-Item -Path $FilePathSave -ItemType file -Force if ($UsingNamespaces) { # Repeat using namespaces $null = New-Item -Path $FilePathUsing -ItemType file -Force $UsingNamespaces = $UsingNamespaces.Trim() | Sort-Object -Unique $UsingNamespaces | Add-Content -LiteralPath $FilePathUsing -Encoding utf8 #$UsingNamespaces | Add-Content -LiteralPath $FilePathUsing -Encoding utf8 #$Content | Add-Content -LiteralPath $FilePathUsing -Encoding utf8 $Content | Add-Content -LiteralPath $FilePathSave -Encoding utf8 return $true } else { $Content | Add-Content -LiteralPath $FilePathSave -Encoding utf8 return $False } } } Function Get-AliasTarget { [cmdletbinding()] param ( [Alias('PSPath', 'FullName')][Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)][string[]]$Path, [string] $Content, [switch] $RecurseFunctionNames ) process { if ($Content) { $ProcessData = $Content $Code = $true } else { $ProcessData = $Path $Code = $false } foreach ($File in $ProcessData) { $Ast = $null if ($Code) { $FileAst = [System.Management.Automation.Language.Parser]::ParseInput($File, [ref]$null, [ref]$null) } else { $FileAst = [System.Management.Automation.Language.Parser]::ParseFile($File , [ref]$null, [ref]$null) } $FunctionName = $FileAst.FindAll( { param ($ast) $ast -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $RecurseFunctionNames).Name $Ast = $Null $AliasDefinitions = $FileAst.FindAll( { param ( $ast ) $ast -is [System.Management.Automation.Language.AttributeAst] -and $ast.TypeName.Name -eq 'Alias' -and $ast.Parent -is [System.Management.Automation.Language.ParamBlockAst] }, $true) $AliasTarget = @( $AliasDefinitions.PositionalArguments.Value foreach ($_ in $AliasDefinitions.Parent.CommandElements) { if ($_.StringConstantType -eq 'BareWord' -and $_.Value -notin ('New-Alias', 'Set-Alias', $FunctionName)) { $_.Value } } ) $AliasTarget = foreach ($_ in $AliasTarget) { if ($_ -ne $null) { $_ } } [PsCustomObject]@{ Function = $FunctionName Alias = $AliasTarget } } } } <# Measure-Command { $Files = Get-ChildItem -LiteralPath 'C:\Support\GitHub\PSWriteHTML\Public' $Functions = foreach ($_ in $Files) { Get-AliasTarget -Path $_.FullName } } Measure-Command { $Files = Get-ChildItem -LiteralPath 'C:\Support\GitHub\PSWriteHTML\Public' $Functions = foreach ($_ in $Files) { [System.Management.Automation.Language.Parser]::ParseFile($_ , [ref]$null, [ref]$null) } } #> <# $AliasDefinitions = $FileAst.FindAll( { param ($ast) $ast -is [System.Management.Automation.Language.StringConstantExpressionAst] -And $ast.Value -match '(New|Set)-Alias' }, $true) #> #Measure-Command { # Get-AliasTarget -Path 'C:\Support\GitHub\PSSharedGoods\Public\Objects\Format-Stream.ps1' #| Select-Object -ExpandProperty Alias #Get-AliasTarget -path 'C:\Support\GitHub\PSPublishModule\Private\Get-AliasTarget.ps1' # get-aliastarget -path 'C:\Support\GitHub\PSPublishModule\Private\Start-ModuleBuilding.ps1' #} #Get-AliasTarget -Path 'C:\Add-TableContent.ps1' #Get-AliasTarget -Path 'C:\Support\GitHub\PSWriteHTML\Private\Add-TableContent.ps1' #Get-FunctionNames -Path 'C:\Support\GitHub\PSWriteHTML\Private\Add-TableContent.ps1' #Get-FunctionAliases -Path 'C:\Support\GitHub\PSSharedGoods\Public\Objects\Format-Stream.ps1' function Get-FilteredScriptCommands { [CmdletBinding()] param( [Array] $Commands, [switch] $NotCmdlet, [switch] $NotUnknown, [switch] $NotApplication, [string[]] $Functions, [string] $FilePath ) if ($Functions.Count -eq 0) { $Functions = Get-FunctionNames -Path $FilePath } $Commands = $Commands | Where-Object { $_ -notin $Functions } $Commands = $Commands | Sort-Object -Unique $Scan = foreach ($Command in $Commands) { try { $IsAlias = $false $Data = Get-Command -Name $Command -ErrorAction Stop if ($Data.CommandType -eq 'Alias') { $Data = Get-Command -Name $Data.Definition $IsAlias = $true } [PSCustomObject] @{ Name = $Data.Name Source = $Data.Source CommandType = $Data.CommandType IsAlias = $IsAlias Error = '' ScriptBlock = $Data.ScriptBlock } } catch { [PSCustomObject] @{ Name = $Command Source = '' CommandType = '' IsAlias = $IsAlias Error = $_.Exception.Message ScriptBlock = '' } } } $Filtered = foreach ($Command in $Scan) { if ($NotCmdlet -and $NotUnknown -and $NotApplication) { if ($Command.CommandType -ne 'Cmdlet' -and $Command.Source -ne '' -and $Command.CommandType -ne 'Application') { $Command } } elseif ($NotCmdlet -and $NotUnknown) { if ($Command.CommandType -ne 'Cmdlet' -and $Command.Source -ne '') { $Command } } elseif ($NotCmdlet) { if ($Command.CommandType -ne 'Cmdlet') { $Command } } elseif ($NotUnknown) { if ($Command.Source -ne '') { $Command } } elseif ($NotApplication) { if ($Command.CommandType -ne 'Application') { $Command } } else { $Command } } $Filtered } function Get-FunctionAliasesFromFolder { [cmdletbinding()] param( [string] $FullProjectPath, [string[]] $Folder, [Array] $Files ) <# foreach ($F in $Folder) { $Path = [IO.Path]::Combine($FullProjectPath, $F) if ($PSEdition -eq 'Core') { $Files = Get-ChildItem -Path $Path -File -Recurse -FollowSymlink } else { $Files = Get-ChildItem -Path $Path -File -Recurse } $AliasesToExport = foreach ($file in $Files) { #Get-FunctionAliases -Path $File.FullName Get-AliasTarget -Path $File.FullName | Select-Object -ExpandProperty Alias } $AliasesToExport } #> $FilesPS1 = foreach ($File in $Files) { if ($file.FullName -like "*\Public\*") { if ($File.Extension -eq '.ps1' -or $File.Extension -eq '*.psm1') { $File } } } [Array] $Content = foreach ($File in $FilesPS1) { '' Get-Content -LiteralPath $File.FullName -Raw -Encoding Default } $Code = $Content -join [System.Environment]::NewLine $AliasesToExport = Get-AliasTarget -Content $Code <# $AliasesToExport = foreach ($file in $FilesPS1) { #Get-FunctionAliases -Path $File.FullName #Write-TextWithTime -Text "Alias $($File.FullName)" { Get-AliasTarget -Path $File.FullName #| Select-Object -ExpandProperty Alias # } } #> $AliasesToExport } function Get-FunctionNames { [cmdletbinding()] param( [string] $Path, [switch] $Recurse ) if ($Path -ne '' -and (Test-Path -LiteralPath $Path)) { $FilePath = Resolve-Path $Path [System.Management.Automation.Language.Parser]::ParseFile(($FilePath), [ref]$null, [ref]$null).FindAll( { param($c)$c -is [Management.Automation.Language.FunctionDefinitionAst] }, $Recurse).Name } } function Get-RecursiveCommands { [CmdletBinding()] param( [Array] $Commands ) $Another = foreach ($Command in $Commands) { if ($Command.ScriptBlock) { Get-ScriptCommands -Code $Command.ScriptBlock -CommandsOnly } } $filter = Get-FilteredScriptCommands -Commands $Another -NotUnknown -NotCmdlet [Array] $ProcessedCommands = foreach ($_ in $Filter) { if ($_.Name -notin $ListCommands.Name) { $ListCommands.Add($_) $_ } } if ($ProcessedCommands.Count -gt 0) { Get-RecursiveCommands -Commands $ProcessedCommands } } function Get-ScriptCommandsOld { param( [string] $FilePath, [switch] $CommandsOnly ) $astTokens = $null $astErr = $null $null = [System.Management.Automation.Language.Parser]::ParseFile($FilePath, [ref]$astTokens, [ref]$astErr) $Commands = [System.Collections.Generic.List[Object]]::new() foreach ($_ in $astTokens) { if ($_.TokenFlags -eq 'Command' -and $_.Kind -eq 'Generic') { if ($_.Value -notin $Commands) { $Commands.Add($_) } } } if ($CommandsOnly) { $Commands.Value | Sort-Object -Unique } else { $Commands } # $astTokens | Group-Object tokenflags -AsHashTable -AsString #$Commands = $astTokens | Where-Object { $_.TokenFlags -eq 'Command' } | Sort-Object -Property Value -Unique } function Get-ScriptCommands { [cmdletBinding(DefaultParameterSetName = 'File')] param ( [alias('Path')][Parameter(ParameterSetName = 'File')][string] $FilePath, [alias('ScriptBlock')][scriptblock] $Code, [switch] $CommandsOnly ) begin { $Errors = $null } process { $Errors = $null if ($Code) { $CodeRead = $Code } else { if ($FilePath -eq '') { Write-Text "[-] Something went wrong. FilePath for Get-ScriptCommands was empty. Rerun tool to verify." -Color Red } else { if (Test-Path -LiteralPath $FilePath) { $CodeRead = Get-Content -Path $FilePath -Raw -Encoding Default } } } if ($CodeRead) { $Tokens = [System.Management.Automation.PSParser]::Tokenize($CodeRead, [ref]$Errors) $Commands = foreach ($_ in $Tokens) { if ($_.Type -eq 'Command') { $_ } } if ($CommandsOnly) { $Commands.Content | Sort-Object -Unique } else { $Commands } } } } function Merge-Module { [CmdletBinding()] param ( [string] $ModuleName, [string] $ModulePathSource, [string] $ModulePathTarget, [Parameter(Mandatory = $false, ValueFromPipeline = $false)] [ValidateSet("ASC", "DESC", "NONE", '')] [string] $Sort = 'NONE', [string[]] $FunctionsToExport, [string[]] $AliasesToExport, [Array] $LibrariesCore, [Array] $LibrariesDefault, [System.Collections.IDictionary] $FormatCodePSM1, [System.Collections.IDictionary] $FormatCodePSD1, [System.Collections.IDictionary] $Configuration ) $TimeToExecute = [System.Diagnostics.Stopwatch]::StartNew() Write-Text "[+] 1st stage merging" -Color Blue $PSM1FilePath = "$ModulePathTarget\$ModuleName.psm1" $PSD1FilePath = "$ModulePathTarget\$ModuleName.psd1" if ($PSEdition -eq 'Core') { $ScriptFunctions = Get-ChildItem -Path $ModulePathSource\*.ps1 -ErrorAction SilentlyContinue -Recurse -FollowSymlink } else { $ScriptFunctions = Get-ChildItem -Path $ModulePathSource\*.ps1 -ErrorAction SilentlyContinue -Recurse } if ($Sort -eq 'ASC') { $ScriptFunctions = $ScriptFunctions | Sort-Object -Property Name } elseif ($Sort -eq 'DESC') { $ScriptFunctions = $ScriptFunctions | Sort-Object -Descending -Property Name } foreach ($FilePath in $ScriptFunctions) { $Content = Get-Content -Path $FilePath -Raw $Content = $Content.Replace('$PSScriptRoot\', '$PSScriptRoot\') $Content = $Content.Replace('$PSScriptRoot\', '$PSScriptRoot\') try { $Content | Out-File -Append -LiteralPath $PSM1FilePath -Encoding utf8 } catch { $ErrorMessage = $_.Exception.Message #Write-Warning "Merge module on file $FilePath failed. Error: $ErrorMessage" Write-Error "Merge-Module - Merge on file $FilePath failed. Error: $ErrorMessage" Exit } } # Using file is needed if there are 'using namespaces' - this is a workaround provided by seeminglyscience $FilePathUsing = "$ModulePathTarget\$ModuleName.Usings.ps1" $UsingInPlace = Format-UsingNamespace -FilePath $PSM1FilePath -FilePathUsing $FilePathUsing if ($UsingInPlace) { Format-Code -FilePath $FilePathUsing -FormatCode $FormatCodePSM1 $Configuration.UsingInPlace = "$ModuleName.Usings.ps1" } $TimeToExecute.Stop() Write-Text "[+] 1st stage merging [Time: $($($TimeToExecute.Elapsed).Tostring())]" -Color Blue $TimeToExecute = [System.Diagnostics.Stopwatch]::StartNew() Write-Text "[+] 2nd stage required modules" -Color Blue $RequiredModules = @( if ($Configuration.Information.Manifest.RequiredModules[0] -is [System.Collections.IDictionary]) { $Configuration.Information.Manifest.RequiredModules.ModuleName } else { $Configuration.Information.Manifest.RequiredModules } ) $DependantRequiredModules = foreach ($_ in $RequiredModules) { Find-RequiredModules -Name $_ } $DependantRequiredModules = $DependantRequiredModules | Sort-Object -Unique $TimeToExecute.Stop() Write-Text "[+] 2nd stage required modules [Time: $($($TimeToExecute.Elapsed).Tostring())]" -Color Blue $TimeToExecute = [System.Diagnostics.Stopwatch]::StartNew() Write-Text "[+] 3rd stage missing functions" -Color Blue [Array] $ApprovedModules = $Configuration.Options.Merge.Integrate.ApprovedModules $MissingFunctions = Get-MissingFunctions -FilePath $PSM1FilePath -SummaryWithCommands -ApprovedModules $ApprovedModules $TimeToExecute.Stop() Write-Text "[+] 3rd stage missing functions [Time: $($($TimeToExecute.Elapsed).Tostring())]" -Color Blue $TimeToExecute = [System.Diagnostics.Stopwatch]::StartNew() Write-Text "[+] 4th stage commands used" -Color Blue foreach ($Module in $MissingFunctions.Summary.Source | Sort-Object -Unique) { if ($Module -in $RequiredModules -and $Module -in $ApprovedModules) { Write-Text "[+] Module $Module is in required modules with ability to merge." -Color Green $MyFunctions = ($MissingFunctions.Summary | Where-Object { $_.Source -eq $Module }) #-join ',' foreach ($F in $MyFunctions) { Write-Text " [>] Command used $($F.Name) (Command Type: $($F.CommandType) / IsAlias: $($F.IsAlias))" -Color Yellow } } elseif ($Module -in $DependantRequiredModules -and $Module -in $ApprovedModules) { Write-Text "[+] Module $Module is in dependant required module within required modules with ability to merge." -Color Green $MyFunctions = ($MissingFunctions.Summary | Where-Object { $_.Source -eq $Module }) #-join ',' foreach ($F in $MyFunctions) { Write-Text " [>] Command used $($F.Name) (Command Type: $($F.CommandType) / IsAlias: $($F.IsAlias))" -Color Yellow } } elseif ($Module -in $DependantRequiredModules) { Write-Text "[+] Module $Module is in dependant required module within required modules." -Color Green $MyFunctions = ($MissingFunctions.Summary | Where-Object { $_.Source -eq $Module }) #-join ',' foreach ($F in $MyFunctions) { Write-Text " [>] Command used $($F.Name) (Command Type: $($F.CommandType) / IsAlias: $($F.IsAlias))" -Color Green } } elseif ($Module -in $RequiredModules) { Write-Text "[+] Module $Module is in required modules." -Color Green $MyFunctions = ($MissingFunctions.Summary | Where-Object { $_.Source -eq $Module }) #-join ',' foreach ($F in $MyFunctions) { Write-Text " [>] Command used $($F.Name) (Command Type: $($F.CommandType) / IsAlias: $($F.IsAlias))" -Color Green } } else { Write-Text "[-] Module $Module is missing in required modules. Potential issue." -Color Red $MyFunctions = ($MissingFunctions.Summary | Where-Object { $_.Source -eq $Module }) #-join ',' foreach ($F in $MyFunctions) { Write-Text " [>] Command affected $($F.Name) (Command Type: $($F.CommandType) / IsAlias: $($F.IsAlias))" -Color Red } } } $TimeToExecute.Stop() Write-Text "[+] 4th stage commands used [Time: $($($TimeToExecute.Elapsed).Tostring())]" -Color Blue if ($Configuration.Steps.BuildModule.MergeMissing -eq $true) { if (Test-Path -LiteralPath $PSM1FilePath) { $TimeToExecute = [System.Diagnostics.Stopwatch]::StartNew() Write-Text "[+] 5th stage merge mergable commands" -Color Blue $PSM1Content = Get-Content -LiteralPath $PSM1FilePath -Raw $IntegrateContent = @( $MissingFunctions.Functions $PSM1Content ) $IntegrateContent | Set-Content -LiteralPath $PSM1FilePath -Encoding UTF8 # Overwrite Required Modules $NewRequiredModules = foreach ($_ in $Configuration.Information.Manifest.RequiredModules) { if ($_ -is [System.Collections.IDictionary]) { if ($_.ModuleName -notin $ApprovedModules) { $_ } } else { if ($_ -notin $ApprovedModules) { $_ } } } $Configuration.Information.Manifest.RequiredModules = $NewRequiredModules #$MissingFunctions.Functions #$MissingFunctions.Summary | Format-Table -AutoSize <# Name Source CommandType Error ScriptBlock ---- ------ ----------- ----- ----------- cmd.exe C:\Windows\system32\cmd.exe Application Import-PowerShellDataFile Microsoft.PowerShell.Utility Function ... New-MarkdownHelp platyPS Function ... Publish-Module PowerShellGet Function ... Update-MarkdownHelpModule platyPS Function ... Find-Module PowerShellGet Function ... Find-Script PowerShellGet Function ... Get-MarkdownMetadata platyPS Function ... Get-PSRepository PowerShellGet Function ... Update-MarkdownHelp platyPS Function ... #> $TimeToExecute.Stop() Write-Text "[+] 5th stage merge mergable commands [Time: $($($TimeToExecute.Elapsed).Tostring())]" -Color Blue } } New-PSMFile -Path $PSM1FilePath ` -FunctionNames $FunctionsToExport ` -FunctionAliaes $AliasesToExport ` -LibrariesCore $LibrariesCore ` -LibrariesDefault $LibrariesDefault ` -ModuleName $ModuleName ` -UsingNamespaces:$UsingInPlace Format-Code -FilePath $PSM1FilePath -FormatCode $FormatCodePSM1 New-PersonalManifest -Configuration $Configuration -ManifestPath $PSD1FilePath -AddUsingsToProcess Format-Code -FilePath $PSD1FilePath -FormatCode $FormatCodePSD1 # cleans up empty directories Get-ChildItem $ModulePathTarget -Recurse -Force -Directory | Sort-Object -Property FullName -Descending | ` Where-Object { $($_ | Get-ChildItem -Force | Select-Object -First 1).Count -eq 0 } | ` Remove-Item #-Verbose } function New-CreateModule { [CmdletBinding()] param ( [string] $ProjectName, [string] $ModulePath, [string] $ProjectPath ) $FullProjectPath = "$projectPath\$projectName" $Folders = 'Private', 'Public', 'Examples', 'Ignore', 'Publish', 'Enums', 'Data' Add-Directory $FullProjectPath foreach ($folder in $Folders) { Add-Directory "$FullProjectPath\$folder" } Copy-File -Source "$PSScriptRoot\Data\Example-Gitignore.txt" -Destination "$FullProjectPath\.gitignore" Copy-File -Source "$PSScriptRoot\Data\Example-LicenseMIT.txt" -Destination "$FullProjectPath\License" Copy-File -Source "$PSScriptRoot\Data\Example-ModuleStarter.ps1" -Destination "$FullProjectPath\$ProjectName.psm1" } function New-GitHubRelease { <# .SYNOPSIS Creates a new Release for the given GitHub repository. .DESCRIPTION Uses the GitHub API to create a new Release for a given repository. Allows you to specify all of the Release properties, such as the Tag, Name, Assets, and if it's a Draft or Prerelease or not. .PARAMETER GitHubUsername The username that the GitHub repository exists under. e.g. For the repository https://github.com/deadlydog/New-GitHubRelease, the username is 'deadlydog'. .PARAMETER GitHubRepositoryName The name of the repository to create the Release for. e.g. For the repository https://github.com/deadlydog/New-GitHubRelease, the repository name is 'New-GitHubRelease'. .PARAMETER GitHubAccessToken The Access Token to use as credentials for GitHub. Access tokens can be generated at https://github.com/settings/tokens. The access token will need to have the repo/public_repo permission on it for it to be allowed to create a new Release. .PARAMETER TagName The name of the tag to create at the Commitish. .PARAMETER ReleaseName The name to use for the new release. If blank, the TagName will be used. .PARAMETER ReleaseNotes The text describing the contents of the release. .PARAMETER AssetFilePaths The full paths of the files to include in the release. .PARAMETER Commitish Specifies the commitish value that determines where the Git tag is created from. Can be any branch or commit SHA. Unused if the Git tag already exists. Default: the repository's default branch (usually master). .PARAMETER IsDraft True to create a draft (unpublished) release, false to create a published one. Default: false .PARAMETER IsPreRelease True to identify the release as a prerelease. false to identify the release as a full release. Default: false .OUTPUTS A hash table with the following properties is returned: Succeeded = $true if the Release was created successfully and all assets were uploaded to it, $false if some part of the process failed. ReleaseCreationSucceeded = $true if the Release was created successfully (does not include asset uploads), $false if the Release was not created. AllAssetUploadsSucceeded = $true if all assets were uploaded to the Release successfully, $false if one of them failed, $null if there were no assets to upload. ReleaseUrl = The URL of the new Release that was created. ErrorMessage = A message describing what went wrong in the case that Succeeded is $false. .EXAMPLE # Import the module dynamically from the PowerShell Gallery. Use CurrentUser scope to avoid having to run as admin. Import-Module -Name New-GitHubRelease -Scope CurrentUser # Specify the parameters required to create the release. Do it as a hash table for easier readability. $newGitHubReleaseParameters = @{ GitHubUsername = 'deadlydog' GitHubRepositoryName = 'New-GitHubRelease' GitHubAccessToken = 'SomeLongHexidecimalString' ReleaseName = "New-GitHubRelease v1.0.0" TagName = "v1.0.0" ReleaseNotes = "This release contains the following changes: ..." AssetFilePaths = @('C:\MyProject\Installer.exe','C:\MyProject\Documentation.md') IsPreRelease = $false IsDraft = $true # Set to true when testing so we don't publish a real release (visible to everyone) by accident. } # Try to create the Release on GitHub and save the results. $result = New-GitHubRelease @newGitHubReleaseParameters # Provide some feedback to the user based on the results. if ($result.Succeeded -eq $true) { Write-Output "Release published successfully! View it at $($result.ReleaseUrl)" } elseif ($result.ReleaseCreationSucceeded -eq $false) { Write-Error "The release was not created. Error message is: $($result.ErrorMessage)" } elseif ($result.AllAssetUploadsSucceeded -eq $false) { Write-Error "The release was created, but not all of the assets were uploaded to it. View it at $($result.ReleaseUrl). Error message is: $($result.ErrorMessage)" } Attempt to create a new Release on GitHub, and provide feedback to the user indicating if it succeeded or not. .LINK Project home: https://github.com/deadlydog/New-GitHubRelease .NOTES Name: New-GitHubRelease Author: Daniel Schroeder (originally based on the script at https://github.com/majkinetor/au/blob/master/scripts/Github-CreateRelease.ps1) GitHub Release API Documentation: https://developer.github.com/v3/repos/releases/#create-a-release Version: 1.0.2 #> [CmdletBinding()] param ( [Parameter(Mandatory = $true, HelpMessage = "The username the repository is under (e.g. deadlydog).")] [string] $GitHubUsername, [Parameter(Mandatory = $true, HelpMessage = "The repository name to create the release in (e.g. Invoke-MsBuild).")] [string] $GitHubRepositoryName, [Parameter(Mandatory = $true, HelpMessage = "The Acess Token to use as credentials for GitHub.")] [string] $GitHubAccessToken, [Parameter(Mandatory = $true, HelpMessage = "The name of the tag to create at the the Commitish.")] [string] $TagName, [Parameter(Mandatory = $false, HelpMessage = "The name of the release. If blank, the TagName will be used.")] [string] $ReleaseName, [Parameter(Mandatory = $false, HelpMessage = "Text describing the contents of the tag.")] [string] $ReleaseNotes, [Parameter(Mandatory = $false, HelpMessage = "The full paths of the files to include in the release.")] [string[]] $AssetFilePaths, [Parameter(Mandatory = $false, HelpMessage = "Specifies the commitish value that determines where the Git tag is created from. Can be any branch or commit SHA. Unused if the Git tag already exists. Default: the repository's default branch (usually master).")] [string] $Commitish, [Parameter(Mandatory = $false, HelpMessage = "True to create a draft (unpublished) release, false to create a published one. Default: false")] [bool] $IsDraft = $false, [Parameter(Mandatory = $false, HelpMessage = "True to identify the release as a prerelease. false to identify the release as a full release. Default: false")] [bool] $IsPreRelease = $false ) BEGIN { # Turn on Strict Mode to help catch syntax-related errors. # This must come after a script's/function's param section. # Forces a function to be the first non-comment code to appear in a PowerShell Script/Module. Set-StrictMode -Version Latest Set-SecurityProtocolForThread [string] $NewLine = [Environment]::NewLine if ([string]::IsNullOrEmpty($ReleaseName)) { $ReleaseName = $TagName } # Ensure that all of the given asset file paths to upload are valid. Test-AllFilePathsAndThrowErrorIfOneIsNotValid $AssetFilePaths } END { } PROCESS { # Create the hash table to return, with default values. $result = @{ } $result.Succeeded = $false $result.ReleaseCreationSucceeded = $false $result.AllAssetUploadsSucceeded = $false $result.ReleaseUrl = $null $result.ErrorMessage = $null [bool] $thereAreNoAssetsToIncludeInTheRelease = ($AssetFilePaths -eq $null) -or ($AssetFilePaths.Count -le 0) if ($thereAreNoAssetsToIncludeInTheRelease) { $result.AllAssetUploadsSucceeded = $null } $authHeader = @{ Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($GitHubAccessToken + ":x-oauth-basic")) } $releaseData = @{ tag_name = $TagName target_commitish = $Commitish name = $ReleaseName body = $ReleaseNotes draft = $IsDraft prerelease = $IsPreRelease } $createReleaseWebRequestParameters = @{ Uri = "https://api.github.com/repos/$GitHubUsername/$GitHubRepositoryName/releases" Method = 'POST' Headers = $authHeader ContentType = 'application/json' Body = (ConvertTo-Json $releaseData -Compress) } try { Write-Verbose "Sending web request to create the new Release..." $createReleaseWebRequestResults = Invoke-RestMethodAndThrowDescriptiveErrorOnFailure $createReleaseWebRequestParameters } catch { $result.ReleaseCreationSucceeded = $false $result.ErrorMessage = $_.Exception.Message return $result } $result.ReleaseCreationSucceeded = $true $result.ReleaseUrl = $createReleaseWebRequestResults.html_url if ($thereAreNoAssetsToIncludeInTheRelease) { $result.Succeeded = $true return $result } # Upload Url has template parameters on the end (e.g. ".../assets{?name,label}"), so remove them. [string] $urlToUploadFilesTo = $createReleaseWebRequestResults.upload_url -replace '{.+}' try { Write-Verbose "Uploading asset files to the new release..." Send-FilesToGitHubRelease -filePathsToUpload $AssetFilePaths -urlToUploadFilesTo $urlToUploadFilesTo -authHeader $authHeader } catch { $result.AllAssetUploadsSucceeded = $false $result.ErrorMessage = $_.Exception.Message return $result } $result.AllAssetUploadsSucceeded = $true $result.Succeeded = $true return $result } } function Send-FilesToGitHubRelease([string[]] $filePathsToUpload, [string] $urlToUploadFilesTo, $authHeader) { [int] $numberOfFilesToUpload = $filePathsToUpload.Count [int] $numberOfFilesUploaded = 0 $filePathsToUpload | ForEach-Object ` { $filePath = $_ $fileName = Get-Item $filePath | Select-Object -ExpandProperty Name $uploadAssetWebRequestParameters = @{ # Append the name of the file to the upload url. Uri = $urlToUploadFilesTo + "?name=$fileName" Method = 'POST' Headers = $authHeader ContentType = 'application/zip' InFile = $filePath } $numberOfFilesUploaded = $numberOfFilesUploaded + 1 Write-Verbose "Uploading asset $numberOfFilesUploaded of $numberOfFilesToUpload, '$filePath'." Invoke-RestMethodAndThrowDescriptiveErrorOnFailure $uploadAssetWebRequestParameters > $null } } function Test-AllFilePathsAndThrowErrorIfOneIsNotValid([string[]] $filePaths) { foreach ($filePath in $filePaths) { [bool] $fileWasNotFoundAtPath = [string]::IsNullOrEmpty($filePath) -or !(Test-Path -Path $filePath -PathType Leaf) if ($fileWasNotFoundAtPath) { throw "There is no file at the specified path, '$filePath'." } } } function Invoke-RestMethodAndThrowDescriptiveErrorOnFailure($requestParametersHashTable) { $requestDetailsAsNicelyFormattedString = Convert-HashTableToNicelyFormattedString $requestParametersHashTable Write-Verbose "Making web request with the following parameters:$NewLine$requestDetailsAsNicelyFormattedString" try { $webRequestResult = Invoke-RestMethod @requestParametersHashTable } catch { [Exception] $exception = $_.Exception [string] $errorMessage = Get-RestMethodExceptionDetailsOrNull -restMethodException $exception if ([string]::IsNullOrWhiteSpace($errorMessage)) { $errorMessage = $exception.ToString() } throw "An unexpected error occurred while making web request:$NewLine$errorMessage" } Write-Verbose "Web request returned the following result:$NewLine$webRequestResult" return $webRequestResult } function Get-RestMethodExceptionDetailsOrNull([Exception] $restMethodException) { try { $responseDetails = @{ ResponseUri = $exception.Response.ResponseUri StatusCode = $exception.Response.StatusCode StatusDescription = $exception.Response.StatusDescription ErrorMessage = $exception.Message } [string] $responseDetailsAsNicelyFormattedString = Convert-HashTableToNicelyFormattedString $responseDetails [string] $errorInfo = "Request Details:" + $NewLine + $requestDetailsAsNicelyFormattedString $errorInfo += $NewLine $errorInfo += "Response Details:" + $NewLine + $responseDetailsAsNicelyFormattedString return $errorInfo } catch { return $null } } function Convert-HashTableToNicelyFormattedString($hashTable) { [string] $nicelyFormattedString = $hashTable.Keys | ForEach-Object ` { $key = $_ $value = $hashTable.$key " $key = $value$NewLine" } return $nicelyFormattedString } function Set-SecurityProtocolForThread { [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 -bor [System.Net.SecurityProtocolType]::Tls11 -bor [System.Net.SecurityProtocolType]::Tls } function New-PersonalManifest { [CmdletBinding()] param( [System.Collections.IDictionary] $Configuration, [string] $ManifestPath, [switch] $AddScriptsToProcess, [switch] $AddUsingsToProcess ) $Manifest = $Configuration.Information.Manifest $Manifest.Path = $ManifestPath if (-not $AddScriptsToProcess) { $Manifest.ScriptsToProcess = @() } if ($AddUsingsToProcess -and $Configuration.UsingInPlace) { $Manifest.ScriptsToProcess = @($Configuration.UsingInPlace) } New-ModuleManifest @Manifest if ($Configuration.Steps.PublishModule.Prerelease -ne '') { #$FilePathPSD1 = Get-Item -Path $Configuration.Information.Manifest.Path $Data = Import-PowerShellDataFile -Path $Configuration.Information.Manifest.Path if ($Data.ScriptsToProcess.Count -eq 0) { $Data.Remove('ScriptsToProcess') } if ($Data.CmdletsToExport.Count -eq 0) { $Data.Remove('CmdletsToExport') } $Data.PrivateData.PSData.Prerelease = $Configuration.Steps.PublishModule.Prerelease $Data | Export-PSData -DataFile $Configuration.Information.Manifest.Path } Write-TextWithTime -Text "[+] Converting $($Configuration.Information.Manifest.Path) UTF8 without BOM" { (Get-Content $Manifest.Path) | Out-FileUtf8NoBom $Manifest.Path } } function New-PrepareManifest { [CmdletBinding()] param( $ProjectName, $modulePath, $projectPath, $functionToExport, $projectUrl ) Set-Location "$projectPath\$ProjectName" $manifest = @{ Path = ".\$ProjectName.psd1" RootModule = "$ProjectName.psm1" Author = 'Przemyslaw Klys' CompanyName = 'Evotec' Copyright = 'Evotec (c) 2011-2019. All rights reserved.' Description = "Simple project" FunctionsToExport = $functionToExport CmdletsToExport = '' VariablesToExport = '' AliasesToExport = '' FileList = "$ProjectName.psm1", "$ProjectName.psd1" HelpInfoURI = $projectUrl ProjectUri = $projectUrl } New-ModuleManifest @manifest } function New-PSMFile { [cmdletbinding()] param( [string] $Path, [string[]] $FunctionNames, [string[]] $FunctionAliaes, [Array] $LibrariesCore, [Array] $LibrariesDefault, [string] $ModuleName, [switch] $UsingNamespaces ) try { if ($FunctionNames.Count -gt 0) { $Functions = ($FunctionNames | Sort-Object -Unique) -join "','" $Functions = "'$Functions'" } else { $Functions = @() } if ($FunctionAliaes.Count -gt 0) { $Aliases = ($FunctionAliaes | Sort-Object -Unique) -join "','" $Aliases = "'$Aliases'" } else { $Aliases = @() } "" | Add-Content -Path $Path if ($LibrariesCore.Count -gt 0 -and $LibrariesDefault.Count -gt 0) { 'if ($PSEdition -eq ''Core'') {' | Add-Content -Path $Path foreach ($File in $LibrariesCore) { $Extension = $File.Substring($File.Length - 4, 4) if ($Extension -eq '.dll') { $Output = 'Add-Type -Path $PSScriptRoot\' + $File $Output | Add-Content -Path $Path } } '} else {' | Add-Content -Path $Path foreach ($File in $LibrariesDefault) { $Extension = $File.Substring($File.Length - 4, 4) if ($Extension -eq '.dll') { $Output = 'Add-Type -Path $PSScriptRoot\' + $File $Output | Add-Content -Path $Path } } '}' | Add-Content -Path $Path } elseif ($LibrariesCore.Count -gt 0) { foreach ($File in $LibrariesCore) { $Extension = $File.Substring($File.Length - 4, 4) if ($Extension -eq '.dll') { $Output = 'Add-Type -Path $PSScriptRoot\' + $File $Output | Add-Content -Path $Path } } } elseif ($LibrariesDefault.Count -gt 0) { foreach ($File in $LibrariesDefault) { $Extension = $File.Substring($File.Length - 4, 4) if ($Extension -eq '.dll') { $Output = 'Add-Type -Path $PSScriptRoot\' + $File $Output | Add-Content -Path $Path } } } #if ($UsingNamespaces) { # '. $PSScriptRoot\' + "$ModuleName.ps1" | Add-Content -Path $Path #} @" Export-ModuleMember `` -Function @($Functions) `` -Alias @($Aliases) "@ | Add-Content -Path $Path } catch { $ErrorMessage = $_.Exception.Message #Write-Warning "New-PSM1File from $ModuleName failed build. Error: $ErrorMessage" Write-Error "New-PSM1File from $ModuleName failed build. Error: $ErrorMessage" Exit } } function New-PublishModule { [cmdletbinding()] param( $projectName, $apikey, [bool] $RequireForce ) Publish-Module -Name $projectName -Repository PSGallery -NuGetApiKey $apikey -Force:$RequireForce -verbose } <# .SYNOPSIS Outputs to a UTF-8-encoded file *without a BOM* (byte-order mark). .DESCRIPTION Mimics the most important aspects of Out-File: * Input objects are sent to Out-String first. * -Append allows you to append to an existing file, -NoClobber prevents overwriting of an existing file. * -Width allows you to specify the line width for the text representations of input objects that aren't strings. However, it is not a complete implementation of all Out-String parameters: * Only a literal output path is supported, and only as a parameter. * -Force is not supported. Caveat: *All* pipeline input is buffered before writing output starts, but the string representations are generated and written to the target file one by one. .NOTES The raison d'être for this advanced function is that, as of PowerShell v5, Out-File still lacks the ability to write UTF-8 files without a BOM: using -Encoding UTF8 invariably prepends a BOM. #> function Out-FileUtf8NoBom { [CmdletBinding()] param( [Parameter(Mandatory, Position = 0)] [string] $LiteralPath, [switch] $Append, [switch] $NoClobber, [AllowNull()] [int] $Width, [Parameter(ValueFromPipeline)] $InputObject ) # Make sure that the .NET framework sees the same working dir. as PS # and resolve the input path to a full path. [System.IO.Directory]::SetCurrentDirectory($PWD) # Caveat: .NET Core doesn't support [Environment]::CurrentDirectory $LiteralPath = [IO.Path]::GetFullPath($LiteralPath) # If -NoClobber was specified, throw an exception if the target file already # exists. if ($NoClobber -and (Test-Path $LiteralPath)) { Throw [IO.IOException] "The file '$LiteralPath' already exists." } # Create a StreamWriter object. # Note that we take advantage of the fact that the StreamWriter class by default: # - uses UTF-8 encoding # - without a BOM. $sw = New-Object IO.StreamWriter $LiteralPath, $Append $htOutStringArgs = @{ } if ($Width) { $htOutStringArgs += @{ Width = $Width } } # Note: By not using begin / process / end blocks, we're effectively running # in the end block, which means that all pipeline input has already # been collected in automatic variable $Input. # We must use this approach, because using | Out-String individually # in each iteration of a process block would format each input object # with an indvidual header. try { $Input | Out-String -Stream @htOutStringArgs | ForEach-Object { $sw.WriteLine($_) } } finally { $sw.Dispose() } } function Remove-Directory { [CmdletBinding()] param ( [string] $Directory ) if ($Directory) { $exists = Test-Path -Path $Directory if ($exists) { #Write-Color 'Removing directory ', $dir -Color White, Yellow Remove-Item -Path $Directory -Confirm:$false -Recurse } else { #Write-Color 'Removing directory ', $dir, ' skipped.' -Color White, Yellow, Red } } } $Script:FormatterSettings = @{ IncludeRules = @( 'PSPlaceOpenBrace', 'PSPlaceCloseBrace', 'PSUseConsistentWhitespace', 'PSUseConsistentIndentation', 'PSAlignAssignmentStatement', 'PSUseCorrectCasing' ) Rules = @{ PSPlaceOpenBrace = @{ Enable = $true OnSameLine = $true NewLineAfter = $true IgnoreOneLineBlock = $true } PSPlaceCloseBrace = @{ Enable = $true NewLineAfter = $false IgnoreOneLineBlock = $true NoEmptyLineBefore = $false } PSUseConsistentIndentation = @{ Enable = $true Kind = 'space' PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' IndentationSize = 4 } PSUseConsistentWhitespace = @{ Enable = $true CheckInnerBrace = $true CheckOpenBrace = $true CheckOpenParen = $true CheckOperator = $true CheckPipe = $true CheckSeparator = $true } PSAlignAssignmentStatement = @{ Enable = $true CheckHashtable = $true } PSUseCorrectCasing = @{ Enable = $true } } } function Set-LinkedFiles { [CmdletBinding()] param( [string[]] $LinkFiles, [string] $FullModulePath, [string] $FullProjectPath, [switch] $Delete ) foreach ($file in $LinkFiles) { [string] $Path = "$FullModulePath\$file" [string] $Path2 = "$FullProjectPath\$file" if ($Delete) { if (Test-ReparsePoint -path $Path) { # Write-Color 'Removing symlink first ', $path -Color White, Yellow #Write-Verbose "Removing symlink first $path" Remove-Item $Path -Confirm:$false } } #Write-Verbose "Creating symlink from $path2 (source) to $path (target)" #Write-Color 'Creating symlink from ', $path2, ' (source) to ', $path, ' (target)' -Color White, Yellow, White, Yellow, White $null = cmd /c mklink $path $path2 } } function Start-ModuleBuilding { [CmdletBinding()] param( [System.Collections.IDictionary] $Configuration ) $DestinationPaths = @{ } if ($Configuration.Information.Manifest.CompatiblePSEditions) { if ($Configuration.Information.Manifest.CompatiblePSEditions -contains 'Desktop') { $DestinationPaths.Desktop = [IO.path]::Combine($Configuration.Information.DirectoryModules, $Configuration.Information.ModuleName) } if ($Configuration.Information.Manifest.CompatiblePSEditions -contains 'Core') { $DestinationPaths.Core = [IO.path]::Combine($Configuration.Information.DirectoryModulesCore, $Configuration.Information.ModuleName) } } else { # Means missing from config - send to both $DestinationPaths.Desktop = [IO.path]::Combine($Configuration.Information.DirectoryModules, $Configuration.Information.ModuleName) $DestinationPaths.Core = [IO.path]::Combine($Configuration.Information.DirectoryModulesCore, $Configuration.Information.ModuleName) } [string] $Random = Get-Random 10000000000 [string] $FullModulePath = [IO.path]::GetTempPath() + '' + $Configuration.Information.ModuleName [string] $FullTemporaryPath = [IO.path]::GetTempPath() + '' + $Configuration.Information.ModuleName + "_TEMP_$Random" [string] $FullProjectPath = [IO.Path]::Combine($Configuration.Information.DirectoryProjects, $Configuration.Information.ModuleName) [string] $ProjectName = $Configuration.Information.ModuleName Write-Text '----------------------------------------------------' Write-Text "[i] Project Name: $ProjectName" -Color Yellow Write-Text "[i] Full module temporary path: $FullModulePath" -Color Yellow Write-Text "[i] Full project path: $FullProjectPath" -Color Yellow Write-Text "[i] Full temporary path: $FullTemporaryPath" -Color Yellow Write-Text "[i] PSScriptRoot: $PSScriptRoot" -Color Yellow Write-Text "[i] Current PSEdition: $PSEdition" -Color Yellow Write-Text "[i] Destination Desktop: $($DestinationPaths.Desktop)" -Color Yellow Write-Text "[i] Destination Core: $($DestinationPaths.Desktop)" -Color Yellow Write-Text '----------------------------------------------------' $CurrentLocation = (Get-Location).Path Set-Location -Path $FullProjectPath # Remove-Directory $FullModulePathDelete Remove-Directory $FullModulePath Remove-Directory $FullTemporaryPath Add-Directory $FullModulePath Add-Directory $FullTemporaryPath # $DirectoryTypes = 'Public', 'Private', 'Lib', 'Bin', 'Enums', 'Images', 'Templates', 'Resources' $LinkDirectories = @() $LinkPrivatePublicFiles = @() # Fix required fields: $Configuration.Information.Manifest.RootModule = "$($ProjectName).psm1" $Configuration.Information.Manifest.FunctionsToExport = @() #$FunctionToExport # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. $Configuration.Information.Manifest.CmdletsToExport = @() # Variables to export from this module $Configuration.Information.Manifest.VariablesToExport = @() # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. $Configuration.Information.Manifest.AliasesToExport = @() $Exclude = '.*', 'Ignore', 'Examples', 'package.json', 'Publish', 'Docs' if ($Configuration.Steps.BuildModule) { $PreparingFilesTime = Write-Text "[+] Preparing files and folders" -Start if ($PSEdition -eq 'core') { $Directories = @( $TempDirectories = Get-ChildItem -Path $FullProjectPath -Directory -Exclude $Exclude -FollowSymlink @( $TempDirectories $TempDirectories | Get-ChildItem -Directory -Recurse -FollowSymlink ) ) $Files = Get-ChildItem -Path $FullProjectPath -Exclude $Exclude -FollowSymlink | Get-ChildItem -File -Recurse -FollowSymlink $FilesRoot = Get-ChildItem -Path "$FullProjectPath\*" -Include '*.psm1', '*.psd1', 'License*' -File -FollowSymlink } else { $Directories = @( $TempDirectories = Get-ChildItem -Path $FullProjectPath -Directory -Exclude $Exclude @( $TempDirectories $TempDirectories | Get-ChildItem -Directory -Recurse ) ) $Files = Get-ChildItem -Path $FullProjectPath -Exclude '.*', 'Ignore', 'Examples', 'package.json', 'Publish', 'Docs' | Get-ChildItem -File -Recurse $FilesRoot = Get-ChildItem -Path "$FullProjectPath\*" -Include '*.psm1', '*.psd1', 'License*' -File } $LinkDirectories = @( foreach ($directory in $Directories) { $RelativeDirectoryPath = (Resolve-Path -LiteralPath $directory.FullName -Relative).Replace('.\', '') $RelativeDirectoryPath = "$RelativeDirectoryPath\" $RelativeDirectoryPath } ) $AllFiles = foreach ($File in $Files) { $RelativeFilePath = (Resolve-Path -LiteralPath $File.FullName -Relative).Replace('.\', '') $RelativeFilePath } $RootFiles = foreach ($File in $FilesRoot) { $RelativeFilePath = (Resolve-Path -LiteralPath $File.FullName -Relative).Replace('.\', '') $RelativeFilePath } # Link only files in Root Directory $LinkFilesRoot = @( foreach ($File in $RootFiles | Sort-Object -Unique) { switch -Wildcard ($file) { '*.psd1' { #Write-Color $File -Color Red # Add-ObjectTo -Object $File -Type 'Root Files List' $File } '*.psm1' { # Write-Color $File.FulllName -Color cd #Add-ObjectTo -Object $File -Type 'Root Files List' $File } 'License*' { #Add-ObjectTo -Object $File -Type 'Root Files List' $File } } } ) # Link only files from subfolers $LinkPrivatePublicFiles = @( foreach ($file in $AllFiles | Sort-Object -Unique) { switch -Wildcard ($file) { '*.ps1' { Add-FilesWithFolders -file $file -FullProjectPath $FullProjectPath -directory 'Private', 'Public', 'Enums' continue } '*.*' { Add-FilesWithFolders -file $file -FullProjectPath $FullProjectPath -directory 'Images\', 'Resources\', 'Templates\', 'Bin\', 'Lib\' continue } } } ) $LinkPrivatePublicFiles = $LinkPrivatePublicFiles | Select-Object -Unique Write-Text -End -Time $PreparingFilesTime $AliasesAndFunctions = Write-TextWithTime -Text '[+] Preparing function and aliases names' { Get-FunctionAliasesFromFolder -FullProjectPath $FullProjectPath -Files $Files #-Folder $Configuration.Information.AliasesToExport } if ($AliasesAndFunctions.Function) { $Configuration.Information.Manifest.FunctionsToExport = $AliasesAndFunctions.Function } if ($AliasesAndFunctions.Alias) { $Configuration.Information.Manifest.AliasesToExport = $AliasesAndFunctions.Alias } if (-not [string]::IsNullOrWhiteSpace($Configuration.Information.ScriptsToProcess)) { $StartsWithEnums = "$($Configuration.Information.ScriptsToProcess)\" $FilesEnums = @( $LinkPrivatePublicFiles | Where-Object { ($_).StartsWith($StartsWithEnums) } ) if ($FilesEnums.Count -gt 0) { #Write-Verbose "ScriptsToProcess export: $FilesEnums" Write-TextWithTime -Text "[+] ScriptsToProcess export $FilesEnums" $Configuration.Information.Manifest.ScriptsToProcess = $FilesEnums } #} } $PSD1FilePath = "$FullProjectPath\$ProjectName.psd1" New-PersonalManifest -Configuration $Configuration -ManifestPath $PSD1FilePath -AddScriptsToProcess Format-Code -FilePath $PSD1FilePath -FormatCode $Configuration.Options.Standard.FormatCodePSD1 } if ($Configuration.Steps.BuildModule.Merge) { foreach ($Directory in $LinkDirectories) { $Dir = "$FullTemporaryPath\$Directory" Add-Directory $Dir } # Workaround to link files that are not ps1/psd1 $LinkDirectoriesWithSupportFiles = $LinkDirectories | Where-Object { $_ -ne 'Public\' -and $_ -ne 'Private\' } foreach ($Directory in $LinkDirectoriesWithSupportFiles) { $Dir = "$FullModulePath\$Directory" Add-Directory $Dir } $LinkingFilesTime = Write-Text "[+] Linking files from root and sub directories" -Start Set-LinkedFiles -LinkFiles $LinkFilesRoot -FullModulePath $FullTemporaryPath -FullProjectPath $FullProjectPath Set-LinkedFiles -LinkFiles $LinkPrivatePublicFiles -FullModulePath $FullTemporaryPath -FullProjectPath $FullProjectPath Write-Text -End -Time $LinkingFilesTime # Workaround to link files that are not ps1/psd1 $FilesToLink = $LinkPrivatePublicFiles | Where-Object { $_ -notlike '*.ps1' -and $_ -notlike '*.psd1' } Set-LinkedFiles -LinkFiles $FilesToLink -FullModulePath $FullModulePath -FullProjectPath $FullProjectPath if (-not [string]::IsNullOrWhiteSpace($Configuration.Information.LibrariesCore)) { $StartsWithCore = "$($Configuration.Information.LibrariesCore)\" $FilesLibrariesCore = $LinkPrivatePublicFiles | Where-Object { ($_).StartsWith($StartsWithCore) } #$FilesLibrariesCore } if (-not [string]::IsNullOrWhiteSpace($Configuration.Information.LibrariesDefault)) { $StartsWithDefault = "$($Configuration.Information.LibrariesDefault)\" $FilesLibrariesDefault = $LinkPrivatePublicFiles | Where-Object { ($_).StartsWith($StartsWithDefault) } #$FilesLibrariesDefault } Merge-Module -ModuleName $ProjectName ` -ModulePathSource $FullTemporaryPath ` -ModulePathTarget $FullModulePath ` -Sort $Configuration.Options.Merge.Sort ` -FunctionsToExport $Configuration.Information.Manifest.FunctionsToExport ` -AliasesToExport $Configuration.Information.Manifest.AliasesToExport ` -LibrariesCore $FilesLibrariesCore ` -LibrariesDefault $FilesLibrariesDefault ` -FormatCodePSM1 $Configuration.Options.Merge.FormatCodePSM1 ` -FormatCodePSD1 $Configuration.Options.Merge.FormatCodePSD1 ` -Configuration $Configuration } else { foreach ($Directory in $LinkDirectories) { $Dir = "$FullModulePath\$Directory" Add-Directory $Dir } $LinkingFilesTime = Write-Text "[+] Linking files from root and sub directories" -Start Set-LinkedFiles -LinkFiles $LinkFilesRoot -FullModulePath $FullModulePath -FullProjectPath $FullProjectPath Set-LinkedFiles -LinkFiles $LinkPrivatePublicFiles -FullModulePath $FullModulePath -FullProjectPath $FullProjectPath Write-Text -End -Time $LinkingFilesTime } # Revers Path to current locatikon Set-Location -Path $CurrentLocation if ($DestinationPaths.Desktop) { Write-TextWithTime -Text "[+] Copy module to PowerShell 5 destination: $($DestinationPaths.Desktop)" { Remove-Directory -Directory $DestinationPaths.Desktop Add-Directory -Directory $DestinationPaths.Desktop Get-ChildItem -LiteralPath $FullModulePath | Copy-Item -Destination $DestinationPaths.Desktop -Recurse } } if ($DestinationPaths.Core) { Write-TextWithTime -Text "[+] Copy module to PowerShell 6/7 destination: $($DestinationPaths.Core)" { Remove-Directory -Directory $DestinationPaths.Core Add-Directory -Directory $DestinationPaths.Core Get-ChildItem -LiteralPath $FullModulePath | Copy-Item -Destination $DestinationPaths.Core -Recurse } } if ($Configuration.Steps.BuildModule.Releases) { $TagName = "v$($Configuration.Information.Manifest.ModuleVersion)" $FileName = -join ("$TagName", '.zip') $FolderPathReleases = [System.IO.Path]::Combine($FullProjectPath, 'Releases') $ZipPath = [System.IO.Path]::Combine($FullProjectPath, 'Releases', $FileName) Write-TextWithTime -Text "[+] Compressing final merged release $ZipPath" { $null = New-Item -ItemType Directory -Path $FolderPathReleases -Force if ($DestinationPaths.Desktop) { $CompressPath = [System.IO.Path]::Combine($DestinationPaths.Desktop, '*') Compress-Archive -Path $CompressPath -DestinationPath $ZipPath -Force } if ($DestinationPaths.Core -and -not $DestinationPaths.Desktop) { $CompressPath = [System.IO.Path]::Combine($DestinationPaths.Core, '*') Compress-Archive -Path $CompressPath -DestinationPath $ZipPath -Force } } if ($Configuration.Steps.PublishModule.GitHub) { if ($Configuration.Options.GitHub.FromFile) { $GitHubAccessToken = Get-Content -LiteralPath $Configuration.Options.GitHub.ApiKey } else { $GitHubAccessToken = $Configuration.Options.GitHub.ApiKey } if ($GitHubAccessToken) { if ($Configuration.Options.GitHub.RepositoryName) { $GitHubRepositoryName = $Configuration.Options.GitHub.RepositoryName } else { $GitHubRepositoryName = $ProjectName } if (Test-Path -LiteralPath $ZipPath) { <# $newGitHubReleaseParameters = @{ GitHubUsername = 'deadlydog' GitHubRepositoryName = 'New-GitHubRelease' GitHubAccessToken = 'SomeLongHexidecimalString' ReleaseName = "New-GitHubRelease v1.0.0" TagName = "v1.0.0" ReleaseNotes = "This release contains the following changes: ..." AssetFilePaths = @('C:\MyProject\Installer.exe','C:\MyProject\Documentation.md') IsPreRelease = $false IsDraft = $true # Set to true when testing so we don't publish a real release (visible to everyone) by accident. } #> if ($Configuration.Steps.PublishModule.Prerelease -ne '') { $IsPreRelease = $true } else { $IsPreRelease = $false } $StatusGithub = New-GitHubRelease -GitHubUsername $Configuration.Options.GitHub.UserName -GitHubRepositoryName $GitHubRepositoryName -GitHubAccessToken $GitHubAccessToken -TagName $TagName -AssetFilePaths $ZipPath -IsPreRelease $IsPreRelease if ($StatusGithub.ReleaseCreationSucceeded -and $statusGithub.Succeeded) { $GithubColor = 'Green' $GitHubText = '+' } else { $GithubColor = 'Red' $GitHubText = '-' } Write-Text "[$GitHubText] GitHub Release Creation Status: $($StatusGithub.ReleaseCreationSucceeded)" -Color $GithubColor Write-Text "[$GitHubText] GitHub Release Succeeded: $($statusGithub.Succeeded)" -Color $GithubColor Write-Text "[$GitHubText] GitHub Release Asset Upload Succeeded: $($statusGithub.AllAssetUploadsSucceeded)" -Color $GithubColor Write-Text "[$GitHubText] GitHub Release URL: $($statusGitHub.ReleaseUrl)" -Color $GithubColor if ($statusGithub.ErrorMessage) { Write-Text "[$GitHubText] GitHub Release ErrorMessage: $($statusGithub.ErrorMessage)" -Color $GithubColor } } } } } if ($Configuration.Steps.PublishModule.Enabled) { if ($Configuration.Options.PowerShellGallery.FromFile) { $ApiKey = Get-Content -Path $Configuration.Options.PowerShellGallery.ApiKey New-PublishModule -ProjectName $Configuration.Information.ModuleName -ApiKey $ApiKey -RequireForce $Configuration.Steps.PublishModule.RequireForce } else { New-PublishModule -ProjectName $Configuration.Information.ModuleName -ApiKey $Configuration.Options.PowerShellGallery.ApiKey -RequireForce $Configuration.Steps.PublishModule.RequireForce } } # Import Modules Section if ($Configuration) { $TemporaryVerbosePreference = $VerbosePreference $VerbosePreference = $false if ($Configuration.Options.ImportModules.RequiredModules) { Write-TextWithTime -Text '[+] Importing modules - REQUIRED' { foreach ($Module in $Configuration.Information.Manifest.RequiredModules) { Import-Module -Name $Module -Force -ErrorAction Stop -Verbose:$false #$Configuration.Options.ImportModules.Verbose } } } if ($Configuration.Options.ImportModules.Self) { Write-TextWithTime -Text '[+] Importing module - SELF' { Import-Module -Name $ProjectName -Force -ErrorAction Stop -Verbose:$false } } $VerbosePreference = $TemporaryVerbosePreference if ($Configuration.Steps.BuildDocumentation) { $WarningVariablesMarkdown = @() $DocumentationPath = "$FullProjectPath\$($Configuration.Options.Documentation.Path)" $ReadMePath = "$FullProjectPath\$($Configuration.Options.Documentation.PathReadme)" Write-Text "[+] Generating documentation to $DocumentationPath with $ReadMePath" -Color Yellow if (-not (Test-Path -Path $DocumentationPath)) { $null = New-Item -Path "$FullProjectPath\Docs" -ItemType Directory -Force } $Files = Get-ChildItem -Path $DocumentationPath if ($Files.Count -gt 0) { $null = Update-MarkdownHelpModule $DocumentationPath -RefreshModulePage -ModulePagePath $ReadMePath -ErrorAction Stop -WarningVariable +WarningVariablesMarkdown -WarningAction SilentlyContinue } else { $null = New-MarkdownHelp -Module $ProjectName -WithModulePage -OutputFolder $DocumentationPath -ErrorAction Stop -WarningVariable +WarningVariablesMarkdown -WarningAction SilentlyContinue $null = Move-Item -Path "$DocumentationPath\$ProjectName.md" -Destination $ReadMePath #Start-Sleep -Seconds 1 # this is temporary workaround - due to diff output on update if ($Configuration.Options.Documentation.UpdateWhenNew) { $null = Update-MarkdownHelpModule $DocumentationPath -RefreshModulePage -ModulePagePath $ReadMePath -ErrorAction Stop -WarningVariable +WarningVariablesMarkdown -WarningAction SilentlyContinue } # } foreach ($_ in $WarningVariablesMarkdown) { Write-Text "[-] Documentation warning: $_" -Color Yellow } } } # Cleanup temp directory Write-Text "[+] Cleaning up directories created in TEMP directory" -Color Yellow Remove-Directory $FullModulePath Remove-Directory $FullTemporaryPath } function Test-ReparsePoint { [CmdletBinding()] param ( [string]$path ) $file = Get-Item $path -Force -ea SilentlyContinue return [bool]($file.Attributes -band [IO.FileAttributes]::ReparsePoint) } function Write-PowerShellHashtable { [cmdletbinding()] <# .Synopsis Takes an creates a script to recreate a hashtable .Description Allows you to take a hashtable and create a hashtable you would embed into a script. Handles nested hashtables and indents nested hashtables automatically. .Parameter inputObject The hashtable to turn into a script .Parameter scriptBlock Determines if a string or a scriptblock is returned .Example # Corrects the presentation of a PowerShell hashtable @{Foo='Bar';Baz='Bing';Boo=@{Bam='Blang'}} | Write-PowerShellHashtable .Outputs [string] .Outputs [ScriptBlock] .Link https://github.com/StartAutomating/Pipeworks about_hash_tables #> [OutputType([string], [ScriptBlock])] param( [Parameter(Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [PSObject] $InputObject, # Returns the content as a script block, rather than a string [Alias('ScriptBlock')] [switch]$AsScriptBlock, # If set, items in the hashtable will be sorted alphabetically [Switch]$Sort ) process { $callstack = @(foreach ($_ in (Get-PSCallStack)) { if ($_.Command -eq "Write-PowerShellHashtable") { $_ } }) $depth = $callStack.Count if ($inputObject -isnot [Hashtable]) { $newInputObject = @{ PSTypeName = @($inputobject.pstypenames)[-1] } foreach ($prop in $inputObject.psobject.properties) { $newInputObject[$prop.Name] = $prop.Value } $inputObject = $newInputObject } if ($inputObject -is [Hashtable]) { #region Indent $scriptString = "" $indent = $depth * 4 $scriptString += "@{ " #endregion Indent #region Include $items = $inputObject.GetEnumerator() if ($Sort) { $items = $items | Sort-Object Key } foreach ($kv in $items) { $scriptString += " " * $indent $keyString = "$($kv.Key)" if ($keyString.IndexOfAny(" _.#-+:;()'!?^@#$%&".ToCharArray()) -ne -1) { if ($keyString.IndexOf("'") -ne -1) { $scriptString += "'$($keyString.Replace("'","''"))'=" } else { $scriptString += "'$keyString'=" } } elseif ($keyString) { $scriptString += "$keyString=" } $value = $kv.Value # Write-Verbose "$value" if ($value -is [string]) { $value = "'" + $value.Replace("'", "''").Replace("’", "’’").Replace("‘", "‘‘") + "'" } elseif ($value -is [ScriptBlock]) { $value = "{$value}" } elseif ($value -is [switch]) { $value = if ($value) { '$true' } else { '$false' } } elseif ($value -is [DateTime]) { $value = if ($value) { "[DateTime]'$($value.ToString("o"))'" } } elseif ($value -is [bool]) { $value = if ($value) { '$true' } else { '$false' } } elseif ($value -and $value.GetType -and ($value.GetType().IsArray -or $value -is [Collections.IList])) { $value = foreach ($v in $value) { if ($v -is [Hashtable]) { Write-PowerShellHashtable $v } elseif ($v -is [Object] -and $v -isnot [string]) { Write-PowerShellHashtable $v } else { ("'" + "$v".Replace("'", "''").Replace("’", "’’").Replace("‘", "‘‘") + "'") } } $oldOfs = $ofs $ofs = ",$(' ' * ($indent + 4))" $value = "$value" $ofs = $oldOfs } elseif ($value -as [Hashtable[]]) { $value = foreach ($v in $value) { Write-PowerShellHashtable $v } $value = $value -join "," } elseif ($value -is [Hashtable]) { $value = "$(Write-PowerShellHashtable $value)" } elseif ($value -as [Double]) { $value = "$value" } else { $valueString = "'$value'" if ($valueString[0] -eq "'" -and $valueString[1] -eq "@" -and $valueString[2] -eq "{") { $value = Write-PowerShellHashtable -InputObject $value } else { $value = $valueString } } $scriptString += "$value " } $scriptString += " " * ($depth - 1) * 4 $scriptString += "}" if ($AsScriptBlock) { [ScriptBlock]::Create($scriptString) } else { $scriptString } #endregion Include } } } function Write-Text { [CmdletBinding()] param( [string] $Text, [System.ConsoleColor] $Color = [System.ConsoleColor]::Cyan, [System.ConsoleColor] $ColorTime = [System.ConsoleColor]::Green, [switch] $Start, [switch] $End, [System.Diagnostics.Stopwatch] $Time ) if (-not $Start -and -not $End) { Write-Host "$Text" -ForegroundColor $Color } if ($Start) { Write-Host "$Text" -NoNewline -ForegroundColor $Color $Time = [System.Diagnostics.Stopwatch]::StartNew() } if ($End) { $TimeToExecute = $Time.Elapsed.ToString() Write-Host " [Time: $TimeToExecute]" -ForegroundColor $ColorTime $Time.Stop() } else { if ($Time) { return $Time } } } function Write-TextWithTime { [CmdletBinding()] param( [ScriptBlock] $Content, [string] $Text, [switch] $Continue, [System.ConsoleColor] $Color = [System.ConsoleColor]::Cyan, [System.ConsoleColor] $ColorTime = [System.ConsoleColor]::Green ) Write-Host "$Text" -NoNewline -ForegroundColor $Color $Time = [System.Diagnostics.Stopwatch]::StartNew() if ($null -ne $Content) { & $Content } $TimeToExecute = $Time.Elapsed.ToString() Write-Host " [Time: $TimeToExecute]" -ForegroundColor $ColorTime if (-not $Continue) { $Time.Stop() } } function Get-GitLog { # Source https://gist.github.com/thedavecarroll/3245449f5ff893e51907f7aa13e33ebe # Author: thedavecarroll/Get-GitLog.ps1 [CmdLetBinding(DefaultParameterSetName = 'Default')] param ( [Parameter(ParameterSetName = 'Default', Mandatory)] [Parameter(ParameterSetName = 'SourceTarget', Mandatory)] [ValidateScript( { Resolve-Path -Path $_ | Test-Path })] [string]$GitFolder, [Parameter(ParameterSetName = 'SourceTarget', Mandatory)] [string]$StartCommitId, [Parameter(ParameterSetName = 'SourceTarget')] [string]$EndCommitId = 'HEAD' ) Push-Location try { Set-Location -Path $GitFolder $GitCommand = Get-Command -Name git -ErrorAction Stop } catch { $PSCmdlet.ThrowTerminatingError($_) } if ($StartCommitId) { $GitLogCommand = '"{0}" log --oneline --format="%H`t%h`t%ai`t%an`t%ae`t%ci`t%cn`t%ce`t%s`t%f" {1}...{2} 2>&1' -f $GitCommand.Source, $StartCommitId, $EndCommitId } else { $GitLogCommand = '"{0}" log --oneline --format="%H`t%h`t%ai`t%an`t%ae`t%ci`t%cn`t%ce`t%s`t%f" 2>&1' -f $GitCommand.Source } Write-Verbose -Message $GitLogCommand $GitLog = Invoke-Expression -Command "& $GitLogCommand" -ErrorAction SilentlyContinue Pop-Location if ($GitLog[0] -notmatch 'fatal:') { $GitLog | ConvertFrom-Csv -Delimiter "`t" -Header 'CommitId', 'ShortCommitId', 'AuthorDate', 'AuthorName', 'AuthorEmail', 'CommitterDate', 'CommitterName', 'ComitterEmail', 'CommitMessage', 'SafeCommitMessage' } else { if ($GitLog[0] -like "fatal: ambiguous argument '*...*'*") { Write-Warning -Message 'Unknown revision. Please check the values for StartCommitId or EndCommitId; omit the parameters to retrieve the entire log.' } else { Write-Error -Category InvalidArgument -Message ($GitLog -join "`n") } } } function Get-MissingFunctions { [CmdletBinding()] param( [alias('Path')][string] $FilePath, [string[]] $Functions, [switch] $Summary, [switch] $SummaryWithCommands, [Array] $ApprovedModules ) $ListCommands = [System.Collections.Generic.List[Object]]::new() $Result = Get-ScriptCommands -FilePath $FilePath -CommandsOnly $FilteredCommands = Get-FilteredScriptCommands -Commands $Result -NotUnknown -NotCmdlet -Functions $Functions -NotApplication -FilePath $FilePath foreach ($_ in $FilteredCommands) { $ListCommands.Add($_) } # this gets commands along their ScriptBlock Get-RecursiveCommands -Commands $FilteredCommands $FunctionsOutput = foreach ($_ in $ListCommands) { if ($ApprovedModules.Count -gt 0 -and $_.Source -in $ApprovedModules) { "function $($_.Name) { $($_.ScriptBlock) }" } if ($ApprovedModules.Count -eq 0) { "function $($_.Name) { $($_.ScriptBlock) }" } } if ($SummaryWithCommands) { $Hash = @{ Summary = $ListCommands Functions = $FunctionsOutput } return $Hash } elseif ($Summary) { return $ListCommands } else { return $FunctionsOutput } } function New-PrepareModule { [alias('New-BuildModule')] [CmdletBinding()] param ( [System.Collections.IDictionary] $Configuration ) if (-not $Configuration) { return } $GlobalTime = [System.Diagnostics.Stopwatch]::StartNew() if (-not $Configuration.Information.DirectoryModulesCore) { $Configuration.Information.DirectoryModulesCore = "$Env:USERPROFILE\Documents\PowerShell\Modules" } if (-not $Configuration.Information.DirectoryModules) { $Configuration.Information.DirectoryModules = "$Env:USERPROFILE\Documents\WindowsPowerShell\Modules" } if ($Configuration.Steps.BuildModule.Enable -or $Configuration.Steps.BuildModule.EnableDesktop -or $Configuration.Steps.BuildModule.EnableCore) { Start-ModuleBuilding -Configuration $Configuration } $Execute = "$($GlobalTime.Elapsed.Days) days, $($GlobalTime.Elapsed.Hours) hours, $($GlobalTime.Elapsed.Minutes) minutes, $($GlobalTime.Elapsed.Seconds) seconds, $($GlobalTime.Elapsed.Milliseconds) milliseconds" Write-Host "[i] Module Building " -NoNewline -ForegroundColor Yellow Write-Host "[Time Total: $Execute]" -ForegroundColor Green } function Remove-Comments { # We are not restricting scriptblock type as Tokenize() can take several types Param ( [string] $FilePath, [parameter( ValueFromPipeline = $True )] $Scriptblock, [string] $ScriptContent ) if ($PSBoundParameters['FilePath']) { $ScriptBlockString = [IO.File]::ReadAllText((Resolve-Path $FilePath)) $ScriptBlock = [ScriptBlock]::Create($ScriptBlockString) } elseif ($PSBoundParameters['ScriptContent']) { $ScriptBlock = [ScriptBlock]::Create($ScriptContent) } else { # Convert the scriptblock to a string so that it can be referenced with array notation #$ScriptBlockString = $ScriptBlock.ToString() } # Convert input to a single string if needed $OldScript = $ScriptBlock -join [environment]::NewLine # If no work to do # We're done If ( -not $OldScript.Trim( " `n`r`t" ) ) { return } # Use the PowerShell tokenizer to break the script into identified tokens $Tokens = [System.Management.Automation.PSParser]::Tokenize( $OldScript, [ref]$Null ) # Define useful, allowed comments $AllowedComments = @( 'requires' '.SYNOPSIS' '.DESCRIPTION' '.PARAMETER' '.EXAMPLE' '.INPUTS' '.OUTPUTS' '.NOTES' '.LINK' '.COMPONENT' '.ROLE' '.FUNCTIONALITY' '.FORWARDHELPCATEGORY' '.REMOTEHELPRUNSPACE' '.EXTERNALHELP' ) # Strip out the Comments, but not useful comments # (Bug: This will break comment-based help that uses leading # instead of multiline <#, # because only the headings will be left behind.) $Tokens = $Tokens.ForEach{ If ( $_.Type -ne 'Comment' ) { $_ } Else { $CommentText = $_.Content.Substring( $_.Content.IndexOf( '#' ) + 1 ) $FirstInnerToken = [System.Management.Automation.PSParser]::Tokenize( $CommentText, [ref]$Null ) | Where-Object { $_.Type -ne 'NewLine' } | Select-Object -First 1 If ( $FirstInnerToken.Content -in $AllowedComments ) { $_ } } } # Initialize script string #$NewScriptText = '' $SkipNext = $False $ScriptProcessing = @( # If there are at least 2 tokens to process... If ( $Tokens.Count -gt 1 ) { # For each token (except the last one)... ForEach ( $i in ( 0..($Tokens.Count - 2) ) ) { # If token is not a line continuation and not a repeated new line or semicolon... If (-not $SkipNext -and $Tokens[$i ].Type -ne 'LineContinuation' -and ( $Tokens[$i ].Type -notin ( 'NewLine', 'StatementSeparator' ) -or $Tokens[$i + 1].Type -notin ( 'NewLine', 'StatementSeparator', 'GroupEnd' ) ) ) { # Add Token to new script # For string and variable, reference old script to include $ and quotes If ( $Tokens[$i].Type -in ( 'String', 'Variable' ) ) { $OldScript.Substring( $Tokens[$i].Start, $Tokens[$i].Length ) } Else { $Tokens[$i].Content } # If the token does not never require a trailing space # And the next token does not never require a leading space # And this token and the next are on the same line # And this token and the next had white space between them in the original... If ($Tokens[$i ].Type -notin ( 'NewLine', 'GroupStart', 'StatementSeparator' ) -and $Tokens[$i + 1].Type -notin ( 'NewLine', 'GroupEnd', 'StatementSeparator' ) -and $Tokens[$i].EndLine -eq $Tokens[$i + 1].StartLine -and $Tokens[$i + 1].StartColumn - $Tokens[$i].EndColumn -gt 0 ) { # Add a space to new script ' ' } # If the next token is a new line or semicolon following # an open parenthesis or curly brace, skip it $SkipNext = $Tokens[$i].Type -eq 'GroupStart' -and $Tokens[$i + 1].Type -in ( 'NewLine', 'StatementSeparator' ) } # Else (Token is a line continuation or a repeated new line or semicolon)... Else { # [Do not include it in the new script] # If the next token is a new line or semicolon following # an open parenthesis or curly brace, skip it $SkipNext = $SkipNext -and $Tokens[$i + 1].Type -in ( 'NewLine', 'StatementSeparator' ) } } } # If there is a last token to process... If ( $Tokens ) { # Add last token to new script # For string and variable, reference old script to include $ and quotes If ( $Tokens[$i].Type -in ( 'String', 'Variable' ) ) { $OldScript.Substring( $Tokens[-1].Start, $Tokens[-1].Length ) } Else { $Tokens[-1].Content } } ) [string] $NewScriptText = $ScriptProcessing -join '' # Trim any leading new lines from the new script $NewScriptText = $NewScriptText.TrimStart( "`n`r;" ) #return [scriptblock]::Create( $NewScriptText ) # Return the new script as the same type as the input If ( $Scriptblock.Count -eq 1 ) { If ( $Scriptblock[0] -is [scriptblock] ) { # Return single scriptblock return [scriptblock]::Create( $NewScriptText ) } Else { # Return single string return $NewScriptText } } Else { # Return array of strings return $NewScriptText.Split( "`n`r", [System.StringSplitOptions]::RemoveEmptyEntries ) } } Function Test-ScriptFile { <# .Synopsis Test a PowerShell script for cmdlets .Description This command will analyze a PowerShell script file and display a list of detected commands such as PowerShell cmdlets and functions. Commands will be compared to what is installed locally. It is recommended you run this on a Windows 8.1 client with the latest version of RSAT installed. Unknown commands could also be internally defined functions. If in doubt view the contents of the script file in the PowerShell ISE or a script editor. You can test any .ps1, .psm1 or .txt file. .Parameter Path The path to the PowerShell script file. You can test any .ps1, .psm1 or .txt file. .Example PS C:\> test-scriptfile C:\scripts\Remove-MyVM2.ps1 CommandType Name ModuleName ----------- ---- ---------- Cmdlet Disable-VMEventing Hyper-V Cmdlet ForEach-Object Microsoft.PowerShell.Core Cmdlet Get-VHD Hyper-V Cmdlet Get-VMSnapshot Hyper-V Cmdlet Invoke-Command Microsoft.PowerShell.Core Cmdlet New-PSSession Microsoft.PowerShell.Core Cmdlet Out-Null Microsoft.PowerShell.Core Cmdlet Out-String Microsoft.PowerShell.Utility Cmdlet Remove-Item Microsoft.PowerShell.Management Cmdlet Remove-PSSession Microsoft.PowerShell.Core Cmdlet Remove-VM Hyper-V Cmdlet Remove-VMSnapshot Hyper-V Cmdlet Write-Debug Microsoft.PowerShell.Utility Cmdlet Write-Verbose Microsoft.PowerShell.Utility Cmdlet Write-Warning Microsoft.PowerShell.Utility .Notes Original script provided by Jeff Hicks at (https://www.petri.com/powershell-problem-solver-find-script-commands) # https://twitter.com/donnie_taylor/status/1160920407031058432 Test-ScriptFile -Path 'C:\Users\przemyslaw.klys\Documents\WindowsPowerShell\Modules\PSWinReportingV2\PSWinReportingV2.psm1' | Sort-Object -Property Source, Name | ft -AutoSize #> [cmdletbinding()] Param( [Parameter(Position = 0, Mandatory = $True, HelpMessage = "Enter the path to a PowerShell script file,", ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)] [ValidatePattern( "\.(ps1|psm1|txt)$")] [ValidateScript( { Test-Path $_ })] [string]$Path ) Begin { Write-Verbose "Starting $($MyInvocation.Mycommand)" Write-Verbose "Defining AST variables" New-Variable astTokens -force New-Variable astErr -force } Process { Write-Verbose "Parsing $path" $null = [System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$astTokens, [ref]$astErr) #group tokens and turn into a hashtable $h = $astTokens | Group-Object tokenflags -AsHashTable -AsString $commandData = $h.CommandName | Where-Object { $_.text -notmatch "-TargetResource$" } | ForEach-Object { Write-Verbose "Processing $($_.text)" Try { $cmd = $_.Text $resolved = $cmd | Get-Command -ErrorAction Stop if ($resolved.CommandType -eq 'Alias') { Write-Verbose "Resolving an alias" #manually handle "?" because Get-Command and Get-Alias won't. Write-Verbose "Detected the Where-Object alias '?'" if ($cmd -eq '?') { Get-Command Where-Object } else { # Since we're dealing with alias we need to recheck $Resolved = $resolved.ResolvedCommandName | Get-Command [PSCustomobject]@{ CommandType = $resolved.CommandType Name = $resolved.Name ModuleName = $resolved.ModuleName Source = $resolved.Source } } } else { #$resolved [PSCustomobject]@{ CommandType = $resolved.CommandType Name = $resolved.Name ModuleName = $resolved.ModuleName Source = $resolved.Source } } } Catch { Write-Verbose "Command is not recognized" #create a custom object for unknown commands [PSCustomobject]@{ CommandType = "Unknown" Name = $cmd ModuleName = "Unknown" Source = "Unknown" } } } $CommandData } End { Write-Verbose -Message "Ending $($MyInvocation.Mycommand)" } } function Test-ScriptModule { [cmdletbinding()] param( [string] $ModuleName, [ValidateSet('Name', 'CommandType', 'ModuleName', 'Source')] $SortName, [switch] $Unique ) $Module = Get-Module -ListAvailable $ModuleName $Path = Join-Path -Path $Module.ModuleBase -ChildPath $Module.RootModule $Output = Test-ScriptFile -Path $Path if ($Unique) { $Output = $Output | Sort-Object -Property 'Name' -Unique:$Unique } if ($SortName) { $Output | Sort-Object -Property $SortName } else { $Output } } Export-ModuleMember ` -Function @('Get-GitLog', 'Get-MissingFunctions', 'New-PrepareModule', 'Remove-Comments', 'Test-ScriptFile', 'Test-ScriptModule') ` -Alias @('New-BuildModule') |