2014-08-28 19:34:59 +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
|
|
|
|
}
|
|
|
|
|
2014-04-10 18:43:34 +08:00
|
|
|
function Ensure-Executable ($command) {
|
|
|
|
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") ){
|
2014-04-11 01:34:31 +08:00
|
|
|
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") ) {
|
2015-03-18 20:28:34 +08:00
|
|
|
set-alias -Name "7z" -Value "$env:programw6432\7-zip\7z.exe" -Scope script
|
2014-08-27 06:52:49 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function Delete-Existing ($path) {
|
|
|
|
Write-Verbose "Remove $path"
|
|
|
|
Remove-Item -Recurse -force $path -ErrorAction SilentlyContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
function Extract-Archive ($source, $target) {
|
2015-03-18 21:21:43 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
function Flatten-Directory ($name) {
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
function Digest-MD5 ($path) {
|
2015-03-18 21:50:00 +08:00
|
|
|
if(Get-Command Get-FileHash -ErrorAction SilentlyContinue){
|
|
|
|
return (Get-FileHash -Algorithm MD5 -Path $path).Hash
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-03-25 22:27:56 +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.
|
|
|
|
$icon = (Split-Path $PathToExe | join-path -ChildPath 'icons/cmder.ico')
|
|
|
|
)
|
|
|
|
Begin
|
|
|
|
{
|
|
|
|
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
|
|
|
|
}
|
|
|
|
Process
|
|
|
|
{
|
|
|
|
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"
|
|
|
|
New-Item -Path "HKCR:\Directory\Shell\Cmder\Command" -Force -Value "`"$PathToExe`" `"$Command`" "
|
|
|
|
}
|
|
|
|
}
|
2015-10-13 16:40:48 +08:00
|
|
|
|
|
|
|
function Download-File {
|
|
|
|
param (
|
|
|
|
$Url,
|
|
|
|
$File
|
|
|
|
)
|
|
|
|
# I think this is the problem
|
|
|
|
$File = $File -Replace "/", "\"
|
|
|
|
Write-Verbose "Downloading from $Url to $File"
|
|
|
|
$wc = new-object System.Net.WebClient
|
2016-04-05 22:27:53 +08:00
|
|
|
if ($env:https_proxy) {
|
|
|
|
$wc.proxy = (new-object System.Net.WebProxy($env:https_proxy))
|
|
|
|
}
|
|
|
|
$wc.Proxy.Credentials=[System.Net.CredentialCache]::DefaultNetworkCredentials;
|
2015-10-13 16:40:48 +08:00
|
|
|
$wc.DownloadFile($Url, $File)
|
|
|
|
}
|