Class: Popo::Runner

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/popo/runner.rb

Constant Summary

Constants included from Constants

Constants::COLOR_GREEN, Constants::COLOR_NONE, Constants::COLOR_RED, Constants::COLOR_YELLOW, Constants::DEFAULT_CONFIG_FILE, Constants::DEFAULT_POPO_TARGET, Constants::POPORC, Constants::POPO_COMMANDS, Constants::POPO_CONFIG, Constants::POPO_DIR_KEY, Constants::POPO_KEY_VALUES, Constants::POPO_WORK_PATH, Constants::POPO_YML_FILE, Constants::POST_INSTALL_NOTE, Constants::REQUIRED_COMMANDS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Runner

Returns a new instance of Runner.



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
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/popo/runner.rb', line 40

def initialize(args)
  @args     = args
  @db_opts  = {}
  @options  = {}
  @app_root = ENV['popo_path'] || Dir.pwd

  if Utils.has_popo_config?(@app_root)
    @options[:target] = POPO_CONFIG['target']
  end

  opt = OptParse.new

  if @args.length >= 1
    opt.optparse.parse!
    @options.merge!(opt.options)

    db_options = {
      :path => File.join(@app_root, POPO_WORK_PATH),
      :target => ENV['CABLING_TARGET'] || @options[:target] || DEFAULT_POPO_TARGET,
      :verbose => @options[:verbose],
      :location => ENV['CABLING_LOCATION'] || @options[:location]
    }

    @db_opts.merge!(db_options)

    # let set these so we can use them in migration files
    Object.const_set("POPO_PATH", @app_root)
    Object.const_set("POPO_USER", @options[:user])
    Object.const_set("POPO_TARGET", @db_opts[:target])
    Object.const_set("POPO_LOCATION", @options[:location])

    mandatory = [:path, :manifest]
    mandatory = mandatory.select { |p| @options[p].nil? }

    if @args.first.eql? 'init' and !mandatory.empty?
      Error.say "Required options: #{mandatory.join(', ')}"
    end

    run
  else
    Utils.say opt.optparse.help
  end
end

Instance Attribute Details

#app_rootObject (readonly)

Returns the value of attribute app_root.



7
8
9
# File 'lib/popo/runner.rb', line 7

def app_root
  @app_root
end

#argsObject (readonly)

Returns the value of attribute args.



7
8
9
# File 'lib/popo/runner.rb', line 7

def args
  @args
end

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/popo/runner.rb', line 5

def config
  @config
end

#databaseObject (readonly)

Returns the value of attribute database.



6
7
8
# File 'lib/popo/runner.rb', line 6

def database
  @database
end

#db_optsObject (readonly)

Returns the value of attribute db_opts.



6
7
8
# File 'lib/popo/runner.rb', line 6

def db_opts
  @db_opts
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/popo/runner.rb', line 7

def options
  @options
end

Class Method Details

.boot(args) ⇒ Object



10
11
12
13
# File 'lib/popo/runner.rb', line 10

def boot(args)
  Utils.colorize
  self.new(args) if has_requirements?
end

.get_bin_path(bin) ⇒ Object



35
36
37
# File 'lib/popo/runner.rb', line 35

def get_bin_path(bin)
  `which #{bin}`.strip
end

.has_requirements?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/popo/runner.rb', line 15

def has_requirements?
  if ENV.include?('SHELL')
    Popo::Constants.const_set("SHELL", ENV['SHELL'])
  else
    Error.say("SHELL is empty.")
  end

  Popo::Constants::REQUIRED_COMMANDS.each do |b|
    const_fn = "#{b.upcase}_CMD"

    Popo::Constants.const_set(const_fn, get_bin_path(b))

    if Popo::Constants.const_get(const_fn).empty?
      Error.say "#{b} is needed and is not found in PATH!"
    end
  end

  true
end

Instance Method Details

#runObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/popo/runner.rb', line 84

def run
  if POPO_COMMANDS.include?(@args.first)
    Utils.in_popo?(@app_root)
  end

  cmd = args.shift

  case cmd
  when 'init'
    @config = get_config!

    if @config['manifests'][@options[:manifest]].nil?
      Error.say "\'#{@options[:manifest]}\' does not exist in #{DEFAULT_CONFIG_FILE}!"
    end

    if !File.exist?(File.join(@app_root, @options[:path]))
      @db_opts[:path] = File.join(@app_root, @options[:path], POPO_WORK_PATH)
      get_database

      Init.boot(self).setup
    else
      Error.say "Path \'#{@options[:path]}\' already exists!"
    end
  when 'sync'
    get_database

    Sync.new(self).sync
  when 'rvm'
    get_database

    RVM.new(self).setup
  when 'update'
    manifest_path = File.join(@app_root, POPO_WORK_PATH)

    case POPO_TARGET
    when DEFAULT_POPO_TARGET
      GitUtils.git_update(manifest_path, DEFAULT_POPO_TARGET)
    else
      GitUtils.git_update(manifest_path, 'master')
    end

    get_database
    @database.migrate_database
  when 'migrate'
    get_database
    @database.migrate_database
  when 'info'
    Utils.say `cat #{File.join(@app_root, POPO_WORK_PATH, POPO_YML_FILE)}`
  when 'shell', 'bash'
    sh!
  when 'diff'
    GitUtils.branch_diff(Dir.pwd)
  else
    Error.say "#{cmd} is not a valid command!"
  end
end