Module: Builddir

Defined in:
lib/builddir.rb,
lib/builddir/utils.rb,
lib/builddir/version.rb

Constant Summary collapse

NAME =
'builddir'
VERSION =
'0.1.4'

Class Method Summary collapse

Class Method Details

.findExactMapping(mapping, path) ⇒ nil, { Symbol => String,Symbol}

Searches through the mapping for a direct path match

Parameters:

  • mapping (Array<Array(String, String)>)

    the mapping which should checked

  • path (String)

    path of the searched directory

Returns:

  • (nil, { Symbol => String,Symbol})

    the found mapping. nil on error



45
46
47
48
49
50
51
52
53
54
# File 'lib/builddir.rb', line 45

def Builddir.findExactMapping(mapping,path)
  mapping.each_with_index do |entry,index|
    if entry[0] == path
      return { :srcDir => entry[0], :buildDir => entry[1], :index => index, :type => :SRC_DIR }
    elsif  entry[1] == path
      return { :srcDir => entry[0], :buildDir => entry[1], :index => index, :type => :BUILD_DIR }
    end
  end
  return nil
end

.findMapping(mapping, path) ⇒ nil, { Symbol => String,Symbol}

Searches through the mapping for a path match. Parent directories are considered for the matching as well.

Parameters:

  • mapping (Array<Array(String, String)>)

    the mapping which should checked

  • path (String)

    path of the searched directory

Returns:

  • (nil, { Symbol => String,Symbol})

    the found mapping. nil on error



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/builddir.rb', line 62

def Builddir.findMapping(mapping,path)
  lastpn = nil
  pn  = Pathname.new(path)
  while pn != lastpn do
    entry = findExactMapping(mapping,pn.to_s)
    return entry if entry
    lastpn = pn
    pn = pn.parent()
  end
  return nil
end

.getBuildBasePathString

Determine the build directory base path

Returns:

  • (String)

    the base path for the build directories



76
77
78
# File 'lib/builddir.rb', line 76

def Builddir.getBuildBasePath()
  return ENV['DEFAULT_BUILDDIR'] || "/tmp/builddir"
end

.getGemLibdirString

Return the directory with the project libraries stackoverflow.com/a/5805783

Returns:

  • (String)

    path to the library directory



9
10
11
12
13
14
# File 'lib/builddir/utils.rb', line 9

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

.getMappingsFilePathString

Determine path to the mapping file

Returns:

  • (String)

    the path to the mapping file



82
83
84
# File 'lib/builddir.rb', line 82

def Builddir.getMappingsFilePath()
  return File.join(getBuildBasePath(),"builddir_mapping.yml")
end

.loadMapping(file = nil) ⇒ nil, Array<Array(String, String)>

Loads the BuildDir <-> SourceDir mapping from the file

Parameters:

  • file (String) (defaults to: nil)

    path of the mapping file

Returns:

  • (nil, Array<Array(String, String)>)

    The loaded mapping. nil on error



14
15
16
17
18
19
20
21
22
# File 'lib/builddir.rb', line 14

def Builddir.loadMapping(file = nil)
  file = getMappingsFilePath() if file.nil?
  begin
    mapping = YAML::load_file(file)
  rescue
    mapping = nil
  end
  return mapping
end

.saveMapping(mapping, file = nil) ⇒ Object

Saves the BuildDir <-> SourceDir mapping to a file

Parameters:

  • mapping (Array<Array(String, String)>)

    the mapping which should be saved

  • file (String) (defaults to: nil)

    path of the mapping file



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/builddir.rb', line 28

def Builddir.saveMapping(mapping, file = nil)
  file = getMappingsFilePath() if file.nil?
  
  # create the directory when needed
  dirname = File.dirname(file)
  FileUtils.mkdir_p(dirname) unless File.exists?(dirname)
  
  File.open(file, 'w') do |f|
    f.write mapping.to_yaml
  end
end