Method: OpenC3::CliGenerator.generate_widget

Defined in:
lib/openc3/utilities/cli_generator.rb

.generate_widget(args) ⇒ Object



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
240
241
242
243
244
245
246
247
248
249
# File 'lib/openc3/utilities/cli_generator.rb', line 205

def self.generate_widget(args)
  if args.length < 2 or args.length > 3
    abort("Usage: cli generate #{args[0]} <SuperdataWidget> (--ruby or --python)")
  end
  # Per https://stackoverflow.com/a/47591707/453280
  if args[1] !~ /.*Widget$/ or args[1][0...-6] != args[1][0...-6].capitalize
    abort("Widget name should be Uppercase followed by Widget, e.g. SuperdataWidget. Found '#{args[1]}'.")
  end

  # Create the local variables
  widget_name = args[1]
  widget_filename = "#{widget_name}.vue"
  widget_path = "src/#{widget_filename}"
  if File.exist?(widget_path)
    abort("Widget #{widget_path} already exists!")
  end
  skip_package = false
  if File.exist?('package.json')
    puts "package.json already exists ... you'll have to manually add this widget to the end of the \"build\" script."
    skip_package = true
  end

  process_template("#{TEMPLATES_DIR}/widget", binding) do |filename|
    if skip_package && filename == 'package.json'
      true # causes the block to skip processing this file
    elsif filename.include?('node_modules')
      true
    else
      filename.sub!("Widget.vue", widget_filename)
      false
    end
  end

  # Add this widget to plugin.txt but remove Widget from the name
  File.open("plugin.txt", 'a') do |file|
    file.puts "\n      WIDGET \#{widget_name[0...-6]}\n    DOC\n  end\n\n  puts \"Widget \#{widget_name} successfully generated!\"\n  puts \"Please be sure \#{widget_name} does not overlap an existing widget: https://docs.openc3.com/docs/configuration/telemetry-screens\"\n  return widget_name\nend\n"