require 'rake/clean' # ================= # project variables # ================= # main name used if filename target = "led-controller" # schema sch = "#{target}.sch" # pcb layout pcb = "#{target}.pcb" # project version, read from "version" file version = IO.read("version").chomp # current date for stamping output date = Time.now.strftime("%Y-%m-%d") # schematic revision, based on the number of schematic commits) sch_rev = `git log --pretty=oneline "#{sch}" | wc -l`.chomp.to_i # pcb layout revision, based on the number of pcb commits) pcb_rev = `git log --pretty=oneline "#{pcb}" | wc -l`.chomp.to_i # schema name with version and revition vsch = "#{target}_v#{version}.#{sch_rev.to_s.rjust(3,'0')}.sch" # pcb layout name with version and revition vpcb = "#{target}_v#{version}.#{pcb_rev.to_s.rjust(3,'0')}.pcb" # ========== # main tasks # ========== task :default => [:version,:print,:notes] desc "set version in schematic and layout" task :version => [vsch,vpcb] CLEAN.include(vsch) CLOBBER.include("#{target}_*.sch") desc "print schematic (as pdf)" task :print => "#{target}_schematic.pdf" CLEAN.include("#{target}_schematic.pdf") desc "export notes from schematic" task :notes => "notes.txt" CLOBBER.include("notes.txt") desc "verify schematic attributes" task :verify => vsch do |t| ["value","footprint"].each do |attribute| bom2(t.prerequisites[0],attribute).each do |data| next unless data[attribute]=="unknown" puts "#{attribute}s not defined for #{data[:refdes]*','}" end end uniq = true numbered = true bom2(t.prerequisites[0],"refdes").each do |data| uniq &= data[:refdes].size==1 numbered &= !data["refdes"].include?("?") end puts "not all refdes uniq" unless uniq puts "not all refdes numbered" unless numbered end # ================ # helper functions # ================ # generate gnetlist bom2 and parse them # arguments: schematic=schematic to usse, attributes=the attributs to use for generating bom2 # return [{:refdes => [refdes of component], => text of attributes}] def bom2(schematic, attributes) to_return = [] # force attributes to be an array attributes = case attributes.class when String [attributes] when Array attributes else [attributes.to_s] end # create gnetlist backend File.open("attribs","w") do |attribs| attributes.each do |attribute| attribs.puts attribute end end # generate bom2 list = `gnetlist -g bom2 -q -o - #{schematic}` # parse bom2 regex = /^(?(\w+,?)+):(?.*):(?\d+)$/ list.each_line do |line| next unless line =~ regex data = line.match regex hash = {refdes: data[:refdes].split(',')} attributes.each_index do |i| hash.merge!({attributes[i] => data[:attributes].split(':')[i]}) end to_return << hash end return to_return end CLEAN.include("attribs") # =============== # file generation # =============== file vsch => sch do |t| sh "cp #{t.prerequisites.join(' ')} #{t.name}" # on \ is to prevent ruby interpreting it, th other is for sed # the version sh "sed -i 's/\\(version=\\)\\$Version\\$/\\1#{version}/' #{t.name}" # the date sh "sed -i 's/\\(date=\\)\\$Date\\$/\\1#{date}/' #{t.name}" # the revision sh "sed -i 's/\\(revision=\\)\\$Revision\\$/\\1#{sch_rev}/' #{t.name}" end file vpcb => pcb do |t| sh "cp #{t.prerequisites.join(' ')} #{t.name}" # on \ is to prevent ruby interpreting it, th other is for sed # the version and revision version_revision = "v#{version}.#{pcb_rev.to_s.rjust(3,'0')}" sh "sed -i 's/\\$version\\$/#{version_revision}/' #{t.name}" # the date sh "sed -i 's/\\$date\\$/#{date}/' #{t.name}" end file "#{target}_schematic.pdf" => vsch do |t| sh "gaf export -f pdf -c -o #{t.name} #{t.prerequisites.join(' ')} 2> /dev/null" end file "notes.txt" => vsch do |t| notes_data = bom2(t.prerequisites[0],"note") File.open(t.name,"w") do |notes_file| notes_data.each do |note| next if note["note"]=="unknown" notes_file.puts "#{note[:refdes]*','}:\n#{note["note"]}\n\n" end end end