Class: Smithy::Formula

Inherits:
Object
  • Object
show all
Defined in:
lib/smithy/formula.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(passed_package = nil) ⇒ Formula

Returns a new instance of Formula.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/smithy/formula.rb', line 12

def initialize(passed_package = nil)
  @formula_file = __FILE__
  raise "no install method implemented" unless self.respond_to?(:install)
  raise "homepage must be specified" if homepage.blank?
  raise "url must be specified" if url.blank?
  if passed_package
    set_package(passed_package)
  else
    # guess name and build_name
    @name = self.formula_name
    @build_name = operating_system
    initialize_modules
  end
end

Instance Attribute Details

#build_nameObject

Returns the value of attribute build_name.



3
4
5
# File 'lib/smithy/formula.rb', line 3

def build_name
  @build_name
end

#formula_fileObject

Returns the value of attribute formula_file.



3
4
5
# File 'lib/smithy/formula.rb', line 3

def formula_file
  @formula_file
end

#module_setupObject

Returns the value of attribute module_setup.



3
4
5
# File 'lib/smithy/formula.rb', line 3

def module_setup
  @module_setup
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/smithy/formula.rb', line 3

def name
  @name
end

#packageObject

Returns the value of attribute package.



3
4
5
# File 'lib/smithy/formula.rb', line 3

def package
  @package
end

#prefixObject

Returns the value of attribute prefix.



3
4
5
# File 'lib/smithy/formula.rb', line 3

def prefix
  @prefix
end

Class Method Details

.disable_group_writable(value = true) ⇒ Object

DLS Version Method, can set a version or guess based on the filename



92
93
94
95
# File 'lib/smithy/formula.rb', line 92

def self.disable_group_writable(value = true)
  @disable_group_writable = true
  @disable_group_writable
end

.formula_nameObject



5
6
7
# File 'lib/smithy/formula.rb', line 5

def self.formula_name
  self.to_s.underscore.split("/").last.gsub /_formula$/, ""
end

.version(value = nil) ⇒ Object

DLS Version Method, can set a version or guess based on the filename



75
76
77
78
79
80
81
82
83
84
# File 'lib/smithy/formula.rb', line 75

def self.version(value = nil)
  unless @version
    if value
      @version = value
    else
      @version = url_filename_version_number(url) if url.present?
    end
  end
  @version
end

Instance Method Details

#check_dependenciesObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/smithy/formula.rb', line 177

def check_dependencies
  @depends_on = [depends_on] if depends_on.is_a? String
  missing_packages = []
  notice "Searching for dependencies"
  depends_on.each do |package|
    name, version, build = package.split('/')
    path = Package.all(:name => name, :version => version, :build => build).first
    if path
      notice_using(path)
      p = Package.new(:path => path)
      new_name = p.name.underscore
      class_eval %Q{
        def #{new_name}
          @#{new_name} = Package.new(:path => "#{path}") if @#{new_name}.nil?
          @#{new_name}
        end
      }
    else
      missing_packages << package
      #TODO build package instead?
    end
  end
  unless missing_packages.empty?
    raise "#{self.class} depends on: #{missing_packages.join(" ")}"
  end
end

#create_modulefileObject



108
109
110
111
112
113
114
115
116
117
# File 'lib/smithy/formula.rb', line 108

def create_modulefile
  return false if modulefile.blank?
  notice "Creating Modulefile for #{package.prefix}"
  m = ModuleFile.new :package => package
  FileUtils.mkdir_p(File.dirname(m.module_file))
  FileOperations.render_erb(:erb_string => modulefile, :binding => m.get_binding, :destination => m.module_file)
  FileOperations.make_group_writable(m.module_path, :recursive => true)
  FileOperations.set_group(m.module_path, package.group, :recursive => true)
  return true
end

#fail_commandObject



144
145
146
147
148
149
150
151
152
# File 'lib/smithy/formula.rb', line 144

def fail_command
  $stdout.flush
  $stderr.flush
  raise <<-EOF.strip_heredoc
    The last command exited with status: #{$?.exitstatus}
      Formula: #{formula_file}
      Build Directory: #{@package.source_directory}
  EOF
end

#formula_nameObject



8
9
10
# File 'lib/smithy/formula.rb', line 8

def formula_name
  self.class.formula_name
end

#group_writable?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/smithy/formula.rb', line 97

def group_writable?
  ! self.class.instance_variables.include?(:@disable_group_writable)
end

#initialize_modulesObject

setup module environment by purging and loading only what’s needed



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/smithy/formula.rb', line 28

def initialize_modules
  @module_setup = ""
  @module_commands = @modules = nil # re-evaluates the blocks

  raise "please specify modules OR modules_command, not both" if modules.present? && module_commands.present?
  raise "module_commands method must return an array" if module_commands.present? && module_commands.class != Array

  if ENV["MODULESHOME"]
    @module_commands = @modules = nil # re-evaluates the blocks
    @modulecmd = "modulecmd sh"
    @modulecmd = "#{ENV["MODULESHOME"]}/bin/modulecmd sh" if File.exists?("#{ENV["MODULESHOME"]}/bin/modulecmd")
    if modules.present?
      @module_setup << `#{@module_setup} #{@modulecmd} purge 2>/dev/null` << " "
      raise "modules must return a list of strings" unless modules.is_a? Array
      @module_setup << `#{@module_setup} #{@modulecmd} load #{modules.join(" ")}` << " "
    elsif module_commands.present?
      module_commands.each do |command|
        @module_setup << `#{@module_setup} #{@modulecmd} #{command}` << " "
      end
    end
  end
end

#module_environment_variable(mod, var) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/smithy/formula.rb', line 135

def module_environment_variable(mod, var)
  module_display = `#{module_setup} #{@modulecmd} display #{mod} 2>&1`
  if module_display =~ /^(\S+)\s+#{var}\s+(.*)$/
    return $2.strip
  else
    return ""
  end
end

#module_is_available?(mod) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
129
130
131
132
133
# File 'lib/smithy/formula.rb', line 126

def module_is_available?(mod)
  module_avail = `#{module_setup} #{@modulecmd} avail #{mod} 2>&1`
  if module_avail =~ /#{mod}/
    true
  else
    false
  end
end

#module_listObject



119
120
121
122
123
124
# File 'lib/smithy/formula.rb', line 119

def module_list
  if ENV['MODULESHOME']
    notice "module list"
    Kernel.system @module_setup + "#{@modulecmd} list 2>&1"
  end
end

#patch(content, *args) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/smithy/formula.rb', line 154

def patch(content, *args)
  patch_file_name = "patch.diff"
  File.open(patch_file_name, "w+") do |f|
    f.write(content)
  end
  args << "-p1" if args.empty?
  patch_command = "patch #{args.join(' ')} <#{patch_file_name}"
  notice patch_command
  Kernel.system patch_command
  fail_command if $?.exitstatus != 0
end

#run_installObject



101
102
103
104
105
106
# File 'lib/smithy/formula.rb', line 101

def run_install
  check_dependencies if depends_on
  install
  notice_success "SUCCESS #{@prefix}"
  return true
end

#set_package(p) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/smithy/formula.rb', line 51

def set_package(p)
  @package    = p
  @name       = p.name
  @version    = p.version
  @build_name = p.build_name
  @prefix     = p.prefix
  initialize_modules
end

#system(*args) ⇒ Object



166
167
168
169
170
171
172
173
174
175
# File 'lib/smithy/formula.rb', line 166

def system(*args)
  notice args.join(' ')
  if args.first == :nomodules
    args.shift
    Kernel.system args.join(' ')
  else
    Kernel.system @module_setup + args.join(' ')
  end
  fail_command if $?.exitstatus != 0
end

#versionObject



86
87
88
89
# File 'lib/smithy/formula.rb', line 86

def version
  @version = self.class.version unless @version
  @version
end