@@ -2,11 +2,12 @@ package dartzee.utils
22
33import dartzee.core.bean.LinkLabel
44import dartzee.core.util.DialogUtil
5- import dartzee.logging.CODE_BATCH_ERROR
5+ import dartzee.core.util.FileUtil
66import dartzee.logging.CODE_PARSE_ERROR
77import dartzee.logging.CODE_UPDATE_CHECK
88import dartzee.logging.CODE_UPDATE_CHECK_RESULT
99import dartzee.logging.CODE_UPDATE_ERROR
10+ import dartzee.logging.CODE_UPDATE_STARTING
1011import dartzee.logging.KEY_RESPONSE_BODY
1112import dartzee.`object`.DartsClient
1213import dartzee.utils.InjectedThings.logger
@@ -15,6 +16,7 @@ import java.io.File
1516import javax.swing.JLabel
1617import javax.swing.JOptionPane
1718import javax.swing.JPanel
19+ import kong.unirest.MimeTypes
1820import kong.unirest.Unirest
1921import kong.unirest.json.JSONObject
2022
@@ -24,6 +26,8 @@ import kong.unirest.json.JSONObject
2426 * https://developer.github.com/v3/repos/releases/#get-the-latest-release
2527 */
2628object UpdateManager {
29+ private val jarDirectory = System .getProperty(" user.dir" )
30+
2731 fun checkForUpdates (currentVersion : String ) {
2832 // Show this here, checking the CRC can take time
2933 logger.info(CODE_UPDATE_CHECK , " Checking for updates - my version is $currentVersion " )
@@ -36,7 +40,7 @@ object UpdateManager {
3640 return
3741 }
3842
39- startUpdate( metadata.getArgs() , Runtime .getRuntime())
43+ doUpdate( DARTZEE_REPOSITORY_URL , metadata, Runtime .getRuntime())
4044 }
4145
4246 fun queryLatestReleaseJson (repositoryUrl : String ): JSONObject ? {
@@ -74,11 +78,6 @@ object UpdateManager {
7478 // An update is available
7579 logger.info(CODE_UPDATE_CHECK_RESULT , " Newer release available - $newVersion " )
7680
77- if (! DartsClient .isWindowsOs()) {
78- showManualDownloadMessage(newVersion)
79- return false
80- }
81-
8281 val answer =
8382 DialogUtil .showQuestion(
8483 " An update is available (${metadata.version} ). Would you like to download it now?" ,
@@ -88,17 +87,18 @@ object UpdateManager {
8887 }
8988
9089 private fun showManualDownloadMessage (newVersion : String ) {
90+ DialogUtil .dismissLoadingDialog()
91+
9192 val fullUrl = " $DARTZEE_MANUAL_DOWNLOAD_URL /tag/$newVersion "
9293 val panel = JPanel ()
9394 panel.layout = BorderLayout (0 , 0 )
94- val lblOne =
95- JLabel (" An update is available ($newVersion ). You can download it manually from:" )
95+ val lblOne = JLabel (" Failed to download $newVersion . You can download it manually from:" )
9696 val linkLabel = LinkLabel (fullUrl) { launchUrl(fullUrl) }
9797
9898 panel.add(lblOne, BorderLayout .NORTH )
9999 panel.add(linkLabel, BorderLayout .SOUTH )
100100
101- DialogUtil .showCustomMessage (panel)
101+ DialogUtil .showCustomError (panel)
102102 }
103103
104104 fun parseUpdateMetadata (responseJson : JSONObject ): UpdateMetadata ? {
@@ -109,8 +109,7 @@ object UpdateManager {
109109
110110 val assetId = asset.getLong(" id" )
111111 val fileName = asset.getString(" name" )
112- val size = asset.getLong(" size" )
113- UpdateMetadata (remoteVersion, assetId, fileName, size)
112+ UpdateMetadata (remoteVersion, assetId, fileName)
114113 } catch (t: Throwable ) {
115114 logger.error(
116115 CODE_PARSE_ERROR ,
@@ -122,38 +121,71 @@ object UpdateManager {
122121 }
123122 }
124123
125- fun startUpdate (args : String , runtime : Runtime ) {
126- prepareBatchFile()
124+ fun doUpdate (repositoryUrl : String , metadata : UpdateMetadata , runtime : Runtime ) {
125+ if (! downloadJar(repositoryUrl, metadata)) {
126+ return
127+ }
127128
128- try {
129- runtime.exec( " cmd /c start update.bat $args " )
130- } catch (t : Throwable ) {
131- logger.error( CODE_BATCH_ERROR , " Error running update.bat " , t )
132- val manualCommand = " update.bat $args "
129+ if ( DartsClient .isWindowsOs()) {
130+ prepareUpdateFile( " update.bat" )
131+ } else if ( DartsClient .isLinux() ) {
132+ prepareUpdateFile( " update.sh " )
133+ }
133134
134- val msg =
135- " Failed to launch update.bat - call the following manually to perform the update: \n\n $manualCommand "
136- DialogUtil .showError(msg)
137- return
135+ val success =
136+ runCommand(
137+ windows =
138+ arrayOf(" cmd" , " /c" , " start" , " $jarDirectory /update.bat" , metadata.fileName),
139+ linux = arrayOf(" sh" , " $jarDirectory /update.sh" , metadata.fileName),
140+ runtime,
141+ )
142+
143+ if (! success) {
144+ DialogUtil .showError(
145+ " Failed to swap in updated file. \n\n Delete the old Dartzee.jar and rename ${metadata.fileName} -> Dartzee.jar"
146+ )
138147 }
139148
140149 InjectedThings .exiter.exit(0 )
141150 }
142151
143- fun prepareBatchFile () {
144- val updateFile = File (" update.bat" )
152+ fun downloadJar (repositoryUrl : String , metadata : UpdateMetadata ): Boolean =
153+ try {
154+ val downloadPath = " $jarDirectory /${metadata.fileName} "
155+ FileUtil .deleteFileIfExists(downloadPath)
156+ val downloadUrl = " $repositoryUrl /releases/assets/${metadata.assetId} "
157+ logger.info(CODE_UPDATE_STARTING , " Downloading from $downloadUrl to $downloadPath " )
158+
159+ DialogUtil .showLoadingDialog(" Downloading ${metadata.version} ..." )
160+ val response = Unirest .get(downloadUrl).accept(MimeTypes .EXE ).asFile(downloadPath)
161+ if (response.status != 200 ) {
162+ logger.error(
163+ CODE_UPDATE_ERROR ,
164+ " Received non-success HTTP status: ${response.status} - ${response.statusText} " ,
165+ KEY_RESPONSE_BODY to response.body,
166+ )
167+
168+ showManualDownloadMessage(metadata.version)
169+ false
170+ } else {
171+ true
172+ }
173+ } catch (e: Exception ) {
174+ logger.error(CODE_UPDATE_ERROR , " Caught $e during download" , e)
175+ showManualDownloadMessage(metadata.version)
176+ false
177+ } finally {
178+ DialogUtil .dismissLoadingDialog()
179+ }
180+
181+ fun prepareUpdateFile (filename : String ) {
182+ val filePath = " $jarDirectory /$filename "
183+ val updateFile = File (filePath)
145184
146185 updateFile.delete()
147- val updateScript = javaClass.getResource(" /update/update.bat " ).readText()
186+ val updateScript = javaClass.getResource(" /update/$filename " ).readText()
148187 updateFile.writeText(updateScript)
149188 }
150189}
151190
152- data class UpdateMetadata (
153- val version : String ,
154- val assetId : Long ,
155- val fileName : String ,
156- val size : Long ,
157- ) {
158- fun getArgs () = " $size $version $fileName $assetId "
159- }
191+ data class UpdateMetadata (val version : String , val assetId : Long , val fileName : String )
0 commit comments