From f45df7fdd577894a2bcdaca3af1f30bdf70b44ff Mon Sep 17 00:00:00 2001 From: abgox Date: Tue, 20 Aug 2024 15:27:07 +0800 Subject: [PATCH] chore(scripts): update --- scripts/create-completion.ps1 | 47 +++++++++++++++ scripts/create.ps1 | 107 ---------------------------------- scripts/link-completion.ps1 | 20 +++++++ scripts/update-guid.ps1 | 22 ++++--- 4 files changed, 81 insertions(+), 115 deletions(-) create mode 100644 scripts/create-completion.ps1 delete mode 100644 scripts/create.ps1 create mode 100644 scripts/link-completion.ps1 diff --git a/scripts/create-completion.ps1 b/scripts/create-completion.ps1 new file mode 100644 index 0000000..1f17c46 --- /dev/null +++ b/scripts/create-completion.ps1 @@ -0,0 +1,47 @@ +param([string]$completion_name) + +if (!$PSCompletions) { + Write-Host "You should install PSCompletions module and import it." -ForegroundColor Red + return +} +$path_guide = "$($PSScriptRoot)/template/guide/$($PSCompletions.language).json" + +if (Test-Path $path_guide) { + $guide = Get-Content -Path $path_guide -Encoding utf8 | ConvertFrom-Json +} +else { + $guide = Get-Content -Path "$($PSScriptRoot)/template/guide/en-US.json" -Encoding utf8 | ConvertFrom-Json +} + +if (!$completion_name.Trim()) { + $PSCompletions.write_with_color("<@Red>You should enter an available completion name.`ne.g. <@Magenta>.\scripts\create-completion.ps1 test") + return +} + +$config = @{ + language = @('en-US', 'zh-CN') +} + +$root_dir = Split-Path $PSScriptRoot -Parent +$completion_dir = $PSCompletions.join_path($root_dir, "completions", $completion_name) +if (Test-Path $completion_dir) { + $PSCompletions.write_with_color($guide.exist) + return +} + +# create new completion +$PSCompletions.ensure_dir($completion_dir) +$PSCompletions.ensure_dir("$completion_dir/language") + +[System.Guid]::NewGuid().Guid | Out-File "$completion_dir\guid.txt" -Encoding utf8 -Force + +Copy-Item "$($PSScriptRoot)/template/config.json" "$completion_dir/config.json" -Force + +Copy-Item "$($PSScriptRoot)/template/language/en-US.json" "$completion_dir/language/en-US.json" -Force +Copy-Item "$($PSScriptRoot)/template/language/zh-CN.json" "$completion_dir/language/zh-CN.json" -Force + + +$test_dir = Join-Path $PSCompletions.path.completions $completion_name +Remove-Item $test_dir -Recurse -Force -ErrorAction SilentlyContinue +New-Item -ItemType SymbolicLink -Path $test_dir -Target $completion_dir > $null +$PSCompletions.write_with_color($PSCompletions.replace_content($guide.success)) diff --git a/scripts/create.ps1 b/scripts/create.ps1 deleted file mode 100644 index 842dc8a..0000000 --- a/scripts/create.ps1 +++ /dev/null @@ -1,107 +0,0 @@ -function _replace { - param ($data, $separator = '') - $data = ($data -join $separator) - $pattern = '\{\{(.*?(\})*)(?=\}\})\}\}' - $matches = [regex]::Matches($data, $pattern) - foreach ($match in $matches) { - $data = $data.Replace($match.Value, (Invoke-Expression $match.Groups[1].Value) -join $separator ) - } - if ($data -match $pattern) { (_replace $data) }else { return $data } -} - -function write_with_color { - param([string]$str) - $color_list = @() - $str = $str -replace "`n", 'n&&_n_n&&' - $str_list = $str -split '(<\@[^>]+>.*?(?=<\@|$))' | Where-Object { $_ -ne '' } | ForEach-Object { - if ($_ -match '<\@([\s\w]+)>(.*)') { - ($matches[2] -replace 'n&&_n_n&&', "`n") -replace '^<\@>', '' - $color = $matches[1] -split ' ' - $color_list += @{ - color = $color[0] - bgcolor = $color[1] - } - } - else { - ($_ -replace 'n&&_n_n&&', "`n") -replace '^<\@>', '' - $color_list += @{} - } - } - $str_list = [array]$str_list - for ($i = 0; $i -lt $str_list.Count; $i++) { - $color = $color_list[$i].color - $bgcolor = $color_list[$i].bgcolor - if ($color) { - if ($bgcolor) { - Write-Host $str_list[$i] -f $color -b $bgcolor -NoNewline - } - else { - Write-Host $str_list[$i] -f $color -NoNewline - } - } - else { - Write-Host $str_list[$i] -NoNewline - } - } - Write-Host '' -} - -$language = if ($PSUICulture -eq 'zh-CN') { $PSUICulture }else { 'en-US' } - -$guide = Get-Content -Path "$($PSScriptRoot)/template/guide/$($language).json" -Encoding utf8 | ConvertFrom-Json - -function get_input($tip, $default = '') { - Write-Host -NoNewline $tip -f Blue - $inputs = Read-Host - if (!$inputs.Trim()) { - return $default - } - return $inputs -} - -$completion_name = get_input $guide.name - -if (!$completion_name.Trim()) { - write_with_color $guide.err.name - return -} - -$config = @{ - language = @('en-US', 'zh-CN') -} - -$root_dir = Split-Path $PSScriptRoot -Parent -$completions_dir = $root_dir + '/completions/' + $completion_name - -if (Test-Path $completions_dir) { - write_with_color $guide.exist - return -} - -if (Get-Command | Where-Object { $_.Name -eq 'PSCompletions' }) { - $needTest = $true -} -else { - $needTest = $false -} - -# create new completion -New-Item -ItemType Directory $completions_dir > $null -New-Item -ItemType Directory "$completions_dir/language" > $null -[System.Guid]::NewGuid().Guid | Out-File "$completions_dir\guid.txt" -Encoding utf8 -Force - -Copy-Item "$($PSScriptRoot)/template/config.json" "$completions_dir/config.json" -Force - -Copy-Item "$($PSScriptRoot)/template/language/en-US.json" "$completions_dir/language/en-US.json" -Force -Copy-Item "$($PSScriptRoot)/template/language/zh-CN.json" "$completions_dir/language/zh-CN.json" -Force - - -if ($needTest) { - $module_completions_dir = Split-Path (PSCompletions which psc) -Parent - $test_dir = Join-Path $module_completions_dir $completion_name - if (Test-Path $test_dir) { - Remove-Item $test_dir -Recurse -Force - } - New-Item -ItemType SymbolicLink -Path $test_dir -Target $completions_dir > $null - write_with_color (_replace $guide.success) -} diff --git a/scripts/link-completion.ps1 b/scripts/link-completion.ps1 new file mode 100644 index 0000000..2fbbc87 --- /dev/null +++ b/scripts/link-completion.ps1 @@ -0,0 +1,20 @@ +param([string]$completion_name) + +if (!$PSCompletions) { + Write-Host "You should install PSCompletions module and import it." -ForegroundColor Red + return +} +if (!$completion_name.Trim()) { + $PSCompletions.write_with_color("<@Red>You should enter an available completion name.`ne.g. <@Magenta>.\scripts\link-completion.ps1 git") + return +} +$root_dir = Split-Path $PSScriptRoot -Parent +$completion_dir = $PSCompletions.join_path($root_dir, "completions", $completion_name) +if (!(Test-Path $completion_dir)) { + $PSCompletions.write_with_color("<@Red><@Magenta>$completion_name<@Red> isn't exist.") + return +} + +$completion_dir = "$($PSCompletions.path.completions)\$completion_name" +Remove-Item $completion_dir -Force -Recurse -ErrorAction SilentlyContinue +New-Item -ItemType SymbolicLink -Path $completion_dir -Target "$PSScriptRoot\..\completions\$completion_name" -Force diff --git a/scripts/update-guid.ps1 b/scripts/update-guid.ps1 index c89b192..c6e973a 100644 --- a/scripts/update-guid.ps1 +++ b/scripts/update-guid.ps1 @@ -1,13 +1,19 @@ -param([string]$path) +param([array]$completion_list) -if (Test-Path $path) { - if ((Get-Item $path).Extension -eq ".txt") { - (New-Guid).Guid | Out-File $path -Force + +$root_dir = Split-Path $PSScriptRoot -Parent +$path_list = $completion_list | ForEach-Object { + $completion_dir = $root_dir + "/completions/" + $_ + if (Test-Path $completion_dir) { + $completion_dir } - else { - Get-ChildItem $path -Recurse -Filter "guid.txt" | ForEach-Object { - (New-Guid).Guid | Out-File $_.FullName -Force - } +} +if ($path_list) { + foreach ($path in $path_list) { + Write-Host "Updating Guid.txt file of " -NoNewline + Write-Host $(Split-Path $path -Leaf) -ForegroundColor Magenta -NoNewline + Write-Host "." + (New-Guid).Guid | Out-File "$path/guid.txt" -Force } return }