Groovy web console

subscribe to the feed Subscribe
to this
site

For Ayoub

Published 3 weeks ago by C3P0
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import java.util.regex.Matcher

String[] getProfiles(String mavenCmd) {
 String[] profiles = []
 Matcher m = (mavenCmd=~ /(^| )-P[ ]*(?<profiles>[^\s]+)/)
 for(matchF in m) {
   profiles += matchF[2].split(',')
 }
 return profiles
}

String cleanProfile(String mavenCmd) {
  return mavenCmd.replaceAll(/(^| )-P[ ]*(?<profiles>[^\s]+)/,"").trim()
}

String addProfile(String mavenCmd, String profile) {
  String[] profiles = getProfiles(mavenCmd)
  if (! profiles.contains(profile)) {
    profiles += profile
  }
  cleanCmd = cleanProfile(mavenCmd)
  return "-P " + profiles.join(',') + " " + cleanCmd
}

String delProfile(String mavenCmd, String profile) {
  String[] profiles = getProfiles(mavenCmd)
  profiles -= profile
  cleanCmd = cleanProfile(mavenCmd)
  return profiles.size() > 0 ? "-P " + profiles.join(',') + " " + cleanCmd : cleanCmd
}

String[] cmds = [
"clean install",
"-P toto clean install",
"-X -P tata -DMAVAR=toto clean install",
"-X -P tata,tototo clean install",
"-X -P tototo,tata clean install",
"-X -P tata -P toto clean install",
"-X -P tata -P tutu,toto clean install",
"-X -P tata,toto -P tutu clean install"
]

String profile="toto"
println "------ CLEANPROFILE --------"
for (it in cmds) { println "$it -> cleanProfile=" + cleanProfile(it) }
println "------ ADDPROFILE --------"
for (it in cmds) { println "$it -> addProfile=" + addProfile(it,profile) }
println "------ DELPROFILE --------"
for (it in cmds) { println "$it -> delProfile=" + delProfile(it,profile) }