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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/smithy/formula_command.rb', line 78

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)

  formula_class = "#{formula_name.underscore.camelize}Formula".constantize
  version = formula_class.version if version.blank?
  build = operating_system        if build.blank?
  p = Package.new :path => [name, version, build].join("/"), :group_writable => !formula_class.disable_group_writable

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

  return f
end

.create_module_command(options, args) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/smithy/formula_command.rb', line 183

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



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

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



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

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
# 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"]
      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



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

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



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

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



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

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



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

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

.install_command(options, args) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/smithy/formula_command.rb', line 97

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.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



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

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

.new_command(options, args) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/smithy/formula_command.rb', line 161

def self.new_command(options,args)
  @formula_name     = options[:name]
  @formula_name     = url_filename_basename(args.first) unless options[:name]
  @formula_name     = @formula_name.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



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

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

.which_command(options, args) ⇒ Object



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

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