Class: BConv::PathAdapt

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

Constant Summary collapse

SLASH =
"/"
COLON =
":"

Class Method Summary collapse

Class Method Details

.adapt_path(outputFilePath, bhash) ⇒ Object



13
14
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
40
41
42
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
72
73
74
75
76
77
# File 'lib/PathAdapt.rb', line 13

def self.adapt_path(outputFilePath, bhash)
  diffPathLevels, diffPathLevelsDown = 0, 0
  dir, diffUp = "", ""
  diffArr = []
  
  splitOutputFile = File.expand_path(File.dirname(outputFilePath)).split("/")
  splitProjMeta   = bhash['BAKE_PROJECTDIR'][0].split("/")
  
  if splitOutputFile.length > splitProjMeta.length
    diffPathLevels = splitOutputFile.length-splitProjMeta.length
    for j in (splitOutputFile.length-diffPathLevels)..(splitOutputFile.length-1)
      diffUp += splitOutputFile[j] + "/"
      diffArr << splitOutputFile[j]
    end
    for i in 0..diffPathLevels-1
      dir += "../"
    end
  elsif splitOutputFile.length < splitProjMeta.length
    diffPathLevelsDown = splitProjMeta.length-splitOutputFile.length
    puts "Warning: this case is not supported!"
    return nil
  else
    return bhash
  end
  
  pathNames = []
  directories = {}

  if File.dirname(outputFilePath).eql? File.dirname(bhash['BAKE_PROJECTDIR'][0])
    #files do not have to be adapted

  else
    bhash.each do |key, value|
      if key == 'BAKE_SOURCES' || key == 'BAKE_INCLUDES'
        value.each do |pathElm|
          if pathElm[0] == SLASH || pathElm[1] == COLON
            pathNames << pathElm #nothing to do - is already absolute

          elsif pathElm[0..diffUp.length-1].eql? diffUp
            pathNames << pathElm.split(/^#{diffUp}/)[-1]
          else 
            tmpElmArr = pathElm.split("/")
            cnt = 0
            dirLoc = ""
            if tmpElmArr[0] != ".." #string does not start with ".."

              for j in (0..diffArr.length-1)
                if diffArr[j] != tmpElmArr[j]
                  cnt = cnt + 1
                  dirLoc << "../"
                end
              end 
              tmpElm = Util.truncateStr(diffUp,pathElm)
              pathNames << (dirLoc + tmpElm)
            else #string starts with ".." -> don't have to cut

              pathNames << (dir + pathElm)
            end
          end
        end
        directories.store(key, pathNames)
        pathNames = []
      else
        directories.store(key, value)
      end
    end
  end
  return directories
end