Class: Pawnee::Actions::Compile

Inherits:
Object
  • Object
show all
Defined in:
lib/pawnee/pawnee/actions/compile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, url, temp_dir, options) ⇒ Compile

Sets up a compile object

Parameters

base<Thor>

The main class

url<String>

The url to download from

temp_dir<String>

A path to a temporary directory where the compilation

can take place



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/pawnee/pawnee/actions/compile.rb', line 63

def initialize(base, url, temp_dir, options)
  @base = base
  @temp_dir = temp_dir
  self.options = options
  @file = File.basename(url)
  @zip_file = @file[/[.]zip$/]
  @file_path = File.join(temp_dir, @file)
  
  # TODO: GET THE FIRST DIRECTORY INSTEAD
  dir_name = @file.gsub(/[.]tar[.]gz$/, '').gsub(/[.]zip$/, '')
  @extracted_path = File.join(temp_dir, dir_name)
  
  @base.say_status 'download and compile', url
  
  download(url)
  extract
  configure
  make
  make_install
end

Instance Attribute Details

#baseObject

Returns the value of attribute base.



53
54
55
# File 'lib/pawnee/pawnee/actions/compile.rb', line 53

def base
  @base
end

#optionsObject

Returns the value of attribute options.



54
55
56
# File 'lib/pawnee/pawnee/actions/compile.rb', line 54

def options
  @options
end

Instance Method Details

#configureObject

Runs ./configure on the files



123
124
125
# File 'lib/pawnee/pawnee/actions/compile.rb', line 123

def configure
  run_with_failure_handler("cd #{@extracted_path} ; ./configure #{options[:configure] || ''}", 'configure')
end

#download(url) ⇒ Object

Uses get to download the file and place it in the temp path



85
86
87
# File 'lib/pawnee/pawnee/actions/compile.rb', line 85

def download(url)
  base.get(url, @file_path)
end

#extractObject

Extract the compressed file



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/pawnee/pawnee/actions/compile.rb', line 90

def extract
  base.say_status 'extract', @file
  if @zip_file
    base.exec("cd #{@temp_dir} ; unzip #{@file}")
  else
    base.exec("cd #{@temp_dir} ; tar xvfpz #{@file}")
  end

  # Remove the file
  base.destination_files.rm_rf(@file_path)
end

#makeObject

Runs make



128
129
130
# File 'lib/pawnee/pawnee/actions/compile.rb', line 128

def make
  run_with_failure_handler("cd #{@extracted_path} ; make", 'make')
end

#make_installObject

Runs sudo make install



133
134
135
# File 'lib/pawnee/pawnee/actions/compile.rb', line 133

def make_install
  run_with_failure_handler("cd #{@extracted_path} ; sudo make install", 'make install')
end

#run_with_failure_handler(command, action_name) ⇒ Object

Takes a command and an action_name. It says its running the action_name, then runs the command and if there is non-zero exit status, it prints out an error, stdout, and stderr, then raises an exception

Parameters

command<String>

The command to be run

action_name<String>

An action name (used to explain what is happening)



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/pawnee/pawnee/actions/compile.rb', line 109

def run_with_failure_handler(command, action_name)
  base.say_status action_name.downcase, ''
  stdout, stderr, exit_code, exit_status = base.exec(command, true)
  
  if exit_code != 0
    base.say_status :error, "Unable to #{action_name}, see output below", :red
    puts stdout
    puts stderr
    
    raise "Unable to configure #{action_name}"
  end
end