pik 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/History.txt +36 -0
  2. data/Manifest.txt +42 -0
  3. data/README.rdoc +174 -0
  4. data/Rakefile +37 -0
  5. data/bin/pik +33 -0
  6. data/lib/pik.rb +30 -0
  7. data/lib/pik/batch_file.rb +70 -0
  8. data/lib/pik/checkup.rb +11 -0
  9. data/lib/pik/commands.rb +35 -0
  10. data/lib/pik/commands/add_command.rb +65 -0
  11. data/lib/pik/commands/batch_file_editor.rb +62 -0
  12. data/lib/pik/commands/checkup_command.rb +67 -0
  13. data/lib/pik/commands/command.rb +166 -0
  14. data/lib/pik/commands/config_command.rb +28 -0
  15. data/lib/pik/commands/config_file_editor.rb +12 -0
  16. data/lib/pik/commands/default_command.rb +22 -0
  17. data/lib/pik/commands/gemsync_command.rb +69 -0
  18. data/lib/pik/commands/help_command.rb +44 -0
  19. data/lib/pik/commands/implode_command.rb +16 -0
  20. data/lib/pik/commands/list_command.rb +34 -0
  21. data/lib/pik/commands/remove_command.rb +29 -0
  22. data/lib/pik/commands/run_command.rb +37 -0
  23. data/lib/pik/commands/switch_command.rb +49 -0
  24. data/lib/pik/config_file.rb +21 -0
  25. data/lib/pik/core_ext/pathname.rb +15 -0
  26. data/lib/pik/options.rb +56 -0
  27. data/lib/pik/runner.rb +16 -0
  28. data/lib/pik/search_path.rb +51 -0
  29. data/lib/pik/windows_env.rb +64 -0
  30. data/spec/add_command_spec.rb +15 -0
  31. data/spec/batch_file_spec.rb +31 -0
  32. data/spec/command_spec.rb +34 -0
  33. data/spec/commands_spec.rb +44 -0
  34. data/spec/default_command_spec.rb +6 -0
  35. data/spec/gemsync_command_spec.rb +15 -0
  36. data/spec/help_command_spec.rb +30 -0
  37. data/spec/list_command_spec.rb +33 -0
  38. data/spec/remove_command_spec.rb +25 -0
  39. data/spec/run_command_spec.rb +36 -0
  40. data/spec/search_path_spec.rb +121 -0
  41. data/spec/spec.opts +3 -0
  42. data/spec/switch_command_spec.rb +44 -0
  43. metadata +143 -0
@@ -0,0 +1,44 @@
1
+ module Pik
2
+
3
+ class Help < Command
4
+
5
+ it 'Displays help information.'
6
+
7
+ def execute
8
+ puts options if @args.empty?
9
+
10
+ @args.each do |a|
11
+ @msg = case a.to_sym
12
+ when *Commands.list
13
+ Commands.find(a).description
14
+ when :commands
15
+ Commands.description
16
+ else
17
+ "There is no command '#{a}' \n" +
18
+ Pik::Help.description
19
+ end
20
+
21
+ puts "\n" + @msg + "\n"
22
+ end
23
+
24
+ end
25
+
26
+ def command_options
27
+ options.program_name = "#{File.basename($0)} command"
28
+ sep = <<SEP
29
+
30
+ To get help with a command
31
+
32
+ pik help (command)
33
+
34
+ To list all commands and descriptions:
35
+
36
+ pik help commands
37
+
38
+ SEP
39
+ options.separator sep
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -0,0 +1,16 @@
1
+
2
+ module Pik
3
+
4
+ class Implode < Command
5
+
6
+ it "Removes your pik configuration."
7
+
8
+ def execute
9
+ if @hl.agree("Are you sure you want pik to implode? This will remove '#{PIK_HOME.to_ruby}'. [Yn] ")
10
+ PIK_HOME.rmtree
11
+ end
12
+ end
13
+
14
+ end
15
+
16
+ end
@@ -0,0 +1,34 @@
1
+ module Pik
2
+
3
+ class List < Command
4
+
5
+ aka :ls
6
+ it "Lists ruby versions that pik is aware of."
7
+
8
+ attr_reader :verbose
9
+
10
+ def execute
11
+ config.sort.each do |name, conf|
12
+ name += ' *' if current_version?(name)
13
+ puts name
14
+ if verbose
15
+ conf.each{|k,v| puts " %s: %s" % [k, v]}
16
+ puts
17
+ end
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def command_options
24
+ super
25
+ @options.on("--verbose", "-v",
26
+ "Verbose output"
27
+ ) do |value|
28
+ @verbose = value
29
+ end
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,29 @@
1
+ module Pik
2
+
3
+ class Remove < Command
4
+
5
+ aka :rm
6
+ it "Removes a ruby location from pik."
7
+ include ConfigFileEditor
8
+
9
+ attr_reader :quiet
10
+
11
+ def execute
12
+ to_remove = self.class.choose_from(@args, @config)
13
+ raise QuitError unless to_remove
14
+ if quiet || @hl.agree("Are you sure you'd like to remove '#{to_remove}'? [Yn] ")
15
+ @config.delete(to_remove)
16
+ @hl.say("#{to_remove} removed") unless quiet
17
+ end
18
+ end
19
+
20
+ def command_options
21
+ super
22
+ options.on("--quiet", "-q", "Remove without prompting") do |value|
23
+ @quiet = value
24
+ end
25
+ end
26
+
27
+ end
28
+
29
+ end
@@ -0,0 +1,37 @@
1
+ module Pik
2
+
3
+ class Run < Command
4
+
5
+ it "Runs command with all version of ruby that pik is aware of."
6
+ include BatchFileEditor
7
+
8
+ def execute
9
+ command = @args.join(' ')
10
+ current_ruby = @config[get_version]
11
+ @config.sort.each do |version,hash|
12
+ switch_path_to(hash)
13
+ switch_gem_home_to(hash[:gem_home])
14
+ echo_running_with_ruby_version
15
+ @batch.call command
16
+ @batch.echo "."
17
+ end
18
+ switch_path_to(current_ruby)
19
+ switch_gem_home_to(current_ruby[:gem_home])
20
+ end
21
+
22
+ def command_options
23
+ super
24
+ sep =<<SEP
25
+ Examples:
26
+
27
+ C:\\>pik run "ruby -v"
28
+
29
+ C:\\>pik run "rake spec"
30
+
31
+ SEP
32
+ options.separator sep
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,49 @@
1
+ module Pik
2
+
3
+ class Switch < Command
4
+
5
+ aka :sw, :use
6
+ it "Switches ruby versions based on patterns."
7
+ include BatchFileEditor
8
+
9
+ attr_accessor :global
10
+ attr_accessor :gem_home
11
+
12
+ def execute
13
+ abort('Nothing matches:') unless new_ver = self.class.choose_from(@args, @config)
14
+ switch_path_to(@config[new_ver])
15
+ switch_gem_home_to(@config[new_ver][:gem_home])
16
+ echo_ruby_version
17
+ end
18
+
19
+ def command_options
20
+ # options.on("--global", "-g", "Make changes globally") do |value|
21
+ # @global = value
22
+ # end
23
+
24
+ # options.on("-m name", "specify gem_home (Named gem sets)") do |value|
25
+ # @gem_home = value
26
+ # end
27
+
28
+ super
29
+ sep =<<SEP
30
+ Examples:
31
+
32
+ C:\\>pik sw 186 mingw
33
+ Using ruby 1.8.6 (2009-03-31 patchlevel 368) [i386-mingw32]
34
+
35
+ C:\\>pik switch 191 p1
36
+ Using ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-mingw32]
37
+
38
+ C:\\>pik use 186 mswin
39
+ Using ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]
40
+
41
+ C:\\>pik 191 p2
42
+ Using ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-mingw32]
43
+ SEP
44
+ options.separator sep
45
+ end
46
+ end
47
+
48
+
49
+ end
@@ -0,0 +1,21 @@
1
+ require 'yaml'
2
+
3
+ module Pik
4
+
5
+ class ConfigFile < Hash
6
+
7
+ def initialize
8
+ @file = File.join(PIK_HOME, 'config.yml')
9
+ super{|h,k| h[k] = Hash.new }
10
+ if File.exists? @file
11
+ self.update( YAML.load( File.read( @file ) ) )
12
+ end
13
+ end
14
+
15
+ def write
16
+ File.open(@file, 'w'){|f| f.puts YAML::dump(Hash[self]) }
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,15 @@
1
+
2
+ class Pathname
3
+
4
+ def to_windows
5
+ @path.gsub!('/','\\')
6
+ self
7
+ end
8
+
9
+ def to_ruby
10
+ @path.gsub!('\\','/')
11
+ self
12
+ end
13
+
14
+ end
15
+
@@ -0,0 +1,56 @@
1
+ require 'optparse'
2
+
3
+ module Pik
4
+
5
+ class Options
6
+
7
+ attr_reader :global
8
+
9
+ attr_reader :interactive
10
+
11
+ attr_reader :default
12
+
13
+ attr_reader :verbose
14
+
15
+
16
+ def self.parse(args)
17
+ new(args).parse
18
+ end
19
+
20
+ def initialize(args)
21
+ @args = args
22
+ end
23
+
24
+ def parse
25
+ opts = OptionParser.new do |opt|
26
+ opt.program_name = File.basename $0
27
+ opt.on("--global", "-g"
28
+ "change the option globally."
29
+ ) do |value|
30
+ @global = value
31
+ end
32
+
33
+ opt.on("--verbose", "-v"
34
+ "Verbose output"
35
+ ) do |value|
36
+ @verbose = value
37
+ end
38
+
39
+ opt.on("--interactive", "-i"
40
+ "run the command interactively"
41
+ ) do |value|
42
+ @interactive = value
43
+ end
44
+
45
+ opt.on("--default", "-d"
46
+ "perform the default action"
47
+ ) do |value|
48
+ @default = value
49
+ end
50
+
51
+ end
52
+ end
53
+
54
+ end
55
+
56
+ end
@@ -0,0 +1,16 @@
1
+ module Pik
2
+
3
+ class Runner
4
+
5
+ def add_gem_home(*patterns)
6
+ to_add = get_version
7
+ new_dir = @options[:default] ? Gem.default_path.first : @hl.ask("Enter a path to a GEM_HOME dir")
8
+ if @hl.agree("Add a GEM_HOME and GEM_PATH for '#{to_add}'? [Yn] ")
9
+ @config[to_add][:gem_home] = new_dir
10
+ @hl.say("GEM_HOME and GEM_PATH added for: #{to_add} ")
11
+ end
12
+ end
13
+
14
+ end
15
+
16
+ end
@@ -0,0 +1,51 @@
1
+
2
+ class SearchPath
3
+
4
+ def initialize(path)
5
+ @path = path.to_s.split(';')
6
+ end
7
+
8
+ def remove(old_path)
9
+ old = Pathname.new(old_path).to_windows
10
+ @path.reject!{|dir| dir =~ regex(old.to_s) }
11
+ self
12
+ end
13
+
14
+ def replace(old_path, new_path)
15
+
16
+ old_path = Pathname.new(old_path)
17
+ new_path = Pathname.new(new_path)
18
+ @path.map!{|dir|
19
+ case dir
20
+ when regex(old_path.to_windows)
21
+ new_path.to_windows.to_s
22
+ else
23
+ dir
24
+ end
25
+ }.uniq.compact
26
+ self
27
+ end
28
+
29
+ def add(new_path)
30
+ new_path = Pathname.new(new_path)
31
+ @path << new_path.to_windows.to_s
32
+ self
33
+ end
34
+
35
+ def replace_or_add(old_path, new_path)
36
+ return self if old_path == new_path
37
+ old_search_path = @path.dup
38
+ replace(old_path, new_path)
39
+ add(new_path) if @path == old_search_path
40
+ self
41
+ end
42
+
43
+ def join
44
+ @path.join(';')
45
+ end
46
+ alias :to_s :join
47
+
48
+ def regex(string)
49
+ Regexp.new(Regexp.escape(string.to_s), true)
50
+ end
51
+ end
@@ -0,0 +1,64 @@
1
+ require 'win32ole'
2
+
3
+ class WindowsEnv
4
+
5
+ def self.system
6
+ @system ||= new('SYSTEM', shell)
7
+ end
8
+
9
+ def self.user
10
+ @user ||= new('User', shell)
11
+ end
12
+
13
+ def initialize(account, shell)
14
+ @env = shell.environment(account)
15
+ end
16
+
17
+ def set(items)
18
+ items.each{|k,v| self[k] = v }
19
+ end
20
+
21
+ def [](name)
22
+ @env.item(name)
23
+ end
24
+
25
+ def []=(name, other)
26
+ if other == nil
27
+ @env.remove(name)
28
+ else
29
+ @env.setproperty('item', name, other)
30
+ end
31
+ end
32
+
33
+ def has_key?(key)
34
+ !!self[key]
35
+ end
36
+
37
+ private
38
+
39
+ def self.shell
40
+ @shell ||= WIN32OLE.new('WScript.Shell')
41
+ end
42
+
43
+ end
44
+
45
+ # env = WindowsEnv.user
46
+
47
+ # home = env['home']
48
+ # p home
49
+
50
+ # env['home'] = nil
51
+
52
+ # p env['home']
53
+
54
+ # env['rubyopt'] = '-rubygems'
55
+
56
+ # env['home'] = home
57
+ # p env['home']
58
+
59
+ # p env['path']
60
+
61
+ # p env.has_key?('path')
62
+ # p env.has_key?('rubyopt')
63
+
64
+