Skip to content

Instantly share code, notes, and snippets.

@mindon
Created April 6, 2022 02:35
Show Gist options
  • Save mindon/7e0b31f1f8b73f7928be92eedf442fa4 to your computer and use it in GitHub Desktop.
Save mindon/7e0b31f1f8b73f7928be92eedf442fa4 to your computer and use it in GitHub Desktop.
hook (previouse) for KLayout DRC, put this file under folder bin-release/drc/
# $autorun-early
require 'open3'
# mindon@live.com 2022-04-02
module DRC
module Hook
def self.on(drc, macro)
name = 'KLAYOUT_HOOK_DRC'
body = macro.text
line = body[0..((body.index("\n") || 0) - 1)] || ''
if !line.start_with?('#!HOOK')
return false
end
stop, cmd = true, ''
i = line.index(' ') || -1
if i > 0
cmd = line[0..(i-1)]
if cmd.end_with?('+')
stop = false
cmd = cmd[0..-2]
end
j = cmd.index(':') || -1
if j > 0
cmd = ENV[name +'_' +cmd[(j+1)..-1]] || ''
if cmd.length == 0
raise 'invalid DRC HOOK: ' + line
return
end
else
cmd = ''
end
end
if cmd.length == 0
cmd = ENV[name] || ''
end
if cmd.start_with?('/') && (not File.file?(cmd))
raise 'invalid DRC HOOK cmd = ' + cmd
end
if cmd.length == 0
return false
end
out, err, status = Open3.capture3(%Q|#{cmd} "#{macro.path}" #{line[(i+1)..-1]}|, :stdin_data=>macro.text)
if status.success?
if out.length > 0
puts out
end
else
raise err || 'DRC HOOK error'
end
return stop
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment