core/utils/download.ps1
$PSCompletions | Add-Member -MemberType ScriptMethod fn_download_list { try { if ($PSCompletions.url) { $res = Invoke-WebRequest -Uri ($PSCompletions.url + '/list.txt') if ($res.StatusCode -eq 200) { $content = $res.Content -split "`n" | Where-Object { $_ -ne '' } $old_list = $PSCompletions.fn_get_content($PSCompletions.path.old_list) $list = $PSCompletions.fn_get_content($PSCompletions.path.list) if (!$content) { $content = "" } if (!$old_list) { $old_list = "" } if (!$list) { $list = "" } if (Compare-Object $list $old_list -PassThru) { Copy-Item $PSCompletions.path.list $PSCompletions.path.old_list -Force } if (Compare-Object $content $list -PassThru) { $content | Out-File $PSCompletions.path.list -Force -Encoding utf8 $PSCompletions.list = $PSCompletions.fn_get_content($PSCompletions.path.list) } return $content } } else { $PSCompletions.fn_write($PSCompletions.fn_replace($PSCompletions.json.repo_add)) return $false } } catch { return $false } } $PSCompletions | Add-Member -MemberType ScriptMethod fn_add_completion { param ( [string]$completion, [bool]$log = $true, [bool]$is_update = $false ) if ($is_update) { $err = $PSCompletions.json.update_download_err $done = $PSCompletions.json.update_done } else { $err = $PSCompletions.json.add_download_err $done = $PSCompletions.json.add_done } $url = $PSCompletions.url + '/completions/' + $completion function _mkdir($path) { if (!(Test-Path $path)) { New-Item -ItemType Directory $path > $null } } $completion_dir = Join-Path $PSCompletions.path.completions $completion _mkdir $PSCompletions.path.completions _mkdir $completion_dir _mkdir ($completion_dir + '/lang') $files = @( @{ Uri = $url + '/' + $completion + '.ps1' OutFile = Join-Path $completion_dir ($completion + '.ps1') }, @{ Uri = $url + '/lang/zh-CN.json' OutFile = $PSCompletions.fn_join_path($completion_dir, 'lang', 'zh-CN.json') }, @{ Uri = $url + '/lang/en-US.json' OutFile = $PSCompletions.fn_join_path($completion_dir, 'lang', 'en-US.json') }, @{ Uri = $url + '/guid.txt' OutFile = Join-Path $completion_dir 'guid.txt' } ) $wc = New-Object System.Net.WebClient foreach ($file in $files) { $wc.DownloadFile($file.Uri, $file.OutFile) } $download = if ($is_update) { $PSCompletions.json.updating }else { $PSCompletions.json.adding } $PSCompletions.fn_write($PSCompletions.fn_replace($download)) $all_exist = $true foreach ($file in $files) { if (!(Test-Path $file.OutFile)) { $all_exist = $false $fail_file = Split-Path $file.OutFile -Leaf $fail_file_url = $file.Uri } } if ($all_exist) { if ($log) { $PSCompletions.fn_write($PSCompletions.fn_replace($done)) } } else { $PSCompletions.fn_write($PSCompletions.fn_replace($err)) Remove-Item $completion_dir -Force -Recurse -ErrorAction SilentlyContinue } $core_info = ($PSCompletions.fn_get_raw_content($completion_dir + '/lang/' + $PSCompletions.lang + '.json') | ConvertFrom-Json).$($completion + '_core_info') if ($core_info.comp_config) { $configs = $core_info.comp_config.PSObject.Properties.Name $PSCompletions.fn_write($PSCompletions.fn_replace($PSCompletions.json.comp_config_tip)) foreach ($config in $configs) { $PSCompletions.fn_write($PSCompletions.fn_replace($core_info.comp_config.$config)) } $PSCompletions.has_config_update = $true } } |