Skip to content

Commit

Permalink
Merge pull request #170 from ZIMkaRU/feature/add-ability-to-turn-off-…
Browse files Browse the repository at this point in the history
…auto-update

Add ability to turn off auto-update
  • Loading branch information
prdn authored Dec 9, 2022
2 parents c90d7d1 + f36c164 commit 38d1a6f
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 27 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*Dockerfile*
*docker-compose*
lastCommit.json
electronEnv.json
dev-app-update.yml
dist
bfx-reports-framework/db/*.db
Expand Down
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
REPO_BRANCH=master
IS_BFX_API_STAGING=0
IS_DEV_ENV=0
IS_AUTO_UPDATE_DISABLED=0
EP_GH_IGNORE_TIME=true
GH_TOKEN=
11 changes: 11 additions & 0 deletions .github/workflows/build-electron-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ on:
version:
description: 'Release version'
required: false
isAutoUpdateDisabled:
description: 'Is auto-update disabled (true / 1)?'
required: false

env:
DOCKER_BUILDKIT: 1
Expand All @@ -38,6 +41,10 @@ jobs:
sed -i -e \
"s/\"version\": \".*\"/\"version\": \"${{ github.event.inputs.version }}\"/g" \
"./package.json"
- if: contains(fromJson('["true", "1", true, 1]'), github.event.inputs.isAutoUpdateDisabled)
name: Turn off auto-update
run: |
echo "IS_AUTO_UPDATE_DISABLED=1" >> $GITHUB_ENV
- name: Cache Docker images
uses: ScribeMD/[email protected]
env:
Expand Down Expand Up @@ -92,6 +99,10 @@ jobs:
sed -i -e \
"s/\"version\": \".*\"/\"version\": \"${{ github.event.inputs.version }}\"/g" \
"./package.json"
- if: contains(fromJson('["true", "1", true, 1]'), github.event.inputs.isAutoUpdateDisabled)
name: Turn off auto-update
run: |
echo "IS_AUTO_UPDATE_DISABLED=1" >> $GITHUB_ENV
- uses: actions/setup-node@v3
with:
node-version: 14.16.0
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Options:
-o Sync only sub-modules
-s Use staging BFX API
-d Set development environment
-u Turn off auto-update
-h Display help
```

Expand Down Expand Up @@ -137,6 +138,7 @@ Options:
-o Sync only sub-modules
-s Use staging BFX API
-d Set development environment
-u Turn off auto-update
-p Publish artifacts
-h Display help
```
Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ services:
COMMON_DIST_FOLDER: /dist
IS_BFX_API_STAGING: ${IS_BFX_API_STAGING:-0}
IS_DEV_ENV: ${IS_DEV_ENV:-0}
IS_AUTO_UPDATE_DISABLED: ${IS_AUTO_UPDATE_DISABLED:-0}
IS_PUBLISHED: ${IS_PUBLISHED:-0}
GH_TOKEN: ${GH_TOKEN:-}
GITHUB_TOKEN: ${GITHUB_TOKEN:-}
Expand All @@ -49,6 +50,7 @@ services:
COMMON_DIST_FOLDER: /dist
IS_BFX_API_STAGING: ${IS_BFX_API_STAGING:-0}
IS_DEV_ENV: ${IS_DEV_ENV:-0}
IS_AUTO_UPDATE_DISABLED: ${IS_AUTO_UPDATE_DISABLED:-0}
IS_PUBLISHED: ${IS_PUBLISHED:-0}
GH_TOKEN: ${GH_TOKEN:-}
GITHUB_TOKEN: ${GITHUB_TOKEN:-}
Expand All @@ -72,6 +74,7 @@ services:
COMMON_DIST_FOLDER: /dist
IS_BFX_API_STAGING: ${IS_BFX_API_STAGING:-0}
IS_DEV_ENV: ${IS_DEV_ENV:-0}
IS_AUTO_UPDATE_DISABLED: ${IS_AUTO_UPDATE_DISABLED:-0}
IS_PUBLISHED: ${IS_PUBLISHED:-0}
GH_TOKEN: ${GH_TOKEN:-}
GITHUB_TOKEN: ${GITHUB_TOKEN:-}
Expand Down
4 changes: 4 additions & 0 deletions electronEnv.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"NODE_ENV": "production",
"IS_AUTO_UPDATE_DISABLED": false
}
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use strict'

try {
const { NODE_ENV } = require('./electronEnv.json')
const envVars = require('./electronEnv.json')

if (
!process.env.NODE_ENV &&
NODE_ENV
) {
process.env.NODE_ENV = NODE_ENV
for (const [key, val] of Object.entries(envVars)) {
if (typeof process.env[key] !== 'undefined') {
continue
}

process.env[key] = val
}
} catch (err) {}

Expand Down
25 changes: 23 additions & 2 deletions scripts/build-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ buildWin=0
buildMac=0
isBfxApiStaging=${IS_BFX_API_STAGING:-0}
isDevEnv=${IS_DEV_ENV:-0}
isAutoUpdateDisabled=${IS_AUTO_UPDATE_DISABLED:-0}
isPublished=${IS_PUBLISHED:-0}

function usage {
Expand All @@ -67,6 +68,7 @@ function usage {
-m Build Mac release
-s Use staging BFX API
-d Set development environment
-u Turn off auto-update
-p Publish artifacts
-h Display help\
${COLOR_NORMAL}" 1>&2
Expand All @@ -85,6 +87,7 @@ while getopts "lwmsdph" opt; do
m) buildMac=1;;
s) isBfxApiStaging=1;;
d) isDevEnv=1;;
u) isAutoUpdateDisabled=1;;
p) isPublished=1;;
h)
usage
Expand Down Expand Up @@ -129,15 +132,33 @@ if [ $buildMac == 1 ]; then
targetPlatform="mac"
fi

cp "$ROOT/$ELECTRON_ENV_FILE_NAME.example" \
"$ROOT/$ELECTRON_ENV_FILE_NAME"

if [ $isBfxApiStaging == 1 ]; then
bfxApiUrl="$STAGING_BFX_API_URL"
fi
if [ $isDevEnv == 1 ]; then
echo -e "\n${COLOR_YELLOW}Developer environment is turned on!${COLOR_NORMAL}"

echo "{\"NODE_ENV\":\"development\"}" > "$ROOT/$ELECTRON_ENV_FILE_NAME"
sed -i -e \
"s/\"NODE_ENV\": \".*\"/\"NODE_ENV\": \"development\"/g" \
"$ROOT/$ELECTRON_ENV_FILE_NAME"
else
sed -i -e \
"s/\"NODE_ENV\": \".*\"/\"NODE_ENV\": \"production\"/g" \
"$ROOT/$ELECTRON_ENV_FILE_NAME"
fi
if [ $isAutoUpdateDisabled == 1 ]; then
echo -e "\n${COLOR_YELLOW}Auto-update is turned off!${COLOR_NORMAL}"

sed -i -e \
"s/\"IS_AUTO_UPDATE_DISABLED\": .*/\"IS_AUTO_UPDATE_DISABLED\": true/g" \
"$ROOT/$ELECTRON_ENV_FILE_NAME"
else
rm -f "$ROOT/$ELECTRON_ENV_FILE_NAME"
sed -i -e \
"s/\"IS_AUTO_UPDATE_DISABLED\": .*/\"IS_AUTO_UPDATE_DISABLED\": false/g" \
"$ROOT/$ELECTRON_ENV_FILE_NAME"
fi

changeDirOwnershipToCurrUser "$ELECTRON_CACHE" "$(id -u):$(id -g)"
Expand Down
6 changes: 6 additions & 0 deletions scripts/launch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ syncRepo=0
syncSubModules=0
isBfxApiStaging=${IS_BFX_API_STAGING:-0}
isDevEnv=${IS_DEV_ENV:-0}
isAutoUpdateDisabled=${IS_AUTO_UPDATE_DISABLED:-0}
isPublished=${IS_PUBLISHED:-0}

function usage {
Expand All @@ -42,6 +43,7 @@ function usage {
-o Sync only sub-modules
-s Use staging BFX API
-d Set development environment
-u Turn off auto-update
-p Publish artifacts
-h Display help\
${COLOR_NORMAL}" 1>&2
Expand All @@ -67,6 +69,7 @@ while getopts "alwmrosdph" opt; do
o) syncSubModules=1;;
s) isBfxApiStaging=1;;
d) isDevEnv=1;;
u) isAutoUpdateDisabled=1;;
p) isPublished=1;;
h)
usage
Expand Down Expand Up @@ -94,6 +97,9 @@ fi
if [ $isDevEnv == 1 ]; then
export IS_DEV_ENV=1
fi
if [ $isAutoUpdateDisabled == 1 ]; then
export IS_AUTO_UPDATE_DISABLED=1
fi
if [ $isPublished == 1 ]; then
export IS_PUBLISHED=1
fi
Expand Down
25 changes: 23 additions & 2 deletions scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ syncRepo=0
syncSubModules=0
isBfxApiStaging=${IS_BFX_API_STAGING:-0}
isDevEnv=${IS_DEV_ENV:-0}
isAutoUpdateDisabled=${IS_AUTO_UPDATE_DISABLED:-0}

function usage {
echo -e "\
Expand All @@ -49,6 +50,7 @@ function usage {
-o Sync only sub-modules
-s Use staging BFX API
-d Set development environment
-u Turn off auto-update
-h Display help\
${COLOR_NORMAL}" 1>&2
}
Expand All @@ -59,6 +61,7 @@ while getopts "rosdh" opt; do
o) syncSubModules=1;;
s) isBfxApiStaging=1;;
d) isDevEnv=1;;
u) isAutoUpdateDisabled=1;;
h)
usage
exit 0
Expand All @@ -71,15 +74,33 @@ while getopts "rosdh" opt; do
esac
done

cp "$ROOT/$ELECTRON_ENV_FILE_NAME.example" \
"$ROOT/$ELECTRON_ENV_FILE_NAME"

if [ $isBfxApiStaging == 1 ]; then
bfxApiUrl="$STAGING_BFX_API_URL"
fi
if [ $isDevEnv == 1 ]; then
echo -e "\n${COLOR_YELLOW}Developer environment is turned on!${COLOR_NORMAL}"

echo "{\"NODE_ENV\":\"development\"}" > "$ROOT/$ELECTRON_ENV_FILE_NAME"
sed -i -e \
"s/\"NODE_ENV\": \".*\"/\"NODE_ENV\": \"development\"/g" \
"$ROOT/$ELECTRON_ENV_FILE_NAME"
else
sed -i -e \
"s/\"NODE_ENV\": \".*\"/\"NODE_ENV\": \"production\"/g" \
"$ROOT/$ELECTRON_ENV_FILE_NAME"
fi
if [ $isAutoUpdateDisabled == 1 ]; then
echo -e "\n${COLOR_YELLOW}Auto-update is turned off!${COLOR_NORMAL}"

sed -i -e \
"s/\"IS_AUTO_UPDATE_DISABLED\": .*/\"IS_AUTO_UPDATE_DISABLED\": true/g" \
"$ROOT/$ELECTRON_ENV_FILE_NAME"
else
rm -f "$ROOT/$ELECTRON_ENV_FILE_NAME"
sed -i -e \
"s/\"IS_AUTO_UPDATE_DISABLED\": .*/\"IS_AUTO_UPDATE_DISABLED\": false/g" \
"$ROOT/$ELECTRON_ENV_FILE_NAME"
fi

if [ $syncRepo == 1 ]; then
Expand Down
43 changes: 39 additions & 4 deletions src/auto-updater/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const { app, ipcMain, Menu } = require('electron')
const { rootPath: appDir } = require('electron-root-path')
const fs = require('fs')
const path = require('path')
const {
Expand All @@ -21,6 +22,7 @@ const {
const {
closeAlert
} = require('../modal-dialog-src/utils')
const isAutoUpdateDisabled = !!process.env.IS_AUTO_UPDATE_DISABLED

const fontsStyle = fs.readFileSync(path.join(
__dirname, '../../bfx-report-ui/build/fonts/roboto.css'
Expand All @@ -37,6 +39,11 @@ let autoUpdater
let uCheckInterval
let isIntervalUpdate = false
let isProgressToastEnabled = false
let electronBuilderConfig = {}

try {
electronBuilderConfig = require(path.join(appDir, 'electron-builder-config'))
} catch (err) {}

const fonts = `<style>${fontsStyle}</style>`
const style = `<style>${toastStyle}</style>`
Expand Down Expand Up @@ -451,6 +458,12 @@ const _autoUpdaterFactory = () => {
const checkForUpdates = () => {
return async () => {
try {
if (isAutoUpdateDisabled) {
console.debug('Auto-update is disabled')

return
}

isIntervalUpdate = false
_switchMenuItem({
isCheckMenuItemDisabled: true
Expand All @@ -468,6 +481,12 @@ const checkForUpdates = () => {

const checkForUpdatesAndNotify = async (opts) => {
try {
if (isAutoUpdateDisabled) {
console.debug('Auto-update is disabled')

return
}

const {
isIntervalUpdate: isIntUp = false
} = { ...opts }
Expand All @@ -488,17 +507,33 @@ const checkForUpdatesAndNotify = async (opts) => {

const quitAndInstall = () => {
return () => {
if (isAutoUpdateDisabled) {
return
}

return _autoUpdaterFactory()
.quitAndInstall(false, true)
}
}

const getAppUpdateConfigSync = () => {
const appUpdateConfigPath = _autoUpdaterFactory()
.app.appUpdateConfigPath
const fileContent = fs.readFileSync(appUpdateConfigPath, 'utf8')
try {
if (isAutoUpdateDisabled) {
return electronBuilderConfig
?.publish ?? {}
}

return yaml.load(fileContent)
const appUpdateConfigPath = _autoUpdaterFactory()
.app.appUpdateConfigPath
const fileContent = fs.readFileSync(appUpdateConfigPath, 'utf8')

return yaml.load(fileContent)
} catch (err) {
console.debug(err)

return electronBuilderConfig
?.publish ?? {}
}
}

module.exports = {
Expand Down
2 changes: 2 additions & 0 deletions src/create-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {
const { manageNewGithubIssue } = require('./error-manager')
const showDocs = require('./show-docs')
const { showChangelog } = require('./changelog-manager')
const isAutoUpdateDisabled = !!process.env.IS_AUTO_UPDATE_DISABLED

module.exports = ({
pathToUserData,
Expand Down Expand Up @@ -140,6 +141,7 @@ module.exports = ({
{ type: 'separator' },
{
label: 'Check for updates',
enabled: !isAutoUpdateDisabled,
id: 'CHECK_UPDATE_MENU_ITEM',
click: checkForUpdates()
},
Expand Down
14 changes: 1 addition & 13 deletions src/helpers/get-debug-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,11 @@ const { getAppUpdateConfigSync } = require('../auto-updater')

const packageJson = require(path.join(appDir, 'package.json'))
const productName = require('./product-name')
let electronBuilderConfig = {}

try {
electronBuilderConfig = require(path.join(appDir, 'electron-builder-config'))
} catch (err) {}

let lastCommit = { hash: '-', date: '-' }
let appUpdateConfig = {}

try {
appUpdateConfig = getAppUpdateConfigSync()
} catch (err) {
console.debug(err)
const appUpdateConfig = getAppUpdateConfigSync()

appUpdateConfig = electronBuilderConfig
?.publish ?? {}
}
try {
lastCommit = require(path.join(appDir, 'lastCommit.json'))
} catch (err) {
Expand Down

0 comments on commit 38d1a6f

Please sign in to comment.