Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(app): Add actual state for API update file #2364

Merged
merged 1 commit into from
Sep 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 43 additions & 27 deletions app-shell/src/api-update.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @flow
// api updater
import assert from 'assert'
import fse from 'fs-extra'
Expand All @@ -6,55 +7,70 @@ import path from 'path'
import pkg from '../package.json'
import createLogger from './log'

import type {ApiUpdateInfo} from '@opentrons/app/src/shell'

const log = createLogger(__filename)

let updateFiles = []
const DIRECTORY = path.join(__dirname, '../../api/dist')
const EXTENSION = '.whl'
const RE_VERSION = /.+-(\d+\.\d+\.\d+)([ab])?(\d+)?-.+/
const SHORT_LABEL_TO_LABEL = {a: 'alpha', b: 'beta'}

let updateFilename: string
let updateVersion: string

export const AVAILABLE_UPDATE = pkg.version

export function initialize () {
updateFiles = [
// API update
{
id: 'whl',
directory: path.join(__dirname, '../../api/dist'),
ext: '.whl',
},
].map(findUpdateFile)
export function registerApiUpdate () {
initializeUpdateFileInfo()

// API update does not need to handle actions at the moment
return () => {}
}

export function getUpdateFiles () {
if (!updateFiles.length || !updateFiles.every(Boolean)) {
return Promise.reject(new Error('Update files were not all found'))
}
export function getUpdateInfo (): ApiUpdateInfo {
assert(updateFilename && updateVersion, 'Update info was not initialized')

return Promise.all(updateFiles.map(readUpdateFile))
return {filename: path.basename(updateFilename), version: updateVersion}
}

function findUpdateFile (file) {
const {directory, ext} = file
export function getUpdateFileContents (): Promise<Buffer> {
assert(updateFilename && updateVersion, 'Update info was not initialized')

return fse.readFile(updateFilename)
}

function initializeUpdateFileInfo () {
try {
const results = fse.readdirSync(directory)
const files = results.filter(f => f.endsWith(ext))
const results = fse.readdirSync(DIRECTORY)
const files = results.filter(f => f.endsWith(EXTENSION))

assert(
files.length === 1,
`Expected 1 ${ext} file in ${directory}, found ${results.join(' ')}`
`Expected 1 ${EXTENSION} file in ${DIRECTORY}, found ${results.join(' ')}`
)

const updateFile = Object.assign({name: files[0]}, file)
log.info(`Found update file`, updateFile)

return updateFile
log.info(`Found update file`, {filename: files[0]})
updateFilename = path.join(DIRECTORY, files[0])
updateVersion = getVersionFromFilename(updateFilename)
} catch (error) {
log.error('Unable to load API update files', {error})
}

return null
}

function readUpdateFile (file) {
return fse.readFile(path.join(file.directory, file.name))
.then(contents => Object.assign({contents}, file))
function getVersionFromFilename (filename: string): string {
const match = filename.toLowerCase().match(RE_VERSION)

assert(match, `could not parse version from "${filename}"`)

// $FlowFixMe: assert above means `match` exists here
const [baseVersion, shortLabel, labelVersion] = match.slice(1)
const label =
shortLabel && labelVersion
? `-${SHORT_LABEL_TO_LABEL[shortLabel]}.${labelVersion}`
: ''

return `${baseVersion}${label}`
}
39 changes: 20 additions & 19 deletions app-shell/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {app, dialog, ipcMain, Menu} from 'electron'

import createUi from './ui'
import initializeMenu from './menu'
import {initialize as initializeApiUpdate} from './api-update'
import createLogger from './log'
import {getConfig, getStore, getOverrides, registerConfig} from './config'
import {registerApiUpdate} from './api-update'
import {registerDiscovery} from './discovery'
import {registerRobotLogs} from './robot-logs'
import {registerUpdate} from './update'
Expand All @@ -31,36 +31,38 @@ app.on('ready', startUp)

function startUp () {
log.info('Starting App')
process.on('uncaughtException', (error) => log.error('Uncaught: ', {error}))
process.on('uncaughtException', error => log.error('Uncaught: ', {error}))

mainWindow = createUi()
rendererLogger = createRendererLogger()

initializeMenu()
initializeApiUpdate()

// wire modules to UI dispatches
const dispatch = (action) => {
const dispatch = action => {
log.debug('Sending action via IPC to renderer', {action})
mainWindow.webContents.send('dispatch', action)
}

const apiUpdateHandler = registerApiUpdate(dispatch)
const configHandler = registerConfig(dispatch)
const discoveryHandler = registerDiscovery(dispatch)
const robotLogsHandler = registerRobotLogs(dispatch, mainWindow)
const updateHandler = registerUpdate(dispatch)

ipcMain.on('dispatch', (_, action) => {
log.debug('Received action via IPC from renderer', {action})
apiUpdateHandler(action)
configHandler(action)
discoveryHandler(action)
robotLogsHandler(action)
updateHandler(action)
})

if (config.devtools) {
installAndOpenExtensions()
.catch((error) => dialog.showErrorBox('Error opening dev tools', error))
installAndOpenExtensions().catch(error =>
dialog.showErrorBox('Error opening dev tools', error)
)
}

log.silly('Global references', {mainWindow, rendererLogger})
Expand All @@ -79,21 +81,20 @@ function installAndOpenExtensions () {
const devtools = require('electron-devtools-installer')
const forceDownload = !!process.env.UPGRADE_EXTENSIONS
const install = devtools.default
const extensions = [
'REACT_DEVELOPER_TOOLS',
'REDUX_DEVTOOLS',
]

return Promise
.all(extensions.map((name) => install(devtools[name], forceDownload)))
.then(() => mainWindow.webContents.on('context-menu', (_, props) => {
const extensions = ['REACT_DEVELOPER_TOOLS', 'REDUX_DEVTOOLS']

return Promise.all(
extensions.map(name => install(devtools[name], forceDownload))
).then(() =>
mainWindow.webContents.on('context-menu', (_, props) => {
const {x, y} = props

Menu
.buildFromTemplate([{
Menu.buildFromTemplate([
{
label: 'Inspect element',
click: () => mainWindow.inspectElement(x, y),
}])
.popup(mainWindow)
}))
},
]).popup(mainWindow)
})
)
}
Loading