Sha256: d0629fc2410c2666c512debaf2d07646a6470847c590016b7fa82e59d4e8f9b6

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

module JetsUpgrade::Rewrite
  # interface methods:
  # marker
  # filename
  # new_content
  class Base
    extend Memoist

    def initialize(options={})
      @options = options
    end

    def lines
      IO.readlines(filename)
    end
    memoize :lines

    def run
      unless File.exist?(filename)
        puts "File does not exist: #{filename}".color(:red)
        return
      end

      marker_found = lines.detect { |l| marker && l.include?(marker) }
      not_marker_found = lines.detect { |l| not_marker && l.include?(not_marker) }
      if marker_found && !not_marker_found
        puts "#{filename} looks good".color(:green)
        return
      end

      if ENV['DEBUG']
        puts "filename: #{filename}".color(:purple)
        puts "content:".color(:purple)
        puts content
      end
      rewrite
    end

    def marker
      nil
    end

    # optional interface method
    def not_marker
      nil
    end

    # optional interface method
    def message
      nil
    end

    def rewrite
      return unless changed?
      IO.write(filename, content)
      puts "Updated #{filename}"
      puts message if message
      true
    end

    def changed?
      content != lines.join('')
    end

    def self.run(options={})
      new(options).run
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jets-upgrade-0.1.1 lib/jets_upgrade/rewrite/base.rb
jets-upgrade-0.1.0 lib/jets_upgrade/rewrite/base.rb