Class: Shaddox::Shadow

Inherits:
Object
  • Object
show all
Defined in:
lib/shaddox/shadow.rb

Instance Method Summary collapse

Constructor Details

#initialize(options, &block) ⇒ Shadow

include FileUtils



12
13
14
15
16
17
18
# File 'lib/shaddox/shadow.rb', line 12

def initialize(options, &block)
  @verbose = options[:verbose] || true
  @installer = options[:installer]
  @tmppath = options[:tmppath] || '/tmp/shaddox/'
  @required = true
  instance_eval(&block)
end

Instance Method Details

#cd(path, &block) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/shaddox/shadow.rb', line 34

def cd(path, &block)
  current_path = Dir.pwd
  mkdir(path)
  FileUtils.cd(path.exp_path)
  instance_eval(&block)
  FileUtils.cd(current_path)
end

#exists(path) ⇒ Object



42
43
44
# File 'lib/shaddox/shadow.rb', line 42

def exists(path)
  system("test -e #{path.exp_path}")
end

#install(package) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/shaddox/shadow.rb', line 66

def install(package)
  unless @installer
    warn "No package manager is defined for this target.", 1
    puts "-------------------"
    require 'highline/import'
    choose do |menu|
      menu.prompt = "Please select a package manager to use:"

      menu.choice(:apt) { @installer = :apt }
      menu.choice(:brew) { @installer = :brew }
    end
    puts "-------------------"
  end
  raise "No installer specified for this target!" unless @installer
  info "Ensuring #{package} is installed with #{@installer}", 1 if @verbose
  unless system("type #{package} >/dev/null 2>&1")
    case @installer
    when :apt
      sh "sudo apt-get install -y #{package}"
    when :brew
      sh "brew install #{package}"
    end
  end
end

#ln_s(source, dest, opts = {}) ⇒ Object



46
47
48
49
# File 'lib/shaddox/shadow.rb', line 46

def ln_s(source, dest, opts = {})
  info "Linking '#{source}' to '#{dest}'", 1 if @verbose
  FileUtils::ln_s(source.exp_path, dest.exp_path, opts)
end

#mkdir(path) ⇒ Object



51
52
53
54
# File 'lib/shaddox/shadow.rb', line 51

def mkdir(path)
  info "Ensuring directory '#{path}' exists", 1 if @verbose
  FileUtils::mkdir_p(path.exp_path)
end

#optional(&block) ⇒ Object



20
21
22
23
24
# File 'lib/shaddox/shadow.rb', line 20

def optional(&block)
  @required = false
  instance_eval(&block)
  @required = true
end

#repo_clone(repo_key, path) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/shaddox/shadow.rb', line 56

def repo_clone(repo_key, path)
  repo = @repos[repo_key]
  cd path do
    case repo.vcs
    when :git
      sh "git clone #{repo.url}"
    end
  end
end

#sh(command, args = nil) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/shaddox/shadow.rb', line 26

def sh(command, args = nil)
  line = "#{command}"
  line += " #{args.join(" ")}" if args
  info "Running '#{line}' in '#{Dir.pwd}'", 1 if @verbose
  system(command, *args)
  raise "#{line} failed" unless $? == 0 or !@required
end