Skip to content

Commit

Permalink
beginning refactoring for global installs
Browse files Browse the repository at this point in the history
  • Loading branch information
lukesampson committed Aug 30, 2013
1 parent f15d8bb commit 555e189
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 33 deletions.
2 changes: 1 addition & 1 deletion bin/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ rm $zipfile
$null > "$dir\last_updated" # save install timestamp

echo 'creating shim...'
shim "$dir\bin\scoop.ps1"
shim "$dir\bin\scoop.ps1" $false

ensure_scoop_in_path
success 'scoop was installed successfully!'
Expand Down
2 changes: 1 addition & 1 deletion bin/refresh.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $output = robocopy $src $dest /mir /njh /njs /nfl /ndl /xd .git /xf .DS_Store la
$output | ? { $_ -ne "" }

echo 'creating shim...'
shim "$dest\bin\scoop.ps1"
shim "$dest\bin\scoop.ps1" $false

ensure_scoop_in_path
success 'scoop was refreshed!'
4 changes: 2 additions & 2 deletions bin/uninstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ installed_apps | % {

echo "uninstalling $app"
run_uninstaller $manifest $architecture $dir
rm_shims $manifest
rm_shims $manifest $false
env_rm_path $manifest $dir
env_rm $manifest

Expand All @@ -43,6 +43,6 @@ try {
abort "couldn't remove $(friendly_path $scoopdir): $_"
}

remove_from_path $shimdir
remove_from_path (shimdir $false)

success "scoop has been uninstalled"
34 changes: 19 additions & 15 deletions lib/core.ps1
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
$scoopdir = "~\appdata\local\scoop"
$shimdir = "$scoopdir\shims"
$cachedir = "$scoopdir\cache"
$globaldir = "$($env:programdata.tolower())\scoop"
$cachedir = "$scoopdir\cache" # always local

# helper functions
function coalesce($a, $b) { if($a) { return $a } $b }
function format($str, $hash) {
$hash.keys | % { set-variable $_ $hash[$_] }
$executionContext.invokeCommand.expandString($str)
}
function is_admin {
$id = [Security.Principal.WindowsIdentity]::GetCurrent()
([Security.Principal.WindowsPrincipal]($id)).isinrole("Administrators")
}


# messages
function abort($msg) { write-host $msg -f darkred; exit 1 }
function warn($msg) { write-host $msg -f darkyellow; }
function success($msg) { write-host $msg -f darkgreen }

# apps
function appdir($app) { "$scoopdir\apps\$app" }
function basedir($global) { if($global) { return $globaldir } $scoopdir }
function appdir($app, $global) { "$(basedir $global)\apps\$app" }
function shimdir($global) { "$(basedir $global))\shims" }

function versiondir($app, $version) { "$(appdir $app)\$version" }
function installed($app) { return test-path (appdir $app) }

function installed($app, $global) { return test-path (appdir $app $global) }
function installed_apps {
if(test-path "$scoopdir\apps") {
gci ( "$scoopdir\apps") | where { $_.psiscontainer -and $_.name -ne 'scoop' } | % { $_.name }
Expand Down Expand Up @@ -70,9 +79,9 @@ function unzip($path,$to,$folder) {
$shell.namespace("$to").copyHere($zipfiles, 4) # 4 = don't show progress dialog
}

function shim($path) {
function shim($path, $global) {
if(!(test-path $path)) { abort "can't shim $(fname $path): couldn't find $path" }
$abs_shimdir = ensure $shimdir
$abs_shimdir = ensure (shimdir $global)
$shim = "$abs_shimdir\$(strip_ext(fname $path).tolower()).ps1"

# note: use > for first line to replace file, then >> to append following lines
Expand All @@ -90,19 +99,14 @@ function shim($path) {
}
}

function ensure_in_path($dir,$first=$false) {
function ensure_in_path($dir) {
$userpath = env 'path'
$dir = fullpath $dir
if($userpath -notmatch [regex]::escape($dir)) {
echo "adding $(friendly_path $dir) to your path"

# for future sessions...
if($first) { env 'path' "$dir;$userpath" }
else { env 'path' "$userpath;$dir" }

# for this session
if($first) { $env:path = "$dir;$env:path" }
else { $env:path = "$env:path;$dir" }
env 'path' "$dir;$userpath" # for future sessions...
$env:path = "$dir;$env:path" # for this session
}
}

Expand All @@ -127,7 +131,7 @@ function remove_from_path($dir) {
}

function ensure_scoop_in_path {
$abs_shimdir = ensure $shimdir
$abs_shimdir = ensure (shimdir $false)
# be aggressive (b-e-aggressive) and install scoop first in the path
ensure_in_path $abs_shimdir $true
}
10 changes: 5 additions & 5 deletions lib/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ function run_uninstaller($manifest, $architecture, $dir) {
}
}

function create_shims($manifest, $dir) {
function create_shims($manifest, $dir, $global) {
$manifest.bin | ?{ $_ -ne $null } | % {
echo "creating shim for $_"

Expand All @@ -327,12 +327,12 @@ function create_shims($manifest, $dir) {
}
if(!(test-path $bin)) { abort "can't shim $_`: file doesn't exist"}

shim "$dir\$_"
shim "$dir\$_" $global
}
}
function rm_shims($manifest) {
function rm_shims($manifest, $global) {
$manifest.bin | ?{ $_ -ne $null } | % {
$shim = "$shimdir\$(strip_ext(fname $_)).ps1"
$shim = "$(shimdir $global)\$(strip_ext(fname $_)).ps1"
$shim_cmd = "$(strip_ext $shim).cmd"

if(!(test-path $shim)) { # handle no shim from failed install
Expand All @@ -346,7 +346,7 @@ function rm_shims($manifest) {
}
}

# for installers that insist on changing path
# for installers that add to path without scoop's knowledge
function ensure_install_dir_not_in_path($dir) {
$user_path = (env 'path')
$machine_path = [environment]::getEnvironmentVariable('path', 'Machine')
Expand Down
14 changes: 10 additions & 4 deletions libexec/scoop-install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ function install($app, $architecture, $global) {
abort "manifest version has unsupported character '$($matches[0])'"
}

if(installed $app) {
if(installed $app $global) {
$global_flag = $null; if($global){$global_flag = ' -global'}

$version = @(versions $app)[-1]
if(!(install_info $app $version)) {
abort "it looks like a previous installation of $app failed.`nrun 'scoop uninstall $app' before retrying the install."
abort "it looks like a previous installation of $app failed.`nrun 'scoop uninstall $app$global_flag' before retrying the install."
}
abort "$app ($version) is already installed.`nuse 'scoop update $app' to install a new version."
abort "$app ($version) is already installed.`nuse 'scoop update $app$global_flag' to install a new version."
}

# check 7zip installed if required
Expand All @@ -84,7 +86,7 @@ function install($app, $architecture, $global) {
$fname = dl_urls $app $version $manifest $architecture $dir
run_installer $fname $manifest $architecture $dir
ensure_install_dir_not_in_path $dir
create_shims $manifest $dir
create_shims $manifest $dir $global
env_add_path $manifest $dir
env_set $manifest $dir
post_install $manifest
Expand All @@ -108,6 +110,10 @@ switch($architecture) {

if(!$apps) { 'ERROR: <app> missing'; my_usage; exit 1 }

if($global -and !(is_admin)) {
'ERROR: admin rights required to install global apps'; exit 1
}

$apps | % { install $_ $architecture $global }

exit 0
2 changes: 1 addition & 1 deletion libexec/scoop-reset.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ $version = current_version $app
$dir = versiondir $app $version
$manifest = installed_manifest $app $version

create_shims $manifest $dir
create_shims $manifest $dir $false
env_add_path $manifest $dir
env_set $manifest $dir
2 changes: 1 addition & 1 deletion libexec/scoop-uninstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ $install = install_info $app $version
$architecture = $install.architecture

run_uninstaller $manifest $architecture $dir
rm_shims $manifest
rm_shims $manifest $false
env_rm_path $manifest $dir
env_rm $manifest

Expand Down
4 changes: 2 additions & 2 deletions libexec/scoop-update.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ if(!$app) {

echo "uninstalling $old_version"
run_uninstaller $manifest $architecture $dir
rm_shims $manifest
rm_shims $manifest $false
# note: keep the old dir in case in contains user files

$dir = ensure (versiondir $app $version)
Expand All @@ -89,7 +89,7 @@ if(!$app) {
$fname = dl_urls $app $version $manifest $architecture $dir
run_installer $fname $manifest $architecture $dir
ensure_install_dir_not_in_path $dir
create_shims $manifest $dir
create_shims $manifest $dir $false
env_add_path $manifest $dir
env_set $manifest $dir
post_install $manifest
Expand Down
2 changes: 1 addition & 1 deletion libexec/scoop-which.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ try { $gcm = gcm $command -ea stop } catch { }
if(!$gcm) { [console]::error.writeline("'$command' not found"); exit 3 }

$path = $gcm.path
$abs_shimdir = "$(resolve-path $shimdir)"
$abs_shimdir = "$(resolve-path $(shimdir $false))"

if("$path" -like "$abs_shimdir*") {
$shimtext = gc $path
Expand Down

0 comments on commit 555e189

Please sign in to comment.