Module: NandoParser

Defined in:
lib/nando/parser.rb

Class Method Summary collapse

Class Method Details

.clear_directory(path) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/nando/parser.rb', line 59

def self.clear_directory (path)
  Dir.foreach(path) do |f|
    fn = File.join(path, f)
    if f != '.' && f != '..'
      File.delete(fn)
    end
  end
end

.parse_from_dbmate(source_path, destination_path) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/nando/parser.rb', line 5

def self.parse_from_dbmate (source_path, destination_path)
  source_files = Dir.children(source_path)
  source_files.sort! # this sort is not really necessary, but ensures the files are created in the same order of their name

  puts "Found #{source_files.length} source files"

  FileUtils.mkdir_p(destination_path)
  clear_directory(destination_path)

  for filename in source_files do
    match = /^(\d+)\_([\w\_]+)\./.match(filename)
    migration_version = match[1]
    migration_name = match[2]
    new_filename = "#{migration_version}_#{migration_name}.rb"
    new_file = File.new(File.join(destination_path, new_filename), 'w')

    source_file_lines = File.readlines(File.join(source_path, filename))
    current_section = nil
    up_method, down_method = '', ''
    with_transaction = true
    for line in source_file_lines do
      next if /^--[\s|\\\/_]*$/.match(line) # if it's just a up/down comment, ignore

      case current_section
      when 'up'
        if match = /--\smigrate:down(.*)/.match(line)
          with_transaction = false if match[1].include?('transaction:false')
          current_section = 'down'
        else
          up_method += "      #{line}".rstrip + "\n"
        end
      when 'down'
        down_method += "      #{line}".rstrip + "\n"
      else
        if match = /--\smigrate:up(.*)/.match(line)
          with_transaction = false if match[1].include?('transaction:false')
          current_section = 'up'
        end
      end
    end

    # binding
    migration_class_name = migration_name.camelize
    migration_type = with_transaction ? Nando::Migration.name.demodulize : Nando::MigrationWithoutTransaction.name.demodulize
    migration_up_code = up_method
    migration_down_code = down_method
    # TODO: check if binding logic is correct, and if pathing changes when it's a gem
    MigrationGenerator.render_to_file(File.join(File.dirname(File.expand_path(__FILE__)), 'parser_templates/migration.rb'), binding, new_file)
  end

  dest_files = Dir.children(destination_path)
  puts "Created #{dest_files.length} migrations in the destination folder"
end