From b625193e997023c34e7e77019e675942f9c8ef85 Mon Sep 17 00:00:00 2001 From: Kevin Marquette Date: Wed, 17 Oct 2018 11:12:21 -0700 Subject: [PATCH] add temporary Move-Statement #3 --- BuildTasks/BuildModule.Task.ps1 | 123 ++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) diff --git a/BuildTasks/BuildModule.Task.ps1 b/BuildTasks/BuildModule.Task.ps1 index 7b2fc88..5b3247c 100644 --- a/BuildTasks/BuildModule.Task.ps1 +++ b/BuildTasks/BuildModule.Task.ps1 @@ -1,4 +1,9 @@ +# namespaces for Move-Statement +using namespace System.Collections.Generic +using namespace System.IO +using namespace System.Management.Automation + function Import-ClassOrder { [cmdletbinding()] @@ -18,6 +23,124 @@ function Import-ClassOrder } } +# Temporarily added this here to be refactored/replaced by LDModuleBuilder Module + + +function Move-Statement +{ +<# +.SYNOPSIS + Moves statements containing a specified token to the specified index in a file. +.DESCRIPTION + Move-Statement moves statements containing a specified token, to the specified index + in a file. This can be used when building a module to move any using directives and + #Requires statements to the top of a file. +.PARAMETER Path + Specifies the path to an item to get its contents. +.PARAMETER Type + Specifies the type of tokens to examine. Accepted values include "Comment" and "Keyword". +.PARAMETER Token + Specifies the contents to filter on when examining a supplied token. +.PARAMETER Index + Specifies the line to move a statement to. Each line in an item has a corresponding + index, starting from 0. +.EXAMPLE + Move-Statement -Path $Path -Type 'Comment', 'Keyword' -Token '#Requires', 'using' -Index 0 + + Moves any using directives or #Requires statements to the top of a file. +.NOTES + Copy/Paste from LDModuleBuilder +#> + [CmdletBinding(SupportsShouldProcess)] + + param( + [Parameter(Mandatory, + Position = 0, + ValueFromPipeline, + ValueFromPipelineByPropertyName)] + [ValidateNotNullOrEmpty()] + [ValidateScript({ Test-Path -Path $PSItem })] + [string] $Path, + + [Parameter(Position = 1, + ValueFromPipelineByPropertyName)] + [ValidateNotNullOrEmpty()] + [ValidateSet('Comment', 'Keyword')] + [string[]] $Type = ('Comment', 'Keyword'), + + [Parameter(Position = 2, + ValueFromPipelineByPropertyName)] + [ValidateNotNullOrEmpty()] + [string[]] $Token = ('#Requires', 'using'), + + [Parameter(Position = 3, + ValueFromPipelineByPropertyName)] + [ValidateNotNullOrEmpty()] + [int] $Index = 0 + ) + + process + { + try + { + $statements = [SortedSet[String]]::new( + [StringComparer]::InvariantCultureIgnoreCase + ) + + Write-Verbose -Message "Reading content from $Path..." + $content = [List[String]] ([File]::ReadLines($Path)) + + Write-Verbose -Message "Tokenizing content from $Path..." + $tokens = [PSParser]::Tokenize($content, [ref] $null) + + $match = $Token -join '|' + + Write-Verbose -Message 'Matching tokens...' + Write-Verbose -Message "Type = [$Type]; Token = [$Token]" + $keywords = $tokens.Where({ + $PSItem.Type -in $Type -and + $PSItem.Content -imatch "^(?:$match)" + }) + + if (-not $keywords) { + Write-Verbose -Message 'No matching tokens found! Returning...' + return + } + + $offset = 1 + foreach ($keyword in $keywords) + { + $line = $keyword.StartLine - $offset + + Write-Verbose -Message "Moving [$($content[$line])] to Index [$Index]..." + $null = $statements.Add($content[$line]), + $content.RemoveAt($line) + $offset++ + } + + [string[]] $comments, [string[]] $statements = $statements.Where({ + $PSItem -match '^#' + }, 'Split') + + foreach ($item in ($statements, $comments)) + { + $content.Insert($Index, '') + $content.InsertRange($Index, $item) + } + + if ($PSCmdlet.ShouldProcess($Path, $MyInvocation.MyCommand.Name)) + { + Write-Verbose -Message "Writing content to $Path..." + [File]::WriteAllLines($Path, $content) + } + } + catch + { + $PSCmdlet.ThrowTerminatingError($PSItem) + } + } +} + taskx BuildModule @{ Inputs = (Get-ChildItem -Path $Source -Recurse -Filter *.ps1) Outputs = $ModulePath