-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathConvert-ExternalScriptToModule.ps1
More file actions
74 lines (69 loc) · 2.57 KB
/
Copy pathConvert-ExternalScriptToModule.ps1
File metadata and controls
74 lines (69 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<#
.SYNOPSIS
Convert a script from external script usage to module cmdlet usage.
.FUNCTIONALITY
PowerShell
.INPUTS
System.String containing the path of the script to update.
#>
#Requires -Version 7
using namespace System.Collections.Generic
using namespace System.IO
using namespace System.Management.Automation.Language
using module Detextive
[CmdletBinding()] Param(
# The module to use instead of external scripts.
[Parameter(Position=0)][string] $ModuleName = 'ModernConveniences',
# The script to update to module usage.
[Parameter(Position=1,ValueFromPipelineByPropertyName=$true)][Alias('FullName')][string] $Path = '*.ps1'
)
Begin
{
filter Update-ScriptFile
{
[CmdletBinding()] Param(
[Parameter(ValueFromPipelineByPropertyName=$true,Mandatory=$true)][FileInfo[]] $Values,
[Parameter(ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
[System.Collections.ObjectModel.Collection[psobject]] $Group
)
if($Values.Count -ne 1) {throw "Unexpected grouping of $($Values.Count) files."}
$ps1,$cmd = $Values.FullName,$Values.BaseName
[int] $usingOffset = Get-ScriptTokens $ps1 |
Where-Object Kind -notin 'Comment','NewLine' |
Select-Object -First 1 |
ForEach-Object {$_.Extent.StartOffset}
$encoding = Get-FileEncoding $ps1
$script = Get-Content $ps1 -Raw
$Group |Sort-Object StartOffset -Descending |ForEach-Object {
$start,$end = $_.StartOffset,$_.EndOffset
$script = $script.Remove($start, $end - $start).Insert($start, (Split-Path $_.Text -LeafBase))
}
$script.Insert($usingOffset, "using module $ModuleName`r`n").Trim() |
Out-File $ps1 -Encoding $encoding
}
function Find-ExternalScriptUsage
{
[CmdletBinding()] Param(
[Parameter(Position=0,Mandatory=$true)][string] $Path
)
[string[]] $cmdlets = Get-Command -Module $ModuleName |Select-Object -ExpandProperty Name
if(!$cmdlets) {throw "Could not find any cmdlets in $ModuleName. Did you import it?"; return}
return Select-ScriptCommands $Path -CommandType ExternalScript |
Where-Object {(Split-Path $_.CommandName -LeafBase) -iin $cmdlets} |
ForEach-Object {$_.Tokens.Extent} |
Group-Object {Get-Item $_.File}
}
function Write-OutstandingWarning
{
[CmdletBinding()] Param(
[Parameter(Position=0,Mandatory=$true)][string] $Path
)
[string[]] $cmdlets = Get-Command -Module $ModuleName |Select-Object -ExpandProperty Name
Select-String -Pattern "\b(?:$($cmdlets -join '|'))\.ps1\b" -Path $Path |Write-Warning
}
}
Process
{
Find-ExternalScriptUsage $Path |Update-ScriptFile
Write-OutstandingWarning $Path
}