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

#ensure_gitObject



71
72
73
74
75
76
# File 'lib/shaddox/shadow.rb', line 71

def ensure_git()
  unless @git_installed
    install 'git'
    @git_installed = true
  end
end

#ensure_parent_dir(path) ⇒ Object



66
67
68
69
# File 'lib/shaddox/shadow.rb', line 66

def ensure_parent_dir(path)
  dir, base = File.split(path.exp_path)
  mkdir(dir)
end

#exists(path) ⇒ Object



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

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

#exists_d(path) ⇒ Object



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

def exists_d(path)
  system("test -d #{path.exp_path}")
end

#exists_f(path) ⇒ Object



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

def exists_f(path)
  system("test -f #{path.exp_path}")
end

#install(package) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/shaddox/shadow.rb', line 114

def install(package)
  unless @installer
    # TODO: Try to autodetect package manager
    warn "No installer is specified 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
  package_installed = lambda { system("type #{package} >/dev/null 2>&1") }
  unless package_installed.call()
    case @installer
    when :apt
      sh "sudo apt-get install -y #{package}"
    when :brew
      sh "brew install #{package}"
    end
  end
  raise "#{package} could not be installed." unless package_installed.call()
end

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



54
55
56
57
58
59
# File 'lib/shaddox/shadow.rb', line 54

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

#mkdir(path) ⇒ Object



61
62
63
64
# File 'lib/shaddox/shadow.rb', line 61

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_deploy(repo_key, deploy_path, opts = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/shaddox/shadow.rb', line 78

def repo_deploy(repo_key, deploy_path, opts ={})
  keep_releases = opts[:keep_releases] || 5
  repo = @repos[repo_key]

  ensure_git()
  ensure_parent_dir(deploy_path)
  deploy_path = deploy_path.exp_path

  cd deploy_path do

    # Get the current release number
    release = 0
    cd 'releases' do
      current_max = Dir.entries('.').select { |e| e =~ /\d+/ }.max
      release = current_max.to_i + 1 if current_max
    end

    # Make a new release dir
    release_path = "./releases/#{release}"
    mkdir(release_path)

    case repo.vcs
    when :git
      # Clone/update repo in vcs:
      if exists_d('vcs')
        cd 'vcs' do
          sh "git fetch #{repo.url} #{repo.branch}:#{repo.branch} --force"
        end
      else
        sh "git clone #{repo.url} vcs -b #{repo.branch} --bare"
      end
      sh "git clone ./vcs #{release_path} --recursive --brach #{repo.branch}"
    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