Class: ReactorHandler

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

Overview

This class will handle all the reactor-operation #

Constant Summary collapse

@@flag =
false
@@projectsWithExternalConfiguration =
Array.new
@@allProjectRepos =
Array.new

Instance Method Summary collapse

Instance Method Details

#addParentInReactorPom(project_directory_path, cordinateArr) ⇒ Object



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/mavenReactorService/ReactorHandler.rb', line 388

def addParentInReactorPom(project_directory_path,cordinateArr)
  if cordinateArr.length == 3
    reactorPom = "#{project_directory_path}/pom.xml"
    pom_document = parse_xml_from_file(reactorPom)
    nokObj = Nokogiri::XML::Node
    projectNode = pom_document.at("project")
    grpIdNode = nokObj.new("groupId",projectNode)
    grpIdNode.content=cordinateArr[0]
    artifactIdNode = nokObj.new("artifactId",projectNode)
    artifactIdNode.content = cordinateArr[1]
    versionNode = nokObj.new("version",projectNode)
    versionNode.content = cordinateArr[2]
    parentNode = nokObj.new("parent",projectNode)
    parentNode.add_child(grpIdNode)
    parentNode.add_child(artifactIdNode)
    parentNode.add_child(versionNode)
    modelVersionNode = pom_document.at_css('modelVersion')
    modelVersionNode.add_previous_sibling("\n")
    modelVersionNode.add_previous_sibling(parentNode.to_xml)
    File.write(reactorPom, pom_document.to_xml)
    puts "Parent tag is added in reactor pom"
  end
end

#checkIdenticalParent(project_directory_path) ⇒ Object



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/mavenReactorService/ReactorHandler.rb', line 360

def checkIdenticalParent(project_directory_path)
  eachModuleArr = fetchGidArtifactIdAndVersionFromChildModule(project_directory_path,false)
  validateProjects = ValidateProjects.new
  cordinateArr = Array.new
  cordinates = validateProjects.checkEligiblityForReactorization(project_directory_path)
  if !cordinates.nil?
    cordinates.each do |key,indx|
      cordinatesArr =  key.split(":")
      cordinateArr.push(cordinatesArr[0])
      cordinateArr.push(cordinatesArr[1])
      cordinateArr.push(cordinatesArr[2])
    end
    if cordinateArr.length == 3
      eachModuleArr.each do |eachPom|
        pom_document = parse_xml_from_file(eachPom)
        if !pom_document.at("project/parent").nil?
          pom_document.at("project/parent").remove
          File.write(eachPom,pom_document)
        end
      end
    end
  end
  if cordinateArr.length==3
    puts "#{cordinateArr[0]}:#{cordinateArr[1]}:#{cordinateArr[2]} parent is removed from child modules"
  end
  return cordinateArr
end

#clean(node) ⇒ Object



251
252
253
254
255
256
# File 'lib/mavenReactorService/ReactorHandler.rb', line 251

def clean(node)
  node.children.each do |child|
    clean(child)
    child.remove if child.content.gsub(/\s+/, "").empty?
  end
end

#closeFileObjObject



338
339
340
341
# File 'lib/mavenReactorService/ReactorHandler.rb', line 338

def closeFileObj()
  @@xml_file.close
  @@xml_file.closed?
end

#createDistributionManagmentInReactorPom(project_directory_path, externalPomUrl) ⇒ Object



236
237
238
239
# File 'lib/mavenReactorService/ReactorHandler.rb', line 236

def createDistributionManagmentInReactorPom(project_directory_path, externalPomUrl)
  moveDistributionManagement = MoveDistributionManagement.new
  moveDistributionManagement.moveDistributionManagementFromFirstModule(project_directory_path, externalPomUrl)
end

#createEffectivePom(project_directory_path, externalPomUrl, isMBP = false) ⇒ Object

This api will generate effective-pom and implements flatten out process # If parent tag is not prersent then effective-pom will not be generated #



11
12
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
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/ReactorHandler.rb', line 11

def createEffectivePom(project_directory_path, externalPomUrl, isMBP = false)
  reactorProjectPOM = Dir.getwd + "/pom.xml"
  if (File.exists?(reactorProjectPOM))
    reactorProjectDoc = parse_xml_from_file(reactorProjectPOM)
    reactorProjectDoc.css("repositories/repository").each do |eachRepo|
      eachUrl = eachRepo.at_css("url").text
      eachUrl = eachUrl.end_with?("/") ? eachUrl.chop : eachUrl
      if !(@@allProjectRepos.include? eachUrl)
        @@allProjectRepos.push(eachUrl)
      end
    end
  end
  eachModuleArr = Array.new
  if isMBP
    eachModuleArr.push("#{project_directory_path}/pom.xml")
  else
    eachModuleArr = fetchGidArtifactIdAndVersionFromChildModule(project_directory_path, false)
  end
  parentPomHandler = ParentPomHandler.new
  cordinatesArr = nil
  if (!externalPomUrl.nil?) and (externalPomUrl.include? "https://" or externalPomUrl.include? "http://")
    cordinatesArr = parentPomHandler.makeCordinateFromUrl(externalPomUrl)
  end
  eachModuleArr.each do |eachPom|
    isIdentical = false
    pom_document = parse_xml_from_file(eachPom)
    if tag_exists?("parent", pom_document)
      @@cordinateArr = parentPomHandler.fetch_mbp_cordinates_found_in_ancestor(pom_document)
      if ((@@cordinateArr.length == 0) and ((!externalPomUrl.nil?) and (externalPomUrl.include? "https://" or externalPomUrl.include? "http://")))
        orginalCordinateArr = parentPomHandler.makeOriginalPomUrl(pom_document)
        isIdentical = parentPomHandler.compareTwoCordinatesArr(cordinatesArr, orginalCordinateArr)
      elsif (@@cordinateArr.length > 0)
        isIdentical = true
      end
      if !isIdentical
        eachPomPath = eachPom.sub("/pom.xml", "")
        Dir.chdir(eachPomPath) {
          result = `mvn help:effective-pom -Doutput=effective_pom.xml`
          puts result # Maven out-put

          generate_temp_xml()
          @@projectsWithExternalConfiguration.push(eachPomPath)
          basicInfoHandler = BasicInfoHandler.new
          basicInfoHandler.handleGrpAndArtifact(eachPomPath)
          basicInfoHandler.handleModelVersion(eachPomPath)
          basicInfoHandler.handleInceptionYearDescAndOrganization(eachPomPath)
          basicInfoHandler.handleProjectUrl(eachPomPath)
          basicInfoHandler.handleDevelopersContributorsMailingLists(eachPomPath)
          basicInfoHandler.handleScmIssueManagementCiManagement(eachPomPath)
          basicInfoHandler.handleDistributionManagement(eachPomPath)
          basicInfoHandler.handleRepositories(eachPomPath)
          basicInfoHandler.handlePluginRepositories(eachPomPath)
          pluginHandler = PluginHandler.new
          pluginHandler.addBuildTag()
          pluginHandler.addReportingTag()
          propertyHandler = PropertyHandler.new
          propertyHandler.handleProperties(eachPomPath)
          dependencyHandler = DependencyHandler.new
          dependencyHandler.projectPomDependencyHandler()
          tempDependencyList = dependencyHandler.getTempDependencyList()
          childDependencyMap = dependencyHandler.getOriginalDependencyMap()
          parentDependencyMap = dependencyHandler.getImmediateParentDependencyMap()
          grandParentDependencyMap = dependencyHandler.getgrandParentDependencyMap()
          dependencyHandler.findPlaceHolderWithinPoms(tempDependencyList, childDependencyMap, parentDependencyMap, grandParentDependencyMap)
          project_end()
          tempPropertyMap = propertyHandler.getTempPropertyMap()
          childPropertyMap = propertyHandler.getOriginalPropertyMap()
          parentPropertyMap = propertyHandler.getImidiateParentPropertyMap()
          grandparentPropertyMap = propertyHandler.getgrandParentPropertyMap()
          propertyHandler.findPlaceHolderWithinProperties(tempPropertyMap, childPropertyMap, parentPropertyMap, grandparentPropertyMap)
          pluginHandler.removePluginManagementTag()
          pluginList = pluginHandler.getGrpAndArtifactIdOfPlugins()
          pluginHandler.compareWithOriginalPomPlugin(pluginList)
          pluginHandler.compareWithParentPomPlugin(pluginList)
          pluginHandler.compareWithGrandParentPomPlugin(pluginList)
          pluginHandler.removeExtraTag("build")
          #Reporting plugin

          reportSwitch = "reporting"
          pluginList = pluginHandler.getGrpAndArtifactIdOfPlugins(reportSwitch)
          pluginHandler.compareWithOriginalPomPlugin(pluginList, reportSwitch)
          pluginHandler.compareWithParentPomPlugin(pluginList, reportSwitch)
          pluginHandler.compareWithGrandParentPomPlugin(pluginList, reportSwitch)
          pluginHandler.removeExtraTag("reporting")
          #Site and eclipse plugin

          handleSiteAndEclipsePlugin(eachPomPath)
          closeFileObj()
          basicInfoHandler.renamePom(eachPomPath)
          basicInfoHandler.renameTemp(eachPomPath)
          puts "Original pom is reanamed to pom_back.xml"
        }
      else
        puts "Provided parent URL and external configuration are same"
      end
    else
      eachPomPath = eachPom.sub("/pom.xml", "")
      index = eachPomPath.rindex("/")
      projectName = eachPomPath[index + 1, eachPomPath.length - 1]
      puts "No parent found for #{projectName} project"
    end
  end
  puts "Effective pom generation is completed"
end

#dmpmForexternalConfiguration(project_directory_path, externalPomUrl) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
# File 'lib/mavenReactorService/ReactorHandler.rb', line 258

def dmpmForexternalConfiguration(project_directory_path, externalPomUrl)
  if @@flag
    dmAndPMManagementForExternalConfiguration = DMAndPMManagementForExternalConfiguration.new
    cordinateArr = dmAndPMManagementForExternalConfiguration.getParentCordinates(externalPomUrl, "dependency")
    dmAndPMManagementForExternalConfiguration.comparetheVersionWithParent(project_directory_path, cordinateArr, "dependencies/dependency")
    dmAndPMManagementForExternalConfiguration.comparetheVersionWithParent(project_directory_path, cordinateArr, "dependencyManagement/dependencies/dependency")
    cordinateArr = dmAndPMManagementForExternalConfiguration.getParentCordinates(externalPomUrl, "plugins")
    dmAndPMManagementForExternalConfiguration.comparethePluginVersionWithParent(project_directory_path, cordinateArr, "project/build/plugins/plugin")
    dmAndPMManagementForExternalConfiguration.comaprePluginVersionWihReactorPom(project_directory_path, cordinateArr, "project/build/pluginManagement/plugins/plugin")
  end
end

#fetchGidArtifactIdAndVersionFromChildModule(project_directory_path, retDecision) ⇒ Object

This api will return the list of projects path of individual projects #



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/mavenReactorService/ReactorHandler.rb', line 195

def fetchGidArtifactIdAndVersionFromChildModule(project_directory_path, retDecision)
  alldir = Dir.entries(project_directory_path)
  pomFilePath = Array.new
  alldir.each do |fileLtst|
    if File.directory? fileLtst
      if !(File.basename(fileLtst) == "." || File.basename(fileLtst) == "..")
        innerFileLtst = Dir.entries(fileLtst)
        innerFileLtst.each do |eachInner|
          fileName = File.basename(eachInner)
          if fileName == "pom.xml"
            pomFilePath.push("#{File.expand_path(fileLtst)}/#{fileName}")
          end
        end
      end
    end
  end
  if (retDecision)
    mvnReactorization = MvnReactorization.new
    finalSnapShot = mvnReactorization.findHighestVersionOfPom(pomFilePath)
    fullDetails = mvnReactorization.getDetails(pomFilePath[0])
    fullDetails[2] = finalSnapShot
    return fullDetails
  else
    return pomFilePath
  end
end

#generate_temp_xmlObject

This api generate blank temp.xml inside the individual projects #



311
312
313
314
# File 'lib/mavenReactorService/ReactorHandler.rb', line 311

def generate_temp_xml()
  dest = "temp.xml"
  File.write("temp.xml", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><project>")
end

#getReactPOMReposObject



113
114
115
# File 'lib/mavenReactorService/ReactorHandler.rb', line 113

def getReactPOMRepos()
  return @@allProjectRepos
end

#handleDeveloperAndContributors(project_directory_path) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/mavenReactorService/ReactorHandler.rb', line 167

def handleDeveloperAndContributors(project_directory_path)
  developerAndContributors = DeveloperAndContributors.new
  commonDevelopers = developerAndContributors.findCommonDevelopers(project_directory_path, "developers/developer")
  developersAndContributorsList = developerAndContributors.mergeDeveloperAndContributors(project_directory_path, commonDevelopers, "developers/developer")
  developerAndContributors.addDeveloperAndContributersInReator(project_directory_path, developersAndContributorsList, "developers")
  developerAndContributors.removeCommonDevelopersAndContributorsFromChild(project_directory_path, commonDevelopers, "developers/developer")
  #developerAndContributors.removeEmptyTags(project_directory_path,"developers")

  #Contributors

  commonDevelopers = developerAndContributors.findCommonDevelopers(project_directory_path, "contributors/contributor")
  developersAndContributorsList = developerAndContributors.mergeDeveloperAndContributors(project_directory_path, commonDevelopers, "contributors/contributor")
  developerAndContributors.addDeveloperAndContributersInReator(project_directory_path, developersAndContributorsList, "contributors")
  developerAndContributors.removeCommonDevelopersAndContributorsFromChild(project_directory_path, commonDevelopers, "contributors/contributor")
  #developerAndContributors.removeEmptyTags(project_directory_path,"contributors")

end

#handleInterDependency(project_directory_path) ⇒ Object



162
163
164
165
# File 'lib/mavenReactorService/ReactorHandler.rb', line 162

def handleInterDependency(project_directory_path)
  interDependencyHandler = InterDependencyHandler.new
  interDependencyHandler.handleInterDependecy(project_directory_path)
end

#handleSiteAndEclipsePlugin(project_directory_path) ⇒ Object



117
118
119
120
121
# File 'lib/mavenReactorService/ReactorHandler.rb', line 117

def handleSiteAndEclipsePlugin(project_directory_path)
  siteAndEclipsePluginHandler = SiteAndEclipsePluginHandler.new
  siteAndEclipsePluginHandler.managePlugins(project_directory_path, "org.apache.maven.plugins:maven-site-plugin")
  siteAndEclipsePluginHandler.managePlugins(project_directory_path, "org.apache.maven.plugins:maven-eclipse-plugin")
end

#handleTeamSpecificParent(project_directory_path, externalConfigUrl) ⇒ Object



187
188
189
190
191
192
# File 'lib/mavenReactorService/ReactorHandler.rb', line 187

def handleTeamSpecificParent(project_directory_path, externalPomUrl)
  if ((!externalPomUrl.nil?) and (externalPomUrl.include? "https://" or externalPomUrl.include? "http://"))
    parentPomHandler = ParentPomHandler.new
    parentPomHandler.setParentInreactor(project_directory_path, externalPomUrl)
  end
end

#makeReator(project_directory_path) ⇒ Object



123
124
125
126
# File 'lib/mavenReactorService/ReactorHandler.rb', line 123

def makeReator(project_directory_path)
  mvnReactorization = MvnReactorization.new
  mvnReactorization.makeReactor(project_directory_path)
end

#manageDepedency(project_directory_path) ⇒ Object



128
129
130
131
# File 'lib/mavenReactorService/ReactorHandler.rb', line 128

def manageDepedency(project_directory_path)
  dependencyManagementHandler = DependencyManagementHandler.new
  dependencyManagementHandler.handleDependencyManagement(project_directory_path)
end

#managePlugins(project_directory_path) ⇒ Object



133
134
135
136
137
138
# File 'lib/mavenReactorService/ReactorHandler.rb', line 133

def managePlugins(project_directory_path)
  pluginManagementHandler = PluginManagementHandler.new
  pluginManagementHandler.comparePluginManagementFromChildModule(project_directory_path)
  pluginManagementHandler.handlePluginManagement(project_directory_path)
  pluginManagementHandler.findCommonPluginFromPluginManagement(project_directory_path)
end

#mergeRepository(project_directory_path) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/mavenReactorService/ReactorHandler.rb', line 145

def mergeRepository(project_directory_path)
  mergeRepository = MergeRepository.new
  #Repositories

  commonUrlList = mergeRepository.findCommonRepos(project_directory_path, "repositories/repository")
  rootRepo = mergeRepository.makeFullRepoFromCommonRepo(project_directory_path, "repositories/repository", commonUrlList)
  mergeRepository.addRepoIntoReactorPom(project_directory_path, rootRepo, "repositories")
  mergeRepository.deleteRepoFromChildModule(project_directory_path, "repositories")
  mergeRepository.renameDuplicateRepoId(project_directory_path, "repositories/repository")
  #PluginRepositories

  commonUrlList = mergeRepository.findCommonRepos(project_directory_path, "pluginRepositories/pluginRepository")
  rootRepo = mergeRepository.makeFullRepoFromCommonRepo(project_directory_path, "pluginRepositories/pluginRepository", commonUrlList)
  mergeRepository.addRepoIntoReactorPom(project_directory_path, rootRepo, "pluginRepositories")
  mergeRepository.deleteRepoFromChildModule(project_directory_path, "pluginRepositories")
  mergeRepository.renameDuplicateRepoId(project_directory_path, "pluginRepositories/pluginRepository")
  puts "Renamed repo is done"
end

#moveCommonProperties(project_directory_path, parent_pom_path) ⇒ Object

Adding parent POM path



223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/mavenReactorService/ReactorHandler.rb', line 223

def moveCommonProperties (project_directory_path, parent_pom_path)
  
  if parent_pom_path.to_s.strip.empty?
    moveCommonPropertiesToRootPom = MoveCommonPropertiesToRootPom.new
    commonProperties = moveCommonPropertiesToRootPom.findCommonProperties(project_directory_path)
    moveCommonPropertiesToRootPom.writeCommonPropertiesToReactorPom(project_directory_path,commonProperties)
    moveCommonPropertiesToRootPom.removeCommonPropertiesfromModules(project_directory_path,commonProperties)
  else
    propertyManager = PropertyManager.new
    propertyManager.restructure(project_directory_path, parent_pom_path)
  end
end

#parse_xml_from_file(file_path) ⇒ Object

This api is used to create dom object of pom #



326
327
328
329
330
331
332
333
334
335
336
# File 'lib/mavenReactorService/ReactorHandler.rb', line 326

def parse_xml_from_file(file_path)
  @@xml_file = File.open(file_path)
  Nokogiri::XML(@@xml_file, &:strict)
rescue Errno::ENOENT
  raise Errno::ENOENT,
        "Couldn't locate a file at that path, please check the path!"
rescue Nokogiri::XML::SyntaxError => e
  raise Nokogiri::XML::SyntaxError,
        "The following errors occurred while parsing your XML file: \n" +
        e.message
end

#plugiConfigHandler(project_directory_path) ⇒ Object



140
141
142
143
# File 'lib/mavenReactorService/ReactorHandler.rb', line 140

def plugiConfigHandler(project_directory_path)
  pluginConfigurationHandler = PluginConfigurationHandler.new
  pluginConfigurationHandler.mergePluginConfiguration(project_directory_path, @@projectsWithExternalConfiguration)
end

#project_endObject



316
317
318
319
320
321
322
323
# File 'lib/mavenReactorService/ReactorHandler.rb', line 316

def project_end()
  tempXml = "temp.xml"
  file = File.open(tempXml)
  contents = file.read
  if !contents.include? "</project>"
    File.write(tempXml, "</project>", File.size(tempXml), mode: "a")
  end
end

#removeEmptyTags(project_directory_path) ⇒ Object



241
242
243
244
245
246
247
248
249
# File 'lib/mavenReactorService/ReactorHandler.rb', line 241

def removeEmptyTags(project_directory_path)
  pomPathArr = fetchGidArtifactIdAndVersionFromChildModule(project_directory_path, false)
  mvnReactorization = MvnReactorization.new
  pomPathArr.each do |eachPomPath|
    pom_document = parse_xml_from_file(eachPomPath)
    clean(pom_document.at("project"))
    mvnReactorization.write_nokogiri_to_xml(eachPomPath, pom_document)
  end
end

#removeTagsWithHardCodedPath(project_directory_path) ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/mavenReactorService/ReactorHandler.rb', line 343

def removeTagsWithHardCodedPath(project_directory_path)
  pomList = fetchGidArtifactIdAndVersionFromChildModule(project_directory_path, false)
  projectDirPath = project_directory_path.gsub('\\', "/")
  pomList.each do |eachPomPath|
    pomDocument = parse_xml_from_file(eachPomPath)
    pomDocument.css("project").each do |plugin|
      plugin.xpath("//*[not(child::*)]").each do |eachLeafNode|
        leafNodeText = eachLeafNode.text().gsub('\\', "/")
        if leafNodeText.include?(projectDirPath)
          eachLeafNode.remove
        end
      end
    end
    mvnReactorization = MvnReactorization.new
    mvnReactorization.write_nokogiri_to_xml(eachPomPath, pomDocument)
  end
end

#sortPomExecutorObject



182
183
184
185
# File 'lib/mavenReactorService/ReactorHandler.rb', line 182

def sortPomExecutor()
  pluginManagementHandler = PluginManagementHandler.new
  pluginManagementHandler.executeShortPom()
end

#tag_exists?(tag, pom_nokogiri, data = nil) ⇒ Boolean

This api checks a particular tag present or not and return boolean value #

Returns:

  • (Boolean)


289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/mavenReactorService/ReactorHandler.rb', line 289

def tag_exists?(tag, pom_nokogiri, data = nil)
  temp = pom_nokogiri.dup
  temp.remove_namespaces!
  if data.nil?
    # Searching only for tag when data is not supplied

    !temp.xpath("//#{tag}").empty?
  else
    # Searching for entire the node with given tag and data

    at_parameter = "#{tag}:contains('#{data}')"
    found_match = temp.at(at_parameter)
    # Confirming that whole string matches as contains can

    # report success if even a part of string matches

    !found_match.nil? && found_match.text.strip == data
  end
rescue Nokogiri::XML::XPath::SyntaxError, RuntimeError, NoMethodError, TypeError
  # Rescue is hit only when

  # Supplied tag is either empty or nil or

  # Supplied pom_nokogiri is nil

  raise "Either tag or pom object supplied is empty or nil"
end

#validateProjectsEligiblity(project_directory_path) ⇒ Object



283
284
285
286
# File 'lib/mavenReactorService/ReactorHandler.rb', line 283

def validateProjectsEligiblity(project_directory_path)
  validateProjects = ValidateProjects.new
  validateProjects.checkEligiblityForReactorization(project_directory_path)
end