Class: PortVCS::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/portvcs/utils.rb

Class Method Summary collapse

Class Method Details

.config_load(conf, file, dirs) ⇒ Object

load config file immediately if it contains ‘/’ in its name, otherwise search through several dirs for it.

conf – a hash to merge result with

return a loaded filename or nil on error



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/portvcs/utils.rb', line 15

def self.config_load(conf, file, dirs)
  p = ->(f) {
    if File.readable?(f)
      begin
        myconf = YAML.load_file(f)
      rescue
        abort("cannot parse #{f}: #{$!}")
      end
      %w(host port user pass cvsroot ports_tree).each { |i|
        fail "missing or nil '#{i}' in #{f}" if ! myconf.key?(i.to_sym) || ! myconf[i.to_sym]
      }
      conf.merge!(myconf)
      return file
    end
    return nil
  }

  if file.index('/')
    return p.call(file)
  else
    dirs.each {|dir| return dir+'/'+file if p.call(dir + '/' + file) }
  end

  return nil
end

.gem_libdirObject



73
74
75
76
77
78
# File 'lib/portvcs/utils.rb', line 73

def self.gem_libdir
  t = ["#{File.dirname(File.expand_path($0))}/../lib/#{PortVCS::Meta::NAME}",
       "#{Gem.dir}/gems/#{PortVCS::Meta::NAME}-#{PortVCS::Meta::VERSION}/lib/#{PortVCS::Meta::NAME}"]
  t.each {|i| return i if File.readable?(i) }
  fail "both paths are invalid: #{t}"
end

.in_path?(file) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
# File 'lib/portvcs/utils.rb', line 80

def self.in_path?(file)
  return true if file =~ %r%\A/% and File.exist? file

  ENV['PATH'].split(File::PATH_SEPARATOR).any? do |path|
    File.exist? File.join(path, file)
  end
end

.port_extract_name(t, ptree) ⇒ Object

p – a string to examine ptree – a directory name with a local port tree, for example “/usr/ports”



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/portvcs/utils.rb', line 43

def self.port_extract_name(t, ptree)
  return nil if t.to_s == '' || (! t.index('/') && Dir.pwd !~ /^#{ptree}/)

  t.sub!(/#{ptree}(\/*)?/, '') if t =~ /^#{ptree}/
  return nil if t[0] == '/'

  # idempotent lambda
  inptree = ->() {
    if Dir.pwd =~ /^#{ptree}/
      l = (d = Dir.pwd.sub(/#{ptree}(\/*)?/, '')).split('/').length
      return File.split(d + '/' + t) if l >= 2 && l <= 3
    end
  }
  
  case t.split('/').length
  when 1
    return inptree.call
  when 2
    # assuming files/patch-aa in /usr/ports/www/firefox/files/
    return inptree.call if inptree.call
    # assuming www/firefox
    return [t]
  when 3..4
    # assuming www/firefox/Makefile
    return File.split(t)
  end

  return nil
end