Skip to content

Commit

Permalink
feat: ipcMain hooks & build/exec utils
Browse files Browse the repository at this point in the history
Signed-off-by: uiuing <uiuing@foxmail.com>
  • Loading branch information
uiuing committed Aug 20, 2022
1 parent 9f677b9 commit da3220b
Show file tree
Hide file tree
Showing 32 changed files with 232 additions and 138 deletions.
45 changes: 0 additions & 45 deletions ingop/main/ipc/api/index.ts

This file was deleted.

25 changes: 24 additions & 1 deletion ingop/main/ipc/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { ipcMain } from 'electron'

import { autoSaveFile, envManage, ingopHome } from './api'
import {
autoSaveFile,
compile,
envManage,
existsEnv,
ingopHome
} from './methods'
import { FileData } from './types'

ipcMain.handle('ingop-home-init', async () => {
Expand All @@ -10,13 +16,30 @@ ipcMain.handle('ingop-home-remove', async () => {
ingopHome.remove()
})

ipcMain.handle('check-gop-exist', async () => {
return await existsEnv.gop.exist()
})
ipcMain.handle('check-gop-isNew', async (_event, newVersion: string) => {
return await existsEnv.gop.isNew(newVersion)
})
ipcMain.handle('check-env-go-exist', async () => {
return await existsEnv.env.go.exist()
})
ipcMain.handle('check-env-go-isNew', async (_event, newVersion: string) => {
return await existsEnv.env.go.isNew(newVersion)
})

ipcMain.handle('auto-sava-gop-file', async (_event, fileData: FileData) => {
await new autoSaveFile(fileData).gop()
})
ipcMain.handle('auto-sava-env-go-file', async (_event, fileData: FileData) => {
await new autoSaveFile(fileData).env.go()
})

ipcMain.handle('compile-gop', async () => {
return await compile.gop()
})

ipcMain.handle('env-gop-init', () => {
envManage.initGop()
})
Expand Down
79 changes: 79 additions & 0 deletions ingop/main/ipc/methods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import decompress from 'decompress'
import { existsSync, readdirSync, renameSync, unlinkSync } from 'fs'
import { join } from 'path'

import { checkVersionEnv, isNewVersion } from '../methods/check'
import { ingopPaths, ingopPathsArray } from '../methods/config'
import { isWin } from '../methods/config'
import { buildGop } from '../methods/env/compile'
import { EnvManage } from '../methods/env/types'
import { envManage as unixEnvManage } from '../methods/env/unix/execute'
import { envManage as winEnvManage } from '../methods/env/win/execute'
import { initDirs, removeDirs, saveFile } from '../methods/files'
import { ExistsEnv, FileData } from './types'

export const ingopHome = {
init: () => {
initDirs(ingopPathsArray)
},
remove: () => {
removeDirs(ingopPathsArray)
}
}

export const existsEnv: ExistsEnv = {
gop: {
exist: async () => {
return (await checkVersionEnv('gop')) !== ''
},
isNew: async (newVersion: string) => {
return await isNewVersion('gop', newVersion)
}
},
env: {
go: {
exist: async () => {
return (await checkVersionEnv('go')) !== ''
},
isNew: async (newVersion: string) => {
return await isNewVersion('go', newVersion)
}
}
}
}

export class autoSaveFile {
path: string
constructor({ fileName, base64Data }: FileData) {
this.path = join(ingopPaths.home, fileName)
saveFile(this.path, base64Data)
}
async gop() {
const { gop_root, home } = ingopPaths
await decompress(this.path, home)
const goplus_gop_hash_dir = join(
home,
readdirSync(home).filter((dirName) =>
/goplus-gop-(.*?)/g.test(dirName)
)[0]
)
removeDirs([gop_root])
renameSync(goplus_gop_hash_dir, gop_root)
removeDirs([goplus_gop_hash_dir])
if (existsSync(this.path)) unlinkSync(this.path)
}
env = {
go: async () => {
await decompress(this.path, ingopPaths.go_root)
if (existsSync(this.path)) unlinkSync(this.path)
}
}
}

export const compile = {
gop: async (): Promise<boolean> => {
return await buildGop()
}
}

export const envManage: EnvManage = isWin ? winEnvManage : unixEnvManage
12 changes: 12 additions & 0 deletions ingop/main/ipc/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,15 @@ export type FileData = {
fileName: string
base64Data: string
}

type ExistsMethod = {
exist: () => Promise<boolean>
isNew: (newVersion: string) => Promise<boolean>
}

export type ExistsEnv = {
gop: ExistsMethod
env: {
go: ExistsMethod
}
}
20 changes: 20 additions & 0 deletions ingop/main/methods/__test__/check.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { checkVersionEnv, execCommand, isNewVersion } from '../check'

async function test() {
console.info(
'test check function [execCommand("ls")] :',
await execCommand('ls')
)

console.info(
'test check function [checkVersionEnv("go")] :',
await checkVersionEnv('go')
)

console.info(
'test check function [isNewVersion("go","1.16")] :',
await isNewVersion('go', '1.16')
)
}

test()
9 changes: 9 additions & 0 deletions ingop/main/methods/__test__/compile_env.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { buildGop } from '../env/compile'

export function testBuildGop() {
buildGop().then((r) => {
console.info('testBuildGop result:', r)
})
}

testBuildGop()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
35 changes: 35 additions & 0 deletions ingop/main/methods/check/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { exec } from 'child_process'

export function execCommand(cmd: string): Promise<string | null> {
return new Promise((resolve) => {
exec(`${cmd}`, { encoding: 'utf8' }, (err, stdout) => {
if (err) resolve(null)
resolve(stdout)
})
})
}

export async function checkVersionEnv(cmd: string): Promise<string> {
const s = await execCommand(`${cmd} version`)
if (s === null) return ''
const r = s.match(/[0-9.]+/)
return r === null ? '' : r[0]
}

export async function isNewVersion(
cmd: string,
newVersion: string
): Promise<boolean> {
const nowVersion = await checkVersionEnv(cmd)
if (!nowVersion) return false
const levels1 = nowVersion.split('.')
const levels2 = newVersion.split('.')
const length = Math.max(levels1.length, levels2.length)
for (let i = 0; i < length; i++) {
const _now = i < levels1.length ? parseInt(levels1[i]) : 0
const _new = i < levels2.length ? parseInt(levels2[i]) : 0
if (_now > _new) return true
if (_now < _new) return false
}
return true
}
Empty file.
File renamed without changes.
File renamed without changes.
17 changes: 17 additions & 0 deletions ingop/main/methods/env/compile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { join } from 'path'

import { checkVersionEnv, execCommand, isNewVersion } from '../check'
import { ingopPaths } from '../config'

export async function buildGop(): Promise<boolean> {
const goBinFile =
(await checkVersionEnv('go')) !== '' && (await isNewVersion('go', '1.16'))
? 'go'
: join(ingopPaths.go_bin, 'go')
const command = `cd "${ingopPaths.gop_root}" && "${goBinFile}" run cmd/make.go --build`
try {
return (await execCommand(command)) !== null
} catch (e) {
return false
}
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ if ! grep -q "$shell_content" "$ingop_env_profile"; then
fi

go_proxy_profile="export GO111MODULE=on
export GOPROXY=https://goproxy.cn"
export GOPROXY=https://goproxy.cn,direct"

if ! ping -c 1 -W 3 google.com > /dev/null; then
if ! grep -q "$go_proxy_profile" "$ingop_env_profile"; then
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ set user_path=!user_path:;;=;!
reg add "%USERregpath%" /v PATH /t REG_EXPAND_SZ /d "%user_path%" /f

set GO111MODULE=on
set GOPROXY=https://goproxy.cn
set GOPROXY=https://goproxy.cn,direct

ping google.com -n 1 -w 3 > nul
if not %errorlevel% leq 0 (
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { screen } from 'electron'

type AutoScreenSize = {
width: number
height: number
}
import { AutoScreenSize } from './types'

export function autoScreenSize(): AutoScreenSize {
const auto: AutoScreenSize = { width: 900, height: 562 }
Expand Down
4 changes: 4 additions & 0 deletions ingop/main/methods/utils/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type AutoScreenSize = {
width: number
height: number
}
23 changes: 0 additions & 23 deletions ingop/main/server/__test__/check.test.ts

This file was deleted.

52 changes: 0 additions & 52 deletions ingop/main/server/check/index.ts

This file was deleted.

11 changes: 0 additions & 11 deletions ingop/main/server/check/types.ts

This file was deleted.

Loading

0 comments on commit da3220b

Please sign in to comment.