Class: Caphub::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/caphub/runner.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Runner

Returns a new instance of Runner.



14
15
16
17
# File 'lib/caphub/runner.rb', line 14

def initialize(args)
  @args = args.dup
  @options = { cap: 3 }
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



19
20
21
# File 'lib/caphub/runner.rb', line 19

def options
  @options
end

Class Method Details

.runObject



7
8
9
10
11
# File 'lib/caphub/runner.rb', line 7

def run
  runner = new(ARGV)
  runner.parse_options!
  runner.run!
end

Instance Method Details

#parse_options!Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/caphub/runner.rb', line 21

def parse_options!
  OptionParser.new do |opts|
    opts.banner = "Usage: #{File.basename($0)} [path]"
    opts.on("-c", "--cap VERSION", "Capistrano version 2 or 3") { |v| options[:cap] = v }
    opts.on_tail("-h", "--help", "Show this message") { puts opts; exit }
    opts.on_tail('-v', '--version', "Show version")   { puts Caphub::VERSION; exit }

    begin
      opts.parse!(@args)
    rescue OptionParser::ParseError => e
      warn e.message
      puts opts
      exit 1
    end
  end

  abort "Please specify the directory for capistrano hub" if @args.empty?
  abort "'#{@args.first}' exists." if File.exists?(@args.first)

  @options[:target] = @args.first
end

#run!Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/caphub/runner.rb', line 47

def run!
  target = options[:target]

  puts "Creating directory #{target}"
  FileUtils.mkdir(target)

  puts "Creating capistrano skeleton in #{target}"
  FileUtils.cp_r("#{skeleton_dir}/.", target)

  puts "Initializating git repository in #{target}"
  Dir.chdir(target) { `git init`;  `git add .` }
end

#skeleton_dirObject



43
44
45
# File 'lib/caphub/runner.rb', line 43

def skeleton_dir
  File.expand_path("../../../skeletons/cap#{options[:cap]}", __FILE__)
end