Skip to content

Commit

Permalink
fix(core): fix #103
Browse files Browse the repository at this point in the history
  • Loading branch information
ilharp committed Apr 25, 2023
1 parent a9bda49 commit 9c8586f
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 99 deletions.
16 changes: 4 additions & 12 deletions packages/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ func postConfig(i *do.Injector, c *koiconfig.Config) error {

p := do.MustInvoke[*message.Printer](i)

c.Computed.DirBin, err = joinAndCreate(i, c.Computed.DirExe, "bin")
if err != nil {
return errors.New(p.Sprintf("failed to process dir bin: %v", err))
}
c.Computed.DirData, err = joinAndCreate(i, c.Computed.DirConfig, "data")
if err != nil {
return errors.New(p.Sprintf("failed to process dir data: %v", err))
Expand All @@ -176,18 +180,6 @@ func postConfig(i *do.Injector, c *koiconfig.Config) error {
if err != nil {
return errors.New(p.Sprintf("failed to process dir data/home: %v", err))
}
c.Computed.DirNode, err = joinAndCreate(i, c.Computed.DirData, "node")
if err != nil {
return errors.New(p.Sprintf("failed to process dir data/node: %v", err))
}
if runtime.GOOS == "windows" {
c.Computed.DirNodeExe = c.Computed.DirNode
} else {
c.Computed.DirNodeExe, err = joinAndCreate(i, c.Computed.DirNode, "bin")
if err != nil {
return errors.New(p.Sprintf("failed to process dir node/bin: %v", err))
}
}
c.Computed.DirLock, err = joinAndCreate(i, c.Computed.DirData, "lock")
if err != nil {
return errors.New(p.Sprintf("failed to process dir data/lock: %v", err))
Expand Down
2 changes: 1 addition & 1 deletion packages/build/tasks/build/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { exec } from '../../utils/spawn'

export const patchNodeRcedit = async () => {
const koishiManifestPath = dir('buildCache', 'koishi.exe.manifest')
const exePath = dir('buildPortableData', 'node/koishi.exe')
const exePath = dir('buildPortableBin', 'koishi.exe')

await fs.promises.writeFile(koishiManifestPath, koishiManifest)

Expand Down
84 changes: 28 additions & 56 deletions packages/build/tasks/prepare/node/extract.ts
Original file line number Diff line number Diff line change
@@ -1,113 +1,85 @@
import del from 'del'
import { TaskFunction } from 'gulp'
import { info } from 'gulplog'
import * as lzma from 'lzma-native'
import StreamZip from 'node-stream-zip'
import * as fs from 'node:fs'
import { createReadStream } from 'node:fs'
import { copyFile } from 'node:fs/promises'
import { join } from 'node:path'
import stream from 'node:stream'
import { promisify } from 'node:util'
import * as tar from 'tar'
import { Exceptions } from '../../../utils/exceptions'
import { exists } from '../../../utils/fs'
import { dir } from '../../../utils/path'
import { destFileLinux, destFileMac, destFileWin, nameWin } from './path'
import {
destFileLinux,
destFileMac,
destFileWin,
extractCachePath,
nameWin,
} from './path'

export const prepareNodeExtractWin = async () => {
const nodeFolder = dir('buildPortableData', 'node')
const cachedFile = dir('buildCache', destFileWin)

info('Checking destination cache.')
if (await exists(dir('buildPortableData', 'node/koishi.exe'))) return
if (await exists(dir('buildPortableBin', 'koishi.exe'))) return

if (!(await exists(cachedFile))) {
throw Exceptions.fileNotFound(cachedFile)
}

const zip = new StreamZip.async({ file: cachedFile })
await zip.extract(nameWin, nodeFolder)
await zip.extract(nameWin, extractCachePath)
await zip.close()

await del(dir('buildPortableData', 'node/CHANGELOG.md'))
await del(dir('buildPortableData', 'node/README.md'))
await del(dir('buildPortableData', 'node/node_modules'))
await del(dir('buildPortableData', 'node/node_etw_provider.man'))
await del(dir('buildPortableData', 'node/install_tools.bat'))
await del(dir('buildPortableData', 'node/nodevars.bat'))
await del(dir('buildPortableData', 'node/corepack'))
await del(dir('buildPortableData', 'node/corepack.cmd'))
await del(dir('buildPortableData', 'node/npm'))
await del(dir('buildPortableData', 'node/npm.cmd'))
await del(dir('buildPortableData', 'node/npx'))
await del(dir('buildPortableData', 'node/npx.cmd'))

await fs.promises.rename(
dir('buildPortableData', 'node/node.exe'),
dir('buildPortableData', 'node/koishi.exe')
await copyFile(
join(extractCachePath, 'node.exe'),
dir('buildPortableBin', 'koishi.exe')
)
}

export const prepareNodeExtractMac = async () => {
const nodeFolder = dir('buildPortableData', 'node')
const cachedFile = dir('buildCache', destFileMac)

info('Checking destination cache.')
if (await exists(dir('buildPortableData', 'node/bin/koishi'))) return
if (await exists(dir('buildPortableBin', 'koishi'))) return

if (!(await exists(cachedFile))) {
throw Exceptions.fileNotFound(cachedFile)
}

await promisify(stream.finished)(
fs
.createReadStream(cachedFile)
.pipe(tar.extract({ cwd: nodeFolder, strip: 1 }))
createReadStream(cachedFile).pipe(
tar.extract({ cwd: extractCachePath, strip: 1 })
)
)

await del(dir('buildPortableData', 'node/CHANGELOG.md'))
await del(dir('buildPortableData', 'node/README.md'))
await del(dir('buildPortableData', 'node/bin/corepack'))
await del(dir('buildPortableData', 'node/bin/npm'))
await del(dir('buildPortableData', 'node/bin/npx'))
await del(dir('buildPortableData', 'node/include'))
await del(dir('buildPortableData', 'node/lib'))
await del(dir('buildPortableData', 'node/share'))

await fs.promises.rename(
dir('buildPortableData', 'node/bin/node'),
dir('buildPortableData', 'node/bin/koishi')
await copyFile(
join(extractCachePath, 'bin/node'),
dir('buildPortableBin', 'koishi')
)
}

export const prepareNodeExtractLinux = async () => {
const nodeFolder = dir('buildPortableData', 'node')
const cachedFile = dir('buildCache', destFileLinux)

info('Checking destination cache.')
if (await exists(dir('buildPortableData', 'node/bin/koishi'))) return
if (await exists(dir('buildPortableBin', 'koishi'))) return

if (!(await exists(cachedFile))) {
throw Exceptions.fileNotFound(cachedFile)
}

await promisify(stream.finished)(
fs
.createReadStream(cachedFile)
createReadStream(cachedFile)
.pipe(lzma.createDecompressor())
.pipe(tar.extract({ cwd: nodeFolder, strip: 1 }))
.pipe(tar.extract({ cwd: extractCachePath, strip: 1 }))
)

await del(dir('buildPortableData', 'node/CHANGELOG.md'))
await del(dir('buildPortableData', 'node/README.md'))
await del(dir('buildPortableData', 'node/bin/corepack'))
await del(dir('buildPortableData', 'node/bin/npm'))
await del(dir('buildPortableData', 'node/bin/npx'))
await del(dir('buildPortableData', 'node/include'))
await del(dir('buildPortableData', 'node/lib'))
await del(dir('buildPortableData', 'node/share'))

await fs.promises.rename(
dir('buildPortableData', 'node/bin/node'),
dir('buildPortableData', 'node/bin/koishi')
await copyFile(
join(extractCachePath, 'bin/node'),
dir('buildPortableBin', 'koishi')
)
}

Expand Down
20 changes: 11 additions & 9 deletions packages/build/tasks/prepare/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@ import { mkdir } from 'node:fs/promises'
import { dir } from '../../../utils/path'
import { prepareNodeDownload } from './download'
import { prepareNodeExtract } from './extract'
import { extractCachePath } from './path'
import { prepareNodeYarn } from './yarn'

export * from './download'
export * from './extract'
export * from './yarn'

export const prepareNodeFolder = () =>
mkdir(
dir(
'buildPortableData',
process.platform === 'win32' ? 'node' : 'node/bin'
),
{
export const prepareNodeFolder = parallel(
() =>
mkdir(dir('buildPortableBin'), {
recursive: true,
}
)
}),

() =>
mkdir(extractCachePath, {
recursive: true,
})
)

export const prepareNode = series(
prepareNodeFolder,
Expand Down
3 changes: 3 additions & 0 deletions packages/build/tasks/prepare/node/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
versionNode,
versionYarn,
} from '../../../utils/config'
import { dir } from '../../../utils/path'

export const nameWin = `node-v${versionNode}-win-x64`
export const nameMac = `node-v${versionNode}-darwin-x64`
Expand All @@ -18,3 +19,5 @@ export const destFileWin = 'node.zip'
export const destFileMac = 'node.tar.gz'
export const destFileLinux = 'node.tar.xz'
export const destFileYarn = 'yarn.cjs'

export const extractCachePath = dir('buildCache', 'node-extract')
5 changes: 1 addition & 4 deletions packages/build/tasks/prepare/node/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ export const prepareNodeYarnDownload = async () => {

export const prepareNodeYarnCopy = async () => {
const cachedFile = dir('buildCache', destFileYarn)
const destFile = dir(
'buildPortableData',
(process.platform === 'win32' ? 'node/' : 'node/bin/') + destFileYarn
)
const destFile = dir('buildPortableBin', destFileYarn)
info('Checking destination cache.')
if (await exists(destFile)) return

Expand Down
12 changes: 12 additions & 0 deletions packages/build/templates/msi/index.wxs.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@
/>
</Component>

<Directory Id="DirectoryInstallBinary" Name="bin">
<Component
Id="ComponentDirectoryInstallBinary"
Guid="0D60E066-265D-45C0-BF8C-91EA9C0887B1"
Win64="yes"
>
<File Id="FileNode" Source="SourceDir\bin\koishi.exe" />
<File Id="FileYarn" Source="SourceDir\bin\yarn.cjs" />
</Component>
</Directory>

<Component
Id="ComponentPuppeteerSupport"
Guid="25275292-80F1-43FA-8A7B-A082F234E177"
Expand Down Expand Up @@ -210,6 +221,7 @@
AllowAdvertise="no"
>
<ComponentRef Id="ComponentDirectoryInstall" />
<ComponentRef Id="ComponentDirectoryInstallBinary" />
<ComponentRef Id="ComponentDirectoryProgramMenuFolder" />
</Feature>

Expand Down
1 change: 1 addition & 0 deletions packages/build/utils/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const buildPaths = {
buildResources: 'build/resources/',

buildPortable: 'build/varients/portable/',
buildPortableBin: 'build/varients/portable/bin/',
buildPortableData: 'build/varients/portable/data/',
buildUnfold: 'build/varients/unfold/',
buildUnfoldData: 'build/varients/unfold/portabledata/',
Expand Down
3 changes: 1 addition & 2 deletions packages/core/koiconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ type ConfigComputed struct {
DirConfig string
DirData string
DirHome string
DirNode string
DirNodeExe string
DirBin string
DirLock string
DirTemp string
DirInstance string
Expand Down
4 changes: 2 additions & 2 deletions packages/core/proc/koiproc_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewNodeProc(
return NewKoiProc(
i,
ch,
cfg.Computed.DirNodeExe,
cfg.Computed.DirBin,
"koishi",
command,
cwd,
Expand All @@ -36,7 +36,7 @@ func NewYarnProc(
return NewNodeProc(
i,
ch,
append([]string{filepath.Join(cfg.Computed.DirNodeExe, "yarn.cjs")}, command...),
append([]string{filepath.Join(cfg.Computed.DirBin, "yarn.cjs")}, command...),
cwd,
)
}
6 changes: 0 additions & 6 deletions packages/unfold/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
const (
EXTRACT_DATA uint8 = 1 << 0
EXTRACT_CONFIG uint8 = 1 << 1
EXTRACT_NODE uint8 = 1 << 2
)

func extract(dest string, mode uint8) error {
Expand Down Expand Up @@ -50,11 +49,6 @@ func extractIntl(dest string, mode uint8, f *zip.File) error {
fmt.Println("Not extracting config, skip")
return nil
}
} else if strings.HasPrefix(f.Name, "data/node") {
if mode&EXTRACT_NODE == 0 {
fmt.Println("Not extracting node, skip")
return nil
}
} else {
if mode&EXTRACT_DATA == 0 {
fmt.Println("Not extracting data, skip")
Expand Down
14 changes: 7 additions & 7 deletions packages/unfold/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,23 @@ func main() {
fmt.Println("User data does not exist. Trying to migrate legacy user data.")

if migrate(folderData) {
fmt.Println("Migration completed. Extracting only node.")
extractMode = EXTRACT_NODE
fmt.Println("Migration completed. Setting up redirect.")
extractMode = 0
} else {
fmt.Println("Legacy user data not found or migration failed. Extracting all files.")
extractMode = EXTRACT_DATA | EXTRACT_CONFIG | EXTRACT_NODE
extractMode = EXTRACT_DATA | EXTRACT_CONFIG
}
} else if err == nil {
fmt.Println("User data exists. Extracting only node.")
extractMode = EXTRACT_NODE
fmt.Println("User data exists. Setting up redirect.")
extractMode = 0
} else {
fmt.Printf("Failed to stat config %s: %v\n", pathConfig, err)
os.Exit(1)
}
} else if mode == "reset-data" {
extractMode = EXTRACT_DATA | EXTRACT_NODE
extractMode = EXTRACT_DATA
} else {
extractMode = EXTRACT_DATA | EXTRACT_CONFIG | EXTRACT_NODE
extractMode = EXTRACT_DATA | EXTRACT_CONFIG
}

fmt.Printf("Extract mode: %v\n", extractMode)
Expand Down

0 comments on commit 9c8586f

Please sign in to comment.