@@ -3,13 +3,15 @@ package org.codeoverflow.chatoverflow.build
3
3
import java .io .File
4
4
import java .util .jar .Manifest
5
5
6
- import com .fasterxml .jackson .databind .ObjectMapper
6
+ import com .fasterxml .jackson .databind .{ JsonNode , ObjectMapper }
7
7
import org .codeoverflow .chatoverflow .build .BuildUtils .withTaskInfo
8
8
import sbt .Keys .Classpath
9
9
import sbt .internal .util .{Attributed , ManagedLogger }
10
10
import sbt .util .{FileFunction , FilesInfo }
11
11
12
+ import scala .collection .mutable .ListBuffer
12
13
import scala .io .Source
14
+ import scala .util .Try
13
15
14
16
class GUIUtility (logger : ManagedLogger ) {
15
17
@@ -99,7 +101,8 @@ class GUIUtility(logger: ManagedLogger) {
99
101
val files = BuildUtils .getAllDirectoryChilds(dir)
100
102
101
103
// 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}" )
103
106
104
107
sbt.IO .jar(jarEntries, getGUIJarFile(guiProjectPath, crossTargetDir), new Manifest ())
105
108
}
@@ -109,27 +112,67 @@ class GUIUtility(logger: ManagedLogger) {
109
112
}
110
113
111
114
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" )
113
116
new File (crossTargetDir, s " chatoverflow-gui- $guiVersion.jar " )
114
117
}
115
118
116
- private def getGUIVersion (guiProjectPath : String ): Option [String ] = {
119
+ private def getPackageJson (guiProjectPath : String ): Option [JsonNode ] = Try {
117
120
val packageJson = new File (s " $guiProjectPath/package.json " )
118
121
if (! packageJson.exists()) {
119
122
logger error " The package.json file of the GUI doesn't exist. Have you cloned the GUI in the correct directory?"
120
123
return None
121
124
}
122
125
123
126
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)
125
128
126
129
content.close()
127
130
131
+ Some (json)
132
+ }.getOrElse(None )
133
+
134
+ private def getGUIVersion (packageJson : JsonNode ): Option [String ] = {
135
+ val version = packageJson.get(" version" ).asText()
136
+
128
137
if (version.isEmpty) {
129
138
logger warn " The GUI version couldn't be loaded from the package.json."
130
139
None
131
140
} else {
132
141
Option (version)
133
142
}
134
143
}
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
+ }
135
178
}
0 commit comments