cmder/scripts/utils.ps1

54 lines
1.6 KiB
PowerShell
Raw Normal View History

2014-04-10 18:43:34 +08:00
function Ensure-Exists ($path) {
if (-not (Test-Path $path)) {
Write-Error "Missing required $path! Ensure it is installed"
2014-04-10 18:43:34 +08:00
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 {
if( ($command -eq "7z") -and (Ensure-Exists "$env:programfiles\7-zip\7z.exe") ){
set-alias -Name "7z" -Value "$env:programfiles\7-zip\7z.exe" -Scope script
} else {
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) {
2014-04-10 20:42:20 +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) {
return Invoke-Expression "md5sum $path"
2014-04-10 18:43:34 +08:00
}