2017-04-07 12:57:16 +08:00
|
|
|
function Ensure-Exists($path) {
|
2014-08-28 19:34:59 +08:00
|
|
|
if (-not (Test-Path $path)) {
|
|
|
|
Write-Error "Missing required $path! Ensure it is installed"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
return $true > $null
|
|
|
|
}
|
|
|
|
|
2017-04-07 12:57:16 +08:00
|
|
|
function Ensure-Executable($command) {
|
2014-04-10 18:43:34 +08:00
|
|
|
try { Get-Command $command -ErrorAction Stop > $null }
|
|
|
|
catch {
|
2022-10-16 20:43:26 +08:00
|
|
|
if( ($command -eq "7z") -and (Test-Path "$env:programfiles\7-zip\7z.exe") ){
|
2022-10-15 19:23:52 +08:00
|
|
|
Set-Alias -Name "7z" -Value "$env:programfiles\7-zip\7z.exe" -Scope script
|
2014-08-27 06:52:49 +08:00
|
|
|
}
|
2022-10-16 20:43:26 +08:00
|
|
|
elseif( ($command -eq "7z") -and (Test-Path "$env:programw6432\7-zip\7z.exe") ) {
|
2022-10-15 19:23:52 +08:00
|
|
|
Set-Alias -Name "7z" -Value "$env:programw6432\7-zip\7z.exe" -Scope script
|
2014-08-27 06:52:49 +08:00
|
|
|
}
|
2022-10-16 20:43:26 +08:00
|
|
|
else {
|
2014-04-11 01:34:31 +08:00
|
|
|
Write-Error "Missing $command! Ensure it is installed and on in the PATH"
|
|
|
|
exit 1
|
|
|
|
}
|
2014-04-10 18:43:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-07 12:57:16 +08:00
|
|
|
function Delete-Existing($path) {
|
2022-10-15 19:23:52 +08:00
|
|
|
if (Test-Path $path) {
|
|
|
|
Write-Verbose "Remove existing $path"
|
|
|
|
}
|
|
|
|
Remove-Item -Recurse -Force $path -ErrorAction SilentlyContinue
|
2014-04-10 18:43:34 +08:00
|
|
|
}
|
|
|
|
|
2017-04-07 12:57:16 +08:00
|
|
|
function Extract-Archive($source, $target) {
|
2016-07-18 07:28:38 +08:00
|
|
|
Write-Verbose $("Extracting Archive '$cmder_root\vendor\" + $source.replace('/','\') + " to '$cmder_root\vendor\$target'")
|
2022-10-15 06:14:07 +08:00
|
|
|
Invoke-Expression "7z x -y -o`"$($target)`" `"$source`" > `$null"
|
2014-04-10 18:43:34 +08:00
|
|
|
if ($lastexitcode -ne 0) {
|
2022-01-16 00:00:58 +08:00
|
|
|
Write-Error "Extracting of $source failed"
|
2014-04-10 18:43:34 +08:00
|
|
|
}
|
|
|
|
Remove-Item $source
|
|
|
|
}
|
|
|
|
|
2017-04-07 12:57:16 +08:00
|
|
|
function Create-Archive($source, $target, $params) {
|
2022-10-15 08:25:44 +08:00
|
|
|
$command = "7z a -xr@`"$source\packignore`" $params `"$target`" `"$source\*`" > `$null"
|
2022-10-15 03:57:22 +08:00
|
|
|
Write-Verbose "Creating Archive from '$source' in '$target' with parameters '$params'"
|
2014-04-10 18:43:34 +08:00
|
|
|
Invoke-Expression $command
|
|
|
|
if ($lastexitcode -ne 0) {
|
2022-01-16 00:00:58 +08:00
|
|
|
Write-Error "Compressing $source failed"
|
2014-04-10 18:43:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# If directory contains only one child directory
|
|
|
|
# Flatten it instead
|
2017-04-07 12:57:16 +08:00
|
|
|
function Flatten-Directory($name) {
|
2022-10-15 02:21:04 +08:00
|
|
|
$name = Resolve-Path $name
|
2022-10-15 02:34:55 +08:00
|
|
|
$moving = "$($name)_moving"
|
|
|
|
Rename-Item $name -NewName $moving
|
2022-10-15 07:17:36 +08:00
|
|
|
Write-Verbose "Flattening the '$name' directory..."
|
|
|
|
$child = (Get-Childitem $moving)[0] | Resolve-Path
|
2022-10-15 02:34:55 +08:00
|
|
|
Move-Item -Path $child -Destination $name
|
|
|
|
Remove-Item -Recurse $moving
|
2014-04-10 19:11:41 +08:00
|
|
|
}
|
|
|
|
|
2017-04-07 12:57:16 +08:00
|
|
|
function Digest-Hash($path) {
|
2022-10-15 03:57:22 +08:00
|
|
|
if (Get-Command Get-FileHash -ErrorAction SilentlyContinue) {
|
2017-04-07 12:57:16 +08:00
|
|
|
return (Get-FileHash -Algorithm SHA256 -Path $path).Hash
|
2015-03-18 21:50:00 +08:00
|
|
|
}
|
|
|
|
|
2014-04-10 19:11:41 +08:00
|
|
|
return Invoke-Expression "md5sum $path"
|
2014-08-28 19:34:59 +08:00
|
|
|
}
|
2015-03-18 20:28:34 +08:00
|
|
|
|
2022-10-15 20:07:39 +08:00
|
|
|
function Set-GHVariable {
|
|
|
|
param(
|
|
|
|
[Parameter(Mandatory = $true)]
|
|
|
|
[string]$Name,
|
|
|
|
[Parameter(Mandatory = $true)]
|
|
|
|
[string]$Value
|
|
|
|
)
|
|
|
|
|
|
|
|
Write-Verbose "Setting CI variable $Name to $Value" -Verbose
|
|
|
|
|
|
|
|
if ($env:GITHUB_ENV) {
|
2022-10-18 04:05:29 +08:00
|
|
|
echo "$Name=$Value" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
|
2022-10-15 20:07:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function Get-GHTempPath {
|
|
|
|
$temp = [System.IO.Path]::GetTempPath()
|
|
|
|
if ($env:RUNNER_TEMP) {
|
|
|
|
$temp = $env:RUNNER_TEMP
|
|
|
|
}
|
|
|
|
|
|
|
|
Write-Verbose "Get CI Temp path: $temp" -Verbose
|
|
|
|
return $temp
|
|
|
|
}
|
|
|
|
|
2022-10-18 00:22:15 +08:00
|
|
|
function Get-VersionStr {
|
2018-03-31 02:53:44 +08:00
|
|
|
# Clear existing variable
|
2018-03-31 03:01:51 +08:00
|
|
|
if ($string) { Clear-Variable -name string }
|
2018-03-31 02:53:44 +08:00
|
|
|
|
|
|
|
# Determine if git is available
|
2022-10-15 17:16:13 +08:00
|
|
|
if (Get-Command "git.exe" -ErrorAction SilentlyContinue) {
|
2022-10-25 00:28:46 +08:00
|
|
|
# Determine if the current directory is a git repository
|
|
|
|
$GitPresent = Invoke-Expression "git rev-parse --is-inside-work-tree" -ErrorAction SilentlyContinue
|
2018-03-31 02:53:44 +08:00
|
|
|
|
2022-10-15 17:16:13 +08:00
|
|
|
if ( $GitPresent -eq 'true' ) {
|
2018-03-31 02:53:44 +08:00
|
|
|
$string = Invoke-Expression "git describe --abbrev=0 --tags"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Fallback used when Git is not available
|
2022-10-15 17:16:13 +08:00
|
|
|
if ( -not($string) ) {
|
2018-03-31 02:53:44 +08:00
|
|
|
$string = Parse-Changelog ($PSScriptRoot + '\..\' + 'CHANGELOG.md')
|
|
|
|
}
|
|
|
|
|
|
|
|
# Add build number, if AppVeyor is present
|
2022-10-15 17:16:13 +08:00
|
|
|
if ( $Env:APPVEYOR -eq 'True' ) {
|
2018-03-31 02:53:44 +08:00
|
|
|
$string = $string + '.' + $Env:APPVEYOR_BUILD_NUMBER
|
|
|
|
}
|
2022-10-15 18:19:02 +08:00
|
|
|
elseif ( $Env:GITHUB_ACTIONS -eq 'true' ) {
|
|
|
|
$string = $string + '.' + $Env:GITHUB_RUN_NUMBER
|
|
|
|
}
|
2018-03-31 02:53:44 +08:00
|
|
|
|
|
|
|
# Remove starting 'v' characters
|
|
|
|
$string = $string -replace '^v+','' # normalize version string
|
|
|
|
|
|
|
|
return $string
|
|
|
|
}
|
|
|
|
|
|
|
|
function Parse-Changelog($file) {
|
2018-03-31 03:27:21 +08:00
|
|
|
# Define the regular expression to match the version string from changelog
|
|
|
|
[regex]$regex = '^## \[(?<version>[\w\-\.]+)\]\([^\n()]+\)\s+\([^\n()]+\)$';
|
2018-03-29 00:11:18 +08:00
|
|
|
|
2018-03-31 03:27:21 +08:00
|
|
|
# Find the first match of the version string which means the latest version
|
|
|
|
$version = Select-String -Path $file -Pattern $regex | Select-Object -First 1 | % { $_.Matches.Groups[1].Value }
|
2018-03-29 00:11:18 +08:00
|
|
|
|
2018-03-31 03:27:21 +08:00
|
|
|
return $version
|
2018-03-29 00:11:18 +08:00
|
|
|
}
|
|
|
|
|
2018-03-29 02:00:09 +08:00
|
|
|
function Create-RC($string, $path) {
|
2018-03-31 02:53:44 +08:00
|
|
|
$version = $string + '.0.0.0.0' # padding for version string
|
2018-03-29 00:43:55 +08:00
|
|
|
|
2018-03-31 03:27:21 +08:00
|
|
|
if ( !(Test-Path "$path.sample") ) {
|
|
|
|
throw "Invalid path provided for resources file."
|
|
|
|
}
|
|
|
|
|
|
|
|
$resource = Get-Content -Path "$path.sample"
|
|
|
|
$pattern = @( "Cmder-Major-Version", "Cmder-Minor-Version", "Cmder-Revision-Version", "Cmder-Build-Version" )
|
|
|
|
$index = 0
|
|
|
|
|
|
|
|
# Replace all non-numeric characters to dots and split to array
|
|
|
|
$version = $version -replace '[^0-9]+','.' -split '\.'
|
|
|
|
|
|
|
|
foreach ($fragment in $version) {
|
|
|
|
if ( !$fragment ) { break }
|
|
|
|
elseif ($index -le $pattern.length) {
|
|
|
|
$resource = $resource.Replace( "{" + $pattern[$index++] + "}", $fragment )
|
|
|
|
}
|
|
|
|
}
|
2022-10-15 17:16:13 +08:00
|
|
|
|
2018-03-31 03:27:21 +08:00
|
|
|
# Add the version string
|
|
|
|
$resource = $resource.Replace( "{Cmder-Version-Str}", '"' + $string + '"' )
|
|
|
|
|
|
|
|
# Write the results
|
|
|
|
Set-Content -Path $path -Value $resource
|
2018-03-29 00:11:18 +08:00
|
|
|
}
|
|
|
|
|
2017-04-07 12:57:16 +08:00
|
|
|
function Register-Cmder() {
|
2015-03-25 22:27:56 +08:00
|
|
|
[CmdletBinding()]
|
|
|
|
Param
|
|
|
|
(
|
|
|
|
# Text for the context menu item.
|
|
|
|
$MenuText = "Cmder Here"
|
|
|
|
|
|
|
|
, # Defaults to the current cmder directory when run from cmder.
|
|
|
|
$PathToExe = (Join-Path $env:CMDER_ROOT "cmder.exe")
|
|
|
|
|
|
|
|
, # Commands the context menu will execute.
|
|
|
|
$Command = "%V"
|
|
|
|
|
|
|
|
, # Defaults to the icons folder in the cmder package.
|
2017-04-07 12:57:16 +08:00
|
|
|
$icon = (Split-Path $PathToExe | Join-Path -ChildPath 'icons/cmder.ico')
|
2015-03-25 22:27:56 +08:00
|
|
|
)
|
|
|
|
Begin
|
|
|
|
{
|
2016-05-24 22:49:30 +08:00
|
|
|
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT > $null
|
2015-03-25 22:27:56 +08:00
|
|
|
}
|
|
|
|
Process
|
|
|
|
{
|
2016-05-24 22:43:25 +08:00
|
|
|
New-Item -Path "HKCR:\Directory\Shell\Cmder" -Force -Value $MenuText
|
2015-03-25 22:27:56 +08:00
|
|
|
New-ItemProperty -Path "HKCR:\Directory\Shell\Cmder" -Force -Name "Icon" -Value `"$icon`"
|
|
|
|
New-ItemProperty -Path "HKCR:\Directory\Shell\Cmder" -Force -Name "NoWorkingDirectory"
|
2016-05-24 22:43:25 +08:00
|
|
|
New-Item -Path "HKCR:\Directory\Shell\Cmder\Command" -Force -Value "`"$PathToExe`" `"$Command`" "
|
2016-05-24 22:44:54 +08:00
|
|
|
|
|
|
|
New-Item -Path "HKCR:\Directory\Background\Shell\Cmder" -Force -Value $MenuText
|
|
|
|
New-ItemProperty -Path "HKCR:\Directory\Background\Shell\Cmder" -Force -Name "Icon" -Value `"$icon`"
|
|
|
|
New-ItemProperty -Path "HKCR:\Directory\Background\Shell\Cmder" -Force -Name "NoWorkingDirectory"
|
|
|
|
New-Item -Path "HKCR:\Directory\Background\Shell\Cmder\Command" -Force -Value "`"$PathToExe`" `"$Command`" "
|
|
|
|
}
|
2016-05-24 22:50:43 +08:00
|
|
|
End
|
|
|
|
{
|
|
|
|
Remove-PSDrive -Name HKCR
|
|
|
|
}
|
2016-05-24 22:44:54 +08:00
|
|
|
}
|
|
|
|
|
2017-04-07 12:57:16 +08:00
|
|
|
function Unregister-Cmder {
|
2016-05-24 22:44:54 +08:00
|
|
|
Begin
|
|
|
|
{
|
2016-05-24 22:49:30 +08:00
|
|
|
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT > $null
|
2016-05-24 22:44:54 +08:00
|
|
|
}
|
|
|
|
Process
|
|
|
|
{
|
|
|
|
Remove-Item -Path "HKCR:\Directory\Shell\Cmder" -Recurse
|
|
|
|
Remove-Item -Path "HKCR:\Directory\Background\Shell\Cmder" -Recurse
|
2015-03-25 22:27:56 +08:00
|
|
|
}
|
2016-05-24 22:50:43 +08:00
|
|
|
End
|
|
|
|
{
|
|
|
|
Remove-PSDrive -Name HKCR
|
|
|
|
}
|
2015-03-25 22:27:56 +08:00
|
|
|
}
|
2015-10-13 16:40:48 +08:00
|
|
|
|
|
|
|
function Download-File {
|
|
|
|
param (
|
|
|
|
$Url,
|
|
|
|
$File
|
|
|
|
)
|
2018-05-04 06:28:14 +08:00
|
|
|
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
2022-10-15 17:16:13 +08:00
|
|
|
|
2022-10-15 19:23:52 +08:00
|
|
|
$useBitTransfer = $null -ne (Get-Module -Name BitsTransfer -ListAvailable) -and ($PSVersionTable.PSVersion.Major -le 5)
|
|
|
|
|
2015-10-13 16:40:48 +08:00
|
|
|
$File = $File -Replace "/", "\"
|
2022-10-15 19:23:52 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
if ($useBitTransfer) {
|
|
|
|
Start-BitsTransfer -Source $Url -Destination $File -DisplayName "Downloading $Url to $File"
|
|
|
|
Return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch {
|
2022-10-26 00:16:56 +08:00
|
|
|
Write-Error "Failed to download file using BITS, reason: $_`nUsing fallback method instead...`n" -ErrorAction:Continue
|
2022-10-15 19:23:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Write-Verbose "Downloading from $Url to $File`n"
|
|
|
|
|
2017-04-07 12:57:16 +08:00
|
|
|
$wc = New-Object System.Net.WebClient
|
2016-04-05 22:27:53 +08:00
|
|
|
if ($env:https_proxy) {
|
2022-10-15 17:16:13 +08:00
|
|
|
$wc.proxy = (New-Object System.Net.WebProxy($env:https_proxy))
|
2016-04-05 22:27:53 +08:00
|
|
|
}
|
2022-10-15 19:23:52 +08:00
|
|
|
$wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials;
|
2015-10-13 16:40:48 +08:00
|
|
|
$wc.DownloadFile($Url, $File)
|
|
|
|
}
|