Skip to content

Instantly share code, notes, and snippets.

@et
Created April 7, 2015 17:56
Show Gist options
  • Save et/bc4efe019da1c4a904e8 to your computer and use it in GitHub Desktop.
Save et/bc4efe019da1c4a904e8 to your computer and use it in GitHub Desktop.
def parse_constant(contents)
results = []
# REMOVE_PROSPECT_LIST: "remove_prospect_list",
regex = /(\w*):\s["'](\w*)["']/
contents.scan(regex) do |constant, id|
results << { constant: constant, id: id }
end
return results;
end
def parse_name_and_icon(contents, results)
new_results = []
results.each do |hash|
regex = /#{hash[:constant]}:\n.*return ['"](.*)['"];/
name, icon = contents.scan(regex)
hash[:name] = name[0] if name
hash[:icon] = icon[0] if icon
new_results << hash
end
return new_results
end
def dasherize(str)
str.gsub('_', '-')
end
def camel_case(str)
str.split('_').map{|e| e.capitalize}.join
end
contents = File.open('rules.js', 'rb') { |f| f.read }
results = parse_constant(contents);
results = parse_name_and_icon(contents, results);
results.each do |hash|
filename = dasherize(hash[:id]) + '.js'
variable = camel_case(hash[:constant])
name = hash[:name]
id = hash[:id]
icon = hash[:icon]
content = <<END
const #{variable} = {
id: '#{id}',
name: '#{name}',
icon: '#{icon}'
};
export default #{variable};
END
File.write("rules/#{filename}", content)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment