Class: Awestruct::CLI::Invoker

Inherits:
Object
  • Object
show all
Defined in:
lib/awestruct/cli/invoker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*options) ⇒ Invoker

Returns a new instance of Invoker.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/awestruct/cli/invoker.rb', line 18

def initialize(*options)
  options = options.flatten
  if ( ( ! options.empty? ) && ( options.first === Awestruct::CLI::Options ) )
    @options = options.first
  else
    @options = Awestruct::CLI::Options.parse! options
  end
  @threads = []
  @profile = nil
  @success = true
  logging_path = Pathname.new '.awestruct'
  logging_path.mkdir unless logging_path.exist?
  $LOG = Logger.new(Awestruct::AwestructLoggerMultiIO.new(@options.debug, STDOUT, File.open('.awestruct/debug.log', 'w')))
  $LOG.level = @options.debug ? Logger::DEBUG : Logger::INFO
  $LOG.formatter = Awestruct::AwestructLogFormatter.new

  # these requires are deferred until after $LOG is set
  require 'awestruct/cli/init'
  require 'awestruct/cli/generate'
  require 'awestruct/cli/auto'
  require 'awestruct/cli/server'
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



14
15
16
# File 'lib/awestruct/cli/invoker.rb', line 14

def config
  @config
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/awestruct/cli/invoker.rb', line 12

def options
  @options
end

#profileObject (readonly)

Returns the value of attribute profile.



15
16
17
# File 'lib/awestruct/cli/invoker.rb', line 15

def profile
  @profile
end

#successObject (readonly)

Returns the value of attribute success.



16
17
18
# File 'lib/awestruct/cli/invoker.rb', line 16

def success
  @success
end

Instance Method Details

#invoke!Object



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
# File 'lib/awestruct/cli/invoker.rb', line 41

def invoke!
  begin
    load_profile() unless ( options.init )

    setup_config()

    invoke_init()      if ( options.init )
    invoke_script()    if ( options.script )
    invoke_force()     if ( options.force )
    invoke_generate()  if ( options.generate )
    invoke_deploy()    if ( options.deploy )
    invoke_server()    if ( options.server )
    invoke_auto()      if ( options.auto )

    wait_for_completion()

    if ExceptionHelper.build_failed? || @success == false
      @success = false
      false
    else
      true
    end
  rescue
    @success = false
    false
  end
end

#invoke_autoObject



142
143
144
145
# File 'lib/awestruct/cli/invoker.rb', line 142

def invoke_auto()
  base_url = profile['base_url'] || options.base_url
  Awestruct::CLI::Auto.new( config, base_url ).run
end

#invoke_deployObject



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/awestruct/cli/invoker.rb', line 129

def invoke_deploy()
  require 'awestruct/cli/deploy'

  deploy_config = profile[ 'deploy' ]

  if ( deploy_config.nil? )
    $LOG.error "No configuration for 'deploy'" if $LOG.error?
    return
  end

  Awestruct::CLI::Deploy.new( config, deploy_config ).run
end

#invoke_forceObject



119
120
121
122
# File 'lib/awestruct/cli/invoker.rb', line 119

def invoke_force()
  FileUtils.rm_rf( File.join( config.dir, '.awestruct', 'dependency-cache' ) )
  FileUtils.rm_rf( config.output_dir )
end

#invoke_generateObject



124
125
126
127
# File 'lib/awestruct/cli/invoker.rb', line 124

def invoke_generate()
  base_url = profile['base_url'] || options.base_url
  @success = Awestruct::CLI::Generate.new( config, options.profile, base_url, Options::DEFAULT_BASE_URL, options.force, !options.generate_on_access ).run
end

#invoke_initObject



112
113
114
# File 'lib/awestruct/cli/invoker.rb', line 112

def invoke_init()
  Awestruct::CLI::Init.new( @options.source_dir, @options.framework, @options.scaffold ).run
end

#invoke_scriptObject



116
117
# File 'lib/awestruct/cli/invoker.rb', line 116

def invoke_script()
end

#invoke_serverObject



147
148
149
# File 'lib/awestruct/cli/invoker.rb', line 147

def invoke_server()
  run_in_thread( Awestruct::CLI::Server.new( options.output_dir, options.bind_addr, options.port, options.generate_on_access ) )
end

#load_profileObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/awestruct/cli/invoker.rb', line 69

def load_profile()
  site_yaml_file = File.join( @options.source_dir, '_config', 'site.yml' )

  if ( !File.exist?( site_yaml_file ) )
    abort( "No config file at #{site_yaml_file}" )
  end

  site_yaml = YAML.load( ERB.new(File.read( site_yaml_file )).result )

  if ( !site_yaml )
    abort( "Failed to parse #{site_yaml_file}" )
  end

  profiles = site_yaml['profiles'] || {}

  profile_name = options.profile

  # use the one specified
  profile = profiles[profile_name]
  if ( !profile )
    profile_name, profile = if ( options.deploy )
      # or the first one having a deploy section
      profiles.select { |k,v| v && v['deploy'] }
    else
      # or the first one having no deploy section
      profiles.select { |k,v| v && !v['deploy'] }
    end.first
  end

  if profile
    $LOG.info "Using profile: #{profile_name}" if $LOG.info?
  end

  @profile = profile || {}
end

#setup_configObject



105
106
107
108
109
110
# File 'lib/awestruct/cli/invoker.rb', line 105

def setup_config()
  @config = Awestruct::Config.new( @options )
  @config.track_dependencies = true if ( @options.auto )
  @config.verbose = true if ( @options.verbose )
  @config.quiet = true if ( @options.quiet )
end