Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: dir-dat-description option, {datDescription} token #547

Merged
merged 3 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/output/tokens.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ ROMs-Sorted/
When using [DATs](../dats.md), you can make use of console & game information contained in them:

- `{datName}` the matching DAT's name, similar to how the `--dir-dat-name` option works
- `{datDescription}` the matching DAT's description, similar to how the `--dir-dat-description` option works
- `{datReleaseLanguage}` each of the ROM's language(s) (e.g. `EN`, `ES`, `JA`)
- `{datReleaseRegion}` each of the ROM's region(s) (e.g. `USA`, `EUR`, `JPN`, `WORLD`)

Expand Down
7 changes: 7 additions & 0 deletions src/modules/argumentsParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ export default class ArgumentsParser {
type: 'boolean',
implies: 'dat',
})
.option('dir-dat-description', {
group: groupRomOutput,
description: 'Use the DAT description as the output subdirectory',
type: 'boolean',
implies: 'dat',
})
.option('dir-letter', {
group: groupRomOutput,
description: 'Append the first letter of the ROM name as an output subdirectory',
Expand Down Expand Up @@ -526,6 +532,7 @@ Advanced usage:

Tokens that are replaced when generating the output (--output) path of a ROM:
{datName} The name of the DAT that contains the ROM (e.g. "Nintendo - Game Boy")
{datDescription} The description of the DAT that contains the ROM
{datReleaseRegion} The region of the ROM release (e.g. "USA"), each ROM can have multiple
{datReleaseLanguage} The language of the ROM release (e.g. "En"), each ROM can have multiple
{gameType} The type of the game (e.g. "Retail", "Demo", "Prototype")
Expand Down
4 changes: 4 additions & 0 deletions src/types/logiqx/dat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ export default class DAT {
.trim();
}

getDescription(): string | undefined {
return this.getHeader().getDescription();
}

getRomNamesContainDirectories(): boolean {
return this.getHeader().getRomNamesContainDirectories()
|| this.isBiosDat();
Expand Down
21 changes: 20 additions & 1 deletion src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface OptionsProps {
readonly output?: string,
readonly dirMirror?: boolean,
readonly dirDatName?: boolean,
readonly dirDatDescription?: boolean,
readonly dirLetter?: boolean,
readonly dirLetterLimit?: number,
readonly overwrite?: boolean,
Expand Down Expand Up @@ -132,6 +133,8 @@ export default class Options implements OptionsProps {

readonly dirDatName: boolean;

readonly dirDatDescription: boolean;

readonly dirLetter: boolean;

readonly dirLetterLimit: number;
Expand Down Expand Up @@ -256,6 +259,7 @@ export default class Options implements OptionsProps {
this.output = options?.output ?? '';
this.dirMirror = options?.dirMirror ?? false;
this.dirDatName = options?.dirDatName ?? false;
this.dirDatDescription = options?.dirDatDescription ?? false;
this.dirLetter = options?.dirLetter ?? false;
this.dirLetterLimit = options?.dirLetterLimit ?? 0;
this.overwrite = options?.overwrite ?? false;
Expand Down Expand Up @@ -681,6 +685,9 @@ export default class Options implements OptionsProps {
if (this.getDirDatName() && dat.getNameShort()) {
output = path.join(output, dat.getNameShort());
}
if (this.getDirDatDescription() && dat.getDescription()) {
output = path.join(output, dat.getDescription() as string);
}

const dirLetter = this.getDirLetterParsed(romFilenameSanitized, romBasenames);
if (dirLetter) {
Expand Down Expand Up @@ -725,7 +732,15 @@ export default class Options implements OptionsProps {
}

private static replaceDatTokens(input: string, dat: DAT): string {
return input.replace('{datName}', dat.getName().replace(/[\\/]/g, '_'));
let output = input;
output = output.replace('{datName}', dat.getName().replace(/[\\/]/g, '_'));

const description = dat.getDescription();
if (description) {
output = output.replace('{datDescription}', description.replace(/[\\/]/g, '_'));
}

return output;
}

private static replaceGameTokens(input: string, game?: Game): string {
Expand Down Expand Up @@ -852,6 +867,10 @@ export default class Options implements OptionsProps {
return this.dirDatName;
}

getDirDatDescription(): boolean {
return this.dirDatDescription;
}

getDirLetter(): boolean {
return this.dirLetter;
}
Expand Down
12 changes: 11 additions & 1 deletion test/modules/argumentsParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ describe('options', () => {
expect(argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dir-mirror', 'true', '--dir-mirror', 'false']).getDirMirror()).toEqual(false);
});

it('should parse "dir-datname"', () => {
it('should parse "dir-dat-name"', () => {
expect(() => argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dir-dat-name'])).toThrow(/dependent|implication/i);
expect(argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dat', os.devNull, '-D']).getDirDatName()).toEqual(true);
expect(argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dat', os.devNull, '--dir-dat-name']).getDirDatName()).toEqual(true);
Expand All @@ -259,6 +259,16 @@ describe('options', () => {
expect(argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dat', os.devNull, '--dir-dat-name', 'true', '--dir-dat-name', 'false']).getDirDatName()).toEqual(false);
});

it('should parse "dir-dat-description"', () => {
expect(() => argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dir-dat-description'])).toThrow(/dependent|implication/i);
expect(argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dat', os.devNull, '--dir-dat-description']).getDirDatDescription()).toEqual(true);
expect(argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dat', os.devNull, '--dir-dat-description', 'true']).getDirDatDescription()).toEqual(true);
expect(argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dat', os.devNull, '--dir-dat-description', 'false']).getDirDatDescription()).toEqual(false);
expect(argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dat', os.devNull, '--dir-dat-description', '--dir-dat-description']).getDirDatDescription()).toEqual(true);
expect(argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dat', os.devNull, '--dir-dat-description', 'false', '--dir-dat-description', 'true']).getDirDatDescription()).toEqual(true);
expect(argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dat', os.devNull, '--dir-dat-description', 'true', '--dir-dat-description', 'false']).getDirDatDescription()).toEqual(false);
});

it('should parse "dir-letter"', () => {
expect(argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dir-letter']).getDirLetter()).toEqual(true);
expect(argumentsParser.parse([...dummyCommandAndRequiredArgs, '--dir-letter', 'true']).getDirLetter()).toEqual(true);
Expand Down
15 changes: 12 additions & 3 deletions test/types/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('getOutputDirRoot', () => {
['games/{mister}/', 'games'],
['Roms/{onion}/', 'Roms'],
['{datName}', '.'],
['{datDescription}', '.'],
])('should find the root dir: %s', (output, expectedPath) => {
expect(new Options({ commands: ['copy'], output }).getOutputDirRoot()).toEqual(expectedPath);
});
Expand Down Expand Up @@ -46,10 +47,11 @@ describe('getOutputFileParsed', () => {
describe('token replacement', () => {
test.each([
['foo/{datName}/bar', path.join('foo', 'DAT _ Name', 'bar', 'game.rom')],
['foo/{datDescription}/bar', path.join('foo', 'DAT _ Description', 'bar', 'game.rom')],
['root/{datReleaseRegion}', path.join('root', 'USA', 'game.rom')],
['root/{datReleaseLanguage}', path.join('root', 'En', 'game.rom')],
])('should replace {dat*}: %s', (output, expectedPath) => {
const dat = new DAT(new Header({ name: 'DAT / Name' }), []);
const dat = new DAT(new Header({ name: 'DAT / Name', description: 'DAT \\ Description' }), []);
const release = new Release('Game Name', 'USA', 'En');
expect(new Options({ commands: ['copy'], output }).getOutputFileParsed(dat, dummyInputRomPath, dummyGame, release, 'game.rom')).toEqual(expectedPath);
});
Expand Down Expand Up @@ -182,12 +184,19 @@ describe('getOutputFileParsed', () => {
});

it('should respect "--dir-dat-name"', () => {
const dat = new DAT(new Header({ name: 'system' }), []);
const dat = new DAT(new Header({ name: 'name', description: 'description' }), []);
expect(new Options({ commands: ['copy'], output: os.devNull, dirDatName: true }).getOutputFileParsed(dummyDat, dummyInputRomPath, dummyGame, dummyRelease, dummyRomFilename)).toEqual(os.devNull);
expect(new Options({ commands: ['copy'], output: os.devNull, dirDatName: true }).getOutputFileParsed(dat, dummyInputRomPath, dummyGame, dummyRelease, dummyRomFilename)).toEqual(path.join(os.devNull, 'system'));
expect(new Options({ commands: ['copy'], output: os.devNull, dirDatName: true }).getOutputFileParsed(dat, dummyInputRomPath, dummyGame, dummyRelease, dummyRomFilename)).toEqual(path.join(os.devNull, 'name'));
expect(new Options({ commands: ['copy'], output: os.devNull, dirDatName: false }).getOutputFileParsed(dat, dummyInputRomPath, dummyGame, dummyRelease, dummyRomFilename)).toEqual(os.devNull);
});

it('should respect "--dir-dat-description"', () => {
const dat = new DAT(new Header({ name: 'name', description: 'description' }), []);
expect(new Options({ commands: ['copy'], output: os.devNull, dirDatDescription: true }).getOutputFileParsed(dummyDat, dummyInputRomPath, dummyGame, dummyRelease, dummyRomFilename)).toEqual(os.devNull);
expect(new Options({ commands: ['copy'], output: os.devNull, dirDatDescription: true }).getOutputFileParsed(dat, dummyInputRomPath, dummyGame, dummyRelease, dummyRomFilename)).toEqual(path.join(os.devNull, 'description'));
expect(new Options({ commands: ['copy'], output: os.devNull, dirDatDescription: false }).getOutputFileParsed(dat, dummyInputRomPath, dummyGame, dummyRelease, dummyRomFilename)).toEqual(os.devNull);
});

it('should respect "--dir-letter"', () => {
expect(new Options({ commands: ['copy'], output: os.devNull, dirLetter: true }).getOutputFileParsed(dummyDat, dummyInputRomPath, dummyGame, dummyRelease, dummyRomFilename)).toEqual(os.devNull);
expect(new Options({ commands: ['copy'], output: os.devNull, dirLetter: true }).getOutputFileParsed(dummyDat, dummyInputRomPath, dummyGame, dummyRelease, 'file.rom')).toEqual(path.join(os.devNull, 'F', 'file.rom'));
Expand Down