更新
更旧
/**
*
* Copyright (c) 2020 Silicon Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const env = require('../util/env.js')
const util = require('../util/util.js')
const queryConfig = require('../db/query-config.js')
const queryGeneric = require('../db/query-generic.js')
const querySession = require('../db/query-session.js')
const exportJs = require('../importexport/export.js')
const preference = require('./preference.js')
const generationEngine = require('../generator/generation-engine.js')
const queryPackage = require('../db/query-package.js')
const dbEnum = require('../../src-shared/db-enum.js')
var httpPort
const template = [
{
role: 'fileMenu',
submenu: [
{
label: 'Generate Code',
},
{
label: 'Handlebar Template Directory',
click(menuItem, browserWindow, event) {
{
label: 'Preferences...',
click(menuItem, browserWindow, event) {

Timotej Ecimovic
已提交
let cookieText = ''
util
.getSessionKeyFromBrowserWindow(browserWindow)
.then((sessionKey) => {
console.log(`Got session key: ${sessionKey}`)
cookieText = sessionKey

Timotej Ecimovic
已提交
})
.then((sessionKey) =>
querySession.getSessionInfoFromSessionKey(
env.mainDatabase(),
sessionKey
)

Timotej Ecimovic
已提交
)
.then((row) => {
dialog.showMessageBox(browserWindow, {
title: 'Information',
message: `Zap session id: ${
}\nWinID Session key: ${row.sessionKey}\nTime: ${new Date(
)}\nCookie session key: ${cookieText}`,
.catch((err) => uiJs.showErrorMessage('Session info', err))
click(menuItem, browserWindow, event) {
doSave(menuItem, browserWindow, event)
},
{
label: 'Help',
submenu: [
{
role: 'about',
},
],
},
/**
* Perform a file->open operation.
*
* @param {*} menuItem
* @param {*} browserWindow
* @param {*} event
*/
function doOpen(menuItem, browserWindow, event) {
.selectFileLocation(env.mainDatabase(), dbEnum.fileLocationCategory.save)
properties: ['openFile', 'multiSelections'],
}
if (filePath != null) {
opts.defaultPath = filePath
}
return dialog.showOpenDialog(opts)
})
fileOpen(env.mainDatabase(), result.filePaths)
.catch((err) => uiJs.showErrorMessage('Open file', err))
/**
* Perform a save, defering to save as if file is not yet selected.
*
* @param {*} menuItem
* @param {*} browserWindow
* @param {*} event
*/
function doSave(menuItem, browserWindow, event) {
util
.getSessionKeyFromBrowserWindow(browserWindow)
.then((sessionKey) =>
querySession.getSessionInfoFromSessionKey(env.mainDatabase(), sessionKey)
)
queryConfig.getSessionKeyValue(
env.mainDatabase(),
row.sessionId,
'filePath'
)
if (filePath == null) {
doSaveAs(menuItem, browserWindow, event)
} else {
return fileSave(env.mainDatabase(), browserWindow, filePath)
}
/**
* Perform save as.
*
* @param {*} menuItem
* @param {*} browserWindow
* @param {*} event
*/
function doSaveAs(menuItem, browserWindow, event) {
.selectFileLocation(env.mainDatabase(), dbEnum.fileLocationCategory.save)
var opts = {
filters: [
{ name: 'ZAP Config', extensions: ['zap'] },
{ name: 'All Files', extensions: ['*'] },
],
}
if (filePath != null) {
opts.defaultPath = filePath
return dialog.showSaveDialog(opts)
})
.then((result) => {
return fileSave(env.mainDatabase(), browserWindow, result.filePath)
queryGeneric.insertFileLocation(
env.mainDatabase(),
filePath,
dbEnum.fileLocationCategory.save
)
dialog.showMessageBox(browserWindow, {
title: 'Save',
message: `Save done. Output: ${filePath}`,
.catch((err) => uiJs.showErrorMessage('Save file', err))
* This function gets the directory where user wants the output and
* calls generateCode function which generates the code in the user selected
* output.
*
* @param {*} browserWindow
*/
.showOpenDialog({
buttonLabel: 'Save',
properties: ['openDirectory', 'createDirectory'],
})
.then((result) => {
if (!result.canceled) {
return Promise.resolve({ path: result.filePaths[0] })
return Promise.resolve({})
.then((context) => {
if (!('path' in context)) return context
return util
.getSessionKeyFromBrowserWindow(browserWindow)
.then((sessionKey) =>
querySession.getSessionInfoFromSessionKey(
env.mainDatabase(),
sessionKey
)
)
.then((session) => {
env.logInfo(`Generating for session ${session.sessionId}`)
context.sessionId = session.sessionId
return context
})
})
.then((context) => {
context.packageIds = []
if (!('sessionId' in context)) return context
env.logInfo(
`Collecting session packages for session ${context.sessionId}`
)
return queryPackage
.getSessionPackagesByType(
context.sessionId,
dbEnum.packageType.genTemplatesJson
.then((pkgs) => {
pkgs.forEach((pkg) => {
env.logInfo(`Package ${pkg.id}, type: ${pkg.type}`)
context.packageIds.push(pkg.id)
})
return context
})
.then((context) => {
var promises = []
context.packageIds.forEach((pkgId) => {
env.logInfo(
`Setting up generation for session ${context.sessionId} and package ${pkgId}`
)
promises.push(
generationEngine.generateAndWriteFiles(
env.mainDatabase(),
context.sessionId,
pkgId,
context.path
)
)
})
return Promise.all(promises).then(() => context)
})
.then((context) => {
dialog.showMessageBox(browserWindow, {
title: 'Generation',
message: `Generation Output: ${context.path}`,
buttons: ['Ok'],
})
.catch((err) => uiJs.showErrorMessage('Save file', err))
* This function gets the directory where user wants the output and calls
* generateCode function which generates the code in the user selected output.
*
* @param {*} browserWindow
*/
function setHandlebarTemplateDirectory(browserWindow) {
dialog
.showOpenDialog({ properties: ['openDirectory'] })
.then((result) => {
if (!result.canceled) {
return Promise.resolve(result.filePaths[0])
} else {
return Promise.resolve(null)
}
})
.then((filePath) => {
if (filePath != null) {
dialog.showMessageBox(browserWindow, {
title: 'Handlebar Templates',
message: `Handlebar Template Directory: ${filePath}`,
buttons: ['Ok'],
})
}
})
.catch((err) => uiJs.showErrorMessage('Save file', err))
* @param {*} browserWindow
* @param {*} filePath
* @returns Promise of saving.
*/
function fileSave(db, browserWindow, filePath) {
util
.getSessionKeyFromBrowserWindow(browserWindow)
.then((sessionKey) =>
querySession.getSessionInfoFromSessionKey(db, sessionKey)
)
return queryConfig
.updateKeyValue(db, row.sessionId, 'filePath', filePath)
.then(() => row)
.then((row) => exportJs.exportDataIntoFile(db, row.sessionId, filePath))
.catch((err) => uiJs.showErrorMessage('File save', err))
* Perform the do open action, possibly reading in multiple files.
function fileOpen(db, filePaths) {

Timotej Ecimovic
已提交
uiJs.readAndOpenFile(db, filePath, httpPort)
/**
* Initialize a menu.
*
* @export
* @param {*} port
*/
httpPort = port
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
}