Sha256: 1ff87c279ec30f15fcc089ee644d3ea9cd9f6e3a78d7a99fce9b76373c885ec6

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

require 'forgitter'

module Forgitter
  class Runner
    def initialize(options = Forgitter::DEFAULT_OPTIONS)
      @types = convert_to_filenames(options[:types])
      @stdout = options[:stdout]
    end

    def run
      failcnt = 0
      output = ''
      @types.each do |type|
        ignore_file = get_ignore_file(type)
        if ignore_file
          output += "# Information from #{type}\n"
          output += ignore_file
        else
          failcnt += 1
        end
      end
      exit(1) if failcnt == @types.length

      if @stdout
        puts output
      else
        File.open('.gitignore', 'w') do |file|
          file.write(output)
        end
      end
    end

    private

    # Given a filename on the gitignore repo, return a string with the contents of the file
    def get_ignore_file(filename)
      STDERR.puts "Using #{filename}"
      begin
        IO.read(File.join(DATA_PATH, filename))
      rescue Errno::ENOENT
        STDERR.puts "#{filename} does not exist!"
        false
      end
    end

    # converts "rails" or "Rails" into "Rails.gitignore"
    def convert_to_filenames(names)
      names.map do |name|
        conversion_table[name.downcase.gsub(/[^+a-z]/i, '')]
      end.compact
    end

    def conversion_table
      Forgitter::TYPES
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
forgitter-0.0.6 lib/forgitter/runner.rb
forgitter-0.0.5 lib/forgitter/runner.rb