Module: RVMBS::Command

Defined in:
lib/rvmbs.rb

Class Method Summary collapse

Class Method Details

.create_directory(options) ⇒ Object

Creates the directory.



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rvmbs.rb', line 82

def self.create_directory(options)
  puts "Making new directory: #{options[:directory]}" if options[:verbose]
  begin
    Dir.mkdir options[:directory]
  rescue 
    if options[:force] 
      FileUtils.rm_rf options[:directory]
      options[:force] = false
      retry 
    end
    puts "ERROR: directory already exists or cannot be created (try with --force)."
  end
end

.create_rvmrc(options) ⇒ Object

Creates the .rvmrc file.



97
98
99
100
101
102
103
104
# File 'lib/rvmbs.rb', line 97

def self.create_rvmrc(options)  
  puts "Creating rvmrc file" if options[:verbose]
  Dir.chdir(options[:directory]) 
  File.open(".rvmrc", "w") do |f|
    f.write("rvm use --create #{options[:implementation]}@#{options[:directory]}\n")
  end
  Dir.chdir('..') 
end

.load_configObject

Load config from user home dir.



73
74
75
76
77
78
79
# File 'lib/rvmbs.rb', line 73

def self.load_config
  config_path = File.expand_path('~/.rvmbsrc')
  config = YAML.load_file( config_path ) if File.exists? config_path
  
  # config values
  @implementation = config['implementation']  || '1.9.2'
end

.run(args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rvmbs.rb', line 10

def self.run(args)
  
  options = {}
  optsp = nil

  load_config

  optparse = OptionParser.new do |opts|
    optsp = opts
    opts.banner = "RVM Bootstrap - Command to create a project directory with a configured .rvmrc into.

Usage: rvmbs -d <PROJECT_NAME> [options]"
    
    # This define the directory name.
    opts.on('-d', '--directory NAME', "The project's directory name.") do |name|
      options[:directory] = name
    end
     
    # This define the ruby implementation.
    options[:implementation] = @implementation
    opts.on('-i', '--ruby-implementation NAME', "The name of the Ruby implementation.") do |name|
      options[:implementation] = name
    end

    # This force to delete de directory if already exists. 
    options[:force] = false
    opts.on('-f', '--force', "Delete directory if already exists.") do 
      options[:force] = true
    end

    # Verbose mode 
    options[:verbose] = false
    opts.on('-v', '--verbose', "Print extra info.") do 
      options[:verbose] = true
    end

    # This will print an options summary.
    opts.on_tail("-h", "--help", "Show this message.") do
      puts opts
      exit
    end

  end

  begin
    optparse.parse!(args)
  rescue OptionParser::MissingArgument
    puts 'rvmbs: missing argument, -h for help'
    exit
  end

  # Directory must be set.
  if options[:directory] == nil
    puts optsp 
    exit
  else
    create_directory(options)
    create_rvmrc(options)
    set_rvmrc_trusted(options, Dir.pwd)
  end
end

.set_rvmrc_trusted(options, current_dir) ⇒ Object

Run rvm rvmrc trust command detached from console.



107
108
109
110
111
112
113
114
115
# File 'lib/rvmbs.rb', line 107

def self.set_rvmrc_trusted(options, current_dir)
  puts "Setting rvmrc file to trusted" if options[:verbose]
  pid = daemonize do 
    current_dir = current_dir + "/" + options[:directory] 
    exit system "rvm rvmrc trust #{current_dir}/"
  end
  _, status = Process.waitpid2(pid)
  puts "ERROR: rvm rvmrc trust command fail!" if status != 0
end