Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/proxb/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
codykonior committed Feb 5, 2018
2 parents 3ba56cb + 2a519c1 commit ca402b2
Show file tree
Hide file tree
Showing 18 changed files with 542 additions and 366 deletions.
25 changes: 12 additions & 13 deletions PoshRSJob/PoshRSJob.psd1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#
# PoshRSJob
# Version 1.7.3.11
# Version 1.7.4.1
#
# Boe Prox (c) 2014
# http://learn-powershell.net
Expand All @@ -14,7 +14,7 @@
ModuleToProcess = 'PoshRSJob.psm1'

# Version number of this module.
ModuleVersion = '1.7.3.11'
ModuleVersion = '1.7.4.1'

# ID used to uniquely identify this module
GUID = '9b17fb0f-e939-4a5c-b194-3f2247452972'
Expand Down Expand Up @@ -84,9 +84,9 @@ AliasesToExport = 'gsj','rmsj','rsj','spsj','ssj','wsj'
#ModuleList = @()

# List of all files packaged with this module
FileList = 'PoshRSJob.psd1', 'PoshRSJob.psm1', 'en-US\about_PoshRSJob.help.txt', 'Private\ConvertScript.ps1', 'Private\ConvertScriptBlockV2.ps1',
'Private\FindFunction.ps1', 'Private\GetParamVariable.ps1', 'Private\GetUsingVariables.ps1', 'Private\GetUsingVariablesV2.ps1', 'Private\GetUsingVariableValues.ps1',
'Private\Increment.ps1', 'Private\IsExistingParamBlock.ps1', 'Private\RegisterScriptScopeFunction.ps1', 'Public\Get-RSJob.ps1', 'Public\Receive-RSJob.ps1',
FileList = 'PoshRSJob.psd1', 'PoshRSJob.psm1', 'en-US\about_PoshRSJob.help.txt', 'Private\ConvertScript.ps1', 'Private\ConvertScriptBlockV2.ps1',
'Private\FindFunction.ps1', 'Private\GetFunctionByFile.ps1', 'Private\GetFunctionDefinitionByFunction.ps1', 'Private\GetParamVariable.ps1', 'Private\GetUsingVariables.ps1', 'Private\GetUsingVariablesV2.ps1',
'Private\Increment.ps1', 'Private\RegisterScriptScopeFunction.ps1', 'Public\Get-RSJob.ps1', 'Public\Receive-RSJob.ps1',
'Public\Remove-RSJob.ps1', 'Public\Start-RSJob.ps1', 'Public\Stop-RSJob.ps1', 'Public\Wait-RSJob.ps1', 'TypeData\PoshRSJob.Format.ps1xml', 'TypeData\PoshRSJob.Types.ps1xml',
'Private\SetIsReceived.ps1'

Expand All @@ -95,28 +95,27 @@ PrivateData = @{
PSData = @{
# The primary categorization of this module (from the TechNet Gallery tech tree).
Category = "Multithreading"

# Keyword tags to help users find this module via navigations and search.
Tags = @('PoshRSJob', 'Runspace','RunspacePool', 'Linux', 'PowerShellCore', 'RSJob')

# The web address of an icon which can be used in galleries to represent this module
#IconUri = ''

# The web address of this module's project or support homepage.
ProjectUri = "https://github.com/proxb/PoshRSJob"

# The web address of this module's license. Points to a page that's embeddable and linkable.
LicenseUri = "https://opensource.org/licenses/MIT"

# Release notes for this particular version of the module
# ReleaseNotes = False

# If true, the LicenseUrl points to an end-user license (not just a source license) which requires the user agreement before use.
RequireLicenseAcceptance = "False"

# Indicates this is a pre-release/testing version of the module.
IsPrerelease = 'False'
}
}
}

17 changes: 11 additions & 6 deletions PoshRSJob/Private/ConvertScript.ps1
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
Function ConvertScript {
Param (
[scriptblock]$ScriptBlock
[scriptblock]$ScriptBlock,
[bool]$HasParam,
$UsingVariables,
$UsingVariableValues,
[bool]$InsertPSItem = $false
)
$UsingVariables = @(GetUsingVariables -ScriptBlock $ScriptBlock)
# $HasParam unused
$List = New-Object 'System.Collections.Generic.List`1[System.Management.Automation.Language.VariableExpressionAst]'
$Params = New-Object System.Collections.ArrayList
If ($Script:Add_) {
If ($InsertPSItem) {
[void]$Params.Add('$_')
}
If ($UsingVariables) {
ForEach ($Ast in $UsingVariables) {
[void]$list.Add($Ast.SubExpression)
}
$UsingVariableData = @(GetUsingVariableValues $UsingVariables)
[void]$Params.AddRange(@($UsingVariableData.NewName | Select-Object -Unique))
}
}
if ($UsingVariableValues) {
[void]$Params.AddRange(@($UsingVariableValues.NewName))
}
$NewParams = $Params -join ', '
$Tuple=[Tuple]::Create($list,$NewParams)
$bindingFlags = [Reflection.BindingFlags]"Default,NonPublic,Instance"
Expand Down
20 changes: 10 additions & 10 deletions PoshRSJob/Private/ConvertScriptBlockV2.ps1
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
Function ConvertScriptBlockV2 {
Param (
[scriptblock]$ScriptBlock,
$UsingVariable,
$UsingVariableValue
[bool]$HasParam,
$UsingVariables,
$UsingVariableValues,
[bool]$InsertPSItem = $false
)
$UsingVariables = $UsingVariable
$UsingVariable = $UsingVariableValue
# $UsingVariables unused
$errors = [System.Management.Automation.PSParseError[]] @()
$Tokens = [Management.Automation.PsParser]::Tokenize($ScriptBlock.tostring(), [ref] $errors)
$StringBuilder = New-Object System.Text.StringBuilder
$UsingHash = @{}
$UsingVariable | ForEach-Object {
$UsingVariableValues | ForEach-Object {
$UsingHash["Using:$($_.Name)"] = $_.NewVarName
}
$HasParam = IsExistingParamBlock -ScriptBlock $ScriptBlock
$Params = New-Object System.Collections.ArrayList
If ($Script:Add_) {
If ($InsertPSItem) {
[void]$Params.Add('$_')
}
If ($UsingVariable) {
[void]$Params.AddRange(@($UsingVariable | Select-Object -ExpandProperty NewName))
}
If ($UsingVariableValues) {
[void]$Params.AddRange(@($UsingVariableValues | Select-Object -ExpandProperty NewName))
}
$NewParams = $Params -join ', '
If (-Not $HasParam) {
[void]$StringBuilder.Append("Param($($NewParams))")
Expand Down
22 changes: 11 additions & 11 deletions PoshRSJob/Private/FindFunction.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Function FindFunction {
[CmdletBinding()]
param (
[string]$ScriptBlock
)
)
#Just in case we have some oddness going on
$ScriptBlock = $ScriptBlock -replace '`','``'
# Tokenize the script
Expand All @@ -25,22 +25,22 @@ Function FindFunction {
}
if ($functionsearch) {
If ($i -gt 1 -AND ($tokens[$i].StartLine -eq $tokens[$i-1].EndLine)) {
$SpaceCount = $tokens[$i].StartColumn - $tokens[$i-1].EndColumn
$SpaceCount = $tokens[$i].StartColumn - $tokens[$i-1].EndColumn
$space = ' '*"$($SpaceCount)"
If ($SpaceCount -gt 0) {
If ($SpaceCount -notmatch '^[5|9]$') {
Write-Verbose "Adding Space: $($SpaceCount)"
Write-Verbose "Adding Space: $($SpaceCount)"
[void]$Definition.Append($Space)
} ElseIf ($SpaceCount -match '^[5|9]$') {
Write-Verbose "Adding NewLine"
Write-Verbose "Adding NewLine"
[void]$Definition.Append("`n")
}
}
}
Write-Verbose $tokens[$i].Content
Write-Verbose $tokens[$i].Content
Switch ($tokens[$i].Type) {
'NewLine' {
Write-Verbose 'Adding NewLine'
Write-Verbose 'Adding NewLine'
[void]$Definition.Append("`n")
}
'CommandArgument' {
Expand Down Expand Up @@ -71,17 +71,17 @@ Function FindFunction {
[void]$Definition.Append($tokens[$i].Content)
}
'Variable' {
[void]$Definition.Append("`$$($tokens[$i].Content)")
[void]$Definition.Append("`$$($tokens[$i].Content)")
}
'Type' {
Switch ($PSVersionTable.PSVersion.Major) {
'2' {
[void]$Definition.Append("[$($tokens[$i].Content)]")
[void]$Definition.Append("[$($tokens[$i].Content)]")
}
Default {
[void]$Definition.Append($($tokens[$i].Content))
[void]$Definition.Append($($tokens[$i].Content))
}
}
}
}
Default {
[void]$Definition.Append($tokens[$i].Content)
Expand All @@ -97,4 +97,4 @@ Function FindFunction {
}
}
}
}
}
35 changes: 35 additions & 0 deletions PoshRSJob/Private/GetFunctionByFile.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Function GetFunctionByFile {
[CmdletBinding()]
param (
[string[]]$FilePath
)

$psMajorVersion = $PSVersionTable.PSVersion.Major
$functionsInFile = @()
ForEach ($thisFilePath in $FilePath) {
Write-Verbose "Working on file : $thisFilePath"

if (-not (Test-Path $thisFilePath)) {
Write-Warning "Cannot find file : $thisFilePath"
continue
}

try {
Switch ($psMajorVersion) {
'2' {
$scriptBlockInFile = [ScriptBlock]::Create($(Get-Content $thisFilePath) -join [Environment]::NewLine)
$functionsInFile += @(FindFunction -ScriptBlock $scriptBlockInFile)
}
Default {
$AST = [System.Management.Automation.Language.Parser]::ParseFile($thisFilePath, [ref]$null, [ref]$null)
$functionsInFile += $AST.FindAll( {$args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst]} , $true)
}
}
Write-Verbose "Functions found in file : $($functionsInFile.Name -join '; ')"
}
catch {
Write-Warning "$thisFilePath : $($_.Exception.Message)"
}
}
$functionsInFile
}
16 changes: 16 additions & 0 deletions PoshRSJob/Private/GetFunctionDefinitionByFunction.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Function GetFunctionDefinitionByFunction {
[CmdletBinding()]
param (
[parameter(ValueFromPipeline = $True)]
$FunctionItem
)

if ($FunctionItem -is [PSCustomObject]) {
# In case of Powershell v2
$function.Body.Trim().Trim("{}")
}
else {
# In case of Powershell v3+
$function.Body.Extent.Text.Trim("{}")
}
}
93 changes: 71 additions & 22 deletions PoshRSJob/Private/GetParamVariable.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,83 @@
)
# Tokenize the script
[array] $tokens = [Management.Automation.PSParser]::Tokenize($ScriptBlock, [ref]$null) | Where-Object {
$_.Type -ne 'NewLine'
$_.Type -ne 'NewLine' -and $_.Type -ne 'Comment'
}

# First Pass - Grab all tokens between the first param block.
$paramsearch = $false
$groupstart = 0
$groupend = 0
for ($i = 0; $i -lt $tokens.Count; $i++) {
if (!$paramsearch) {
if ($tokens[$i].Content -eq "param" ) {
$paramsearch = $true
}
}
if ($paramsearch) {
if (($tokens[$i].Type -eq "GroupStart") -and ($tokens[$i].Content -eq '(') ) {
$groupstart++
# old code was buggy - it can grab internal param block for code like { $a = 1; invoke-command { param($b) } -argumentlist $a }
# New code know that scriptblock param() can only be the first token or right after [attribute] tokens, any other - ignored.
# It also get right variable names when param($a = $b + $c, $d) and other difficult cases (see tests)
$state = 0
$bracket = 0
$awaitVariable = $false
foreach ($token in $tokens)
{
# using state machine method
switch ($state) {
0 { # search for sttribute start or param
if ($token.Type -eq 'Keyword' -and $token.Content -eq 'param') {
$state = 3 # collect variables start
$awaitVariable = $true # catch variable name after param(
}
elseif ($token.Type -eq 'Operator' -and $token.Content -eq '[') { #attribute start
$state = 1 # check for attribute token
$bracket++
}
else { # no param found, break
$state = -1
}
}
if (($tokens[$i].Type -eq "GroupEnd") -and ($tokens[$i].Content -eq ')') ) {
$groupend++
1 { # Attribute token check. may be excessive?
if ($token.Type -eq 'Attribute') {
$state = 2 # wait for close attribute block
}
}
if (($groupstart -ge 1) -and ($groupstart -eq $groupend)) {
$paramsearch = $false
2 { # await attribte end
if ($token.Type -eq 'Operator') {
if ($token.Content -eq '[') {
$bracket++
}
elseif ($token.Content -eq ']') {
$bracket--
if ($bracket -eq 0) {
# catched attribute close bracket
$state = 0 # back to param() search
}
}
}
}
if (($tokens[$i].Type -eq 'Variable') -and ($tokens[($i-1)].Content -ne '=')) {
if ((($groupstart - $groupend) -eq 1)) {
"$($tokens[$i].Content)"
3 { # inside params
if ($token.Type -eq 'GroupStart' -and $token.Content -eq '(') {
$bracket++
}
elseif ($token.Type -eq 'GroupEnd' -and $token.Content -eq ')') {
$bracket--
if ($bracket -eq 0) {
# param() closed, exiting
$state = -1
}
}
elseif ($token.Type -eq 'Operator' -and $token.Content -eq '[') {
$bracket += 2 #count square brackets
}
elseif ($token.Type -eq 'Operator' -and $token.Content -eq ']') {
$bracket -= 2 #count square brackets
}
elseif ($token.Type -eq 'GroupStart' -and ($token.Content -eq '{' -or $token.Content -eq '@{')) {
$bracket += 2 #count curly brackets
}
elseif ($token.Type -eq 'GroupEnd' -and $token.Content -eq '}') {
$bracket -= 2 #count curly brackets
}
elseif ($token.Type -eq 'Operator' -and $token.Content -eq ',' -and ($bracket -eq 1)) {
$awaitVariable = $true # await variable name after comma without extra brackets
}
elseif ($token.Type -eq 'Variable' -and ($bracket -eq 1) -and $awaitVariable)
{
$awaitVariable = $false
$token.Content
}
}
}
if ($state -eq -1) { break }
}
}
17 changes: 0 additions & 17 deletions PoshRSJob/Private/GetUsingVariableValues.ps1

This file was deleted.

2 changes: 1 addition & 1 deletion PoshRSJob/Private/GetUsingVariables.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Function GetUsingVariables {
Param ([scriptblock]$ScriptBlock)
$ScriptBlock.ast.FindAll({$args[0] -is [System.Management.Automation.Language.UsingExpressionAst]},$True)
$ScriptBlock.ast.FindAll( {$args[0] -is [System.Management.Automation.Language.UsingExpressionAst]}, $True)
}
Loading

0 comments on commit ca402b2

Please sign in to comment.