This repository has been archived by the owner on Apr 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bootstrap for auto update (#137)
* POC autoupdate * WIP: Bootstrap update UI * Fix PR lint * Refactor * Fix PR lint * Fix run build auto-update config * Update release workflow * Comment out the version check on startup * Fix jacoco test report * Refactoring * Fix PR lint * Update path * Update path * Fix PR lint Co-authored-by: Auto Lint <[email protected]>
- Loading branch information
1 parent
9c4d4b8
commit 746a8b2
Showing
16 changed files
with
256 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
plugins { | ||
id 'insulator.application' | ||
id 'insulator.jpackage' | ||
} | ||
|
||
compileKotlin { kotlinOptions.jvmTarget = "1.8" } | ||
compileTestKotlin { kotlinOptions.jvmTarget = "1.8" } | ||
|
||
jar { manifest { attributes('Main-Class': 'insulator.BootstrapKt') } } | ||
|
||
application { mainClassName ='insulator.BootstrapKt' } | ||
|
||
dependencies { | ||
implementation(group: 'org.update4j', name: 'update4j', version: "1.5.6") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package insulator | ||
|
||
import org.update4j.Archive | ||
import org.update4j.Configuration | ||
import org.update4j.UpdateOptions | ||
import org.update4j.service.UpdateHandler | ||
import java.io.File | ||
import java.io.FileInputStream | ||
import java.io.FileNotFoundException | ||
import java.io.InputStream | ||
import java.io.InputStreamReader | ||
import java.net.SocketTimeoutException | ||
import java.net.URL | ||
import java.net.UnknownHostException | ||
import java.nio.file.Files | ||
import java.nio.file.Paths | ||
import java.nio.file.StandardCopyOption | ||
import javax.swing.JFrame | ||
import javax.swing.JOptionPane | ||
import javax.swing.WindowConstants.DISPOSE_ON_CLOSE | ||
import kotlin.system.exitProcess | ||
|
||
private val view = BootstrapViewManager(JFrame("Bootstrap").apply { defaultCloseOperation = DISPOSE_ON_CLOSE }) | ||
|
||
fun main(args: Array<String>) { | ||
tryLoadLocalConfig() | ||
.let { (left, right) -> left?.let { Result.failure(it) } ?: Result.success(right!!) } | ||
.mapCatching { config -> | ||
if (config.requiresUpdate()) { | ||
val result = config.update(UpdateOptions.archive(updatePath).updateHandler(InsulatorUpdateHandler())) | ||
result.exception?.let { throw it } ?: Archive.read(updatePath).install() | ||
} | ||
config | ||
} | ||
.mapCatching { it.launch() } | ||
.fold({ Unit }, { handleErrors(it) }) | ||
} | ||
|
||
private fun handleErrors(exception: Throwable) { | ||
val errorMessage = when (exception) { | ||
is UnknownHostException -> Triple("Unable to check for updates. Check your internet connection and retry", "Download error", JOptionPane.WARNING_MESSAGE) | ||
is SocketTimeoutException -> Triple("Unable to complete the download. Check your internet connection and retry", "Timeout error", JOptionPane.WARNING_MESSAGE) | ||
is FileNotFoundException -> Triple("Unable to find the remote configuration file.", "Download error", JOptionPane.ERROR_MESSAGE) | ||
else -> Triple("Unexpected error: $exception.", "Unexpected error", JOptionPane.ERROR_MESSAGE) | ||
} | ||
view.showMessageDialog(errorMessage.first, errorMessage.second, errorMessage.third) | ||
exitProcess(-1) | ||
} | ||
|
||
fun saveConfig(stream: InputStream): InputStream { | ||
if (!File(localPath).exists()) File(localPath).mkdirs() | ||
Files.copy( | ||
stream, | ||
Paths.get(localConfigFile), | ||
StandardCopyOption.REPLACE_EXISTING | ||
) | ||
return FileInputStream(localConfigFile) | ||
} | ||
|
||
fun tryLoadLocalConfig(): Pair<Throwable?, Configuration?> = | ||
URL(configPath).runCatching { openConnection().getInputStream() } | ||
.fold( | ||
{ Result.success(it) }, | ||
{ error -> | ||
with(File(localConfigFile)) { | ||
if (exists()) Result.success(inputStream()) | ||
else Result.failure(error) | ||
} | ||
} | ||
) | ||
.mapCatching { stream -> saveConfig(stream) } | ||
.mapCatching { stream -> InputStreamReader(stream).use { Configuration.read(it) } } | ||
.fold({ Pair(null, it) }, { Pair(it, null) }) | ||
|
||
class InsulatorUpdateHandler : UpdateHandler { | ||
override fun startDownloads() = view.showUpdateView() | ||
override fun updateDownloadProgress(frac: Float) = view.updateDownloadProgress(frac) | ||
override fun doneDownloads() = view.closeUpdateView() | ||
} |
24 changes: 24 additions & 0 deletions
24
bootstrap/src/main/kotlin/insulator/BootstrapViewManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package insulator | ||
|
||
import java.awt.event.WindowEvent | ||
import javax.swing.JFrame | ||
import javax.swing.JOptionPane | ||
|
||
class BootstrapViewManager(private val frame: JFrame) { | ||
|
||
private var updateView: UpdateView? = null | ||
|
||
fun updateDownloadProgress(fraction: Float) { | ||
updateView?.updateDownloadProgress(fraction) | ||
} | ||
|
||
fun showMessageDialog(message: String, title: String, iconId: Int) = JOptionPane.showMessageDialog(frame, message, title, iconId) | ||
|
||
fun closeUpdateView() { | ||
frame.dispatchEvent(WindowEvent(frame, WindowEvent.WINDOW_CLOSING)) | ||
} | ||
|
||
fun showUpdateView() { | ||
updateView = UpdateView(frame) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package insulator | ||
|
||
import java.nio.file.Path | ||
|
||
const val appName = "Insulator" | ||
val localPath: String by lazy { | ||
with(System.getProperty("os.name")) { | ||
when { | ||
contains("nux") -> "${System.getProperty("user.home")!!}/.config/$appName/" | ||
contains("win") -> "${System.getenv("LOCALAPPDATA")}/$appName" | ||
else -> "${System.getProperty("user.home")!!}/Library/Application Support/$appName/" | ||
} | ||
} | ||
} | ||
|
||
val localConfigFile = "${localPath}insulator-update.xml" | ||
val updatePath: Path = Path.of(System.getProperty("java.io.tmpdir") ?: "", "update.zip") | ||
const val configPath = "https://github.com/andrea-vinci/Insulator/releases/latest/download/insulator-update.xml" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package insulator | ||
|
||
import java.awt.Component | ||
import java.awt.Dimension | ||
import java.awt.Image | ||
import java.awt.RenderingHints | ||
import java.awt.Toolkit | ||
import java.awt.image.BufferedImage | ||
import javax.imageio.ImageIO | ||
import javax.swing.BorderFactory | ||
import javax.swing.BoxLayout | ||
import javax.swing.ImageIcon | ||
import javax.swing.JFrame | ||
import javax.swing.JLabel | ||
import javax.swing.JPanel | ||
import javax.swing.JProgressBar | ||
import kotlin.math.floor | ||
|
||
class UpdateView(private val frame: JFrame) { | ||
private val progressBar = JProgressBar().apply { isStringPainted = true } | ||
|
||
init { | ||
with(frame) { | ||
add( | ||
JPanel().apply { | ||
add(insulatorIcon()) | ||
add(progressBar) | ||
|
||
layout = BoxLayout(this, BoxLayout.PAGE_AXIS) | ||
border = BorderFactory.createEmptyBorder(10, 20, 20, 20) | ||
} | ||
) | ||
fixSize(300, 130) | ||
center() | ||
isVisible = true | ||
} | ||
} | ||
|
||
fun updateDownloadProgress(frac: Float) = | ||
with(progressBar) { | ||
value = floor(frac.toDouble() * 100).toInt() | ||
if (value >= 98) frame.isVisible = false | ||
} | ||
|
||
private fun insulatorIcon() = | ||
ImageIO.read(this.javaClass.getResource("/icon.png")) | ||
.let { JLabel(ImageIcon(getScaledImage(it, 60, 60))).apply { text = "Insulator auto-update" } } | ||
.also { it.alignmentX = Component.CENTER_ALIGNMENT } | ||
|
||
private fun getScaledImage(srcImg: Image, width: Int, height: Int) = | ||
BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB).apply { | ||
with(createGraphics()) { | ||
setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR) | ||
drawImage(srcImg, 0, 0, width, height, null) | ||
dispose() | ||
} | ||
} | ||
|
||
private fun JFrame.center() = apply { | ||
val screen = Toolkit.getDefaultToolkit().screenSize | ||
setLocation(screen.width / 2 - size.width / 2, screen.height / 2 - size.height / 2) | ||
} | ||
|
||
private fun JFrame.fixSize(width: Int, height: Int) = this.apply { | ||
setSize(width, height) | ||
maximumSize = Dimension(width, height) | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
plugins{ | ||
id 'org.jetbrains.kotlin.jvm' | ||
id 'org.jetbrains.kotlin.kapt' | ||
} | ||
|
||
repositories { | ||
jcenter() | ||
mavenCentral() | ||
maven { url "https://plugins.gradle.org/m2/" } | ||
maven { url "https://dl.bintray.com/arrow-kt/arrow-kt/" } | ||
maven { url "https://packages.confluent.io/maven/" } | ||
maven { url "https://kotlin.bintray.com/kotlinx/" } | ||
maven { url "https://repository.mulesoft.org/nexus/content/repositories/public/" } | ||
} | ||
|
||
compileKotlin { kotlinOptions.jvmTarget = "1.8" } | ||
compileTestKotlin { kotlinOptions.jvmTarget = "1.8" } | ||
|
||
dependencies { | ||
// Kotlin | ||
implementation platform('org.jetbrains.kotlin:kotlin-bom') | ||
implementation(group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib-jdk8') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.