module ActiveFacts::Generators::Rails::RubyFolderGenerator

Constants

Public Instance Methods

create_if_ok(dir, filename) click to toggle source
# File lib/activefacts/generator/rails/ruby_folder_generator.rb, line 68
def create_if_ok dir, filename
  # Create a file in the output directory, being careful not to overwrite carelessly
  out = $stdout
  if dir
    FileUtils.mkdir_p(dir)
    pathname = (dir+'/'+filename).gsub(%r{//+}, '/')
    @preexisting_files.reject!{|f| f == pathname }    # Don't clean up this file
    if generated_file_exists(pathname) == false
      warn "not overwriting non-generated file #{pathname}"
      @ok = false
      return nil
    end
    out = File.open(pathname, 'w')
  end
  out
end
delete_old_generated_files() click to toggle source
# File lib/activefacts/generator/rails/ruby_folder_generator.rb, line 41
def delete_old_generated_files
  remaining = []
  cleaned = 0
  @preexisting_files.each do |pathname|
    if generated_file_exists(pathname) == true
      File.unlink(pathname)
      cleaned += 1
    else
      remaining << pathname
    end
  end
  $stderr.puts "Cleaned up #{cleaned} old generated files" if @preexisting_files.size > 0
  $stderr.puts "Remaining non-generated files:\n\t#{remaining*"\n\t"}" if remaining.size > 0
end
generate() click to toggle source
# File lib/activefacts/generator/rails/ruby_folder_generator.rb, line 23
def generate
  record_extant_files_to_remove

  @ok = true
  result = generate_files

  warn "\# #{@composition.name} generated with errors" unless @ok
  delete_old_generated_files if @option_output && !@option_keep

  result
end
generated_file_exists(pathname) click to toggle source
# File lib/activefacts/generator/rails/ruby_folder_generator.rb, line 56
def generated_file_exists pathname
  File.open(pathname, 'r') do |existing|
    first_lines = existing.read(1024)     # Make it possible to pass over a magic charset comment
    if first_lines.length == 0 or first_lines =~ %r{^#{Regexp.quote HEADER}}
      return true
    end
  end
  return false    # File exists, but is not generated
rescue Errno::ENOENT
  return nil      # File does not exist
end
record_extant_files_to_remove() click to toggle source
# File lib/activefacts/generator/rails/ruby_folder_generator.rb, line 35
def record_extant_files_to_remove
  @preexisting_files = []
  return if @option_keep
  @preexisting_files = extant_files || []
end
warn(*a) click to toggle source
# File lib/activefacts/generator/rails/ruby_folder_generator.rb, line 19
def warn *a
  $stderr.puts *a
end