Skip to content

Commit

Permalink
Fix relative paths in Parser and add verbosity
Browse files Browse the repository at this point in the history
  • Loading branch information
szapp committed May 6, 2024
1 parent e7beb70 commit 3dbde40
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
12 changes: 8 additions & 4 deletions __tests__/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import * as glob from '@actions/glob'
import { posix } from 'path'
import os from 'os'

// eslint-disable-next-line @typescript-eslint/no-unused-vars
let consoleLogMock: jest.SpiedFunction<typeof console.log>
let fsExistsSyncMock: jest.SpiedFunction<typeof fs.existsSync>
let fsReadFileSyncMock: jest.SpiedFunction<typeof fs.readFileSync>
let trueCasePathSyncMock: jest.SpiedFunction<typeof tcp.trueCasePathSync>
Expand All @@ -29,6 +31,7 @@ let tcExtractTarMock: jest.SpiedFunction<typeof tc.extractTar>

describe('Parser', () => {
beforeEach(() => {
consoleLogMock = jest.spyOn(console, 'log').mockImplementation()
fsExistsSyncMock = jest.spyOn(fs, 'existsSync')
fsReadFileSyncMock = jest.spyOn(fs, 'readFileSync')
trueCasePathSyncMock = jest.spyOn(tcp, 'trueCasePathSync')
Expand All @@ -44,7 +47,7 @@ describe('Parser', () => {
const parser = new Parser(patchName, filepath, workingDir)

expect(parser.filepath).toBe(filepath)
expect(parser.workingDir).toBe(workingDir)
expect(parser.workingDir).toBe(workingDir + '/')
expect(parser.exists).toBe(false)
expect(parser.filename).toBe('conTENT_g1.src')
expect(parser.type).toBe('CONTENT')
Expand Down Expand Up @@ -272,7 +275,7 @@ describe('Parser', () => {

await expect(parser['parseSrc'](filepath, true)).rejects.toThrow('Wildcards are not supported')

expect(fsReadFileSyncMock).toHaveBeenCalledWith('path/to/file.src', 'ascii')
expect(fsReadFileSyncMock).toHaveBeenCalledWith('/path/to/file.src', 'ascii')
expect(fsReadFileSyncMock).toHaveReturnedWith('some/path/*\n')
})

Expand Down Expand Up @@ -361,9 +364,10 @@ describe('Parser', () => {

it('should not parse the file twice', () => {
const patchName = 'test'
const workingDir = '/path/'
const filepath = '/path/to/file.d'
const relPath = 'path/to/file.d'
const parser = new Parser(patchName, filepath)
const relPath = 'to/file.d'
const parser = new Parser(patchName, filepath, workingDir)

const stripPath = jest.spyOn(parser as any, 'stripPath')
trueCasePathSyncMock.mockImplementation((path) => String(path))
Expand Down
11 changes: 9 additions & 2 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

13 changes: 11 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class Parser {
this.patchName = patchName.toUpperCase()
this.filepath = normalizePath(filepath)
this.workingDir = normalizePath(workingDir)
if (this.workingDir.length > 0 && !this.workingDir.endsWith('/')) this.workingDir += '/'
this.exists = fs.existsSync(this.filepath)
this.filename = posix.basename(this.filepath)
const baseName = posix.basename(this.filepath, posix.extname(this.filepath)).toUpperCase()
Expand Down Expand Up @@ -90,7 +91,7 @@ export class Parser {
*/
private stripPath(filepath: string): { fullPath: string; relPath: string } {
const fullPath = normalizePath(filepath)
const relPath = fullPath.replace(this.workingDir, '').replace(/^\//, '')
const relPath = fullPath.replace(this.workingDir, '')
return { fullPath, relPath }
}

Expand Down Expand Up @@ -170,6 +171,8 @@ export class Parser {

switch (pattern.toLowerCase()) {
case 'ikarus':
console.log('Requesting Ikarus')

// Download Ikarus from the official repository (caution: not the compatibility version)
repoUrl = 'https://github.com/Lehona/Ikarus/archive/refs/heads/gameversions.tar.gz'
srcPath = posix.join(tmpPath, 'Ikarus-gameversions', `Ikarus_G${this.version}.src`)
Expand Down Expand Up @@ -197,6 +200,8 @@ export class Parser {
]
break
case 'lego':
console.log('Requesting LeGo')

// Download LeGo from the official repository (caution: not the compatibility version)
repoUrl = 'https://github.com/Lehona/LeGo/archive/refs/heads/gameversions.tar.gz'
srcPath = posix.join(tmpPath, 'LeGo-gameversions', `Header_G${this.version}.src`)
Expand All @@ -210,6 +215,7 @@ export class Parser {

// Download the repository
if (!fs.existsSync(srcPath)) {
console.log('Downloading repository')
const archivePath = await tc.downloadTool(repoUrl)
await io.mkdirP(tmpPath)
await tc.extractTar(archivePath, tmpPath)
Expand Down Expand Up @@ -246,6 +252,7 @@ export class Parser {
return
}

console.log(`Reading ${relPath}`)
const srcRootPath = posix.dirname(fullPath)
const input = fs.readFileSync(fullPath, 'ascii')
let lines = input.split(/\r?\n/).filter((line) => line.trim() !== '')
Expand Down Expand Up @@ -298,6 +305,8 @@ export class Parser {
if (this.filelist.includes(relPath)) return
this.filelist.push(relPath)

console.log(`Parsing ${relPath}`)

const input = fs.readFileSync(fullPath, 'ascii')
this.parseStr(input, exclude ? '' : relPath)
}
Expand Down Expand Up @@ -376,7 +385,7 @@ export class Parser {
*/
public validateOverwrites(): void {
if (this.type !== 'CONTENT') return
// See: https://ninja.szapp.de/s/src/data/symbols.asm
// See: https://github.com/szapp/Ninja/blob/master/src/data/symbols.asm
const illegal = [
'INIT_GLOBAL',
'INITPERCEPTIONS',
Expand Down

0 comments on commit 3dbde40

Please sign in to comment.