Class: RIM::DirtyCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/rim/dirty_check.rb

Overview

Module dirty state checker.

Provides means to mark modules as “clean” and check if they become “dirty” later on.

Once a module has been marked as being clean, it will become dirty if any of the following is true:

  • Number of contained files has changed

  • File names or location have changed

  • File contents have changed

  • One of the RIM info attributes listed in ChecksumAttributes has changed

  • The RIM info file is missing or became invalid

Ignored files are not considered by this check.

Defined Under Namespace

Classes: MissingInfoException

Constant Summary collapse

ChecksumAttributes =

attributes to be included into checksum calculation checksum calculation fails if those attributes are not present

[
  :remote_url,
  :revision_sha1
]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dirty?(dir) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/rim/dirty_check.rb', line 47

def self.dirty?(dir)
  mi = RimInfo.from_dir(dir)
  # always fails if there is no checksum
  mi.dirty? || !mi.checksum || mi.checksum != self.new.calc_checksum(mi, dir)
end

.mark_clean(dir) ⇒ Object

rim info must exist in dir and must be valid it also must contain attributes listed in ChecksumAttributes otherwise a MissingInfoException is raised



39
40
41
42
43
44
45
# File 'lib/rim/dirty_check.rb', line 39

def self.mark_clean(dir)
  mi = RimInfo.from_dir(dir)
  cs = self.new.calc_checksum(mi, dir)
  raise MissingInfoException unless cs
  mi.checksum = cs
  mi.to_dir(dir)
end

Instance Method Details

#calc_checksum(mi, dir) ⇒ Object

returns nil if checksum can’t be calculated due to missing info



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rim/dirty_check.rb', line 54

def calc_checksum(mi, dir)
  if check_required_attributes(mi)
    sha1 = Digest::SHA1.new
    # all files and directories within dir
    files = FileHelper.find_matching_files(dir, false, "/**/*", File::FNM_DOTMATCH)
    # Dir.glob with FNM_DOTMATCH might return . and ..
    files.delete(".")
    files.delete("..")
    # ignore the info file itself
    files.delete(RimInfo::InfoFileName)
    # ignores defined by user
    files -= FileHelper.find_matching_files(dir, false, mi.ignores)
    # order of files makes a difference
    # sort to eliminate platform specific glob behavior
    files.sort!
    files.each do |fn|
      update_file(sha1, dir, fn)
    end
    ChecksumAttributes.each do |a|
      sha1.update(mi.send(a))
    end
    sha1.hexdigest
  else
    # can't calc checksum
    nil
  end
end