Skip to content

Commit

Permalink
Merge pull request #29 from KELiON/feature/autoupdate
Browse files Browse the repository at this point in the history
Updates
  • Loading branch information
KELiON authored Jan 24, 2017
2 parents e1ff0e1 + 3cdc97e commit bf70ce9
Show file tree
Hide file tree
Showing 17 changed files with 192 additions and 17 deletions.
29 changes: 23 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
before_install:
- npm install -g npm@3.10.8
sudo: required
dist: trusty

sudo: false
language: node_js
node_js:
- "6"

matrix:
include:
- os: linux
node_js: 6
env: CC=clang CXX=clang++ npm_config_clang=1
compiler: clang

addons:
apt:
packages:
- libgnome-keyring-dev
- libicns
- graphicsmagick
- xz-utils
- rpm
- bsdtar

before_install:
- npm install -g npm@3.10.8

cache:
directories:
Expand All @@ -30,4 +47,4 @@ before_script:
script:
- npm run lint
- npm run test
- npm run build
- npm run package
22 changes: 22 additions & 0 deletions app/AppUpdater.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as os from "os";
import { dialog, } from 'electron'
import { autoUpdater } from "electron-updater";

const event = 'update-downloaded'

export default class AppUpdater {
constructor(w) {
if (process.env.NODE_ENV === 'development' || os.platform() === "linux") {
return
}

autoUpdater.on(event, (payload) => {
w.webContents.send('message', {
message: event,
payload
})
})

autoUpdater.checkForUpdates()
}
}
6 changes: 5 additions & 1 deletion app/main.development.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import createMainWindow from './main/createWindow'
import createBackgroundWindow from './background/createWindow'
import config from './lib/config'
import AppTray from './main/createWindow/AppTray'
import AppUpdater from './AppUpdater'

const trayIconSrc = `${__dirname}/tray_iconTemplate@2x.png`
const isDev = () => (
Expand All @@ -12,6 +13,7 @@ const isDev = () => (
let mainWindow
let backgroundWindow
let tray
let appUpdater

if (process.env.NODE_ENV !== 'development') {
// Set up crash reporter before creating windows in production builds
Expand All @@ -38,14 +40,16 @@ app.on('ready', () => {
src: trayIconSrc,
isDev: isDev(),
mainWindow,
backgroundWindow
backgroundWindow,
})

// Show tray icon if it is set in configuration
if (config.get('showInTray')) {
tray.show()
}

appUpdater = new AppUpdater(mainWindow)

app.dock && app.dock.hide()
})

Expand Down
7 changes: 6 additions & 1 deletion app/main/createWindow/AppTray.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Menu, Tray, app } from 'electron'
import showWindowWithTerm from './showWindowWithTerm'
import toggleWindow from './toggleWindow'
import checkForUpdates from './checkForUpdates'

/**
* Class that controls state of icon in menu bar
Expand Down Expand Up @@ -44,10 +45,14 @@ export default class AppTray {
label: 'Plugins',
click: () => showWindowWithTerm(mainWindow, 'plugins'),
},
separator,
{
label: 'Preferences...',
click: () => showWindowWithTerm(mainWindow, 'settings'),
},
separator,
{
label: 'Check for updates',
click: () => checkForUpdates(),
}
]

Expand Down
80 changes: 80 additions & 0 deletions app/main/createWindow/checkForUpdates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { dialog, app, shell } from 'electron'
import os from 'os'
import semver from 'semver'
import https from 'https'

const currentVersion = app.getVersion()
const DEFAULT_DOWNLOAD_URL = 'https://github.com/KELiON/cerebro/releases'

const TITLE = 'Cerebro Updates'

const PLATFORM_EXTENSIONS = {
darwin: 'dmg',
linux: 'AppImage',
win32: 'exe'
}
const platform = os.platform()
const installerExtension = PLATFORM_EXTENSIONS[platform]

const findInstaller = (assets) => {
if (!installerExtension) {
return DEFAULT_DOWNLOAD_URL
}
const regexp = new RegExp(`\.${installerExtension}$`)
const downloadUrl = assets
.map(a => a.browser_download_url)
.find(url => url.match(regexp))
return downloadUrl || DEFAULT_DOWNLOAD_URL
}

const getLatestRelease = () => (
new Promise((resolve, reject) => {
const opts = {
host: 'api.github.com',
path: '/repos/KELiON/cerebro/releases',
headers: {
'User-Agent': `CerebroApp v${currentVersion}`
}
}
https.get(opts, res => {
let json = ''
res.on('data', (chunk) => {
json += chunk
})
res.on('end', () => resolve(JSON.parse(json)[0]))
}).on('error', () => reject())
})
)

export default () => {
getLatestRelease().then(release => {
const version = semver.valid(release.tag_name)
if (version && semver.gt(version, currentVersion)) {
dialog.showMessageBox({
buttons: ['Skip', 'Download'],
defaultId: 1,
cancelId: 0,
title: TITLE,
message: `New version available: ${version}`,
detail: 'Click download to get it now',
}, (response) => {
if (response === 1) {
const url = findInstaller(release.assets)
shell.openExternal(url)
}
})
} else {
dialog.showMessageBox({
title: TITLE,
message: `Your are using latest version of Cerebro (${currentVersion})`,
buttons: []
})
}
}).catch(err => {
console.log('Catch error!', err)
dialog.showErrorBox(
TITLE,
'Error fetching latest version',
)
})
}
7 changes: 7 additions & 0 deletions app/main/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ on('showTerm', (term) => {
store.dispatch(updateTerm(term));
});

on('update-downloaded', (payload) => {
new Notification('Cerebro: update is ready to install', {
body: 'New version is downloaded and will be automatically installed on quit'
})
});


// Handle `updateTheme` rpc event and change current theme
on('updateTheme', changeTheme);

Expand Down
2 changes: 1 addition & 1 deletion app/main/plugins/core/cerebro/plugins/Preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default class Preview extends Component {
installedVersion,
isUpdateAvailable
} = this.props
const match = repo.match(/^.+github.com\/([^\/]+\/[^\/]+).*?/)
const match = repo && repo.match(/^.+github.com\/([^\/]+\/[^\/]+).*?/)
return (
<div className={styles.preview} key={name}>
<h2>{name} ({version})</h2>
Expand Down
1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"rebuild": "npm rebuild --runtime=electron --target=1.4.12 --disturl=https://atom.io/download/electron --abi=50"
},
"dependencies": {
"electron-updater": "^1.1.1",
"nodobjc": "^2.1.0",
"rmdir": "^1.2.0",
"semver": "^5.3.0",
Expand Down
29 changes: 29 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# https://github.com/sindresorhus/appveyor-node/blob/master/appveyor.yml

environment:
matrix:
- platform: x64

image: Visual Studio 2015

init:
- npm config set msvs_version 2015 # we need this to build `pty.js`

install:
- ps: Install-Product node 6 x64
- set CI=true
- npm install -g npm@3.10.8
- npm install

build: off

shallow_clone: true

test_script:
- node --version
- npm --version
- npm run lint
- npm run test

on_success:
- npm run package
Binary file added build/icons/1024x1024.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added build/icons/128x128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added build/icons/16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added build/icons/256x256.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added build/icons/32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added build/icons/48x48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added build/icons/512x512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 18 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"start": "cross-env NODE_ENV=production electron ./app",
"start-hot": "cross-env HOT=1 NODE_ENV=development ./node_modules/.bin/electron -r babel-register ./app/main.development",
"package": "npm run build && build --publish never",
"release": "build -mwl --draft",
"dev": "concurrently --kill-others \"npm run hot-server\" \"npm run start-hot\"",
"postinstall": "concurrently \"node node_modules/fbjs-scripts/node/check-dev-engines.js package.json\""
},
Expand All @@ -24,10 +25,16 @@
"homepage": "https://cerebroapp.com",
"appId": "com.cerebroapp.Cerebro",
"category": "public.app-category.productivity",
"protocols" : {
"protocols": {
"name": "Cerebro URLs",
"role": "Viewer",
"schemes": ["cerebro"]
"schemes": [
"cerebro"
]
},
"directories": {
"app": "./app",
"output": "release"
},
"linux": {
"arch": [
Expand Down Expand Up @@ -75,11 +82,14 @@
"!**/node_modules/.bin",
"!**/*.{o,hprof,orig,pyc,pyo,rbc}",
"!**/{.DS_Store,.git,.hg,.svn,CVS,RCS,SCCS,__pycache__,thumbs.db,.gitignore,.gitattributes,.editorconfig,.flowconfig,.yarn-metadata.json,.idea,appveyor.yml,.travis.yml,circle.yml,npm-debug.log,.nyc_output,yarn.lock,.yarn-integrity}"
]
},
"directories": {
"app": "./app",
"output": "release"
],
"squirrelWindows": {
"iconUrl": "https://raw.githubusercontent.com/KELiON/cerebro/master/build/icon.ico"
},
"publish": {
"provider": "github",
"vPrefixedTagName": false
}
},
"bin": {
"electron": "./node_modules/.bin/electron"
Expand Down Expand Up @@ -136,7 +146,7 @@
"css-loader": "0.23.1",
"del": "2.2.0",
"devtron": "1.2.0",
"electron-builder": "10.8.1",
"electron-builder": "11.5.1",
"electron-prebuilt": "1.4.12",
"electron-rebuild": "1.4.0",
"eslint": "2.10.2",
Expand Down

0 comments on commit bf70ce9

Please sign in to comment.