Skip to content

Commit

Permalink
fix: use auto-updater
Browse files Browse the repository at this point in the history
  • Loading branch information
gabaldon committed Mar 6, 2024
1 parent ab91164 commit 16611b8
Show file tree
Hide file tree
Showing 18 changed files with 7,286 additions and 12,656 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ jobs:
- name: Create Draft
uses: ncipollo/release-action@v1
with:
artifacts: "release/**/*.exe,release/**/*.AppImage,release/**/*.dmg,release/**/*.snap"
artifacts: "release/**/*.exe,release/**/*.AppImage,release/**/*.dmg,release/**/*.snap,release/**/*.zip,release/**/*.yml"
draft: true
2 changes: 1 addition & 1 deletion electron-builder.json5
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"mac": {
"artifactName": "${productName}_${version}.${ext}",
"target": [
"dmg"
"default"
]
},
"win": {
Expand Down
78 changes: 78 additions & 0 deletions electron/autoUpdaterManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { autoUpdater } from 'electron-updater'
import { BrowserWindow, MessageBoxOptions, dialog } from 'electron'
import { WalletManager } from './walletManager'
import { Actions } from './main'
import { IPC_ACTIONS } from './ipc/ipcActions'
const { SET_MESSAGE } = IPC_ACTIONS.Window

export class AutoUpdaterManager {
public isBeingUpdated: boolean = false
public wallet
public win

constructor(walletManager: WalletManager, win: BrowserWindow) {
this.wallet = walletManager
this.win = win
}

public run(actions: Actions) {
console.log('running auto updater...')
autoUpdater.checkForUpdatesAndNotify()
this.win.webContents.send(SET_MESSAGE, 'AUTO UPDATER BEFORE CHECK')
this.win.webContents.send(
SET_MESSAGE,
autoUpdater.currentVersion,
autoUpdater.getFeedURL(),
)
autoUpdater.on('update-available', () => {
this.win.webContents.send(SET_MESSAGE, 'Update available')
this.showDialog()
})
autoUpdater.on('error', err => {
this.win.webContents.send(SET_MESSAGE, 'Error in auto-updater. ' + err)
console.log('Error in auto-updater. ' + err)
})
autoUpdater.on('update-downloaded', () => {
this.closeWindowAndRestart(actions)
})

autoUpdater.autoDownload = false
}

showDialog() {
this.win.webContents.send(SET_MESSAGE, 'update available')
const options = {
type: 'info',
title: 'DOClever',
message: `There is a new Sheikah version`,
buttons: ['Download and install', 'Cancel'],
defaultId: 0, // bound to buttons array
cancelId: 1, // bound to buttons array
} as MessageBoxOptions
this.wallet.setIsUpdating(true)
dialog.showMessageBox(this.win, options).then(result => {
if (result.response === 0) {
autoUpdater
.downloadUpdate()
.then(path => {
console.log('Release path to download', path)
})
.catch(e => {
console.log('Error', e)
})
} else if (result.response === 1) {
this.wallet.setIsUpdating(false)
this.wallet.runWallet()
}
})
}

private closeWindowAndRestart(actions: Actions) {
this.win.webContents.send(SET_MESSAGE, 'close window and restart')
actions.killWalletProcess()
autoUpdater.quitAndInstall(false, true)
if (this.win == null) {
actions.relaunch()
}
}
}
1 change: 1 addition & 0 deletions electron/ipc/ipcActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const IPC_ACTIONS = {
Window: {
SET_MESSAGE: 'SET_MESSAGE',
SHUTDOWN: 'SHUTDOWN',
SHUTDOWN_FINISHED: 'SHUTDOWN_FINISHED',
SET_RUNNING_STATUS: 'SET_RUNNING_STATUS',
SET_DOWNLOADED_STATUS: 'SET_DOWNLOADED_STATUS',
SET_DOWNLOADING_STATUS: 'SET_DOWNLOADING_STATUS',
Expand Down
88 changes: 59 additions & 29 deletions electron/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { fileURLToPath } from 'node:url'
import { join, dirname } from 'node:path'
import { release } from 'os'
import { app, BrowserWindow, shell, ipcMain, Menu } from 'electron'
import kill from 'tree-kill'
import { WalletManager } from '../walletManager'
import { IPC_ACTIONS } from '../ipc/ipcActions'
import { AutoUpdaterManager } from '../autoUpdaterManager'

const { SHUTDOWN } = IPC_ACTIONS.Window
const { SHUTDOWN, SET_MESSAGE, SHUTDOWN_FINISHED } = IPC_ACTIONS.Window

globalThis.__filename = fileURLToPath(import.meta.url)
globalThis.__dirname = dirname(__filename)
Expand Down Expand Up @@ -44,6 +44,7 @@ if (!app.requestSingleInstanceLock()) {

let win: BrowserWindow | null = null
let walletPid

Check warning on line 46 in electron/main/index.ts

View workflow job for this annotation

GitHub Actions / lint_and_test

'walletPid' is assigned a value but never used
let walletManager: WalletManager
// Here, you can also use other preload
const preload = join(__dirname, '../preload/index.mjs')
const url = process.env.VITE_DEV_SERVER_URL
Expand All @@ -63,32 +64,35 @@ async function createWindow() {
autoHideMenuBar: true,
})

new WalletManager(win?.webContents).run(actions)
walletManager = new WalletManager(win?.webContents)
walletManager.run(actions)
win?.webContents.send(SET_MESSAGE)

if (!process.env.VITE_DEV_SERVER_URL) {
// Hide electron toolbar in production environment
// win.setMenuBarVisibility(false)
// const menu = Menu.buildFromTemplate([
// {
// label: 'Menu',
// submenu: [
// {
// label: 'Quit',
// accelerator: 'CmdOrCtrl+Q',
// click: () => {
// win.webContents.send(SHUTDOWN)
// },
// },
// { label: 'Reload', accelerator: 'CmdOrCtrl+R', click: () => {} },
// { label: 'ZoomOut', accelerator: 'CmdOrCtrl+-', click: () => {} },
// { label: 'ZoomIn', accelerator: 'CmdOrCtrl+Plus', click: () => {} },
// { label: 'Cut', accelerator: 'CmdOrCtrl+X', role: 'cut' },
// { label: 'Copy', accelerator: 'CmdOrCtrl+C', role: 'copy' },
// { label: 'Paste', accelerator: 'CmdOrCtrl+V', role: 'paste' },
// ],
// },
// ])
// Menu.setApplicationMenu(menu)
win.webContents.openDevTools()
const menu = Menu.buildFromTemplate([
{
label: 'Menu',
submenu: [
{
label: 'Quit',
accelerator: 'CmdOrCtrl+Q',
click: () => {
win.webContents.send(SHUTDOWN)
},
},
{ label: 'Reload', accelerator: 'CmdOrCtrl+R', click: () => {} },
{ label: 'ZoomOut', accelerator: 'CmdOrCtrl+-', click: () => {} },
{ label: 'ZoomIn', accelerator: 'CmdOrCtrl+Plus', click: () => {} },
{ label: 'Cut', accelerator: 'CmdOrCtrl+X', role: 'cut' },
{ label: 'Copy', accelerator: 'CmdOrCtrl+C', role: 'copy' },
{ label: 'Paste', accelerator: 'CmdOrCtrl+V', role: 'paste' },
],
},
])
Menu.setApplicationMenu(menu)
}

if (app.isPackaged) {
Expand All @@ -99,6 +103,10 @@ async function createWindow() {
win.webContents.openDevTools()
}

win.once('ready-to-show', () => {
new AutoUpdaterManager(walletManager, win).run(actions)
})

// Test actively push message to the Electron-Renderer
win.webContents.on('did-finish-load', () => {
win?.webContents.setZoomFactor(1)
Expand All @@ -121,12 +129,18 @@ function setWalletPid(pid: number) {

export type Actions = {
closeWindow: () => unknown
relaunch: () => unknown
setWalletPid: (pid: number) => unknown
quitApp: () => unknown
killWalletProcess: () => unknown
}

const actions: Actions = {
relaunch: relaunch,
closeWindow: closeWindow,
setWalletPid: setWalletPid,
quitApp: quitApp,
killWalletProcess: killWalletProcess,
}

app.whenReady().then(() => {
Expand All @@ -152,6 +166,11 @@ app.on('second-instance', () => {
}
})

ipcMain.on(SHUTDOWN_FINISHED, () => {
actions.killWalletProcess()
actions.quitApp()
})

app.on('activate', () => {
const allWindows = BrowserWindow.getAllWindows()
if (allWindows.length) {
Expand All @@ -172,11 +191,8 @@ ipcMain.handle('open-win', (event, arg) => {
})

ipcMain.on('shutdown-finished', () => {
win?.hide()
if (walletPid) {
kill(walletPid)
}
app.exit()
actions.killWalletProcess()
actions.quitApp()
})

if (app.isPackaged) {
Expand All @@ -192,6 +208,20 @@ function closeWindow() {
win.close()
}

function killWalletProcess() {
if (walletManager.walletProcess) {
walletManager.walletProcess.kill(9)
}
}

function quitApp() {
app.quit()
}

function relaunch() {
app.relaunch()
}

win?.on('close', closeApp)

function closeApp(event: Event) {
Expand Down
4 changes: 4 additions & 0 deletions electron/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
SET_DOWNLOAD_PROGRESS,
SET_LOADED_STATUS,
SET_OS_NOT_SUPPORTED,
SHUTDOWN_FINISHED,
} = IPC_ACTIONS.Window

// --------- Expose some API to the Renderer process ---------
Expand Down Expand Up @@ -154,4 +155,7 @@ contextBridge.exposeInMainWorld('ipcAPI', {
onOSNotSupported: (fn: any) => {
ipcRenderer.on(SET_OS_NOT_SUPPORTED, (event, ...args) => fn(...args))
},
sendShutdownFinished: () => {
ipcRenderer.send(SHUTDOWN_FINISHED)
},
})
1 change: 0 additions & 1 deletion electron/walletManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
WITNET_RUST_VERSION,
WITNET_CONFIG_FILE_NAME,
WALLET_COMPRESS_FILE_NAME,
Status,
URLS_PUBLIC_WITNET_NODES,
DEFAULT_WALLET_LOG_LEVEL,
ARCH,
Expand Down
Loading

0 comments on commit 16611b8

Please sign in to comment.