Class: DependencyHandler

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

Overview

handle the dependencies and it’s place holder

Constant Summary collapse

@@reactorHandler =
ReactorHandler.new
@@basicInfoHandler =
BasicInfoHandler.new

Instance Method Summary collapse

Instance Method Details

#add_node_element(childs_parent_name, child_element, pom_nokogiri) ⇒ Object

This api add child node in any tag #



253
254
255
256
257
258
259
260
261
# File 'lib/mavenReactorService/DependencyHandler.rb', line 253

def add_node_element(childs_parent_name, child_element, pom_nokogiri)
  raise 'Parent tag is empty or nil' if childs_parent_name.nil? ||
  childs_parent_name.empty?
  raise 'Child element is nil' if child_element.nil?
  raise 'POM Document is nil' if pom_nokogiri.nil?
  childs_parent = pom_nokogiri.at_css(childs_parent_name)
  childs_parent.add_child(child_element)
  pom_nokogiri
end

#findPlaceHolderWithinPoms(tempDependencyList, originalDependencyMap, parentDependencyMap, grandParentDependencyMap) ⇒ Object

This api takes decision to indentify the dependency where the place holder found. #



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/mavenReactorService/DependencyHandler.rb', line 200

def findPlaceHolderWithinPoms(tempDependencyList,originalDependencyMap,parentDependencyMap,grandParentDependencyMap)
  if (!tempDependencyList.nil?)
    originalVersion = nil
    parentVersion = nil
    grandParentVersion = nil
    tempDependencyList.each do |eachDependency|
      if (!originalDependencyMap.empty?)
        originalVersion = originalDependencyMap[eachDependency]
      end
      if (!parentDependencyMap.empty?)
        parentVersion = parentDependencyMap[eachDependency]
      end
      if (!grandParentDependencyMap.empty?)
        grandParentVersion = grandParentDependencyMap[eachDependency]
      end
      if (!originalVersion.nil? and originalVersion.include? "${")
        replaceVersionInTempFile(eachDependency,originalVersion)
      elsif (!parentVersion.nil? and parentVersion.include? "${")
        replaceVersionInTempFile(eachDependency,parentVersion)
      elsif (!grandParentVersion.nil? and grandParentVersion.include? "${")
        replaceVersionInTempFile(eachDependency,grandParentVersion)
      end
    end
  end
end

#getgrandParentDependencyMapObject

return the union of dependencies in grand-parent



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
# File 'lib/mavenReactorService/DependencyHandler.rb', line 114

def getgrandParentDependencyMap()
  pom_document = @@basicInfoHandler.copyTagsFromOriginalPom(Dir.getwd)
  parent = pom_document.at("project/parent")
  dependencyMap = Hash.new
  if !parent.nil?
    parent_pom_document =  getParentPomDomObjFromUrl(pom_document)
    grandParent = parent_pom_document.at("project/parent")
    if !grandParent.nil?
      grand_parent_pom_document =  getParentPomDomObjFromUrl(parent_pom_document)

      if (!grand_parent_pom_document.at_css("project/dependencyManagement/dependencies/dependency").nil?)
        grand_parent_pom_document.css("project/dependencyManagement/dependencies/dependency").each do |eachDependecy|
          grpId = eachDependecy.at("groupId").text
          artifactId = eachDependecy.at("artifactId").text
          fullDependency = "#{grpId}:#{artifactId}"
          version = nil
          if (!eachDependecy.at("version").nil?)
            version = eachDependecy.at("version").text
          end
          dependencyMap[fullDependency] = version
        end
      end

      if (!grand_parent_pom_document.at_css("project/dependencies/dependency").nil?)
        grand_parent_pom_document.css("project/dependencies/dependency").each do |eachDependecy|
          grpId = eachDependecy.at("groupId").text
          artifactId = eachDependecy.at("artifactId").text
          fullDependency = "#{grpId}:#{artifactId}"
          version = nil
          if (!eachDependecy.at("version").nil?)
            version = eachDependecy.at("version").text
          end
          exsistingDependency = dependencyMap[fullDependency]
          if (!exsistingDependency.nil? and !version.nil?)
            dependencyMap[fullDependency] = version
          end
        end
      end
    end
  end
  return dependencyMap
end

#getImmediateParentDependencyMapObject

return the union of dependencies in parent



74
75
76
77
78
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
# File 'lib/mavenReactorService/DependencyHandler.rb', line 74

def getImmediateParentDependencyMap()
  pom_document = @@basicInfoHandler.copyTagsFromOriginalPom(Dir.getwd)
  parent = pom_document.at("project/parent")
  dependencyMap = Hash.new
  if !parent.nil?
    parent_pom_document =  getParentPomDomObjFromUrl(pom_document)
    if (!parent_pom_document.at_css("project/dependencyManagement/dependencies/dependency").nil?)
      parent_pom_document.css("project/dependencyManagement/dependencies/dependency").each do |eachDependecy|
        grpId = eachDependecy.at("groupId").text
        artifactId = eachDependecy.at("artifactId").text
        fullDependency = "#{grpId}:#{artifactId}"
        version = nil
        if (!eachDependecy.at("version").nil?)
          version = eachDependecy.at("version").text
        end
        dependencyMap[fullDependency] = version
      end
    end

    if (!parent_pom_document.at_css("project/dependencies/dependency").nil?)
      parent_pom_document.css("project/dependencies/dependency").each do |eachDependecy|
        grpId = eachDependecy.at("groupId").text
        artifactId = eachDependecy.at("artifactId").text
        fullDependency = "#{grpId}:#{artifactId}"
        version = nil
        if (!eachDependecy.at("version").nil?)
          version = eachDependecy.at("version").text
        end
        exsistingDependency = dependencyMap[fullDependency]
        if (!exsistingDependency.nil? and !version.nil?)
          dependencyMap[fullDependency] = version
        end
      end
    end

  end
  return dependencyMap
end

#getOriginalDependencyMapObject

return the union of dependencies in original-pom



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
# File 'lib/mavenReactorService/DependencyHandler.rb', line 38

def getOriginalDependencyMap()
  pom_document =  @@basicInfoHandler.copyTagsFromOriginalPom(Dir.getwd)
  dependencyMap = Hash.new

  if (!pom_document.at_css("project/dependencyManagement/dependencies/dependency").nil?)
    pom_document.css("project/dependencyManagement/dependencies/dependency").each do |eachDependecy|
      grpId = eachDependecy.at("groupId").text
      artifactId = eachDependecy.at("artifactId").text
      fullDependency = "#{grpId}:#{artifactId}"
      version = nil
      if (!eachDependecy.at("version").nil?)
        version = eachDependecy.at("version").text
      end
      dependencyMap[fullDependency] = version
    end
  end

  if (!pom_document.at_css("project/dependencies/dependency").nil?)
    pom_document.css("project/dependencies/dependency").each do |eachDependecy|
      grpId = eachDependecy.at("groupId").text
      artifactId = eachDependecy.at("artifactId").text
      fullDependency = "#{grpId}:#{artifactId}"
      version = nil
      if (!eachDependecy.at("version").nil?)
        version = eachDependecy.at("version").text
      end
      exsistingDependency = dependencyMap[fullDependency]
      if (!exsistingDependency.nil? and !version.nil?)
        dependencyMap[fullDependency] = version
      end
    end
  end
  return dependencyMap
end

#getParentPomDomObjFromUrl(pom_document) ⇒ Object

create domobj of parent and grand parent



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
# File 'lib/mavenReactorService/DependencyHandler.rb', line 158

def getParentPomDomObjFromUrl(pom_document)
  groupId =  pom_document.css("parent/groupId").text
  artifactId = pom_document.css("parent/artifactId").text
  version = pom_document.css("parent/version").text
  groupId = "#{groupId.gsub(".","/")}"
  urlArr =  getRepositories(pom_document)
  urlArr.each do |eachUrl|
    filteredUrl = "#{eachUrl}/#{groupId}/#{artifactId}/#{version}"
    begin
      pom_html_doc = Nokogiri::HTML(open(filteredUrl))
      latestPom = nil
      pom_html_doc.css('a').each do |eachref|
        if (eachref.text.end_with?".pom")
          latestPom = eachref.text
        end
      end
      filteredUrl = "#{filteredUrl}/#{latestPom}"
      parent_pom_document = Nokogiri::XML(open(filteredUrl))
    rescue
      puts "Wrong Url :::: #{filteredUrl}"
    end
    if !parent_pom_document.nil?
      return parent_pom_document
    end
  end
end

#getRepositories(pom_document) ⇒ Object

indentify the actual url of parent



186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/mavenReactorService/DependencyHandler.rb', line 186

def getRepositories(pom_document)
  urlArr = Array.new
  pom_document.css("repositories/repository").each do |eachRepo|
    eachUrl =  eachRepo.at_css("url").text
    lastChar =  eachUrl[-1,1]
    if lastChar=="/"
      eachUrl=  eachUrl[0,eachUrl.length-1]
    end
    urlArr.push(eachUrl)
  end
  return urlArr
end

#getTempDependencyListObject

return the dependency list of effetive-pom



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mavenReactorService/DependencyHandler.rb', line 25

def getTempDependencyList()
  temp_pom_document = Nokogiri::XML(open("temp.xml"))
  dependencyList = Array.new
  temp_pom_document.css("project/dependencies/dependency").each do |eachDependecy|
    grpId = eachDependecy.at("groupId").text
    artifactId = eachDependecy.at("artifactId").text
    fullDependency = "#{grpId}:#{artifactId}"
    dependencyList.push(fullDependency)
  end
  return dependencyList
end

#projectPomDependencyHandlerObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/mavenReactorService/DependencyHandler.rb', line 7

def projectPomDependencyHandler()
 efftive_pom_document = @@basicInfoHandler.copyTagsFromEffectivePom(Dir.getwd)
 alldependencies =  efftive_pom_document.at("project/dependencies")
 alldependencyManagement =  efftive_pom_document.at("project/dependencyManagement")
 if !alldependencies.nil?
   tempXml = "temp.xml"
   File.write(tempXml, alldependencies, File.size(tempXml), mode: 'a')
   puts "Dependencies are added"
 end
 if !alldependencyManagement.nil?
   tempXml = "temp.xml"
   File.write(tempXml, alldependencyManagement, File.size(tempXml), mode: 'a')
   puts "Dependency management is added"
 end
end

#replaceVersionInTempFile(eachDependency, version) ⇒ Object

This api replace version by place-holder #



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/mavenReactorService/DependencyHandler.rb', line 227

def replaceVersionInTempFile (eachDependency,version)
  dependencyArr = eachDependency.split(":")
  gid = dependencyArr[0]
  artifactId = dependencyArr[1]
  temp_pom_document = Nokogiri::XML(open("temp.xml"))
  nokObj = Nokogiri::XML::Node
  dependeciesNode = temp_pom_document.at("project/dependencies")
  versionNode = nokObj.new("version",dependeciesNode)
  versionNode.content=version
  dependeciesNode.css("dependency").each do |tempDependency|
    replaceDependency=nil
    tempGid = tempDependency.at("groupId").text
    tempArtifactId = tempDependency.at("artifactId").text
    fullDependency = "#{tempGid}:#{tempArtifactId}"
    if (eachDependency == fullDependency)
      tempDependency.remove
      tempDependency.at("version").remove
      tempDependency.add_child(versionNode)
      dependeciesNode.add_child(tempDependency)
    end
  end
  formated_pom_doc = add_node_element("project",dependeciesNode,temp_pom_document)
  File.write("temp.xml", formated_pom_doc.to_xml)
end