@@ -3,13 +3,15 @@ package org.codeoverflow.chatoverflow.build
33import java .io .File
44import java .util .jar .Manifest
55
6- import com .fasterxml .jackson .databind .ObjectMapper
6+ import com .fasterxml .jackson .databind .{ JsonNode , ObjectMapper }
77import org .codeoverflow .chatoverflow .build .BuildUtils .withTaskInfo
88import sbt .Keys .Classpath
99import sbt .internal .util .{Attributed , ManagedLogger }
1010import sbt .util .{FileFunction , FilesInfo }
1111
12+ import scala .collection .mutable .ListBuffer
1213import scala .io .Source
14+ import scala .util .Try
1315
1416class GUIUtility (logger : ManagedLogger ) {
1517
@@ -99,7 +101,8 @@ class GUIUtility(logger: ManagedLogger) {
99101 val files = BuildUtils .getAllDirectoryChilds(dir)
100102
101103 // contains tuples with the actual file as the first value and the name with directory in the jar as the second value
102- val jarEntries = files.map(file => file -> s " /chatoverflow-gui/ ${dir.toURI.relativize(file.toURI).toString}" )
104+ val jarEntries = files.map(file => file -> s " /chatoverflow-gui/ ${dir.toURI.relativize(file.toURI).toString}" ) ++
105+ getVersionFiles(guiProjectPath).map(file => file -> s " / ${file.getName}" )
103106
104107 sbt.IO .jar(jarEntries, getGUIJarFile(guiProjectPath, crossTargetDir), new Manifest ())
105108 }
@@ -109,27 +112,67 @@ class GUIUtility(logger: ManagedLogger) {
109112 }
110113
111114 private def getGUIJarFile (guiProjectPath : String , crossTargetDir : File ): File = {
112- val guiVersion = getGUIVersion (guiProjectPath).getOrElse(" unknown" )
115+ val guiVersion = getPackageJson (guiProjectPath).flatMap(json => getGUIVersion(json) ).getOrElse(" unknown" )
113116 new File (crossTargetDir, s " chatoverflow-gui- $guiVersion.jar " )
114117 }
115118
116- private def getGUIVersion (guiProjectPath : String ): Option [String ] = {
119+ private def getPackageJson (guiProjectPath : String ): Option [JsonNode ] = Try {
117120 val packageJson = new File (s " $guiProjectPath/package.json " )
118121 if (! packageJson.exists()) {
119122 logger error " The package.json file of the GUI doesn't exist. Have you cloned the GUI in the correct directory?"
120123 return None
121124 }
122125
123126 val content = Source .fromFile(packageJson)
124- val version = new ObjectMapper ().reader().readTree(content.mkString).get( " version " ).asText( )
127+ val json = new ObjectMapper ().reader().readTree(content.mkString)
125128
126129 content.close()
127130
131+ Some (json)
132+ }.getOrElse(None )
133+
134+ private def getGUIVersion (packageJson : JsonNode ): Option [String ] = {
135+ val version = packageJson.get(" version" ).asText()
136+
128137 if (version.isEmpty) {
129138 logger warn " The GUI version couldn't be loaded from the package.json."
130139 None
131140 } else {
132141 Option (version)
133142 }
134143 }
144+
145+ private def getRestVersion (packageJson : JsonNode ): Option [String ] = {
146+ val version = packageJson.get(" dependencies" ).get(" @codeoverflow-org/chatoverflow" ).asText()
147+
148+ if (version.isEmpty) {
149+ logger warn " The used REST api version couldn't be loaded from the package.json."
150+ None
151+ } else {
152+ Option (version)
153+ }
154+ }
155+
156+ private def getVersionFiles (guiProjectPath : String ): List [File ] = {
157+ val json = getPackageJson(guiProjectPath)
158+ if (json.isDefined) {
159+ val files = ListBuffer [File ]()
160+ val tempDir = sbt.IO .createTemporaryDirectory
161+
162+ getGUIVersion(json.get).foreach {ver =>
163+ val f = new File (tempDir, " version_gui.txt" )
164+ sbt.IO .write(f, ver)
165+ files += f
166+ }
167+ getRestVersion(json.get).foreach {ver =>
168+ val f = new File (tempDir, " version_gui_rest.txt" )
169+ sbt.IO .write(f, ver)
170+ files += f
171+ }
172+
173+ files.toList
174+ } else {
175+ List ()
176+ }
177+ }
135178}
0 commit comments