Method: OpenC3::CliGenerator.generate_tool

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

.generate_tool(args) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/openc3/utilities/cli_generator.rb', line 251

def self.generate_tool(args)
  if args.length < 2 or args.length > 3
    abort("Usage: cli generate #{args[0]} 'Tool Name' (--ruby or --python)")
  end

  # Create the local variables
  tool_type = args[0].to_s.downcase.gsub('-', '_')
  tool_type = 'tool_vue' if tool_type == 'tool'
  tool_name_display = args[1]
  tool_name = args[1].to_s.downcase.gsub('-', '').gsub(' ', '')
  tool_path = "tools/#{tool_name}"
  if File.exist?(tool_path)
    abort("Tool #{tool_path} already exists!")
  end
  skip_package = false
  if File.exist?('package.json')
    puts "package.json already exists ... you'll have to manually add this tool and its dependencies"
    skip_package = true
  end

  process_template("#{TEMPLATES_DIR}/#{tool_type}", 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.gsub!("tool_name", tool_name)
      false
    end
  end

  # Add this tool to plugin.txt
  js_file = 'main.js'
  File.open("plugin.txt", 'a') do |file|
    file.puts <<~DOC

    TOOL #{tool_name} "#{tool_name_display}"
      INLINE_URL #{js_file}
      ICON mdi-file-cad-box
    DOC
  end

  puts "Tool #{tool_name} successfully generated!"
  puts "Please be sure #{tool_name} does not conflict with any other tools"
  return tool_name
end