Skip to content

Commit

Permalink
added support for Git Bash. Some code cleanup. Fixed up use specs
Browse files Browse the repository at this point in the history
  • Loading branch information
vertiginous committed Jun 19, 2010
1 parent a9af535 commit 450e07d
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 47 deletions.
45 changes: 37 additions & 8 deletions bin/pik_install
Expand Up @@ -25,15 +25,36 @@ abort usage unless ARGV.first

puts "Thank you for using pik.\n\n"

install_dir = ARGV.first
tools_dir = File.join(File.dirname(__FILE__), '..', 'tools')
install_dir = ARGV.first

pik_home = File.join(ENV['USERPROFILE'], '.pik')

src_dir = File.join(File.dirname(File.dirname(__FILE__)), 'tools')

bash_install_dir = install_dir.tr('\\','/').sub(/^(.):/){|s| "/#{s[0,1].downcase}"}

mkdir_p install_dir, :verbose => true
mkdir_p pik_home, :verbose => true

puts "Installing to #{install_dir}\n\n"
cp File.join(tools_dir, 'pik.bat'), install_dir, :verbose => true
cp File.join(tools_dir, 'pik.ps1'), install_dir, :verbose => true
cp File.join(tools_dir, 'pik'), install_dir, :verbose => true
cp File.join(tools_dir, 'pik_runner.exe'), install_dir, :verbose => true

cp File.join(src_dir, 'pik_runner.exe'), install_dir, :verbose => true
cp File.join(src_dir, 'pik.bat'), install_dir, :verbose => true
cp File.join(src_dir, 'pik.ps1'), install_dir, :verbose => true

pikrc_file = File.join(pik_home, '.bashrc')
pikrc_contents =<<PIKRC
#!/bin/sh
pik_path=#{bash_install_dir}
function pik {
$pik_path/pik_runner.exe pik.sh $@
[[ -s $USERPROFILE/.pik/pik.sh ]] && source $USERPROFILE/.pik/pik.sh

This comment has been minimized.

Copy link
@aeurielesn

aeurielesn Feb 3, 2011

I happened to need to add after this line:
unset GEM_HOME
unset GEM_PATH
for pik to properly set my gems when switching rubies. Although, I have just tested it with ruby 1.9.2 and 1.8.7.

}
PIKRC
puts
puts "creating #{pikrc_file}"
File.open(pikrc_file, 'w+'){|f| f.puts pikrc_contents }

puts "\npik is installed"

unless ENV['PATH'].downcase.include?( install_dir.downcase.gsub('/',"\\") )
Expand All @@ -49,4 +70,12 @@ SYSTEM_PATH_MSG

puts system_path_msg

end
end

puts
puts bash_msg =<<BASH_MSG
if you want to use pik with git bash, add the following line to your ~/.bashrc:
[[ -s $USERPROFILE/.pik/.pikrc ]] && source $USERPROFILE/.pik/.pikrc
BASH_MSG
10 changes: 9 additions & 1 deletion lib/pik.rb
Expand Up @@ -15,6 +15,7 @@
require 'pik/scripts/script_file'
require 'pik/scripts/batch_file'
require 'pik/scripts/ps_file'
require 'pik/scripts/bash_file'
require 'pik/commands'
require 'pik/commands/config_file_editor'
require 'pik/commands/script_file_editor'
Expand Down Expand Up @@ -44,7 +45,12 @@

module Pik
VERSION = '0.2.7'
Scripts = {'.cmd' => BatchFile, '.bat' => BatchFile, '.ps1' => PsFile}
Scripts = {
'.cmd' => BatchFile,
'.bat' => BatchFile,
'.ps1' => PsFile,
'.sh' => BashFile
}

def self.print_error(error)
puts "\nThere was an error."
Expand All @@ -65,4 +71,6 @@ def self.print_error(error)
PIK_SCRIPT = Pathname.new(ARGV.shift).ruby
SCRIPT_LANG = Pik::Scripts[PIK_SCRIPT.extname]
SCRIPT_FILE = SCRIPT_LANG.new(PIK_HOME + 'pik')
else
SCRIPT_FILE = Pik::BatchFile.new(PIK_HOME + 'pik')
end
15 changes: 2 additions & 13 deletions lib/pik/commands/command.rb
Expand Up @@ -47,14 +47,6 @@ def self.aka(*aliases)
def self.names
@names ||= [cmd_name]
end

# def self.clean_gem_batch
# BatchFile.open(PIK_SCRIPT) do |gem_bat|
# # remove old calls to .pik/pik batches
# gem_bat.remove_line( /call.+pik.+bat/i )
# gem_bat.write
# end
# end

def self.choose_from(patterns, config)
if patterns.empty?
Expand Down Expand Up @@ -85,7 +77,7 @@ def initialize(args=ARGV, config_=nil)
@config = config_ || ConfigFile.new
@hl = HighLine.new
add_sigint_handler
options.program_name = "#{PIK_SCRIPT.basename('.*')} #{self.class.names.join('|')}"
options.program_name = "pik #{self.class.names.join('|')}"
command_options
parse_options
create(PIK_HOME) unless PIK_HOME.exist?
Expand Down Expand Up @@ -162,10 +154,7 @@ def create(home)
home.mkpath
end

def delete_old_pik_script #s ( cutoff=(Time.now - (2 * 60 * 60)) )
# Dir[(PIK_HOME + "*#{SCRIPT_FILE.extname}").to_ruby.to_s].each do |f|
# File.delete(f) if File.ctime(f) < cutoff
# end
def delete_old_pik_script
SCRIPT_FILE.path.delete if SCRIPT_FILE.path.exist?
end

Expand Down
2 changes: 1 addition & 1 deletion lib/pik/commands/default_command.rb
Expand Up @@ -13,7 +13,7 @@ def execute
sys = WindowsEnv.system
usr = WindowsEnv.user
new_path = [sys['PATH'],usr['PATH']].compact.join(';')
@script.set('PATH' => new_path )
@script.set('PATH' => SearchPath.new(new_path) )
@script.set('GEM_PATH' => usr['GEM_PATH'] || sys['GEM_PATH'] )
@script.set('GEM_HOME' => usr['GEM_HOME'] || sys['GEM_HOME'] )
echo_ruby_version(Which::Ruby.find(new_path)) if verbose
Expand Down
3 changes: 1 addition & 2 deletions lib/pik/commands/script_file_editor.rb
Expand Up @@ -28,8 +28,7 @@ def switch_path_to(other)

# if the new version has a GEM_HOME, add it's bin dir to the path
new_path.add(Pathname.new(other[:gem_home]) + 'bin') if other[:gem_home]

@script.set('PATH' => new_path.join )
@script.set('PATH' => new_path )
end

def switch_gem_home_to(gem_home)
Expand Down
8 changes: 6 additions & 2 deletions lib/pik/core_ext/pathname.rb
Expand Up @@ -2,12 +2,16 @@
class Pathname

def to_s
@path.tr('/','\\').sub(/^.:/){|s| s.upcase }
@path.tr('/','\\').sub(/^(.):/){|s| s.upcase }
end
alias to_windows to_s

def to_ruby
@path.tr('\\','/').sub(/^.:/){|s| s.upcase }
@path.tr('\\','/').sub(/^(.):/){|s| s.upcase }
end

def to_bash
@path.tr('\\','/').sub(/^(.):/){|s| "/#{s[0,1].downcase}"}
end

def ruby
Expand Down
36 changes: 36 additions & 0 deletions lib/pik/scripts/bash_file.rb
@@ -0,0 +1,36 @@
module Pik

class BashFile < ScriptFile

attr_accessor :file_data, :file_name, :ruby_dir

def extname
".sh"
end

def header
string = "#!/bin/sh\n"
string << "# This bash script generated by Pik, the\n"
string << "# Ruby Manager for Windows\n"
end

def call(exe)
@file_data << "#{exe}\n"
self
end

def set(items)
items.each{|k,v|
v = v.to_bash if v.respond_to? 'to_bash'
@file_data << "export #{k}='#{v}'" }
self
end

def unset(items)
items.each{|k| @file_data << "export #{k}=" }
self
end

end

end
4 changes: 4 additions & 0 deletions lib/pik/search_path.rb
Expand Up @@ -57,6 +57,10 @@ def join
end
alias :to_s :join

def to_bash
@path.map{|i| Pathname.new(i).to_bash }.join(':')
end

def regex(string)
Regexp.new(Regexp.escape(string.to_s), true)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/batch_file_spec.rb
@@ -1,8 +1,8 @@

describe BatchFile do
describe Pik::BatchFile do

before do
@bf = BatchFile.new('pik.bat')
@bf = Pik::BatchFile.new('pik.bat')
end

it "should have a header" do
Expand Down
7 changes: 7 additions & 0 deletions spec/pathname_spec.rb
Expand Up @@ -35,4 +35,11 @@
end
end

describe 'to_bash' do
it 'should change the file and disk separator' do
path = Pathname("z:\\This\\is\\to\\test\\separators")
result = '/z/This/is/to/test/separators'
path.to_bash.should eql(result)
end
end
end
37 changes: 29 additions & 8 deletions spec/search_path_spec.rb
Expand Up @@ -117,16 +117,37 @@
@path.replace_or_add('C:/xray/yankee/alpha', path)
@path.join.should == new_path
end
end

describe "find" do
it "should return the first path where block is not false" do
path = SearchPath.new(ENV['PATH'])
dir = path.find{|i| !!Dir[ File.join(i.gsub('\\','/'), '{ruby.exe, ir.exe}') ].first }
dir.should_not be_nil
dir.should be_a(String)
end
describe "#find" do
it "should return the first path where block is not false" do
path = SearchPath.new(ENV['PATH'])
dir = path.find{|i| !!Dir[ File.join(i.gsub('\\','/'), '{ruby.exe, ir.exe}') ].first }
dir.should_not be_nil
dir.should be_a(String)
end

end

describe '#to_bash' do
it 'should change the file and disk separator' do
new_path = 'C:\xray\yankee\zebra;'
new_path << 'C:\Program Files\Common Files\Shoes\0.r1134\..;'
new_path << 'C:\bin;'
new_path << 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;'
new_path << 'C:\Program Files\Common Files\Shoes\0.r395\..;'
new_path << 'C:\windows\system32'

bash_path = '/c/xray/yankee/zebra:'
bash_path << '/c/Program Files/Common Files/Shoes/0.r1134/..:'
bash_path << '/c/bin:'
bash_path << '/c/WINDOWS/Microsoft.NET/Framework/v2.0.50727:'
bash_path << '/c/Program Files/Common Files/Shoes/0.r395/..:'
bash_path << '/c/windows/system32'

path = SearchPath.new(new_path)
path.should be_a SearchPath
path.to_bash.should eql(bash_path)
end
end

end
20 changes: 10 additions & 10 deletions spec/switch_command_spec.rb
@@ -1,26 +1,26 @@
require 'pathname'

describe Pik::Switch do
describe Pik::Use do

it "should have a summary" do
Pik::Switch.summary.should eql("Switches ruby versions based on patterns.")
Pik::Use.summary.should eql("Switches ruby versions based on patterns.")
end

it "should have an alias of sw" do
Pik::Switch.names.should include(:sw)
Pik::Use.names.should include(:sw)
end

it "should have an alias of use" do
Pik::Switch.names.should include(:use)
Pik::Use.names.should include(:use)
end

it "should have a global option" #do
# sw = Pik::Switch.new(['-g'])
# sw = Pik::Use.new(['-g'])
# sw.global.should be_true
# end

it "should have a gem_home option" # do
# sw = Pik::Switch.new(['-m', 'test'])
# sw = Pik::Use.new(['-m', 'test'])
# sw.gem_home.should eql("test")
# end

Expand All @@ -29,9 +29,9 @@
'spec ' => {:path => Pathname('C:/ruby/bin')},
'real ' => {:path => Pathname.new(::RbConfig::CONFIG['bindir'] )}
}
cmd = Pik::Switch.new(['spec'], conf)
cmd = Pik::Use.new(['spec'], conf)
cmd.execute
batch = cmd.instance_variable_get('@batch').file_data
batch = cmd.instance_variable_get('@script').file_data
batch.should include("SET GEM_PATH=")
batch.should include("SET GEM_HOME=")
set_path = batch.grep(/set/i).first
Expand All @@ -46,9 +46,9 @@
},
'real ' => {:path => Pathname.new(::RbConfig::CONFIG['bindir'] )}
}
cmd = Pik::Switch.new(['spec'], conf)
cmd = Pik::Use.new(['spec'], conf)
cmd.execute
batch = cmd.instance_variable_get('@batch').file_data
batch = cmd.instance_variable_get('@script').file_data
batch.should include("SET GEM_PATH=C:\\Users\\martin_blanke\\.gems")
batch.should include("SET GEM_HOME=C:\\Users\\martin_blanke\\.gems")
end
Expand Down

0 comments on commit 450e07d

Please sign in to comment.