Skip to content

Commit

Permalink
Update claritas integration to support large result sets.
Browse files Browse the repository at this point in the history
The output is now pulled from a json file.
  • Loading branch information
dscalzi committed Jul 18, 2020
1 parent 0ac31e5 commit 0e83a07
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 14 deletions.
Binary file modified libraries/java/Claritas.jar
Binary file not shown.
6 changes: 3 additions & 3 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@typescript-eslint/parser": "^3.6.1",
"eslint": "^7.4.0",
"rimraf": "^3.0.2",
"typescript": "^3.9.6"
"typescript": "^3.9.7"
},
"dependencies": {
"axios": "^0.19.2",
Expand Down
2 changes: 1 addition & 1 deletion src/model/struct/model/module/module.struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export abstract class ModuleStructure extends BaseModelStructure<Module> {

protected async invokeClaritas(moduleCandidates: ModuleCandidate[]): Promise<void> {
if(this.getClaritasType() != null) {
const claritasExecutor = new ClaritasWrapper()
const claritasExecutor = new ClaritasWrapper(this.absoluteRoot)

let claritasCandidates = moduleCandidates
const exceptionCandidates: [ModuleCandidate, ClaritasException][] = []
Expand Down
40 changes: 32 additions & 8 deletions src/util/java/ClaritasWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
import { JarExecutor } from './JarExecutor'
import { join } from 'path'
import { join, resolve } from 'path'
import { ClaritasResult } from '../../model/claritas/ClaritasResult'
import { MinecraftVersion } from '../MinecraftVersion'
import { LibraryType } from '../../model/claritas/ClaritasLibraryType'
import { pathExists, remove, readFile } from 'fs-extra'

export class ClaritasWrapper extends JarExecutor<ClaritasResult> {

constructor() {
private readonly WORK_DIR: string
private readonly OUTPUT_FILE: string

constructor(cwd: string) {
super('Claritas')
this.stdoutListeners.push((data) => {
const clean = data.toString('utf8').trim() as string
const spike = 'results::'
if(clean.startsWith(spike)) {
this.lastExecutionResult = JSON.parse(clean.substr(spike.length)) as ClaritasResult

this.WORK_DIR = resolve(cwd, 'claritasWork')
this.OUTPUT_FILE = resolve(this.WORK_DIR, 'claritasOutput.json')

this.onCloseListeners.push(async (code) => {
if(code !== 0) {
this.logger.error('Claritas finished with non-zero exit code, ', code)
this.lastExecutionResult = undefined!
} else {
if(pathExists(this.OUTPUT_FILE)) {
this.lastExecutionResult = JSON.parse((await readFile(this.OUTPUT_FILE)).toString('utf8'))
} else {
this.logger.error('Claritas output file not found when exit code is 0, is this a bug?')
this.lastExecutionResult = undefined!
}
}
await this.cleanOutput()
})

}

protected getJarPath(): string {
Expand All @@ -25,8 +41,16 @@ export class ClaritasWrapper extends JarExecutor<ClaritasResult> {
return super.executeJar(
'--absoluteJarPaths', absoluteJarPaths.join(','),
'--libraryType', libraryType,
'--mcVersion', mcVersion.toString()
'--mcVersion', mcVersion.toString(),
'--outputFile', this.OUTPUT_FILE,
'--previewOutput', 'true'
)
}

private async cleanOutput(): Promise<void> {
if(pathExists(this.WORK_DIR)) {
remove(this.WORK_DIR)
}
}

}
7 changes: 6 additions & 1 deletion src/util/java/JarExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export abstract class JarExecutor<T> {
protected stdoutListeners: ((chunk: any) => void)[] = []
// eslint-disable-next-line @typescript-eslint/no-explicit-any
protected stderrListeners: ((chunk: any) => void)[] = []
protected onCloseListeners: ((code: number) => Promise<void>)[] = []

protected lastExecutionResult!: T

Expand All @@ -34,10 +35,14 @@ export abstract class JarExecutor<T> {
child.stderr.on('data', (data) => this.logger.error(data.toString('utf8').trim()))
this.stderrListeners.forEach(l => child.stderr.on('data', l))

child.on('close', code => {
child.on('close', async code => {
this.logger.info('Exited with code', code)
for(const l of this.onCloseListeners) {
await l(code)
}
resolve(this.lastExecutionResult)
})

child.on('error', (err) => {
this.logger.info('Error during process execution', err)
reject(err)
Expand Down

0 comments on commit 0e83a07

Please sign in to comment.