PageRenderTime 33ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/src/main/scala/de/lecturedoc/tools/Init.scala

https://bitbucket.org/mjacobasch/lecturedoc
Scala | 135 lines | 102 code | 21 blank | 12 comment | 42 complexity | 067c21067452e9ceb6331a5868962348 MD5 | raw file
  1. /*
  2. * (c) 2013 Michael Eichberg et al.
  3. * https://bitbucket.org/delors/lecturedoc
  4. *
  5. * See LICENSE and Actuarius-LICSENSE for license details.
  6. */
  7. package de.lecturedoc
  8. package tools
  9. object Init {
  10. import java.io._
  11. import java.util.jar._
  12. import scala.Console
  13. import java.util.jar.JarInputStream
  14. def main(args: Array[String]): Unit = {
  15. initFolder(args);
  16. }
  17. def initFolder(args: Array[String]): Unit = {
  18. val path = new File(".").getCanonicalPath
  19. var buffer = new Array[Byte](1024)
  20. //Filtering the Jar files for further processing
  21. def accept(file: File): Boolean =
  22. List(".jar").find { ext file.getName.endsWith(ext) } != None
  23. val fn = new File(path)
  24. if (fn.isDirectory) {
  25. val files = fn.listFiles.filter { f accept(f) }
  26. files.foreach { f extractJar(f) }
  27. }
  28. else if (accept(fn))
  29. extractJar(fn)
  30. def extractJar(file: File): Unit = {
  31. val basename = file.getName.substring(0, file.getName.lastIndexOf("."))
  32. val sourceDir = new File(file.getParentFile, "")
  33. val destDir = new File(args(1))
  34. val jar = new JarFile(file)
  35. val enu = jar.entries
  36. println("===> Processing File: "+file)
  37. //Check for Folder Existence
  38. if (new File(destDir+"/Library").isDirectory()) {
  39. print("Folder Already Exists, Do you Want to Replace(R) Update(U) Abort(A) (R/U/A): ")
  40. val conInp = System.console.readLine()
  41. if (conInp == "R" || conInp == "r") {
  42. print("Doing this will delete and replace all the contents of Folder, do you want to continue Yes/No(Abort) (Y/N): ")
  43. val conInp1 = System.console.readLine()
  44. if (conInp1 == "Y" || conInp1 == "y") {
  45. loopJarEntries(false)
  46. }
  47. else {
  48. System.exit(0)
  49. }
  50. }
  51. else if (conInp == "U" || conInp == "u") loopJarEntries(true)
  52. else System.exit(0)
  53. }
  54. else loopJarEntries(false)
  55. //Loop through the main Jar contents
  56. def loopJarEntries(update: Boolean) = {
  57. while (enu.hasMoreElements) {
  58. val entry = enu.nextElement
  59. val name = entry.getName
  60. //Search for inner jar
  61. if (entry.getName.endsWith(".jar") && entry.getName().contains("main")) {
  62. val secJarName = entry.getName().substring(entry.getName().indexOf("/") + 1, entry.getName().length())
  63. val jarIn = new JarInputStream(jar.getInputStream(entry))
  64. loopInnerJar(jarIn, secJarName, update)
  65. if (!update) println("===> Folder has been created/Replaced at destination: "+destDir)
  66. else println("===> Above file(s) have been updated in destination folder")
  67. System.exit(0)
  68. }
  69. }
  70. }
  71. //Loop through the contents of inner jar
  72. def loopInnerJar(jarIn: JarInputStream, secJarName: String, update: Boolean) = {
  73. var jarEntry = jarIn.getNextJarEntry()
  74. while (jarEntry != null) {
  75. val name = jarEntry.getName()
  76. val entryPath =
  77. if (name.startsWith(secJarName)) name.substring(secJarName.length)
  78. else name
  79. if (entryPath.contains("Library")) {
  80. if (jarEntry.isDirectory) {
  81. if (update) {
  82. if (!new File(destDir, entryPath).isDirectory())
  83. new File(destDir, entryPath).mkdirs
  84. }
  85. else new File(destDir, entryPath).mkdirs
  86. }
  87. else {
  88. if (update && new File(destDir, entryPath).exists()) {
  89. var oldFile = new File(destDir, entryPath).lastModified()
  90. var newFile = jarEntry.getTime()
  91. if (newFile > oldFile) {
  92. println("File Updated: "+jarEntry.getName())
  93. copyStream(entryPath, jarIn, buffer)
  94. }
  95. }
  96. else {
  97. copyStream(entryPath, jarIn, buffer)
  98. }
  99. }
  100. }
  101. jarEntry = jarIn.getNextJarEntry()
  102. }
  103. }
  104. //Copying the individual streams
  105. def copyStream(entryPath: String, jarIn: JarInputStream, buffer: Array[Byte]) = {
  106. val ostream = new FileOutputStream(new File(destDir, entryPath))
  107. var len = -1
  108. while ({ len = jarIn.read(buffer, 0, 1024); len != -1 })
  109. ostream.write(buffer, 0, len)
  110. ostream.close
  111. }
  112. }
  113. }
  114. }