cmder/scripts/utils.ps1

175 lines
5.5 KiB
PowerShell
Raw Normal View History

2017-04-07 12:57:16 +08:00
function Ensure-Exists($path) {
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 {
2014-08-27 06:52:49 +08:00
If( ($command -eq "7z") -and (Test-Path "$env:programfiles\7-zip\7z.exe") ){
set-alias -Name "7z" -Value "$env:programfiles\7-zip\7z.exe" -Scope script
2014-08-27 06:52:49 +08:00
}
ElseIf( ($command -eq "7z") -and (Test-Path "$env:programw6432\7-zip\7z.exe") ) {
set-alias -Name "7z" -Value "$env:programw6432\7-zip\7z.exe" -Scope script
2014-08-27 06:52:49 +08:00
}
Else {
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) {
2014-04-10 18:43:34 +08:00
Write-Verbose "Remove $path"
Remove-Item -Recurse -force $path -ErrorAction SilentlyContinue
}
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'")
Invoke-Expression "7z x -y -o`"$($target)`" `"$source`" > `$null"
2014-04-10 18:43:34 +08:00
if ($lastexitcode -ne 0) {
Write-Error "Extracting of $source failied"
}
Remove-Item $source
}
2017-04-07 12:57:16 +08:00
function Create-Archive($source, $target, $params) {
2014-04-10 20:42:20 +08:00
$command = "7z a -x@`"$source\packignore`" $params $target $source > `$null"
2014-04-10 18:43:34 +08:00
Write-Verbose "Running: $command"
Invoke-Expression $command
if ($lastexitcode -ne 0) {
Write-Error "Compressing $source failied"
}
}
# If directory contains only one child directory
# Flatten it instead
2017-04-07 12:57:16 +08:00
function Flatten-Directory($name) {
2014-04-10 18:43:34 +08:00
$child = (Get-Childitem $name)[0]
Rename-Item $name -NewName "$($name)_moving"
Move-Item -Path "$($name)_moving\$child" -Destination $name
Remove-Item -Recurse "$($name)_moving"
2014-04-10 19:11:41 +08:00
}
2017-04-07 12:57:16 +08:00
function Digest-Hash($path) {
if(Get-Command Get-FileHash -ErrorAction SilentlyContinue){
2017-04-07 12:57:16 +08:00
return (Get-FileHash -Algorithm SHA256 -Path $path).Hash
}
2014-04-10 19:11:41 +08:00
return Invoke-Expression "md5sum $path"
}
function Get-Version-Str($file) {
# Define the regular expression to match the version string from changelog
[regex]$regex = '^## \[(?<version>[\w\-\.]+)\]\([^\n()]+\)\s+\([^\n()]+\)$';
# 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 }
return $version
}
function Create-RC($version, $path) {
2018-03-29 00:43:55 +08:00
$string = $version
if ( !(Test-Path "$path.sample") ) {
2018-03-29 00:43:55 +08:00
throw "Invalid path provided for resources file."
}
$resource = Get-Content -Path "$path.sample"
$pattern = @( "Cmder-Major-Version", "Cmder-Minor-Version", "Cmder-Revision-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 -lt $pattern.length) {
2018-03-29 00:54:02 +08:00
$resource = $resource.Replace( "{" + $pattern[$index++] + "}", '"' + $fragment + '"' )
}
}
2018-03-29 00:43:55 +08:00
# Add the version string
$resource = $resource.Replace( "{Cmder-Version-Str}", $string )
# Write the results
Set-Content -Path $path -Value $resource
}
2017-04-07 12:57:16 +08:00
function Register-Cmder() {
[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')
)
Begin
{
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT > $null
}
Process
{
2016-05-24 22:43:25 +08:00
New-Item -Path "HKCR:\Directory\Shell\Cmder" -Force -Value $MenuText
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`" "
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`" "
}
End
{
Remove-PSDrive -Name HKCR
}
}
2017-04-07 12:57:16 +08:00
function Unregister-Cmder {
Begin
{
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT > $null
}
Process
{
Remove-Item -Path "HKCR:\Directory\Shell\Cmder" -Recurse
Remove-Item -Path "HKCR:\Directory\Background\Shell\Cmder" -Recurse
}
End
{
Remove-PSDrive -Name HKCR
}
}
function Download-File {
param (
$Url,
$File
)
# I think this is the problem
$File = $File -Replace "/", "\"
Write-Verbose "Downloading from $Url to $File"
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) {
2017-04-07 12:57:16 +08:00
$wc.proxy = (New-Object System.Net.WebProxy($env:https_proxy))
2016-04-05 22:27:53 +08:00
}
$wc.Proxy.Credentials=[System.Net.CredentialCache]::DefaultNetworkCredentials;
$wc.DownloadFile($Url, $File)
}