Brian Silverman | 1fc17c0 | 2013-12-10 12:57:47 -0800 | [diff] [blame] | 1 | #!/usr/bin/env ruby |
| 2 | |
| 3 | # This generates something designed to be copied into Mouser's BOM creation |
| 4 | # copy/paste box. It turns out that it's usually easier to just manually add |
| 5 | # everything to your cart with the "EZBuy" thingie, but it's still a reasonable |
| 6 | # format. |
| 7 | # |
| 8 | # Usage: generate_mouser_bom.rb FILE [COPIES] |
| 9 | |
| 10 | lines = File.open(ARGV[0]) do |f| |
| 11 | lines = f.readlines |
| 12 | lines.shift |
| 13 | lines.collect do |line| |
| 14 | line.split(', ') |
| 15 | end |
| 16 | end |
| 17 | |
| 18 | $parts = {} |
| 19 | |
| 20 | def print_part(pn_string) |
Brian Silverman | 1fc17c0 | 2013-12-10 12:57:47 -0800 | [diff] [blame] | 21 | pn = pn_string.intern |
| 22 | if $parts[pn] |
| 23 | $parts[pn] = $parts[pn] + 1 |
| 24 | else |
| 25 | $parts[pn] = 1 |
| 26 | end |
| 27 | end |
| 28 | |
| 29 | lines.each do |line| |
| 30 | pn = line[4] |
| 31 | if pn.index(';') |
| 32 | parts = pn.split('; ') |
| 33 | parts.each do |part_string| |
| 34 | part = part_string.match(/(.+) x([0-9]+)?/) |
| 35 | if part |
| 36 | name = part[1] |
| 37 | number = part[2] |
| 38 | number.to_i.times do |
| 39 | print_part name |
| 40 | end |
| 41 | else |
| 42 | print_part part_string |
| 43 | end |
| 44 | end |
| 45 | else |
| 46 | print_part pn unless pn.empty? |
| 47 | end |
| 48 | end |
| 49 | |
| 50 | times = (ARGV[1] || 1).to_i |
| 51 | $parts.each do |pn, number| |
| 52 | puts "#{pn}|#{number * times}" |
| 53 | end |