Skip to content

Commit

Permalink
fix(cspell-tools): support conditional builds. (#4668)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Jul 21, 2023
1 parent a497276 commit de4d897
Show file tree
Hide file tree
Showing 13 changed files with 348 additions and 94 deletions.
7 changes: 7 additions & 0 deletions packages/cspell-tools/cspell-tools.config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@
],
"description": "Words in the `allowedSplitWords` are considered correct and can be used as a basis for splitting compound words.\n\nIf entries can be split so that all the words in the entry are allowed, then only the individual words are added, otherwise the entire entry is added. This is to prevent misspellings in CamelCase words from being introduced into the dictionary."
},
"checksumFile": {
"description": "Path to checksum file. `true` - defaults to `./checksum.txt`.",
"type": [
"string",
"boolean"
]
},
"generateNonStrict": {
"default": true,
"description": "Generate lower case / accent free versions of words.",
Expand Down
7 changes: 3 additions & 4 deletions packages/cspell-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
"watch": "tsc -p . -w",
"clean-build": "pnpm run clean && pnpm run build",
"clean": "shx rm -rf dist temp coverage \"*.tsbuildInfo\"",
"coverage": "pnpm coverage:vitest && pnpm coverage:fix",
"coverage": "pnpm coverage:vitest",
"coverage:vitest": "vitest run --coverage",
"coverage:fix": "nyc report --temp-dir \"$(pwd)/coverage\" --reporter lcov --report-dir \"$(pwd)/coverage\" --cwd ../..",
"test-watch": "jest --watch",
"test:watch": "vitest",
"test": "vitest run",
"update-snapshot": "jest --updateSnapshot"
"update-snapshot": "vitest run -u"
},
"repository": {
"type": "git",
Expand Down
1 change: 1 addition & 0 deletions packages/cspell-tools/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export async function run(program: program.Command, argv: string[], flags?: Feat
.command('build [targets...]')
.description('Build the targets defined in the run configuration.')
.option('-c, --config <path to run configuration>', 'Specify the run configuration file.')
.option('--conditional', 'Conditional build.')
.option('-r, --root <directory>', 'Specify the run directory')
.action(build);

Expand Down
15 changes: 11 additions & 4 deletions packages/cspell-tools/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export interface BuildOptions {

/** Current working directory */
cwd?: string | undefined;

/** Conditional build based upon the targets matching the `checksum.txt` file. */
conditional?: boolean;
}

const moduleName = 'cspell-tools';
Expand Down Expand Up @@ -46,11 +49,15 @@ export async function build(targets: string[] | undefined, options: BuildOptions
throw 'cspell-tools.config not found.';
}

const buildInfo: CompileRequest = normalizeRequest(config.config, options.root || path.dirname(config.filepath));
await compile(buildInfo, { filter, cwd: options.cwd });
const configDir = path.dirname(config.filepath);
const buildInfo: CompileRequest = normalizeRequest(
config.config,
path.resolve(configDir, options.root || configDir),
);
await compile(buildInfo, { filter, cwd: options.cwd, conditionalBuild: options.conditional || false });
}

function normalizeRequest(buildInfo: CompileRequest, root: string): CompileRequest {
const { rootDir = root, targets = [] } = buildInfo;
return { rootDir, targets };
const { rootDir = root, targets = [], checksumFile } = buildInfo;
return { rootDir: path.resolve(rootDir), targets, checksumFile };
}
153 changes: 153 additions & 0 deletions packages/cspell-tools/src/compiler/__snapshots__/compile.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,156 @@ Error+
msg
"
`;

exports[`compile > compile conditional 'cities.txt' fmt: 'plaintext' gz: false alt: true 1`] = `
"
# cspell-tools: keep-case no-split
London
Los Angeles
Mexico City
New Amsterdam
New Delhi
New York
Paris
San Francisco
~london
~los angeles
~mexico city
~new amsterdam
~new delhi
~new york
~paris
~san francisco
"
`;

exports[`compile > compile conditional 'cities.txt' fmt: 'plaintext' gz: false alt: undefined 1`] = `
"
# cspell-tools: keep-case no-split
London
Los Angeles
Mexico City
New Amsterdam
New Delhi
New York
Paris
San Francisco
"
`;

exports[`compile > compile conditional 'cities.txt' fmt: 'plaintext' gz: true alt: true 1`] = `
"
# cspell-tools: keep-case no-split
London
Los Angeles
Mexico City
New Amsterdam
New Delhi
New York
Paris
San Francisco
~london
~los angeles
~mexico city
~new amsterdam
~new delhi
~new york
~paris
~san francisco
"
`;

exports[`compile > compile conditional 'cities.txt' fmt: 'plaintext' gz: true alt: undefined 1`] = `
"
# cspell-tools: keep-case no-split
London
Los Angeles
Mexico City
New Amsterdam
New Delhi
New York
Paris
San Francisco
"
`;

exports[`compile > compile conditional 'cities.txt' fmt: 'trie3' gz: false alt: undefined 1`] = `
"#!/usr/bin/env cspell-trie reader
TrieXv3
base=10
# Built by cspell-tools.
# Data:
__DATA__
L
o
ndon$4s Angeles$9<2
M
e
xico City$9<2
N
e
w Amsterdam$9Delhi$5York$8
P
a
ri#13;<4
S
a
n Francisco$9<4
"
`;

exports[`compile > compile conditional 'sampleCodeDic.txt' fmt: 'plaintext' gz: false alt: true 1`] = `
"
# cspell-tools: keep-case no-split
!Codemsg
!Errorerror
!codecode
!err
+code
+code+
+error
+error+
+msg
Café
Code
Code+
Error
Error+
msg
~!codemsg
~!errorerror
~cafe
~café
~code
~code+
~error
~error+
"
`;

exports[`compile > compile conditional 'sampleCodeDic.txt' fmt: 'plaintext' gz: false alt: undefined 1`] = `
"
# cspell-tools: keep-case no-split
!Codemsg
!Errorerror
!codecode
!err
+code
+code+
+error
+error+
+msg
Café
Code
Code+
Error
Error+
msg
"
`;
44 changes: 44 additions & 0 deletions packages/cspell-tools/src/compiler/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { spyOnConsole } from '../test/console.js';
import { createTestHelper } from '../test/TestHelper.js';
import { compile } from './compile.js';
import { readTextFile } from './readers/readTextFile.js';
import { checkShasumFile } from '../shasum/shasum.js';

const testHelper = createTestHelper(import.meta.url);

Expand Down Expand Up @@ -60,6 +61,49 @@ describe('compile', () => {
expect(content).toMatchSnapshot();
},
);

test.each`
file | format | compress | generateNonStrict
${'cities.txt'} | ${'plaintext'} | ${false} | ${true}
${'cities.txt'} | ${'plaintext'} | ${true} | ${true}
${'cities.txt'} | ${'plaintext'} | ${false} | ${undefined}
${'cities.txt'} | ${'plaintext'} | ${true} | ${undefined}
${'cities.txt'} | ${'trie3'} | ${false} | ${undefined}
${'sampleCodeDic.txt'} | ${'plaintext'} | ${false} | ${undefined}
${'sampleCodeDic.txt'} | ${'plaintext'} | ${false} | ${true}
`(
'compile conditional $file fmt: $format gz: $compress alt: $generateNonStrict',
async ({ format, file, generateNonStrict, compress }) => {
const targetDirectory = t(`.`);
const target: Target = {
name: 'myDictionary',
targetDirectory,
format,
sources: [sample(file)],
compress,
generateNonStrict,
trieBase: 10,
sort: true,
};
const req: CompileRequest = {
targets: [target],
rootDir: targetDirectory,
checksumFile: true,
};

await compile(req, { conditionalBuild: true });

const ext = (format === 'plaintext' ? '.txt' : '.trie') + ((compress && '.gz') || '');
const content = await readTextFile(`${targetDirectory}/myDictionary${ext}`);
expect(content).toMatchSnapshot();
const check = await checkShasumFile(path.join(targetDirectory, 'checksum.txt'), [], targetDirectory);
expect(check.passed).toBe(true);

await compile(req, { conditionalBuild: true });
const check2 = await checkShasumFile(path.join(targetDirectory, 'checksum.txt'), [], targetDirectory);
expect(check2.passed).toBe(true);
},
);
});

function t(...parts: string[]): string {
Expand Down
Loading

0 comments on commit de4d897

Please sign in to comment.