Class: Nandi::Lockfile

Inherits:
Object
  • Object
show all
Defined in:
lib/nandi/lockfile.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.lockfileObject

Returns the value of attribute lockfile.



75
76
77
# File 'lib/nandi/lockfile.rb', line 75

def lockfile
  @lockfile
end

Class Method Details

.add(file_name:, source_digest:, compiled_digest:) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/nandi/lockfile.rb', line 19

def add(file_name:, source_digest:, compiled_digest:)
  load!

  lockfile[file_name] = {
    source_digest: source_digest,
    compiled_digest: compiled_digest,
  }
end

.create!Object



13
14
15
16
17
# File 'lib/nandi/lockfile.rb', line 13

def create!
  return if file_present?

  File.write(path, {}.to_yaml)
end

.file_present?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/nandi/lockfile.rb', line 9

def file_present?
  File.exist?(path)
end

.get(file_name) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/nandi/lockfile.rb', line 28

def get(file_name)
  load!

  {
    source_digest: lockfile.dig(file_name, :source_digest),
    compiled_digest: lockfile.dig(file_name, :compiled_digest),
  }
end

.load!Object



37
38
39
40
41
42
43
# File 'lib/nandi/lockfile.rb', line 37

def load!
  return lockfile if lockfile

  Nandi::Lockfile.create! unless Nandi::Lockfile.file_present?

  @lockfile = YAML.safe_load_file(path).with_indifferent_access
end

.pathObject



68
69
70
71
72
73
# File 'lib/nandi/lockfile.rb', line 68

def path
  File.join(
    Nandi.config.lockfile_directory,
    ".nandilock.yml",
  )
end

.persist!Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/nandi/lockfile.rb', line 45

def persist!
  # This is a somewhat ridiculous trick to avoid merge conflicts in git.
  #
  # Normally, new migrations are added to the bottom of the Nandi lockfile.
  # This is relatively unfriendly to git's merge algorithm, and means that
  # if someone merges a pull request with a completely unrelated migration,
  # you'll have to rebase to get yours merged as the last line of the file
  # will be seen as a conflict (both branches added content there).
  #
  # This is in contrast to something like Gemfile.lock, where changes tend
  # to be distributed throughout the file. The idea behind sorting by
  # SHA-256 hash is to distribute new Nandi lockfile entries evenly, but
  # also stably through the file. It needs to be stable or we'd have even
  # worse merge conflict problems (e.g. if we randomised the order on
  # writing the file, the whole thing would conflict pretty much every time
  # it was regenerated).
  content = lockfile.to_h.deep_stringify_keys.sort_by do |k, _|
    Digest::SHA256.hexdigest(k)
  end.to_h.to_yaml

  File.write(path, content)
end