Class: OpenC3::CliGenerator

Inherits:
Object show all
Defined in:
lib/openc3/utilities/cli_generator.rb

Constant Summary collapse

GENERATORS =
%w(plugin target microservice widget conversion limits_response tool tool_vue tool_angular tool_react tool_svelte)
TEMPLATES_DIR =
"#{File.dirname(__FILE__)}/../../../templates"

Class Method Summary collapse

Class Method Details

.check_args(args) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/openc3/utilities/cli_generator.rb', line 33

def self.check_args(args)
  args.each do |arg|
    if arg =~ /\s/
      abort("#{args[0].to_s.downcase} arguments can not have spaces!") unless args[0].to_s.downcase[0..3] == 'tool'
    end
  end
  # All generators except 'plugin' must be within an existing plugin
  if args[0] != 'plugin' and Dir.glob("*.gemspec").empty?
    abort("No gemspec file detected. #{args[0].to_s.downcase} generator should be run within an existing plugin.")
  end
end

.generate(args) ⇒ Object

Called by openc3cli with ARGV



25
26
27
28
29
30
31
# File 'lib/openc3/utilities/cli_generator.rb', line 25

def self.generate(args)
  unless GENERATORS.include?(args[0])
    abort("Unknown generator '#{args[0]}'. Valid generators: #{GENERATORS.join(', ')}")
  end
  check_args(args)
  send("generate_#{args[0].to_s.downcase.gsub('-', '_')}", args)
end

.generate_conversion(args) ⇒ Object



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
# File 'lib/openc3/utilities/cli_generator.rb', line 254

def self.generate_conversion(args)
  if args.length != 3
    abort("Usage: cli generate conversion <TARGET> <NAME>")
  end

  # Create the local variables
  target_name = args[1].upcase
  unless File.exist?("targets/#{target_name}")
    abort("Target '#{target_name}' does not exist! Conversions must be created for existing targets.")
  end
  conversion_name = "#{args[2].upcase.gsub(/_+|-+/, '_')}_CONVERSION"
  conversion_path = "targets/#{target_name}/lib/"
  conversion_basename = "#{conversion_name.downcase}.rb"
  conversion_class = conversion_basename.filename_to_class_name
  conversion_filename = "targets/#{target_name}/lib/#{conversion_basename}"
  if File.exist?(conversion_filename)
    abort("Conversion #{conversion_filename} already exists!")
  end

  process_template("#{TEMPLATES_DIR}/conversion", binding) do |filename|
    filename.sub!("conversion.rb", conversion_filename)
    false
  end

  puts "Conversion #{conversion_filename} successfully generated!"
  puts "To use the conversion add the following to a telemetry item:"
  puts "  READ_CONVERSION #{conversion_basename}"
  return conversion_name
end

.generate_limits_response(args) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/openc3/utilities/cli_generator.rb', line 284

def self.generate_limits_response(args)
  if args.length != 3
    abort("Usage: cli generate limits_response <TARGET> <NAME>")
  end

  # Create the local variables
  target_name = args[1].upcase
  unless File.exist?("targets/#{target_name}")
    abort("Target '#{target_name}' does not exist! Limits responses must be created for existing targets.")
  end
  response_name = "#{args[2].upcase.gsub(/_+|-+/, '_')}_LIMITS_RESPONSE"
  response_path = "targets/#{target_name}/lib/"
  response_basename = "#{response_name.downcase}.rb"
  response_class = response_basename.filename_to_class_name
  response_filename = "targets/#{target_name}/lib/#{response_basename}"
  if File.exist?(response_filename)
    abort("response #{response_filename} already exists!")
  end

  process_template("#{TEMPLATES_DIR}/limits_response", binding) do |filename|
    filename.sub!("response.rb", response_filename)
    false
  end

  puts "Limits response #{response_filename} successfully generated!"
  puts "To use the limits response add the following to a telemetry item:"
  puts "  LIMITS_RESPONSE #{response_basename}"
  return response_name
end

.generate_microservice(args) ⇒ Object



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
# File 'lib/openc3/utilities/cli_generator.rb', line 122

def self.generate_microservice(args)
  if args.length != 2
    abort("Usage: cli generate #{args[0]} <NAME>")
  end

  # Create the local variables
  microservice_name = args[1].upcase.gsub(/_+|-+/, '_')
  microservice_path = "microservices/#{microservice_name}"
  if File.exist?(microservice_path)
    abort("Microservice #{microservice_path} already exists!")
  end
  microservice_filename = "#{microservice_name.downcase}.rb"
  microservice_class = microservice_filename.filename_to_class_name

  process_template("#{TEMPLATES_DIR}/microservice", binding) do |filename|
    # Rename the template MICROSERVICE to our actual microservice name
    filename.sub!("microservices/TEMPLATE", "microservices/#{microservice_name}")
    filename.sub!("microservice.rb", microservice_filename)
    false
  end

  # Add this microservice to plugin.txt
  File.open("plugin.txt", 'a') do |file|
    file.puts <<~DOC

      MICROSERVICE #{microservice_name} #{microservice_name.downcase.gsub('_','-')}-microservice
        CMD ruby #{microservice_name.downcase}.rb
    DOC
  end

  puts "Microservice #{microservice_name} successfully generated!"
  return microservice_name
end

.generate_plugin(args) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/openc3/utilities/cli_generator.rb', line 61

def self.generate_plugin(args)
  if args.length != 2
    abort("Usage: cli generate #{args[0]} <NAME>")
  end

  # Create the local variables
  plugin = args[1].downcase.gsub(/_+|-+/, '-')
  plugin_name = "openc3-cosmos-#{plugin}"
  if File.exist?(plugin_name)
    abort("Plugin #{plugin_name} already exists!")
  end
  FileUtils.mkdir(plugin_name)
  Dir.chdir(plugin_name) # Change to the plugin path to make copying easier

  process_template("#{TEMPLATES_DIR}/plugin", binding) do |filename|
    filename.sub!("plugin.gemspec", "#{plugin_name}.gemspec")
    false
  end

  puts "Plugin #{plugin_name} successfully generated!"
  return plugin_name
end

.generate_target(args) ⇒ Object



84
85
86
87
88
89
90
91
92
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
# File 'lib/openc3/utilities/cli_generator.rb', line 84

def self.generate_target(args)
  if args.length != 2
    abort("Usage: cli generate #{args[0]} <NAME>")
  end

  # Create the local variables
  target_name = args[1].upcase.gsub(/_+|-+/, '_')
  target_path = "targets/#{target_name}"
  if File.exist?(target_path)
    abort("Target #{target_path} already exists!")
  end
  target_lib_filename = "#{target_name.downcase}.rb"
  target_class = target_lib_filename.filename_to_class_name
  target_object = target_name.downcase

  process_template("#{TEMPLATES_DIR}/target", binding) do |filename|
    # Rename the template TARGET to our actual target named after the plugin
    filename.sub!("targets/TARGET", "targets/#{target_name}")
    filename.sub!("target.rb", target_lib_filename)
    false
  end

  # Add this target to plugin.txt
  File.open("plugin.txt", 'a') do |file|
    file.puts <<~DOC

      VARIABLE #{target_name.downcase}_target_name #{target_name}

      TARGET #{target_name} <%= #{target_name.downcase}_target_name %>
      INTERFACE <%= #{target_name.downcase}_target_name %>_INT tcpip_client_interface.rb host.docker.internal 8080 8081 10.0 nil BURST
        MAP_TARGET <%= #{target_name.downcase}_target_name %>
    DOC
  end

  puts "Target #{target_name} successfully generated!"
  return target_name
end

.generate_tool(args) ⇒ Object



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

def self.generate_tool(args)
  if args.length != 2
    abort("Usage: cli generate #{args[0]} 'Tool Name'")
  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'
  js_file = 'js/app.js' if tool_type == 'tool_vue'
  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

.generate_widget(args) ⇒ Object



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
# File 'lib/openc3/utilities/cli_generator.rb', line 156

def self.generate_widget(args)
  if args.length != 2
    abort("Usage: cli generate #{args[0]} <SuperdataWidget>")
  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 <<~DOC

      WIDGET #{widget_name[0...-6]}
    DOC
  end

  puts "Widget #{widget_name} successfully generated!"
  puts "Please be sure #{widget_name} does not overlap an existing widget: https://openc3.com/docs/v5/telemetry-screens"
  return widget_name
end

.process_template(template_dir, the_binding) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/openc3/utilities/cli_generator.rb', line 45

def self.process_template(template_dir, the_binding)
  Dir.glob("#{template_dir}/**/*", File::FNM_DOTMATCH).each do |file|
    next if File.basename(file) == '.'
    base_name = file.sub("#{template_dir}/", '')
    next if yield base_name
    if File.directory?(file)
      FileUtils.mkdir(base_name) unless File.exist?(base_name)
      next
    end
    output = ERB.new(File.read(file).comment_erb(), trim_mode: "-").result(the_binding)
    File.open(base_name, 'w') do |file|
      file.write output
    end
  end
end