Last active
June 23, 2016 01:32
-
-
Save jrolfs/72c4b0d62d7656db7fae7558802ae4f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require 'find' | |
| require 'fileutils' | |
| require 'pathname' | |
| require 'pry' | |
| require 'active_support/inflector' | |
| Find.find('./models/') do |model_path| | |
| next if FileTest.directory?(model_path) || model_path.match('custom-fields') | |
| contents = File.open(model_path, 'rb').read | |
| basename = File.basename(model_path) | |
| extension = File.extname(model_path) | |
| filename = basename.sub!(extension, '') | |
| plural = filename.pluralize | |
| collection_path = Pathname.new(model_path).sub('models', 'collections').sub(filename, plural).to_s | |
| contents = contents.split(/^class/) | |
| next unless contents.length > 2 | |
| prefix = '' | |
| contents[0].split("\n").each do |line| | |
| next if line.match(/mavenlink-(model|collection)/) | |
| prefix += "#{line.strip}\n" | |
| end | |
| model_contents = contents[1].strip | |
| collection_contents = contents[2].strip | |
| File.open(model_path, 'w') do |file| | |
| file.puts '#= require ../model' | |
| file.print "\n#{prefix}" if prefix.length > 0 | |
| file.puts "\nclass #{model_contents}" | |
| end | |
| File.open(collection_path, 'w') do |file| | |
| file.puts '#= require ../collection' | |
| file.puts "#= require ../models/#{filename}" | |
| file.puts "\nclass #{collection_contents}" | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment