Class: Lyman::CLI::Commands::Add

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

Overview

lyman add ARTIFACT — plant one artifact into an already-scaffolded project. Where new plants everything blind, add has to reconcile against whatever the manifest and filesystem already say, so most of this class is the branching the design doc calls out: managed/owned no-op, ejected tombstone (ask first), untracked file (refuse first).

Instance Method Summary collapse

Constructor Details

#initialize(thor, source_root:) ⇒ Add

Returns a new instance of Add.



10
11
12
13
# File 'lib/lyman/cli/commands/add.rb', line 10

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

Instance Method Details

#call(artifact, force: false) ⇒ Object



15
16
17
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
45
46
47
# File 'lib/lyman/cli/commands/add.rb', line 15

def call(artifact, force: false)
  project_root = Manifest.find!
  manifest = Manifest.load(project_root)
  name = Registry.resolve(artifact, manifest: manifest)
  spec = Registry.fetch(name)
  entry = manifest.artifact(name)
  dest = File.join(project_root, spec[:dest])

  case entry&.fetch("status", nil)
  when "managed", "owned"
    @thor.say "#{name} is already #{entry["status"]}; nothing to do."
    return
  when "ejected"
    unless force || @thor.yes?("#{name} was ejected at #{entry["ejected_at"]}; re-adding replaces your fork with the current upstream version. Continue? (y/N)")
      @thor.say "Left #{name} as-is."
      return
    end
  else
    if File.exist?(dest) && !force
      message = "#{dest} already exists and isn't tracked by lyman. " \
        "Move it aside, or run `lyman add #{name} --force` to overwrite it."
      if (alt = spec[:alternative])
        message += " Or plant `lyman add #{alt}` instead, " \
          "which leaves #{spec[:dest]} untouched."
      end
      raise Thor::Error, message
    end
  end

  plant(manifest, name, spec, project_root)
  manifest.save
  @thor.say "Planted #{name} (#{spec[:role]}) at #{spec[:dest]}."
end