Class: Smithy::FormulaCommand

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

Overview

This class acts as a controller for formula subcommands

Class Method Summary collapse

Class Method Details

.build_formula(package, formula_name = nil) ⇒ Object

construct a new fomula object given a formula name or full name/version/build



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
113
114
115
116
117
118
119
# File 'lib/smithy/formula_command.rb', line 79

def self.build_formula(package, formula_name = nil)
  name, version, build = package.split("/")
  formula_name = name if formula_name.blank?
  raise "unknown formula #{formula_name}" unless formula_names.include?(formula_name)

  require formula_file_path(formula_name)
  f = "#{formula_name.underscore.camelize}Formula".constantize.new
  # Set the actual formula file path, otherwise it's just formula.rb
  f.formula_file = formula_file_path(formula_name)

  guessing_new_name = true if version.blank? || build.blank?
  version = f.version      if version.blank?
  build = operating_system if build.blank?

  if guessing_new_name
    guessed_install_path = File.join(
      # Smithy::Config.global[:full_software_root_path],
      [name, version, build].join("/") )

    use_guessed_name = false
    while use_guessed_name == false do
      prompt = Readline.readline("Did you mean #{guessed_install_path} ? (enter \"h\" for help) [ynh] ")
      case prompt.downcase
      when "y"
        use_guessed_name = true
      when "n"
        notice_warn "Please re-run with a full target name including version and build name."
        raise "Aborting install"
      when "h"
        puts "    y - yes, continue"
        puts "    n - no, abort"
        puts "    h - help, show this help"
      end
    end
  end

  p = Package.new :path => [name, version, build].join("/"), :group_writable => f.group_writable?
  f.set_package(p) if p.valid?

  return f
end

.create_module_command(options, args) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/smithy/formula_command.rb', line 208

def self.create_module_command(options,args)
  packages = args.dup
  raise "You must supply at least one package to install" if packages.empty?

  packages.each do |package|
    f = build_formula(package, options[:"formula-name"])
    if f.modulefile.present?
      f.create_modulefile
    else
      ModuleFile.new(:package => f.package).create
    end
  end
end

.display_command(options, args) ⇒ Object



67
68
69
70
71
# File 'lib/smithy/formula_command.rb', line 67

def self.display_command(options,args)
  initialize_directories(options)
  formula_name = args.first.split("/").first
  puts formula_contents(formula_name)
end

.formula_contents(name) ⇒ Object

Return the file contents of a formula



52
53
54
55
# File 'lib/smithy/formula_command.rb', line 52

def self.formula_contents(name)
  raise "unkown formula '#{name}'" unless formula_file_path(name).present? && File.exists?(formula_file_path(name))
  File.read(formula_file_path(name))
end

.formula_directoriesObject

Return formula directories in order of precedence

  1. formula directories specified on the command line

  2. formulas located in ~/.smithy/formulas/

  3. smithy’s built in formulas



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/smithy/formula_command.rb', line 8

def self.formula_directories
  unless @formula_directories
    @formula_directories = [ File.join(Smithy::Config.homedir, ".smithy/formulas") ]
    if Smithy::Config.global[:"formula-directories"]
      raise "The formula-directories option in $SMITHY_CONFIG should be an array" if Smithy::Config.global[:"formula-directories"].class != Array
      Smithy::Config.global[:"formula-directories"].reverse.each do |dir|
        @formula_directories << dir
      end
    end
    @formula_directories << File.join(@@smithy_bin_root, "formulas")
  end
  @formula_directories
end

.formula_file_path(name) ⇒ Object

Return the file path of a single formula file



47
48
49
# File 'lib/smithy/formula_command.rb', line 47

def self.formula_file_path(name)
  formula_files.select{|f| f =~ /\/#{name}_formula.rb/}.first
end

.formula_filesObject

Returns an array containing the full paths to each file name in order of precedence



30
31
32
33
34
35
36
37
38
# File 'lib/smithy/formula_command.rb', line 30

def self.formula_files
  unless @formula_files
    @formula_files = []
    formula_directories.each do |dir|
      @formula_files += Dir.glob(File.join(File.expand_path(dir),"*.rb")).sort
    end
  end
  @formula_files
end

.formula_namesObject

Format the full file paths to display only the name



41
42
43
44
# File 'lib/smithy/formula_command.rb', line 41

def self.formula_names
  @formula_names = formula_files.collect{|f| File.basename(f,"_formula.rb")}.uniq unless @formula_names
  @formula_names
end

.initialize_directories(options = {}) ⇒ Object



57
58
59
# File 'lib/smithy/formula_command.rb', line 57

def self.initialize_directories(options = {})
  prepend_formula_directory(options[:d]) if options[:d]
end

.install_command(options, args) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/smithy/formula_command.rb', line 121

def self.install_command(options,args)
  initialize_directories(options)

  packages = args.dup
  raise "You must supply at least one package to install" if packages.empty?

  packages.each do |package|
    f = build_formula(package, options[:"formula-name"])
    f.check_dependencies
    f.package.create(:formula => true)

    formula_prefix_contents = Dir["#{f.prefix}/*"]
    unless formula_prefix_contents.empty?
      # overwrite = "unknown"
      # while overwrite == "unknown" do
      #   notice_conflict f.package.prefix
      #   prompt = Readline.readline(" "*FILE_NOTICE_COLUMNS+"Is not empty, delete contents? (enter \"h\" for help) [ynhq] ")
      #   case prompt.downcase
      #   when "y"
      #     overwrite = true
      #   when "n"
      #     overwrite = false
      #   when "h"
      #     indent = " "*FILE_NOTICE_COLUMNS
      #     puts indent+"y - yes, delete existing install"
      #     puts indent+"n - no, do not overwrite"
      #     puts indent+"h - help, show this help"
      #     puts indent+"q - exit now"
      #   when "q"
      #     raise "Abort new formula install"
      #   end
      # end
      # if overwrite
      if options[:clean]
        notice "cleaning #{f.prefix}"
        formula_prefix_contents.each do |f|
          FileUtils.rm_rf(f)
        end
      end
      # end
    end

    if f.url.eql?("none")
      Dir.chdir File.join(f.package.prefix)
    else
      d = DownloadCache.new(f, options[:"formula-name"]).get
      raise "Download failure" unless d
      # f.package.extract(:archive => d, :overwrite => true)
      f.package.extract(:archive => d)
      Dir.chdir File.join(f.package.prefix, "source")
    end

    if f.run_install
      f.package.create_valid_build_file
      f.package.set_file_permissions_recursive

      if f.modulefile.present?
        f.create_modulefile
      elsif options[:"modulefile"]
        ModuleFile.new(:package => f.package).create
      end
    end
  end #packages.each
end

.list_command(options, args) ⇒ Object



73
74
75
76
# File 'lib/smithy/formula_command.rb', line 73

def self.list_command(options,args)
  initialize_directories(options)
  puts formula_names
end

.new_command(options, args) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/smithy/formula_command.rb', line 186

def self.new_command(options,args)
  @formula_name     = options[:name]
  @formula_name     = url_filename_basename(args.first) unless options[:name]
  @formula_name     = @formula_name.underscore.camelize
  @formula_url      = args.first
  @formula_homepage = options[:homepage]
  @formula_homepage = "#{URI(@formula_url).scheme}://#{URI(@formula_url).host}/" unless options[:homepage]

  destination = File.join(Smithy::Config.homedir, ".smithy/formulas")
  destination = Smithy::Config.global[:"formula-directories"].first if Smithy::Config.global[:"formula-directories"]
  FileUtils::mkdir_p(destination)

  destination = File.join(destination, "#{@formula_name.underscore}_formula.rb")
  FileOperations.render_erb :binding => binding,
    :erb         => File.join(@@smithy_bin_root, "etc/templates/formula.rb.erb"),
    :destination => destination
  if Smithy::Config.global[:"file-group-name"]
    FileOperations.set_group(destination, Smithy::Config.global[:"file-group-name"])
    FileOperations.make_group_writable(destination)
  end
end

.prepend_formula_directory(dir) ⇒ Object

Prepend a directory to the formula file paths



23
24
25
26
# File 'lib/smithy/formula_command.rb', line 23

def self.prepend_formula_directory(dir)
  @formula_directories.unshift(dir)
  @formula_directories
end

.which_command(options, args) ⇒ Object



61
62
63
64
65
# File 'lib/smithy/formula_command.rb', line 61

def self.which_command(options,args)
  initialize_directories(options)
  formula_name = args.first.split("/").first
  puts formula_file_path(formula_name)
end