Class: Cf::Line

Inherits:
Thor
  • Object
show all
Includes:
Config
Defined in:
lib/cf/cli/line.rb

Overview

:nodoc: all

Instance Method Summary collapse

Methods included from Config

#config_file, #find_home, #get_api_key, #load_config, #save_config, #set_api_key, #set_target_uri

Instance Method Details

#createObject



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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/cf/cli/line.rb', line 95

def create
  line_source = Dir.pwd
  yaml_source = "#{line_source}/line.yml"

  unless File.exist?(yaml_source)
    say "The line.yml file does not exist in this directory", :red
    return
  end

  set_target_uri(false)
  set_api_key(yaml_source)

    CF. = CF::Account.info.name

    line_dump = YAML::load(File.open(yaml_source))
    line_title = line_dump['title'].parameterize
    line_description = line_dump['description']
    line_department = line_dump['department']
    
    line = CF::Line.info(line_title)
    if line.error.blank? && options.force? 
      rollback(line.title)
    elsif line.error.blank?
      say("This line already exist.", :yellow)
      override = agree("Do you want to override? [y/n] ")
      if override
        say("Deleting the line forcefuly..", :yellow)
        rollback(line.title)
      else
        say("Line creation aborted!!", :yellow) and exit(1)
      end
    end
    line = CF::Line.new(line_title, line_department, :description => line_description)
    say "Creating new assembly line: #{line.title}", :green
    say("Error: #{line.errors}", :red) and exit(1) if line.errors.present?
    
    say "Adding InputFormats", :green

    # Creation of InputFormat from yaml file
    input_formats = line_dump['input_formats']
    input_formats.each_with_index do |input_format, index|
      
      attrs = {
        :name => input_format['name'],
        :required => input_format['required'],
        :valid_type => input_format['valid_type']
      }
      input_format_for_line = CF::InputFormat.new(attrs)
      input_format = line.input_formats input_format_for_line
      say_status "input", "#{attrs[:name]}"
      display_error(line_title, "#{line.input_formats[index].errors}") if line.input_formats[index].errors.present?
    end

    # Creation of Station
    stations = line_dump['stations']
    stations.each do |station_file|
      type = station_file['station']['station_type']
      index = station_file['station']['station_index']
      input_formats_for_station = station_file['station']['input_formats']
      if type == "tournament"
        jury_worker = station_file['station']['jury_worker']
        auto_judge = station_file['station']['auto_judge']
        station_params = {:line => line, :type => type, :jury_worker => jury_worker, :auto_judge => auto_judge, :input_formats => input_formats_for_station}
      else
        station_params = {:line => line, :type => type, :input_formats => input_formats_for_station}
      end
      station = CF::Station.create(station_params) do |s|
        say "Adding Station #{index}: #{s.type}", :green
        display_error(line_title, "#{s.errors}") if s.errors.present?            

        # For Worker
        worker = station_file['station']['worker']
        number = worker['num_workers']
        reward = worker['reward']
        worker_type = worker['worker_type']
        if worker_type == "human"
          skill_badges = worker['skill_badges']
          stat_badge = worker['stat_badge']
          if stat_badge.nil?
            human_worker = CF::HumanWorker.new({:station => s, :number => number, :reward => reward})
          else
            human_worker = CF::HumanWorker.new({:station => s, :number => number, :reward => reward, :stat_badge => stat_badge})
          end
          
          if worker['skill_badges'].present?
            skill_badges.each do |badge|
              human_worker.badge = badge
            end
          end

          say_status "worker", "#{number} Cloud #{pluralize(number, "Worker")} with reward of #{reward} #{pluralize(reward, "cent")}"
          display_error(line_title, "#{human_worker.errors}") if human_worker.errors.present?
        elsif worker_type =~ /robot/
          settings = worker['settings']
          robot_worker = CF::RobotWorker.create({:station => s, :type => worker_type, :settings => settings})

          say_status "robot", "Robot worker: #{worker_type}"
          display_error(line_title, "#{robot_worker.errors}") if robot_worker.errors.present?              
        else
          display_error(line_title, "Invalid worker type: #{worker_type}")
        end

        # Creation of Form
        # Creation of TaskForm
        if station_file['station']['task_form'].present?
          title = station_file['station']['task_form']['form_title']
          instruction = station_file['station']['task_form']['instruction']
          form = CF::TaskForm.create({:station => s, :title => title, :instruction => instruction}) do |f|
            
            # Creation of FormFields
            say_status "form", "TaskForm '#{f.title}'"
            display_error(line_title, "#{f.errors}") if f.errors.present?
            
            station_file['station']['task_form']['form_fields'].each do |form_field|
              form_field_params = form_field.merge(:form => f)
              field = CF::FormField.new(form_field_params.symbolize_keys)
              say_status "form_field", "FormField '#{field.form_field_params}'"
              display_error(line_title, field.errors) if field.errors.present?
            end
            
          end

        elsif station_file['station']['custom_task_form'].present?
          # Creation of CustomTaskForm
          title = station_file['station']['custom_task_form']['form_title']
          instruction = station_file['station']['custom_task_form']['instruction']

          html_file = station_file['station']['custom_task_form']['html']
          html = File.read("#{line_source}/station_#{station_file['station']['station_index']}/#{html_file}")
          css_file = station_file['station']['custom_task_form']['css']
          css = File.read("#{line_source}/station_#{station_file['station']['station_index']}/#{css_file}")
          js_file = station_file['station']['custom_task_form']['js']
          js = File.read("#{line_source}/station_#{station_file['station']['station_index']}/#{js_file}")
          form = CF::CustomTaskForm.create({:station => s, :title => title, :instruction => instruction, :raw_html => html, :raw_css => css, :raw_javascript => js})
          say_status "form", "CustomTaskForm '#{form.title}'"
          display_error(line_title, "#{form.errors}") if form.errors.present?
        end

      end
    end
    say " ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁", :white
    say "Line was successfully created.", :green
    say "View your line at http://#{CF.}.#{CF.api_url.split("/")[-2]}/lines/#{CF.}/#{line.title}", :yellow
    say "\nNow you can do production runs with: cf production start <your-run-title>", :green
end

#deleteObject



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

def delete
  if options['line'].blank?
    line_source = Dir.pwd
    yaml_source = "#{line_source}/line.yml"
    say("The line.yml file does not exist in this directory", :red) and exit(1) unless File.exist?(yaml_source)
    set_target_uri(false)
    set_api_key(yaml_source)
    CF. = CF::Account.info.name
    line_dump = YAML::load(File.open(yaml_source))
    line_title = line_dump['title'].parameterize
  else
    set_target_uri(false)
    set_api_key
    CF. = CF::Account.info.name
    line_title = options['line'].parameterize
  end
  
  line = CF::Line.find(line_title)
  if line.errors.blank?
    CF::Line.destroy(line_title)
    say("The line #{line_title} deleted successfully!", :yellow)
  else
    say("The line #{line_title} doesn't exist!", :yellow)
  end
end

#generate(title = nil) ⇒ Object

method_option :with_custom_form, :type => :boolean, :default => false, :aliases => “-wcf”, :desc => “generate the template with custom task form and the sample files, default is true”



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cf/cli/line.rb', line 34

def generate(title=nil)
  if title.present?
    line_destination = "#{title.parameterize}"
    yaml_destination = "#{line_destination}/line.yml"
    FileUtils.rm_rf(line_destination, :verbose => true) if options.force? && File.exist?(line_destination)
    if File.exist?(line_destination)
      say "Skipping #{yaml_destination} because it already exists.\nUse the -f flag to force it to overwrite or check and delete it manually.", :red
    else
      say "Generating #{yaml_destination}", :green
      Cf::Newline.start([title, yaml_destination])
      say "A new line named #{line_destination} generated.", :green
      say "Modify the #{yaml_destination} file and you can create this line with: cf line create", :yellow
    end
  else
    say "Title for the line is required.", :red
  end
end

#listObject



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/cf/cli/line.rb', line 242

def list
  line_source = Dir.pwd
  yaml_source = "#{line_source}/line.yml"

  set_target_uri(false)
  set_api_key(yaml_source)
  CF. = CF::Account.info.name
  lines = CF::Line.all
  lines.sort! {|a, b| a[:name] <=> b[:name] }
  say "\n"
  say("No Lines", :yellow) if lines.blank?

  lines_table = table do |t|
    t.headings = ["Line Title", 'URL']
    lines.each do |line|
      t << [line.title, "http://#{CF.}.cloudfactory.com/lines/#{CF.}/#{line.title.parameterize}"]
    end
  end
  say(lines_table)
end