Groovy web console

subscribe to the feed Subscribe
to this
site

Link reordering

Published 2 months ago by Alex
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def input = """a1: link1.msi
a1: link1.rpm
a2: link2.tgz
a3: link3.msi
a3: link3.rpm
a4: link4.tgz"""
def lines = input.split(System.lineSeparator())
println lines

def links = [:]
lines.each { String line ->
  def appName = (line.substring(0, line.indexOf(':'))).trim()
  def artifactUrl = (line.substring(line.lastIndexOf(': ') + 1)).trim()
  def artifactType = ''
  if (!links.containsKey(appName)) {
      links[appName] = [:]
  }
  //links[appName].name = appName
  if  (artifactUrl.endsWith('.rpm')) {
      links[appName].rpm = artifactUrl
  } else if (artifactUrl.endsWith('.msi')) {
      links[appName].msi = artifactUrl
  } else if (artifactUrl.endsWith('.tgz')) {
      links[appName].tgz = artifactUrl
  } else {
      links[appName].url = artifactUrl
  }
}
println links
def result = ""
links.each { key, appLinks ->
  result += key + ":"
  appLinks.each { type, link ->
      result += " " + link
  }
  result += System.lineSeparator()
}
println result