Class: Bonchi::Setup

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

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 = []) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bonchi/setup.rb', line 13

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

  config = Config.from_main_worktree
  abort "#{color(:red)}Error:#{reset} .worktree.yml not found in main worktree" unless config

  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}"

  allocate_ports(config.ports) if config.ports.any?
  copy_files(config.copy)
  replace_in_files(config.replace) if config.replace.any?
  run_pre_setup(config.pre_setup)
  exec_setup(config.setup, args)
end