Class: Capistrano::Former03::Install

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano/former03/install.rb

Overview

Install application

Constant Summary collapse

@@name =
'f03capinstall'

Instance Method Summary collapse

Constructor Details

#initializeInstall

Returns a new instance of Install.



13
14
15
16
17
18
19
20
# File 'lib/capistrano/former03/install.rb', line 13

def initialize
  @options = {}
  @argv = nil
  @log = Logger.new(STDOUT)
  @log.level = Logger::INFO


end

Instance Method Details

#generate_file(path, template, stage = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/capistrano/former03/install.rb', line 70

def generate_file (path,template,stage=nil)
  require 'capistrano/version'

  if not path.dirname.directory?
    @log.debug "create directory '#{path.dirname}"
    FileUtils.mkdir_p path.dirname
  end

  if path.exist?
    if @options.force
      @log.warn "Overwriting file '#{path}'. To overwrite it use flag --force"
      path.delete
    else
      @log.warn "won't overwrite already existing file '#{path}'. To overwrite it use flag --force"
      return
    end
  end

  f=path.open('w')
  f.write(template.result(binding))
  f.close

  @log.info "Wrote file '#{path}'"
end

#generate_filesObject



61
62
63
64
65
66
67
68
# File 'lib/capistrano/former03/install.rb', line 61

def generate_files
  pwd = Pathname.pwd
  generate_file pwd.join('Capfile'), @template_capfile
  generate_file pwd.join('config/deploy.rb'), @template_deploy_rb
  @options.stages.each do |stage|
    generate_file pwd.join("config/deploy/#{stage}.rb"), @template_stage_rb, stage
  end
end

#parse_argumentsObject



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
# File 'lib/capistrano/former03/install.rb', line 22

def parse_arguments
  options = OpenStruct.new
  options.stages = ['production','staging','development']
  options.force = false
  options.verbose = false

  opt_parser = OptionParser.new do |opts|
    opts.banner = "Usage: #{@@name} [options]"
    # Boolean switch
    opts.on("-s", "--stages [#{options.stages.join(',')}]", Array, "Specify custom stage names") do |s|
      options.stages = stages
    end

    opts.on("-f", "--force", "Overwrite existing files") do |v|
      options.force = v
    end

    opts.on("-v", "--verbose", "Run verbosely") do |v|
      options.verbose = v
    end
  end

  opt_parser.parse!(@argv)

  @options = options

  if @options.verbose
    @log.level = Logger::DEBUG
  end
end

#runObject

Main method of application



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/capistrano/former03/install.rb', line 96

def run
  @argv = ARGV.dup

  # Parse arguments
  parse_arguments

  # logging
  @log.debug "#{@@name} is starting with arguments: #{ARGV}"
  @log.debug "Parsed options: #{@options}"

  # setup templates
  setup_templates

  # generate the actual files
  generate_files

end

#setup_templatesObject



53
54
55
56
57
58
59
# File 'lib/capistrano/former03/install.rb', line 53

def setup_templates
  template_dir = Pathname.new(File.expand_path("../templates/", __FILE__))
  @log.debug "Search for templates in path '#{template_dir}'"
  @template_deploy_rb = ERB.new template_dir.join('deploy.rb.erb').read
  @template_stage_rb = ERB.new template_dir.join('stage.rb.erb').read
  @template_capfile = ERB.new template_dir.join('Capfile').read
end