Class: Lyman::CLI::Commands::Diff

Inherits:
Object
  • Object
show all
Defined in:
lib/lyman/cli/commands/diff.rb

Overview

lyman diff ARTIFACT — shows "your changes" (pristine vs. working file) and "upstream changes since your copy was planted" (pristine vs. a freshly rendered copy), by shelling out to diff -u. The pristine-as-planted copy is the fork point in both comparisons, so the two sections never mix upstream's changes with the user's own.

Instance Method Summary collapse

Constructor Details

#initialize(thor, source_root:) ⇒ Diff

Returns a new instance of Diff.



13
14
15
16
# File 'lib/lyman/cli/commands/diff.rb', line 13

def initialize(thor, source_root:)
  @thor = thor
  @source_root = source_root
end

Instance Method Details

#call(artifact) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/lyman/cli/commands/diff.rb', line 18

def call(artifact)
  project_root = Manifest.find!
  manifest = Manifest.load(project_root)
  name = Registry.resolve(artifact, manifest: manifest)
  spec = Registry.fetch(name)
  entry = manifest.artifact(name)
  path = entry && entry["path"]

  unless path && manifest.pristine?(path)
    raise Thor::Error, "#{name} has no pristine copy in this project; nothing to diff against."
  end

  pristine_path = manifest.pristine_path(path)
  dest = File.join(project_root, path)

  @thor.say "--- your changes (#{name}) ---"
  @thor.say section(pristine_path, dest, "pristine/#{path}", path)

  @thor.say ""
  @thor.say "--- upstream changes since planted (#{name}) ---"
  rendered = Planter.render(name, spec, source_root: @source_root)
  Tempfile.create(name) do |upstream|
    upstream.write(rendered)
    upstream.flush
    @thor.say section(pristine_path, upstream.path, "pristine/#{path}", "upstream/#{spec[:dest]}")
  end
end