BogaudioModules

BogaudioModules for VCV Rack
Log | Files | Refs | README | LICENSE

svg_make_stub.rb (3025B)


      1 #!/usr/bin/env ruby
      2 
      3 require 'optparse'
      4 
      5 options = {
      6   type: nil,
      7   name: nil,
      8   hp: 3,
      9   width: 30,
     10   height: 30,
     11   noskin: false,
     12   force: false
     13 }
     14 option_parser = OptionParser.new do |opts|
     15   opts.banner = "Usage: #{$0} [options] <module slug or widget name, becomes output file prefix>"
     16   opts.on('--module', 'Creates a module stub; --module or --widget is required') do
     17     options[:type] = 'module'
     18   end
     19   opts.on('--widget', 'Creates a widget stub; --widget or --module is required') do
     20     options[:type] = 'widget'
     21   end
     22   opts.on('--name=[module name]', 'With --module, specifies the module name/title; defaults to upcased slug') do |v|
     23     options[:name] = v if v
     24   end
     25   opts.on('--hp=[module width in HP]', "With --module, sets the module width; default=#{options[:hp]}") do |v|
     26     options[:hp] = v.to_i if v
     27   end
     28   opts.on('--width=[widget width]', "With --widget, sets the widget width; default=#{options[:width]}") do |v|
     29     options[:width] = v.to_f if v
     30   end
     31   opts.on('--height=[widget height]', "With --widget, sets the widget height; default=#{options[:height]}") do |v|
     32     options[:height] = v.to_f if v
     33   end
     34   opts.on('--noskin', 'Mark the module or widget as not skinnable') do
     35     options[:noskin] = true
     36   end
     37   opts.on('-f', '--force', 'Overwrite an existing .svg for the specified slug') do
     38     options[:force] = true
     39   end
     40   opts.on_tail('-h', '--help', 'Show this message') do
     41     puts opts
     42     exit
     43   end
     44 end
     45 begin
     46   option_parser.parse!
     47 rescue => e
     48   STDERR.puts e.to_s
     49   STDERR.puts "\n"
     50   STDERR.puts option_parser.help
     51   exit 1
     52 end
     53 
     54 unless ARGV.size >= 1 && (options[:type] == 'module' || options[:type] == 'widget')
     55   STDERR.puts option_parser.help
     56   exit 1
     57 end
     58 slug = ARGV[0]
     59 unless slug =~ /^\w+$/
     60   STDERR.puts "Invalid slug '#{slug}'; the slug must be only alphanumeric characters and underscore."
     61   exit 1
     62 end
     63 if options[:type] == 'module' && slug !~ /^[A-Z]/
     64   STDERR.puts "Invalid slug '#{slug}'; a module slug must start with a capital letter"
     65   exit 1
     66 end
     67 if options[:type] == 'widget' && slug !~ /^[a-z]/
     68   STDERR.puts "Invalid slug '#{slug}'; a widget slug must start with a lower-case letter"
     69   exit 1
     70 end
     71 options[:name] = slug.upcase unless options[:name]
     72 
     73 out_fn = File.absolute_path(File.join(File.dirname($0), '..', 'res-src', "#{slug}-src.svg"))
     74 if File.exist?(out_fn) && !options[:force]
     75   STDERR.puts "Output file exists; use -f to overwrite: #{out_fn}"
     76   exit 1
     77 end
     78 
     79 out = nil
     80 if options[:type] == 'module'
     81   href = '#module'
     82   case options[:hp]
     83   when 0..3
     84     href += '3'
     85   when 4..5
     86     href += '5'
     87   when 6
     88     href += '6'
     89   when 7..8
     90     href += '8'
     91   end
     92 
     93   out = %Q{<module hp="%d"%s>
     94   <style/>
     95 
     96   <def xlink:href="%s" var-name="%s"/>
     97 </module>}
     98   out = out % [options[:hp], options[:noskin] ? ' noskin="true"' : '', href, options[:name]]
     99 elsif options[:type] == 'widget'
    100   out = %Q{<widget width="%0.1f" height="%0.1f">
    101   <style/>
    102 </widget>}
    103   out = out % [options[:width], options[:height]]
    104 end
    105 
    106 # puts out
    107 File.write(out_fn, out)
    108 puts "Wrote #{out_fn}"