Sha256: 34f7c12201f43ad54e34526f41c319a912332ddbec01485f1a608aee67501d9b

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

require 'rake/file_task'

module GitDirty
  class FileTask < Rake::FileTask
    include Rake::DSL

    def initialize(name, *args)
      super
      desc "Write git status to #{name} (should be git-ignored)"
    end

    def needed?
      (actual_state != persisted_state).tap do |needed|
        if application.options.trace
          if needed
            application.trace "*** #{name} IS needed"
          else
            application.trace "*** #{name} is NOT needed"
          end
        end
      end
    end

    def execute(args=nil)
      if application.options.trace
        application.trace "*** Writing actual state '#{actual_state}' to #{name}"
      end

      File.write(name, actual_state)
    end

    private

    def actual_state
      if clean?
        rev
      else
        "#{rev}*"
      end
    end

    def persisted_state
      return nil unless File.exist?(name)
      File.read(name).chomp
    end

    def clean?
      `git status --porcelain`.chomp.empty?
    end

    def rev
      `git rev-parse --short HEAD`.chomp
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
git-dirty-1.0.1 lib/git-dirty/file_task.rb
git-dirty-1.0.0 lib/git-dirty/file_task.rb