Class: Bonchi::Setup

Inherits:
Object
  • Object
show all
Includes:
Colors
Defined in:
lib/bonchi/setup.rb

Constant Summary collapse

STEPS =
%w[copy link ports replace pre_setup setup].freeze

Instance Method Summary collapse

Constructor Details

#initialize(worktree: nil) ⇒ Setup

Returns a new instance of Setup.



8
9
10
11
# File 'lib/bonchi/setup.rb', line 8

def initialize(worktree: nil)
  @worktree = worktree || Dir.pwd
  @main_worktree = Git.main_worktree
end

Instance Method Details

#run(args = [], upto: nil) ⇒ 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
48
49
# File 'lib/bonchi/setup.rb', line 15

def run(args = [], upto: nil)
  if upto && !STEPS.include?(upto)
    abort "#{color(:red)}Error:#{reset} unknown step '#{upto}'. Valid steps: #{STEPS.join(", ")}"
  end

  if @worktree == @main_worktree
    abort "#{color(:red)}Error:#{reset} already in the main worktree"
  end

  config = Config.from_worktree(@worktree)
  if config
    puts "Using .worktree.yml from linked worktree"
  else
    config = Config.from_main_worktree
    abort "#{color(:red)}Error:#{reset} .worktree.yml not found in main worktree" unless config
  end

  last_step = upto || STEPS.last
  run_steps = STEPS[0..STEPS.index(last_step)]

  ENV["WORKTREE_MAIN"] = @main_worktree
  ENV["WORKTREE_LINKED"] = @worktree
  ENV["WORKTREE_BRANCH"] = Git.current_branch(@worktree)
  ENV["WORKTREE_BRANCH_SLUG"] = ENV["WORKTREE_BRANCH"].gsub(/[^a-zA-Z0-9_]/, "_")
  ENV["WORKTREE_ROOT"] ||= GlobalConfig.new.worktree_root

  puts "Setting up worktree from: #{@main_worktree}"

  copy_files(config.copy) if run_steps.include?("copy")
  link_files(config.link) if run_steps.include?("link")
  allocate_ports(config.ports) if run_steps.include?("ports") && config.ports.any?
  replace_in_files(config.replace) if run_steps.include?("replace") && config.replace.any?
  run_pre_setup(config.pre_setup) if run_steps.include?("pre_setup")
  exec_setup(config.setup, args) if run_steps.include?("setup")
end