Skip to content

Commit

Permalink
fix: some issues on darwin (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen authored May 20, 2024
1 parent 1dd9dd4 commit b40eea5
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 20 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
make build
make copy
make build copy
sudo atest service install
sudo atest service restart
sudo atest service status
Expand Down Expand Up @@ -118,7 +117,7 @@ jobs:
cache-dependency-path: console/atest-ui/package-lock.json
- name: Build
run: |
make build-embed-ui
make build-embed-ui copy
sudo atest service install
sudo atest service restart
- name: Test
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ oryxBuildBinary
/helm/api-testing/charts/
console/atest-desktop/out
console/atest-desktop/node_modules
console/atest-desktop/atest
console/atest-desktop/atest.exe
Binary file added console/atest-desktop/api-testing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 4 additions & 3 deletions console/atest-desktop/forge.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const { FusesPlugin } = require('@electron-forge/plugin-fuses');
const { FuseV1Options, FuseVersion } = require('@electron/fuses');
const path = require('node:path');

module.exports = {
packagerConfig: {
icon: 'api-testing.ico',
asar: true,
icon: path.join(__dirname, 'api-testing'),
asar: true
},
rebuildConfig: {},
makers: [
Expand Down Expand Up @@ -32,7 +33,7 @@ module.exports = {
name: '@electron-forge/maker-dmg',
config: {
format: 'ULFO',
icon: 'api-testing.ico'
icon: path.join(__dirname, 'api-testing.png')
}
},
{
Expand Down
3 changes: 2 additions & 1 deletion console/atest-desktop/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<!-- You can also require other files to run in this process -->
<script src="./renderer.js"></script>
<script src="./api.js"></script>
<script>
const actionBut = document.getElementById('action');
actionBut.addEventListener('click', (e) => {
Expand All @@ -35,7 +36,7 @@

const openServerBut = document.getElementById('open-server-page');
openServerBut.addEventListener('click', (e) => {
window.location = 'http://localhost:8080'
window.location = getHomePage()
})
</script>
</body>
Expand Down
79 changes: 70 additions & 9 deletions console/atest-desktop/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@

// Modules to control application life and create native browser window
const { app, BrowserWindow, Menu, MenuItem } = require('electron')
const log = require('electron-log/main');
const path = require('node:path')
const fs = require('node:fs')
const server = require('./api')
const spawn = require("child_process").spawn;
const homedir = require('os').homedir();
const atestHome = path.join(homedir, ".config", 'atest')

// setup log output
log.initialize();
log.transports.file.level = 'info';
log.transports.file.resolvePathFn = () => path.join(atestHome, 'log.log');

app.dock.setIcon(path.join(__dirname, "api-testing.png"))
const createWindow = () => {
// Create the browser window.
const mainWindow = new BrowserWindow({
Expand All @@ -20,12 +30,17 @@ const createWindow = () => {
icon: path.join(__dirname, '/api-testing.ico'),
})

server.control(() => {
if (!isNaN(serverProcess.pid)) {
// server process started by app
mainWindow.loadURL(server.getHomePage())
}, () => {
// and load the index.html of the app.
mainWindow.loadFile('index.html')
})
} else {
server.control(() => {
mainWindow.loadURL(server.getHomePage())
}, () => {
// and load the index.html of the app.
mainWindow.loadFile('index.html')
})
}
}

const menu = new Menu()
Expand Down Expand Up @@ -71,14 +86,55 @@ let serverProcess;
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
const homedir = require('os').homedir();
const homeData = path.join(atestHome, 'data')
const homeBin = path.join(atestHome, 'bin')

fs.mkdirSync(homeData, {
recursive: true
})
fs.mkdirSync(homeBin, {
recursive: true
})

// try to find the atest file first
const serverFile = process.platform === "win32" ? "atest.exe" : "atest"
const atestFromHome = path.join(homeBin, serverFile)
const atestFromPkg = path.join(__dirname, serverFile)

if (!fs.existsSync(atestFromHome)) {
log.info('cannot find from %s', atestFromHome)

serverProcess = spawn("atest", [
const data = fs.readFileSync(atestFromPkg)
log.info('start to write file with length %d', data.length)

try {
fs.writeFileSync(atestFromHome, data);
}
catch (e) {
log.error('Error Code: %s', e.code);
}
}
fs.chmodSync(atestFromHome, 0o755);

serverProcess = spawn(atestFromHome, [
"server",
"--http-port", server.getPort(),
// TODO below setting is not working
"--local-storage", path.join(homedir, ".atest", "data", "*.yaml")
"--local-storage", path.join(homeData, "*.yaml")
]);
serverProcess.stdout.on('data', (data) => {
log.info(data.toString())
if (data.toString().indexOf('Server is running') != -1) {
BrowserWindow.getFocusedWindow().loadURL(server.getHomePage())
}
});
serverProcess.stderr.on('data', (data) => {
log.error(data.toString())
});
serverProcess.on('close', (code) => {
log.log(`child process exited with code ${code}`);
});
log.info('start atest server as pid:', serverProcess.pid)
log.info(serverProcess.spawnargs)

createWindow()

Expand All @@ -101,6 +157,11 @@ app.on('window-all-closed', () => {
}
}
})
app.on('before-quit', () => {
if (serverProcess) {
serverProcess.kill();
}
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
9 changes: 9 additions & 0 deletions console/atest-desktop/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion console/atest-desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
},
"dependencies": {
"child_process": "^1.0.2",
"electron-log": "^5.1.4",
"electron-squirrel-startup": "^1.0.1"
},
"build": {
"extraResources": [
"./assets/atest.exe"
"./assets/*"
]
},
"optionalDependencies": {
Expand Down
1 change: 1 addition & 0 deletions tools/make/common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ include tools/make/ui.mk
include tools/make/run.mk
include tools/make/proto.mk
include tools/make/test.mk
include tools/make/desktop.mk

# Log the running target
# \033[0;32m -> green
Expand Down
15 changes: 15 additions & 0 deletions tools/make/desktop.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Building docs makefile defined.
#
# All make targets related to docs are defined in this file.

##@ Desktop

.PHONY: desktop-start
desktop-start: ## Start Electron Desktop
desktop-start:
cd console/atest-desktop && npm run start

desktop-package: ## Package Electron Desktop
desktop-package: build.embed.ui
cp ${OUTPUT_DIR}/${BINARY} console/atest-desktop
cd console/atest-desktop && npm run package
2 changes: 1 addition & 1 deletion tools/make/run.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ run-console:
.PHONY: copy
copy: ## Copy the binary to /usr/local/bin
copy:
sudo cp ${OUTPUT_DIR}/$(GOOS)/$(GOARCH)/atest /usr/local/bin/
sudo cp $(OUTPUT_DIR)/$(OS)/$(ARCH)/${BINARY} /usr/local/bin/

.PHONY: copy-restart
copy-restart: ## Copy the binary to /usr/local/bin and restart the service
Expand Down
3 changes: 1 addition & 2 deletions tools/make/ui.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ build.ui:

.PHONY: build.embed.ui
build.embed.ui: embed.ui
GOOS=${GOOS} go build -a -ldflags "${BUILD_FLAGS}" -o ${OUTPUT_DIR}/embedui/${BINARY} ${ROOT_PACKAGE}/main.go
cp ${OUTPUT_DIR}/embedui/${BINARY} /usr/local/bin/
GOOS=${GOOS} go build -a -ldflags "${BUILD_FLAGS}" -o $(OUTPUT_DIR)/$(OS)/$(ARCH)/${BINARY} ${ROOT_PACKAGE}/main.go
make clean.embed.ui

.PHONY: embed.ui
Expand Down

0 comments on commit b40eea5

Please sign in to comment.