Class: Appifier::CLI::MainCommand

Inherits:
Thor
  • Object
show all
Defined in:
lib/appifier/cli.rb

Overview

The CLI Command structure for Thor

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ MainCommand

Returns a new instance of MainCommand.



14
15
16
17
18
# File 'lib/appifier/cli.rb', line 14

def initialize(*args)
  super
  @output = Carioca::Registry.get.get_service name: :output
  @finisher = Carioca::Registry.get.get_service name: :finisher
end

Class Method Details

.exit_on_failure?Boolean

callback for managing ARGV errors

Returns:



21
22
23
# File 'lib/appifier/cli.rb', line 21

def self.exit_on_failure?
  true
end

Instance Method Details

#collect(template) ⇒ Object



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
# File 'lib/appifier/cli.rb', line 93

def collect(template)
  data = nil
  begin
    raise "Options incompatibles --file and --stdin" if options[:stdin] and options[:file]
    @output.info "Force mode, rewrite dataset if exist" if options[:force]
    if options[:file]
      Appifier::Actors::Collector.new template: template, dataset: open_yaml(filename: options[:file]), force: options[:force]
      @output.info "Getting Dataset from file : #{options[:file]} for #{template}"
    elsif options[:stdin]
      @output.info "Getting Dataset from STDIN for #{template}"
      begin 
        data = STDIN.readlines.join
      rescue Interrupt
        @output.error "Dataset input from STDIN cancelled"
        @finisher.terminate exit_case: :interrupt
      end
      Appifier::Actors::Collector.new template: template, dataset: YAML::load(data), force: options[:force]
    else
      @output.info "Beginning interactive Dataset input for #{template}"
      collector = Appifier::Actors::Collector.new template: template, force: options[:force]
      collector.collect 
    end
    @finisher.terminate exit_case: :quiet_exit
  rescue RuntimeError => e
    @output.error e.message
    @finisher.terminate exit_case: :error_exit
  rescue 
    
  end
end

#generate(template, target = '.') ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/appifier/cli.rb', line 40

def generate(template, target = '.')
  root = "#{File.expand_path(Appifier::DEFAULT_TEMPLATES_PATH)}/#{template}"
  source  = "#{root}/skeleton"
  
  if File::exist? File::expand_path("#{DEFAULT_DATASETS_PATH}/#{template}.yml") then
    dataset = open_dataset(template: template)
    @output.info "Dataset file found for #{template}"
  else
    @output.ko "Dataset file not found for #{template}"
    @finisher.terminate exit_case: :error_exit
  end

  begin
    generator = Appifier::Actors::Generator.new src_root: source, target_root: File.expand_path(target), dataset: dataset
    generator.generate dry_run: options[:simulate], force: options[:force]
    @finisher.terminate exit_case: :quiet_exit
  rescue RuntimeError => e
    @output.error e.message
    @finisher.terminate exit_case: :error_exit
  end
end

#retrieve(origin) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/appifier/cli.rb', line 69

def retrieve(origin)
  begin
    type = options[:type].to_sym
    retriever = Appifier::Actors::Retriever.new type: type, origin: origin
    retriever.get
    @finisher.terminate exit_case: :quiet_exit
  rescue RuntimeError => e
    @output.error e.message
    @finisher.terminate exit_case: :error_exit
  end
end