Electron-433 (Changes the logic for updating user config) (#341)

- change logic in handling user config selection on install
- fix log issues
- Update user config by validating execPath or is mac
- Remove post install from aip config
- Update user config by validating execPath or is mac
- Fix lint issues
- use native isNaN method
- Remove unwanted code
- PR review
This commit is contained in:
Kiran Niranjan 2018-04-19 17:56:42 +00:00 committed by Vishwas Shashidhar
parent b1209083cb
commit ec98374bcf
8 changed files with 197 additions and 117 deletions

View File

@ -93,9 +93,4 @@ sed -i "" -E "s#\"fullscreen\" ?: ?([Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee])#\"ful
sed -i "" -E "s#\"openExternal\" ?: ?([Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee])#\"openExternal\":\ $open_external_app#g" ${newPath}
## Remove the temp permissions file created ##
rm -f ${permissionsFilePath}
## For launching symphony with sandbox enabled, create a shell script that is used as the launch point for the app
EXEC_PATH=${installPath}/Symphony.app/Contents/MacOS
exec ${EXEC_PATH}/Symphony --install ${newPath} ${launch_on_startup}
chmod 755 ${EXEC_PATH}/Symphony
rm -f ${permissionsFilePath}

View File

@ -335,7 +335,6 @@
</COMPONENT>
<COMPONENT cid="caphyon.advinst.msicomp.BuildComponent">
<ROW BuildKey="DefaultBuild" BuildName="DefaultBuild" BuildOrder="1" BuildType="1" Languages="en" InstallationType="4" UseLargeSchema="true" MsiPackageType="x64"/>
<ATTRIBUTE name="CurrentBuild" value="DefaultBuild"/>
</COMPONENT>
<COMPONENT cid="caphyon.advinst.msicomp.DictionaryComponent">
<ROW Path="&lt;AI_DICTS&gt;ui.ail"/>
@ -648,8 +647,6 @@
<ROW Action="SET_APPDIR" Type="307" Source="APPDIR" Target="[ProgramFilesFolder][Manufacturer]\[ProductName]" MultiBuildTarget="DefaultBuild:[AI_UserProgramFiles][Manufacturer]\[ProductName]"/>
<ROW Action="SET_SHORTCUTDIR" Type="307" Source="SHORTCUTDIR" Target="[ProgramMenuFolder][ProductName]"/>
<ROW Action="SET_TARGETDIR_TO_APPDIR" Type="51" Source="TARGETDIR" Target="[APPDIR]"/>
<ROW Action="Symphony.exe" Type="1042" Source="Symphony.exe" Target="--install --peruser"/>
<ROW Action="Symphony.exe_All_User" Type="1042" Source="Symphony.exe" Target="--install"/>
<ROW Action="UninstallPreviousVersions" Type="1" Source="aicustact.dll" Target="UninstallPreviousVersions" Options="1"/>
</COMPONENT>
<COMPONENT cid="caphyon.advinst.msicomp.MsiDialogComponent">
@ -672,8 +669,6 @@
<ROW Action="KillRenderer" Sequence="55"/>
<ROW Action="AI_DATA_SETTER" Sequence="54"/>
<ROW Action="AI_SETMIXINSTLOCATION" Sequence="749"/>
<ROW Action="Symphony.exe" Condition="( NOT Installed ) AND ( MSIINSTALLPERUSER )" Sequence="5935"/>
<ROW Action="Symphony.exe_All_User" Condition="( NOT Installed ) AND ( ALLUSERS )" Sequence="5936"/>
<ROW Action="AI_AiBackupImmediate" Sequence="1001"/>
<ROW Action="AI_AiBackupRollback" Sequence="1501"/>
<ROW Action="AI_AiRestoreDeferred" Sequence="6599"/>

View File

@ -321,7 +321,6 @@
</COMPONENT>
<COMPONENT cid="caphyon.advinst.msicomp.BuildComponent">
<ROW BuildKey="DefaultBuild" BuildName="DefaultBuild" BuildOrder="1" BuildType="1" Languages="en" InstallationType="4" UseLargeSchema="true"/>
<ATTRIBUTE name="CurrentBuild" value="DefaultBuild"/>
</COMPONENT>
<COMPONENT cid="caphyon.advinst.msicomp.DictionaryComponent">
<ROW Path="&lt;AI_DICTS&gt;ui.ail"/>
@ -623,8 +622,6 @@
<ROW Action="SET_APPDIR" Type="307" Source="APPDIR" Target="[ProgramFilesFolder][Manufacturer]\[ProductName]" MultiBuildTarget="DefaultBuild:[AI_UserProgramFiles][Manufacturer]\[ProductName]"/>
<ROW Action="SET_SHORTCUTDIR" Type="307" Source="SHORTCUTDIR" Target="[ProgramMenuFolder][ProductName]"/>
<ROW Action="SET_TARGETDIR_TO_APPDIR" Type="51" Source="TARGETDIR" Target="[APPDIR]"/>
<ROW Action="Symphony.exe" Type="1042" Source="Symphony.exe" Target="--install --peruser"/>
<ROW Action="Symphony.exe_All_User" Type="1042" Source="Symphony.exe" Target="--install"/>
<ROW Action="UninstallPreviousVersions" Type="1" Source="aicustact.dll" Target="UninstallPreviousVersions" Options="1"/>
</COMPONENT>
<COMPONENT cid="caphyon.advinst.msicomp.MsiDialogComponent">
@ -646,8 +643,6 @@
<ROW Action="AI_DATA_SETTER_2" Sequence="51"/>
<ROW Action="KillRenderer" Sequence="55"/>
<ROW Action="AI_DATA_SETTER" Sequence="54"/>
<ROW Action="Symphony.exe" Condition="( NOT Installed ) AND ( MSIINSTALLPERUSER )" Sequence="5935"/>
<ROW Action="Symphony.exe_All_User" Condition="( NOT Installed ) AND ( ALLUSERS )" Sequence="5936"/>
<ROW Action="AI_SETMIXINSTLOCATION" Sequence="748"/>
<ROW Action="AI_TxtUpdaterInstall" Sequence="5101"/>
</COMPONENT>

View File

@ -4,7 +4,6 @@ const electron = require('electron');
const app = electron.app;
const path = require('path');
const fs = require('fs');
const AppDirectory = require('appdirectory');
const omit = require('lodash.omit');
const pick = require('lodash.pick');
const difference = require('lodash.difference');
@ -16,7 +15,6 @@ const log = require('./log.js');
const logLevels = require('./enums/logLevels.js');
const configFileName = 'Symphony.config';
const dirs = new AppDirectory('Symphony');
// cached config when first reading files. initially undefined and will be
// updated when read from disk.
@ -70,7 +68,11 @@ function getUserConfigField(fieldName) {
* @returns {Promise}
*/
function readUserConfig(customConfigPath) {
log.send(logLevels.INFO, `custom config path ${customConfigPath}`);
return new Promise((resolve, reject) => {
if (userConfig) {
resolve(userConfig);
return;
@ -81,26 +83,33 @@ function readUserConfig(customConfigPath) {
if (!configPath) {
configPath = path.join(app.getPath('userData'), configFileName);
}
log.send(logLevels.INFO, `config path ${configPath}`);
fs.readFile(configPath, 'utf8', (err, data) => {
if (err) {
reject('cannot open user config file: ' + configPath + ', error: ' + err);
} else {
try {
// data is the contents of the text file we just read
userConfig = JSON.parse(data);
} catch (e) {
reject('can not parse user config file data: ' + data + ', error: ' + err);
return;
}
resolve(userConfig);
log.send(logLevels.INFO, `cannot open user config file ${configPath}, error is ${err}`);
reject(`cannot open user config file ${configPath}, error is ${err}`);
return;
}
try {
// data is the contents of the text file we just read
userConfig = JSON.parse(data);
log.send(logLevels.INFO, `user config data is ${JSON.stringify(userConfig)}`);
resolve(userConfig);
} catch (e) {
log.send(logLevels.INFO, `cannot parse user config data ${data}, error is ${e}`);
reject(`cannot parse user config data ${data}, error is ${e}`);
}
});
});
}
/**
* Gets a specific user config value for a field
* Gets a specific global config value for a field
* @param fieldName
* @returns {Promise}
*/
@ -230,19 +239,18 @@ function updateUserConfig(oldUserConfig) {
// create a new object from the old user config
// by ommitting the user related settings from
// the old user config
log.send(logLevels.INFO, `old user config string ${JSON.stringify(oldUserConfig)}`);
let newUserConfig = omit(oldUserConfig, ignoreSettings);
let newUserConfigString = JSON.stringify(newUserConfig, null, 2);
log.send(logLevels.INFO, `new config string ${newUserConfigString}`);
// get the user config path
let userConfigFile;
if (isMac) {
userConfigFile = path.join(dirs.userConfig(), configFileName);
} else {
userConfigFile = path.join(app.getPath('userData'), configFileName);
}
userConfigFile = path.join(app.getPath('userData'), configFileName);
if (!userConfigFile) {
return reject('user config file doesn\'t exist');
return reject(`user config file doesn't exist`);
}
// write the new user config changes to the user config file
@ -255,11 +263,10 @@ function updateUserConfig(oldUserConfig) {
}
/**
* Manipulates user config on windows
* @param {String} perUserInstall - Is a flag to determine if we are installing for an individual user
* Manipulates user config on first time launch
* @returns {Promise}
*/
function updateUserConfigWin(perUserInstall) {
function updateUserConfigOnLaunch() {
return new Promise((resolve, reject) => {
@ -268,36 +275,6 @@ function updateUserConfigWin(perUserInstall) {
// if it's not a per user installation or if the
// user config file doesn't exist, we simple move on
if (!perUserInstall || !fs.existsSync(userConfigFile)) {
log.send(logLevels.WARN, 'config: Could not find the user config file!');
reject();
return;
}
// In case the file exists, we remove it so that all the
// values are fetched from the global config
// https://perzoinc.atlassian.net/browse/ELECTRON-126
readUserConfig(userConfigFile).then((data) => {
resolve(updateUserConfig(data));
}).catch((err) => {
reject(err);
});
});
}
/**
* Manipulates user config on macOS
* @param {String} globalConfigPath - The global config path from installer
* @returns {Promise}
*/
function updateUserConfigMac() {
return new Promise((resolve, reject) => {
const userConfigFile = path.join(dirs.userConfig(), configFileName);
// if user config file does't exist, just use the global config settings
// i.e. until an user makes changes manually using the menu items
if (!fs.existsSync(userConfigFile)) {
log.send(logLevels.WARN, 'config: Could not find the user config file!');
reject();
@ -307,13 +284,18 @@ function updateUserConfigMac() {
// In case the file exists, we remove it so that all the
// values are fetched from the global config
// https://perzoinc.atlassian.net/browse/ELECTRON-126
readUserConfig(userConfigFile).then((data) => {
resolve(updateUserConfig(data));
readUserConfig(userConfigFile).then((data) => {
// Add version info to the user config data
const version = app.getVersion().toString();
const updatedData = Object.assign(data, { version });
resolve(updateUserConfig(updatedData));
}).catch((err) => {
reject(err);
});
});
}
/**
@ -417,8 +399,8 @@ module.exports = {
getConfigField,
updateConfigField,
updateUserConfigWin,
updateUserConfigMac,
updateUserConfigOnLaunch,
getMultipleConfigField,
// items below here are only exported for testing, do NOT use!
@ -429,5 +411,5 @@ module.exports = {
// use only if you specifically need to read global config fields
getGlobalConfigField,
getUserConfigField
};

View File

@ -1,5 +1,7 @@
'use strict';
const {app} = require('electron');
const path = require('path');
const getCmdLineArg = require('./utils/getCmdLineArg.js');
const logLevels = require('./enums/logLevels.js');
@ -102,6 +104,7 @@ let loggerInstance = new Logger();
function initializeLocalLogger() {
// eslint-disable-next-line global-require
electronLog = require('electron-log');
electronLog.transports.file.file = path.join(app.getPath('logs'), 'app.log');
electronLog.transports.file.level = 'debug';
electronLog.transports.file.format = '{h}:{i}:{s}:{ms} {text}';
electronLog.transports.file.maxSize = 10 * 1024 * 1024;

View File

@ -9,9 +9,17 @@ const shellPath = require('shell-path');
const squirrelStartup = require('electron-squirrel-startup');
const AutoLaunch = require('auto-launch');
const urlParser = require('url');
const nodePath = require('path');
const compareSemVersions = require('./utils/compareSemVersions.js');
// Local Dependencies
const {getConfigField, updateUserConfigWin, updateUserConfigMac, readConfigFileSync} = require('./config.js');
const {
getConfigField,
getGlobalConfigField,
readConfigFileSync,
updateUserConfigOnLaunch,
getUserConfigField
} = require('./config.js');
const {setCheckboxValues} = require('./menus/menuTemplate.js');
const { isMac, isDevEnv } = require('./utils/misc.js');
const protocolHandler = require('./protocolHandler');
@ -22,10 +30,6 @@ const { deleteIndexFolder } = require('./search/search.js');
require('electron-dl')();
// ELECTRON-261: On Windows, due to gpu issues, we need to disable gpu
// to ensure screen sharing works effectively with multiple monitors
// https://github.com/electron/electron/issues/4380
//setting the env path child_process issue https://github.com/electron/electron/issues/7688
shellPath()
.then((path) => {
@ -155,7 +159,10 @@ setChromeFlags();
* initialization and is ready to create browser windows.
* Some APIs can only be used after this event occurs.
*/
app.on('ready', readConfigThenOpenMainWindow);
app.on('ready', () => {
checkFirstTimeLaunch()
.then(readConfigThenOpenMainWindow);
});
/**
* Is triggered when all the windows are closed
@ -219,45 +226,63 @@ function setupThenOpenMainWindow() {
processProtocolAction(process.argv);
isAppAlreadyOpen = true;
// allows installer to launch app and set appropriate global / user config params.
let hasInstallFlag = getCmdLineArg(process.argv, '--install', true);
let perUserInstall = getCmdLineArg(process.argv, '--peruser', true);
let customDataArg = getCmdLineArg(process.argv, '--userDataPath=', false);
if (customDataArg && customDataArg.split('=').length > 1) {
let customDataFolder = customDataArg.split('=')[1];
app.setPath('userData', customDataFolder);
}
if (!isMac && hasInstallFlag) {
getConfigField('launchOnStartup')
.then(setStartup)
.then(() => updateUserConfigWin(perUserInstall))
.then(app.quit)
.catch(app.quit);
return;
}
// allows mac installer to overwrite user config
if (isMac && hasInstallFlag) {
// This value is being sent from post install script
// as the app is launched as a root user we don't get
// access to the config file
let launchOnStartup = process.argv[3];
// We wire this in via the post install script
// to get the config file path where the app is installed
setStartup(launchOnStartup)
.then(updateUserConfigMac)
.then(app.quit)
.catch(app.quit);
return;
}
getUrlAndCreateMainWindow();
// Event that fixes the remote desktop issue in Windows
// by repositioning the browser window
electron.screen.on('display-removed', windowMgr.verifyDisplays);
}
function checkFirstTimeLaunch() {
return new Promise((resolve) => {
getUserConfigField('version')
.then((configVersion) => {
const appVersionString = app.getVersion().toString();
const execPath = nodePath.dirname(app.getPath('exe'));
const shouldUpdateUserConfig = execPath.indexOf('AppData/Local/Programs') !== -1 || isMac;
if (!(configVersion
&& typeof configVersion === 'string'
&& (compareSemVersions.check(appVersionString, configVersion) !== 1)) && shouldUpdateUserConfig) {
return setupFirstTimeLaunch();
}
return resolve();
})
.catch(() => {
return setupFirstTimeLaunch();
});
return resolve();
});
}
/**
* Setup and update user config
* on first time launch or if the latest app version
*
* @return {Promise<any>}
*/
function setupFirstTimeLaunch() {
return new Promise(resolve => {
log.send(logLevels.INFO, 'setting first time launch config');
getGlobalConfigField('launchOnStartup')
.then(setStartup)
.then(updateUserConfigOnLaunch)
.then(() => {
log.send(logLevels.INFO, 'first time launch config changes succeeded -> ');
return resolve();
})
.catch((err) => {
log.send(logLevels.ERROR, 'first time launch config changes failed -> ' + err);
return resolve();
});
});
}
/**

View File

@ -0,0 +1,86 @@
// regex match the semver (semantic version) this checks for the pattern X.Y.Z
// ex-valid v1.2.0, 1.2.0, 2.3.4-r51
const semver = /^v?(?:\d+)(\.(?:[x*]|\d+)(\.(?:[x*]|\d+)(?:-[\da-z-]+(?:\.[\da-z-]+)*)?(?:\+[\da-z-]+(?:\.[\da-z-]+)*)?)?)?$/i;
const patch = /-([0-9A-Za-z-.]+)/;
/**
* This function splits the versions
* into major, minor and patch
* @param v
* @returns {T[]}
*/
function split(v) {
const temp = v.replace(/^v/, '').split('.');
const arr = temp.splice(0, 2);
arr.push(temp.join('.'));
return arr;
}
function tryParse(v) {
return isNaN(Number(v)) ? v : Number(v);
}
/**
* This validates the version
* with the semver regex and returns
* -1 if not valid else 1
* @param version
* @returns {number}
*/
function validate(version) {
if (typeof version !== 'string') {
return -1;
}
if (!semver.test(version)) {
return -1;
}
return 1;
}
/**
* This function compares the v1 version
* with the v2 version for all major, minor, patch
* if v1 > v2 returns 1
* if v1 < v2 returns -1
* if v1 = v2 returns 0
* @param v1
* @param v2
* @returns {number}
*/
function check(v1, v2) {
if (validate(v1) === -1 || validate(v2) === -1) {
return -1;
}
const s1 = split(v1);
const s2 = split(v2);
for (let i = 0; i < 3; i++) {
const n1 = parseInt(s1[i] || '0', 10);
const n2 = parseInt(s2[i] || '0', 10);
if (n1 > n2) return 1;
if (n2 > n1) return -1;
}
if ([ s1[2], s2[2] ].every(patch.test.bind(patch))) {
const p1 = patch.exec(s1[2])[1].split('.').map(tryParse);
const p2 = patch.exec(s2[2])[1].split('.').map(tryParse);
for (let k = 0; k < Math.max(p1.length, p2.length); k++) {
if (p1[k] === undefined || typeof p2[k] === 'string' && typeof p1[k] === 'number') return -1;
if (p2[k] === undefined || typeof p1[k] === 'string' && typeof p2[k] === 'number') return 1;
if (p1[k] > p2[k]) return 1;
if (p2[k] > p1[k]) return -1;
}
} else if ([ s1[2], s2[2] ].some(patch.test.bind(patch))) {
return patch.test(s1[2]) ? -1 : 1;
}
return 0;
}
module.exports = {
check
};

View File

@ -48,7 +48,7 @@
"library/search-launch-agent.sh",
"library/search-launch-daemon.sh"
],
"appId": "symphony-electron-desktop",
"appId": "com.symphony.electron-desktop",
"mac": {
"target": "dmg",
"category": "public.app-category.business"
@ -106,7 +106,6 @@
},
"dependencies": {
"@paulcbetts/system-idle-time": "1.0.4",
"appdirectory": "0.1.0",
"archiver": "2.1.1",
"async.map": "0.5.2",
"async.mapseries": "0.5.2",