Skip to content

Instantly share code, notes, and snippets.

@monkstone
Last active May 12, 2017 10:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monkstone/1a658bdda4ea21c204c5 to your computer and use it in GitHub Desktop.
Save monkstone/1a658bdda4ea21c204c5 to your computer and use it in GitHub Desktop.
Script to convert bare ruby-processing sketches from processing-2.0 to processing-3.0
# convert.rb
def titleize(str)
str.gsub(/::/, '/')
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
.tr('-', '_')
.downcase
.gsub(/_id$/, '')
.gsub(/_/, ' ').capitalize
.gsub(/\b([a-z])/) { Regexp.last_match[1].capitalize }
end
SIZER = /^\s*(size)\s*\(?\d+\,\s*\d+\s*.*\)?/
VECLIBR = /\:(vecmath|fastmath)/
SMOOTHR = /^\s*(smooth)/
settings = <<CODE
def settings
%s
end
CODE
Dir['/home/tux/test/**/*.rb'].each do |rb_file|
next if File.directory? rb_file
sz = nil
old = IO.readlines(rb_file)
@title = titleize(File.basename(rb_file, '.rb'))
modified = []
old.each do |line|
sz_match = line.match(SIZER)
vec_match = line.match(VECLIBR)
sm_match = line.match(SMOOTHR)
modified << line if [sz_match, vec_match, sm_match].all?(&:nil?)
modified << format(" sketch_title \'%s\'\n", @title) unless sz_match.nil?
if vec_match
lib = line.gsub(/\,?\s?\:(fastmath|vecmath)\,*/, '')
modified << lib unless lib.length < 14
end
sz = line unless sz_match.nil?
sz << line unless sm_match.nil?
end
next if sz.nil?
modified << format(settings, sz.strip)
modified << "\n"
IO.write(rb_file, modified.join(''))
end
@monkstone
Copy link
Author

Only works one folder at a time, @todo make it work for nested folders?

@monkstone
Copy link
Author

Updated to work on folders.

@monkstone
Copy link
Author

Neater solution to :vecmath and :fastmath libraries

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment