From 8ebddce654a56fdebf83dd6f4d357d447390efeb Mon Sep 17 00:00:00 2001 From: Luke Sampson Date: Sun, 16 Jun 2013 01:16:26 +1000 Subject: [PATCH] started on command dispatch --- bin/install.ps1 | 4 ++-- bin/scoop.ps1 | 9 +++++-- bin/uninstall.ps1 | 2 +- lib/cmd/help.ps1 | 3 +++ lib/cmd/install.ps1 | 4 +++- lib/commands.ps1 | 10 ++++++-- lib/{init.ps1 => core.ps1} | 15 ++++++++---- lib/install-ps.ps1 | 48 -------------------------------------- 8 files changed, 34 insertions(+), 61 deletions(-) create mode 100644 lib/cmd/help.ps1 rename lib/{init.ps1 => core.ps1} (89%) delete mode 100644 lib/install-ps.ps1 diff --git a/bin/install.ps1 b/bin/install.ps1 index 742ba825..07b41d9d 100644 --- a/bin/install.ps1 +++ b/bin/install.ps1 @@ -3,9 +3,9 @@ $erroractionpreference='stop' # quit if anything goes wrong # get core functions -$init_url = 'https://raw.github.com/lukesampson/scoop/master/lib/init.ps1' +$core_url = 'https://raw.github.com/lukesampson/scoop/master/lib/core.ps1' echo 'initializing...' -iex (new-object net.webclient).downloadstring($init_url) +iex (new-object net.webclient).downloadstring($core_url) # prep assert_not_installed 'scoop' diff --git a/bin/scoop.ps1 b/bin/scoop.ps1 index 2be215c0..ba612db1 100644 --- a/bin/scoop.ps1 +++ b/bin/scoop.ps1 @@ -1,6 +1,11 @@ +# Usage: sub param($cmd) -. "$(split-path $myinvocation.mycommand.path)\..\lib\init.ps1" +. "$(split-path $myinvocation.mycommand.path)\..\lib\core.ps1" +. (resolve '..\lib\commands') -require '..\lib\commands' +$commands = commands +if (@($null, '-h', '--help') -contains $cmd) { exec 'help' $args } +elseif ($commands -contains $cmd) { exec $cmd $args } +else { "scoop: '$cmd' isn't a scoop command. See 'scoop help'"; exit 1 } \ No newline at end of file diff --git a/bin/uninstall.ps1 b/bin/uninstall.ps1 index 7bef7564..b34bc53e 100644 --- a/bin/uninstall.ps1 +++ b/bin/uninstall.ps1 @@ -1,4 +1,4 @@ -. "$(split-path $myinvocation.mycommand.path)\..\lib\init.ps1" +. "$(split-path $myinvocation.mycommand.path)\..\lib\core.ps1" if(test-path $scoopdir) { try { diff --git a/lib/cmd/help.ps1 b/lib/cmd/help.ps1 new file mode 100644 index 00000000..97aaa4ec --- /dev/null +++ b/lib/cmd/help.ps1 @@ -0,0 +1,3 @@ +# Usage: scoop help +# Summary: show help for a command +echo "help!" \ No newline at end of file diff --git a/lib/cmd/install.ps1 b/lib/cmd/install.ps1 index 43d1e3e0..c29f4696 100644 --- a/lib/cmd/install.ps1 +++ b/lib/cmd/install.ps1 @@ -1,2 +1,4 @@ # Usage: scoop install -# Summary: Installs an app \ No newline at end of file +# Summary: Installs an app + +echo 'install!' \ No newline at end of file diff --git a/lib/commands.ps1 b/lib/commands.ps1 index b76de7a0..b4db5aba 100644 --- a/lib/commands.ps1 +++ b/lib/commands.ps1 @@ -1,3 +1,9 @@ -function all_commands { - echo "all_commands" +function commands { + gci (resolve 'cmd') | + where { $_.name.endswith('.ps1') } | + % { ($_.name -replace '\.ps1$', '') } +} + +function exec($cmd) { + & (resolve "cmd\$cmd.ps1") } \ No newline at end of file diff --git a/lib/init.ps1 b/lib/core.ps1 similarity index 89% rename from lib/init.ps1 rename to lib/core.ps1 index 4e1d26d5..875d1d7a 100644 --- a/lib/init.ps1 +++ b/lib/core.ps1 @@ -13,13 +13,13 @@ function appdir($name) { "$scoopdir\apps\$name" } function fname($path) { split-path $path -leaf } function ensure($dir) { if(!(test-path $dir)) { mkdir $dir > $null }; resolve-path $dir } -function full_path($path) { +function full_path($path) { # should be ~ rooted $executionContext.sessionState.path.getUnresolvedProviderPathFromPSPath($path) } function friendly_path($path) { return "$path" -replace ([regex]::escape($home)), "~" } -function script_rel_path($path) { full_path "$($myInvocation.PSScriptRoot)\$path" } +function resolve($path) { "$($myInvocation.PSScriptRoot)\$path" } # relative to calling script function installed($name) { return test-path (appdir $name) } function assert_not_installed($name) { @@ -51,7 +51,9 @@ function ensure_scoop_in_path { } } -$required = @( $myInvocation.myCommand.path.tolower ) +# failed +<# +$required = @( $myInvocation.myCommand.path.tolower() ) function require($name) { if(!$name.endsWith('.ps1')) { $name += '.ps1' } $path = "$($myInvocation.PSScriptRoot)\$name" @@ -59,6 +61,9 @@ function require($name) { $path = "$(resolve-path $path)".tolower() if(!($required -contains $path)) { $required += $path - iex "$path" + #iex $path + #. $path -global + #import-module $path -global } -} \ No newline at end of file +} +#> \ No newline at end of file diff --git a/lib/install-ps.ps1 b/lib/install-ps.ps1 deleted file mode 100644 index 7f1ded4d..00000000 --- a/lib/install-ps.ps1 +++ /dev/null @@ -1,48 +0,0 @@ -# 1. installs a powershell script to ~\appdata\local\scoop\[name]\[name].ps1 -# 2. adds a stub to ~\appdata\local\scoop\bin, to avoid path pollution -# 3. makes sure ~\appdata\local\scoop\bin is in your path -function install-ps($name, $url) { - $erroractionpreference = 'stop' - - # helpers - function dl($url,$to) { (new-object system.net.webClient).downloadFile($url,$to) } - function env($name,$val) { - if($val) { [environment]::setEnvironmentVariable($name,$val,'User') } # set - else { [environment]::getEnvironmentVariable($name, 'User') } # get - } - function abort($msg) { write-host $msg -b darkred -f white; exit 1 } - - # prep - echo "installing $name..." - if($name.endswith(".ps1")) { $name = $name -replace '\.ps1$', '' } - - $scoopdir = "~\appdata\local\scoop" - $bindir = "$scoopdir\bin" - $appdir = "$scoopdir\$name" - - if(test-path $appdir) { abort("It looks like ``$name`` is already installed. If you'd like to re-install, please run ``rmdir ~\appdata\local\scoop\$name`` first.") } - - # install - mkdir $appdir > $null - $appdir = resolve-path $appdir - echo "downloading $url..." - dl $url "$appdir\$name.ps1" - - # binstub - echo "creating stub in ~\appdata\local\bin" - if(!(test-path $bindir)) { mkdir $bindir > $null } - $bindir = resolve-path $bindir - - echo "`$rawargs = `$myInvocation.line -replace `"^`$([regex]::escape(`$myInvocation.invocationName))\s+`", `"`"" >> "$bindir\$name.ps1" - echo "iex `"$appdir\$name.ps1 `$rawargs`"" >> "$bindir\$name.ps1" - - # ensure path - $userpath = env 'path' - if($userpath -notmatch [regex]::escape($bindir)) { - echo "adding ~\appdata\local\bin to your user path" - env 'path' "$userpath;$bindir" # for future sessions - $env:path = "$env:path;$bindir" # for this session - } - - echo "done!" -} \ No newline at end of file