Skip to content

Commit

Permalink
fix(module): update version to 4.0.7
Browse files Browse the repository at this point in the history
- Replace ForEach-Object with foreach
- Clean up some code
  • Loading branch information
abgox committed Jul 6, 2024
1 parent 009d290 commit 071fb58
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 134 deletions.
2 changes: 1 addition & 1 deletion module/.version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.0.6
4.0.7
2 changes: 1 addition & 1 deletion module/PSCompletions.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

RootModule = 'PSCompletions.psm1'

ModuleVersion = '4.0.6'
ModuleVersion = '4.0.7'

GUID = '00929632-527d-4dab-a5b3-21197faccd05'

Expand Down
17 changes: 8 additions & 9 deletions module/PSCompletions.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function PSCompletions {
function Show-List() {
$max_len = ($PSCompletions.cmd.keys | Measure-Object -Maximum Length).Maximum
$max_len = if ($max_len -lt 10) { 10 }else { $max_len }
$PSCompletions.cmd.keys | ForEach-Object {
foreach ($_ in $PSCompletions.cmd.keys) {
$alias = $PSCompletions.cmd.$_ -join ' '
$data.Add(@{
content = "{0,-$($max_len + 3)} {1}" -f ($_, $alias)
Expand All @@ -54,7 +54,7 @@ function PSCompletions {
return
}
$max_len = ($PSCompletions.list | Measure-Object -Maximum Length).Maximum
$PSCompletions.list | ForEach-Object {
foreach ($_ in $PSCompletions.list) {
$status = if ($PSCompletions.cmd.$_) { $PSCompletions.info.list.added_symbol }else { $PSCompletions.info.list.add_symbol }
$data.Add(@{
content = "{0,-$($max_len + 3)} {1}" -f ($_, $status)
Expand Down Expand Up @@ -151,7 +151,7 @@ function PSCompletions {
else {
if ($arg[1] -eq '*') {
# 更新全部可以更新的补全
$PSCompletions.update | ForEach-Object {
foreach ($_ in $PSCompletions.update) {
$PSCompletions.add_completion($_, $true, $true)
}
}
Expand Down Expand Up @@ -407,8 +407,7 @@ function PSCompletions {
$old_value = $PSCompletions.config.comp_config.$($arg[1]).$($arg[2])
$new_value = $arg[3]
$PSCompletions.config.comp_config.$($arg[1]).$($arg[2]) = $arg[3]

$PSCompletions.cmd.keys | ForEach-Object {
foreach ($_ in $PSCompletions.cmd.keys) {
$path = "$($PSCompletions.path.completions)/$($_)/config.json"
$json = $PSCompletions.get_raw_content($path) | ConvertFrom-Json
$path = "$($PSCompletions.path.completions)/$($_)/language/$($json.language[0]).json"
Expand Down Expand Up @@ -719,7 +718,7 @@ function PSCompletions {
}
function handle_reset($cmd) {
$change_list = [System.Collections.Generic.List[System.Object]]@()
$PSCompletions.default.$cmd.Keys | ForEach-Object {
foreach ($_ in $PSCompletions.default.$cmd.Keys) {
$change_list.Add(@{
item = $_
old_value = $PSCompletions.config.$_
Expand Down Expand Up @@ -757,7 +756,7 @@ function PSCompletions {
$is_change_config = $false
}
"order" {
$PSCompletions.cmd.Keys | ForEach-Object {
foreach ($_ in $PSCompletions.cmd.Keys) {
$path_order = "$($PSCompletions.path.completions)/$($_)/order.json"
Remove-Item $path_order -Force -ErrorAction SilentlyContinue
}
Expand Down Expand Up @@ -788,7 +787,7 @@ function PSCompletions {
}
if ($arg[2] -eq '*') {
$PSCompletions.config.comp_config = @{}
$PSCompletions.cmd.keys | ForEach-Object {
foreach ($_ in $PSCompletions.cmd.keys) {
_do $_ -is_all
}
}
Expand Down Expand Up @@ -840,7 +839,7 @@ function PSCompletions {
}
'*' {
$is_init_module = $PSCompletions.confirm_do($PSCompletions.replace_content($PSCompletions.info.reset.init_confirm), {
@('completions', 'completions_json', 'config', 'update', 'change') | ForEach-Object {
foreach ($_ in @('completions', 'completions_json', 'config', 'update', 'change')) {
Remove-Item $PSCompletions.path.$_ -Force -Recurse -ErrorAction SilentlyContinue
}
Remove-Item "$($PSCompletions.path.core)/log.json" -Force -Recurse -ErrorAction SilentlyContinue
Expand Down
77 changes: 37 additions & 40 deletions module/core/init.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using namespace System.Management.Automation
New-Variable -Name PSCompletions -Value @{} -Option Constant

# 模块版本
$PSCompletions.version = '4.0.6'
$PSCompletions.version = '4.0.7'
$PSCompletions.path = @{}
$PSCompletions.path.root = Split-Path $PSScriptRoot -Parent
$PSCompletions.path.completions = Join-Path $PSCompletions.path.root 'completions'
Expand Down Expand Up @@ -96,15 +96,13 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod ensure_dir {

if (!(Test-Path $PSCompletions.path.config) -and !(Test-Path $PSCompletions.path.completions)) {
Add-Member -InputObject $PSCompletions -MemberType ScriptMethod move_old_version {
$version = (Get-ChildItem (Split-Path $this.path.root -Parent)).Name | Sort-Object { [Version]$_ } -ErrorAction SilentlyContinue
$version = (Get-ChildItem (Split-Path $this.path.root -Parent) -ErrorAction SilentlyContinue).Name | Sort-Object { [Version]$_ } -ErrorAction SilentlyContinue
if ($version -is [array]) {
$old_version = $version[-2]
if ($old_version -match "^\d+\.\d.*" -and $old_version -ge "4") {
$old_version_dir = Join-Path (Split-Path $this.path.root -Parent) $old_version
$this.ensure_dir($this.path.completions)
Get-ChildItem "$($old_version_dir)/completions" -ErrorAction SilentlyContinue | Where-Object {
$_.BaseName -ne 'psc'
} | ForEach-Object {
foreach ($_ in Get-ChildItem "$($old_version_dir)/completions" -ErrorAction SilentlyContinue) {
Move-Item $_.FullName $this.path.completions -Force -ErrorAction SilentlyContinue
}
Move-Item "$($old_version_dir)/config.json" $this.path.config -Force -ErrorAction SilentlyContinue
Expand All @@ -115,18 +113,17 @@ if (!(Test-Path $PSCompletions.path.config) -and !(Test-Path $PSCompletions.path
}

if ($PSEdition -eq "Core") {
# pwsh (Unix)
if ($PSVersionTable.Platform -eq 'Unix') {
. $PSScriptRoot\pwsh\Unix\completion.ps1
. $PSScriptRoot\pwsh\Unix\config.ps1
. $PSScriptRoot\pwsh\Unix\menu.ps1
}
else {
if ($IsWindows) {
# pwsh (Windows)
. $PSScriptRoot\pwsh\Win\completion.ps1
. $PSScriptRoot\pwsh\Win\config.ps1
. $PSScriptRoot\pwsh\Win\menu.ps1
}
else {
. $PSScriptRoot\pwsh\Unix\completion.ps1
. $PSScriptRoot\pwsh\Unix\config.ps1
. $PSScriptRoot\pwsh\Unix\menu.ps1
}
}
else {
# powershell 5.x
Expand Down Expand Up @@ -156,7 +153,7 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod ConvertFrom_Json
function ConvertToHashtable($obj) {
$hash = @{}
if ($obj -is [System.Management.Automation.PSCustomObject]) {
$obj | Get-Member -MemberType Properties | ForEach-Object {
foreach ($_ in $obj | Get-Member -MemberType Properties) {
$k = $_.Name # Key
$v = $obj.$k # Value
if ($v -is [System.Collections.IEnumerable] -and $v -isnot [string]) {
Expand Down Expand Up @@ -230,7 +227,7 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod write_with_color
param([string]$str)
$color_list = @()
$str = $str -replace "`n", 'n&&_n_n&&'
$str_list = $str -split '(<\@[^>]+>.*?(?=<\@|$))' | Where-Object { $_ -ne '' } | ForEach-Object {
$str_list = foreach ($_ in $str -split '(<\@[^>]+>.*?(?=<\@|$))' | Where-Object { $_ -ne '' }) {
if ($_ -match '<\@([\s\w]+)>(.*)') {
($matches[2] -replace 'n&&_n_n&&', "`n") -replace '^<\@>', ''
$color = $matches[1] -split ' '
Expand Down Expand Up @@ -302,7 +299,7 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod show_with_less {
}
}
else {
$str_list | ForEach-Object { Write-Host $_ -f $color }
foreach ($_ in $str_list) { Write-Host $_ -f $color }
}
}
Add-Member -InputObject $PSCompletions -MemberType ScriptMethod show_with_less_table {
Expand Down Expand Up @@ -363,7 +360,7 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod show_with_less_t
}
else {
& $do
$str_list | ForEach-Object {
foreach ($_ in $str_list) {
if ($_.bgColor) {
Write-Host $_.content -f $_.color -b $_.bgColor[2]
}
Expand Down Expand Up @@ -472,8 +469,7 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod add_completion {
OutFile = Join-Path $completion_dir 'guid.txt'
}
)

$config.language | ForEach-Object {
foreach ($_ in $config.language) {
$files += @{
Uri = "$($url)/language/$($_).json"
OutFile = $PSCompletions.join_path($completion_dir, 'language', "$($_).json")
Expand Down Expand Up @@ -509,7 +505,8 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod add_completion {

$path_alias = Join-Path $completion_dir 'alias.txt'
if (!(Test-Path $path_alias) -or !$this.get_raw_content($path_alias)) {
$completion | Out-File $path_alias -encoding utf8 -Force
$alias = if ($config.alias) { $config.alias -join "`n" }else { $completion }
$alias | Out-File $path_alias -encoding utf8 -Force
}
$language = $PSCompletions.get_language($completion)
$json = $this.get_raw_content("$($completion_dir)/language/$($language).json") | ConvertFrom-Json
Expand All @@ -524,7 +521,7 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod add_completion {
if (!$this.config.comp_config.$completion) {
$this.config.comp_config.$completion = @{}
}
$json.config | ForEach-Object {
foreach ($_ in $json.config) {
if (!($this.config.comp_config.$completion.$($_.name))) {
$this.config.comp_config.$completion.$($_.name) = $_.value
$need_update_config = $true
Expand Down Expand Up @@ -626,7 +623,7 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod init_data {
$this.ensure_file($this.path.update)
$this.update = $this.get_content($this.path.update)

Get-ChildItem -Path $this.path.completions -Directory | Sort-Object CreationTime | ForEach-Object {
foreach ($_ in Get-ChildItem -Path $this.path.completions -Directory -ErrorAction SilentlyContinue | Sort-Object CreationTime) {
if (Test-Path "$($_.FullName)/config.json") {
$this.cmd.$($_.Name) = $_.Name
}
Expand All @@ -635,7 +632,7 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod init_data {
}
}

Get-ChildItem -Path $this.path.completions | ForEach-Object {
foreach ($_ in Get-ChildItem -Path $this.path.completions -ErrorAction SilentlyContinue) {
$path_alias = Join-Path $_.FullName 'alias.txt'
$this.cmd.$($_.Name) = @()
if (Test-Path $path_alias) {
Expand All @@ -655,11 +652,11 @@ Add-Member -InputObject $PSCompletions -MemberType ScriptMethod init_data {
}

$this.alias = @{}
$this.cmd.Keys | ForEach-Object {
foreach ($_ in $this.cmd.Keys) {
foreach ($alias in $this.cmd.$_) { $this.alias.$($alias) = $_ }
}

$this.cmd.keys | ForEach-Object {
foreach ($_ in $this.cmd.Keys) {
$this.handle_completion($_)
if ((!$this.config.comp_config.$_)) {
$this.config.comp_config.$_ = @{}
Expand All @@ -673,7 +670,7 @@ if ($PSCompletions.is_first_init) {
$PSCompletions.write_with_color($PSCompletions.replace_content($PSCompletions.info.init_info))
}

$PSCompletions.cmd.keys | Where-Object { $_ -ne 'psc' } | ForEach-Object {
foreach ($_ in $PSCompletions.cmd.keys | Where-Object { $_ -ne 'psc' }) {
<#
这里使用 PowerShell 的内置变量 $args 作为临时变量
虽然 $args 不是一个有意义的变量名,但是它的特性很适合作为一个不污染全局的临时变量
Expand All @@ -684,15 +681,12 @@ $PSCompletions.cmd.keys | Where-Object { $_ -ne 'psc' } | ForEach-Object {
}
}

$PSCompletions.cmd.psc | ForEach-Object {
foreach ($_ in $PSCompletions.cmd.psc) {
if ($_ -ne 'PSCompletions') { Set-Alias $_ PSCompletions }
}

if ($PSCompletions.config.module_update -match "^\d+\.\d.*") {
if ($PSCompletions.config.module_update -eq $PSCompletions.version) {
$PSCompletions.set_config('module_update', 1)
}
else {
if ($PSCompletions.config.module_update -gt $PSCompletions.version) {
$PSCompletions.wc.DownloadFile("$($PSCompletions.url)/module/log.json", (Join-Path $PSCompletions.path.core 'log.json'))
$null = $PSCompletions.confirm_do($PSCompletions.info.module.update, {
if ($PSEdition -eq "Desktop") {
Expand All @@ -704,7 +698,7 @@ if ($PSCompletions.config.module_update -match "^\d+\.\d.*") {
catch {
$PSCompletions.write_with_color($PSCompletions.replace_content($PSCompletions.info.module.update_err))
}
if ($PSCompletions.config.module_update -in (Get-ChildItem (Split-Path $PSCompletions.path.root -Parent)).BaseName) {
if ($PSCompletions.config.module_update -in (Get-ChildItem (Split-Path $PSCompletions.path.root -Parent) -ErrorAction SilentlyContinue).BaseName) {
$PSCompletions.write_with_color($PSCompletions.replace_content($PSCompletions.info.module.update_done))
}
else {
Expand All @@ -720,7 +714,7 @@ if ($PSCompletions.config.module_update -match "^\d+\.\d.*") {
catch {
$PSCompletions.write_with_color($PSCompletions.replace_content($PSCompletions.info.module.update_err))
}
if ($PSCompletions.config.module_update -in (Get-ChildItem (Split-Path $PSCompletions.path.root -Parent)).BaseName) {
if ($PSCompletions.config.module_update -in (Get-ChildItem (Split-Path $PSCompletions.path.root -Parent) -ErrorAction SilentlyContinue).BaseName) {
$PSCompletions.write_with_color($PSCompletions.replace_content($PSCompletions.info.module.update_done))
}
else {
Expand All @@ -729,6 +723,9 @@ if ($PSCompletions.config.module_update -match "^\d+\.\d.*") {
}
})
}
else {
$PSCompletions.set_config('module_update', 1)
}
}
else {
if ($PSCompletions.config.update -eq 1) {
Expand All @@ -747,10 +744,10 @@ $PSCompletions.job = Start-Job -ScriptBlock {
[string]$json
)
<#
# 处理 json 文件中的一些特殊情况
# - 如果有键名为空的情况,则替换为随机的 guid
# - 并移除最后一个属性的 ,
#>
# 处理 json 文件中的一些特殊情况
# - 如果有键名为空的情况,则替换为随机的 guid
# - 并移除最后一个属性的 ,
#>
$matches = [regex]::Matches($json, '\s*"\s*"\s*:')
foreach ($match in $matches) {
$json = $json -replace $match.Value, "`"empty_key_$([System.Guid]::NewGuid().Guid)`":"
Expand All @@ -759,7 +756,7 @@ $PSCompletions.job = Start-Job -ScriptBlock {
function ConvertToHashtable($obj) {
$hash = @{}
if ($obj -is [System.Management.Automation.PSCustomObject]) {
$obj | Get-Member -MemberType Properties | ForEach-Object {
foreach ($_ in $obj | Get-Member -MemberType Properties) {
$k = $_.Name # Key
$v = $obj.$k # Value
if ($v -is [System.Collections.IEnumerable] -and $v -isnot [string]) {
Expand Down Expand Up @@ -816,7 +813,7 @@ $PSCompletions.job = Start-Job -ScriptBlock {
}

$completion_datas = @{}
$PSCompletions.cmd.Keys | ForEach-Object {
foreach ($_ in $PSCompletions.cmd.Keys) {
$language = $PSCompletions.get_language($_)
$path_language = "$($PSCompletions.path.completions)/$($_)/language/$($language).json"
if (Test-Path $path_language) {
Expand Down Expand Up @@ -864,7 +861,7 @@ $PSCompletions.job = Start-Job -ScriptBlock {
}

# ensure completion config
$PSCompletions.cmd.keys | ForEach-Object {
foreach ($_ in $PSCompletions.cmd.Keys) {
$path = "$($PSCompletions.path.completions)/$($_)/config.json"
$json = $PSCompletions.get_raw_content($path) | ConvertFrom-Json
$path = "$($PSCompletions.path.completions)/$($_)/language/$($json.language[0]).json"
Expand Down Expand Up @@ -898,7 +895,7 @@ $PSCompletions.job = Start-Job -ScriptBlock {
# check update
if ($PSCompletions.config.update -eq 1) {
$update_list = [System.Collections.Generic.List[string]]@()
Get-ChildItem $PSCompletions.path.completions | Where-Object { $_.Name -in $PSCompletions.list } | ForEach-Object {
foreach ($_ in Get-ChildItem $PSCompletions.path.completions -ErrorAction SilentlyContinue | Where-Object { $_.Name -in $PSCompletions.list }) {
try {
$response = Invoke-WebRequest -Uri "$($PSCompletions.url)/completions/$($_.Name)/guid.txt"
$content = $response.Content.Trim()
Expand Down
Loading

0 comments on commit 071fb58

Please sign in to comment.