Skip to content

Commit 9276e88

Browse files
author
FriedrichWeinmann
committed
Added new command
1 parent 7e6eb88 commit 9276e88

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

PSModuleDevelopment/PSModuleDevelopment.psproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<File Build="2" Shared="True" ReferenceFunction="Invoke-Rename-PSMDParameter_ps1">functions\refactor\Rename-PSMDParameter.ps1</File>
4747
<File Build="2" Shared="True" ReferenceFunction="Invoke-Expand-PSMDTypeName_ps1">functions\assembly\Expand-PSMDTypeName.ps1</File>
4848
<File Build="2" Shared="True" ReferenceFunction="Invoke-Set-PSMDParameterHelp_ps1">functions\refactor\Set-PSMDParameterHelp.ps1</File>
49+
<File Build="2" Shared="True" ReferenceFunction="Invoke-Split-PSMDScriptFile_ps1">functions\refactor\Split-PSMDScriptFile.ps1</File>
4950
</Files>
5051
<StartupScript>F:\Synchronized Data\Scripte\Powershell Studio\Projects\PSModuleDevelopment\Test-Module.ps1</StartupScript>
5152
</Project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
function Split-PSMDScriptFile
2+
{
3+
<#
4+
.SYNOPSIS
5+
Parses a file and exports all top-level functions from it into a dedicated file, just for the function.
6+
7+
.DESCRIPTION
8+
Parses a file and exports all top-level functions from it into a dedicated file, just for the function.
9+
The original file remains unharmed by this.
10+
11+
Note: Any comments outside the function definition will not be copied.
12+
13+
.PARAMETER File
14+
The file(s) to extract functions from.
15+
16+
.PARAMETER Path
17+
The folder to export to
18+
19+
.PARAMETER Encoding
20+
Default: UTF8
21+
The output encoding. Can usually be left alone.
22+
23+
.EXAMPLE
24+
PS C:\> Split-PSMDScriptFile -File ".\module.ps1" -Path .\files
25+
26+
Exports all functions in module.ps1 and puts them in individual files in the folder .\files.
27+
#>
28+
[CmdletBinding()]
29+
Param (
30+
[Parameter(ValueFromPipeline = $true)]
31+
[string[]]
32+
$File,
33+
34+
[string]
35+
$Path,
36+
37+
$Encoding = "UTF8"
38+
)
39+
40+
process
41+
{
42+
foreach ($item in $File)
43+
{
44+
$a = $null
45+
$b = $null
46+
$ast = [System.Management.Automation.Language.Parser]::ParseFile((Resolve-Path $item), [ref]$a, [ref]$b)
47+
48+
foreach ($functionAst in ($ast.EndBlock.Statements | Where-Object { $_.GetType().FullName -eq "System.Management.Automation.Language.FunctionDefinitionAst" }))
49+
{
50+
$ast.Extent.Text.Substring($functionAst.Extent.StartOffset, ($functionAst.Extent.EndOffset - $functionAst.Extent.StartOffset)) | Set-Content "$Path\$($functionAst.Name).ps1" -Encoding UTF8
51+
}
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)