Class: OpenC3::CliGenerator
- 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
- .check_args(args) ⇒ Object
-
.generate(args) ⇒ Object
Called by openc3cli with ARGV.
- .generate_conversion(args) ⇒ Object
- .generate_limits_response(args) ⇒ Object
- .generate_microservice(args) ⇒ Object
- .generate_plugin(args) ⇒ Object
- .generate_target(args) ⇒ Object
- .generate_tool(args) ⇒ Object
- .generate_widget(args) ⇒ Object
- .process_template(template_dir, the_binding) ⇒ Object
Class Method Details
.check_args(args) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# 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 gen_lang = ENV['OPENC3_LANGUAGE'] if (args[-1] == '--python' || args[-1] == '--ruby') gen_lang = args[-1][2, 6] end case gen_lang when 'python' @@language = 'py' when 'ruby' @@language = 'rb' else abort("One of --python or --ruby is required unless OPENC3_LANGUAGE is set.") 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
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/openc3/utilities/cli_generator.rb', line 303 def self.generate_conversion(args) if args.length < 3 or args.length > 4 abort("Usage: cli generate conversion <TARGET> <NAME> (--ruby or --python)") 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}.#{@@language}" 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.#{@@language}", 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
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/openc3/utilities/cli_generator.rb', line 333 def self.generate_limits_response(args) if args.length < 3 or args.length > 4 abort("Usage: cli generate limits_response <TARGET> <NAME> (--ruby or --python)") 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}.#{@@language}" 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.#{@@language}", 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
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 |
# File 'lib/openc3/utilities/cli_generator.rb', line 166 def self.generate_microservice(args) if args.length < 2 or args.length > 3 abort("Usage: cli generate #{args[0]} <NAME> (--ruby or --python)") 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}.#{@@language}" 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.#{@@language}", microservice_filename) false end cmd_line = "CMD ruby #{microservice_name.downcase}.rb" if @@language == 'py' cmd_line = "CMD python #{microservice_name.downcase}.py" 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_line} DOC end puts "Microservice #{microservice_name} successfully generated!" return microservice_name end |
.generate_plugin(args) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/openc3/utilities/cli_generator.rb', line 81 def self.generate_plugin(args) if args.length < 2 or args.length > 3 abort("Usage: cli generate #{args[0]} <NAME> (--ruby or --python)") 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
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 |
# File 'lib/openc3/utilities/cli_generator.rb', line 104 def self.generate_target(args) if args.length < 2 or args.length > 3 abort("Usage: cli generate #{args[0]} <NAME> (--ruby or --python)") 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}.#{@@language}" 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.#{@@language}", target_lib_filename) false end if @@language == 'py' # If we're using Python create a requirements.txt and list it in the gemspec # However, don't write over an existing file they may have already created unless File.exist?("requirements.txt") File.open("requirements.txt", 'w') do |file| file.puts "# Python dependencies" end end gemspec_filename = Dir['*.gemspec'][0] gemspec = File.read(gemspec_filename) gemspec.gsub!('plugin.txt', 'plugin.txt requirements.txt') File.write(gemspec_filename, gemspec) target_txt_filename = "targets/#{target_name}/target.txt" target_txt = File.read(target_txt_filename) target_txt.gsub!('LANGUAGE ruby', 'LANGUAGE python') File.write(target_txt_filename, target_txt) end interface_line = "INTERFACE <%= #{target_name.downcase}_target_name %>_INT tcpip_client_interface.rb host.docker.internal 8080 8081 10.0 nil BURST" if @@language == 'py' interface_line = "INTERFACE <%= #{target_name.downcase}_target_name %>_INT openc3/interfaces/tcpip_client_interface.py host.docker.internal 8080 8081 10.0 None BURST" 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_line} MAP_TARGET <%= #{target_name.downcase}_target_name %> DOC end puts "Target #{target_name} successfully generated!" return target_name end |
.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 297 |
# 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' 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
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.(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 = args[1] = "#{}.vue" = "src/#{}" if File.exist?() abort("Widget #{} 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", ) 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 #{[0...-6]} DOC end puts "Widget #{} successfully generated!" puts "Please be sure #{} does not overlap an existing widget: https://docs.openc3.com/docs/configuration/telemetry-screens" return end |
.process_template(template_dir, the_binding) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/openc3/utilities/cli_generator.rb', line 58 def self.process_template(template_dir, the_binding) Dir.glob("#{template_dir}/**/*", File::FNM_DOTMATCH).each do |file| next if File.basename(file) == '.' if @@language == 'rb' # Ignore python files if we're ruby next if File.extname(file) == '.py' elsif @@language == 'py' # Ignore ruby files if we're python next if File.extname(file) == '.rb' end base_name = file.sub("#{template_dir}/", '') next if yield base_name if File.directory?(file) FileUtils.mkdir_p(base_name) next end output = ERB.new(File.read(file), trim_mode: "-").result(the_binding) File.open(base_name, 'w') do |base_file| base_file.write output end end end |