@keymanapp/kmc-kmn 17.0.85-alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,93 @@
1
+ /*
2
+ TODO: implement additional interfaces:
3
+
4
+ extern "C" bool kmcmp_CompileKeyboardFileToBuffer(
5
+ char* pszInfile,
6
+ void* pfkBuffer,
7
+ bool ACompilerWarningsAsErrors,
8
+ bool AWarnDeprecatedCode,
9
+ kmcmp_CompilerMessageProc pMsgproc,
10
+ void* AmsgprocContext,
11
+ int Target
12
+ );
13
+
14
+ typedef bool (*kmcmp_ValidateJsonMessageProc)(int64_t offset, const char* szText, void* context);
15
+
16
+ extern "C" bool kmcmp_ValidateJsonFile(
17
+ std::fstream& f,
18
+ std::fstream& fd,
19
+ kmcmp_ValidateJsonMessageProc MessageProc,
20
+ void* context
21
+ );
22
+ */
23
+
24
+ // TODO: rename wasm-host?
25
+ import loadWasmHost from '../import/kmcmplib/wasm-host.js';
26
+
27
+ export interface CompilerOptions {
28
+ shouldAddCompilerVersion?: boolean;
29
+ saveDebug?: boolean;
30
+ compilerWarningsAsErrors?: boolean;
31
+ warnDeprecatedCode?: boolean;
32
+ };
33
+
34
+ const baseOptions: CompilerOptions = {
35
+ shouldAddCompilerVersion: true,
36
+ saveDebug: true,
37
+ compilerWarningsAsErrors: false,
38
+ warnDeprecatedCode: true
39
+ };
40
+
41
+ export class Compiler {
42
+ wasmModule: any;
43
+ compileKeyboardFile: any;
44
+ setCompilerOptions: any;
45
+
46
+ public async init(): Promise<boolean> {
47
+ if(!this.wasmModule) {
48
+ this.wasmModule = await loadWasmHost();
49
+ this.compileKeyboardFile = this.wasmModule.cwrap('kmcmp_Wasm_CompileKeyboardFile', 'number', ['string', 'string',
50
+ 'number', 'number', 'number', 'string']);
51
+ this.setCompilerOptions = this.wasmModule.cwrap('kmcmp_Wasm_SetCompilerOptions', 'number', ['number']);
52
+ }
53
+ return this.compileKeyboardFile !== undefined && this.setCompilerOptions !== undefined;
54
+ }
55
+
56
+ public run(infile: string, outfile: string, options?: CompilerOptions): boolean {
57
+ if(!this.wasmModule) {
58
+ return false;
59
+ }
60
+
61
+ options = {...baseOptions, ...options};
62
+
63
+ (globalThis as any).msgproc = function(line: number, code: number, msg: string) {
64
+ // TODO: link into the kmc error reporting infrastructure
65
+ console.log(`[${line}] ${code}: ${msg}`);
66
+ }
67
+
68
+ // TODO: use callbacks for file access -- so kmc-kmn is entirely fs agnostic
69
+ let result = this.runCompiler(infile, outfile, options) == 1;
70
+
71
+ (globalThis as any).msgproc = null;
72
+
73
+ return result;
74
+ }
75
+
76
+ private runCompiler(infile: string, outfile: string, options: CompilerOptions): number {
77
+ try {
78
+ if(!this.setCompilerOptions(options.shouldAddCompilerVersion)) {
79
+ console.error('Unable to set compiler options');
80
+ }
81
+ return this.compileKeyboardFile(
82
+ infile,
83
+ outfile,
84
+ options.saveDebug ? 1 : 0,
85
+ options.compilerWarningsAsErrors ? 1 : 0,
86
+ options.warnDeprecatedCode ? 1 : 0, 'msgproc');
87
+ } catch(e) {
88
+ // TODO: use sentry
89
+ console.error(e);
90
+ }
91
+ return 0;
92
+ }
93
+ }
package/src/main.ts ADDED
@@ -0,0 +1,2 @@
1
+
2
+ export { Compiler } from './compiler/compiler.js';
package/test/README.md ADDED
@@ -0,0 +1,7 @@
1
+ Keyman kmn Keyboard Compiler Tests
2
+ ===================================
3
+
4
+ Test
5
+ ----
6
+
7
+ ../build.sh test
@@ -0,0 +1,11 @@
1
+ c Description: Tests null keyboard
2
+ c keys: [K_A][RALT K_B][SHIFT K_C]
3
+ c expected: aC
4
+ c context:
5
+
6
+ store(&version) '6.0'
7
+
8
+ begin Unicode > use(Main)
9
+
10
+ group(Main) using keys
11
+
Binary file
@@ -0,0 +1,47 @@
1
+ import 'mocha';
2
+ import sinon from 'sinon';
3
+ // import chai, { expect } from 'chai';
4
+ import chai, { assert } from 'chai';
5
+ import sinonChai from 'sinon-chai';
6
+ import { Compiler } from '../src/main.js';
7
+ import { dirname } from 'path';
8
+ import { fileURLToPath } from 'url';
9
+ import fs from 'fs';
10
+
11
+ const __dirname = dirname(fileURLToPath(import.meta.url)).replace(/\\/g, '/');
12
+
13
+ chai.use(sinonChai);
14
+
15
+ describe('Compiler class', function() {
16
+ let consoleLog: any;
17
+
18
+ beforeEach(function() {
19
+ consoleLog = sinon.spy(console, 'log');
20
+ });
21
+
22
+ afterEach(function() {
23
+ consoleLog.restore();
24
+ });
25
+
26
+ it('should start', async function() {
27
+ const compiler = new Compiler();
28
+ assert(await compiler.init());
29
+ });
30
+
31
+ it('should compile a basic keyboard', async function() {
32
+ const compiler = new Compiler();
33
+ assert(await compiler.init());
34
+
35
+ const fixtureName = __dirname + '/../../test/fixtures/000.kmx';
36
+ const infile = __dirname + '/../../test/fixtures/000.kmn';
37
+ const outfile = __dirname + '/000.kmx';
38
+
39
+ assert(compiler.run(infile, outfile, {saveDebug: true, shouldAddCompilerVersion: false}));
40
+
41
+ assert(fs.existsSync(outfile));
42
+ const outfileData = fs.readFileSync(outfile);
43
+ const fixtureData = fs.readFileSync(fixtureName);
44
+ assert.equal(outfileData.byteLength, fixtureData.byteLength);
45
+ assert.deepEqual(outfileData, fixtureData);
46
+ });
47
+ });
@@ -0,0 +1,24 @@
1
+ {
2
+ "extends": "../../kmc/tsconfig.kmc-base.json",
3
+
4
+ "compilerOptions": {
5
+ "rootDir": ".",
6
+ "rootDirs": ["./", "../src/"],
7
+ "outDir": "../build/test",
8
+ "baseUrl": ".",
9
+ "allowSyntheticDefaultImports": true, // for chai
10
+ "paths": {
11
+ "@keymanapp/common-types": ["../../../../common/web/types/src/main"],
12
+ },
13
+ },
14
+ "include": [
15
+ "**/test-*.ts",
16
+ "./helpers/index.ts"
17
+ ],
18
+ "references": [
19
+ { "path": "../../../../common/web/keyman-version/tsconfig.esm.json" },
20
+ { "path": "../../../../common/web/types/" },
21
+ { "path": "../../../../common/tools/hextobin/" },
22
+ { "path": "../" }
23
+ ]
24
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "extends": "../../../tsconfig.esm-base.json",
3
+
4
+ "compilerOptions": {
5
+ "outDir": "build/src/",
6
+ "rootDir": "src/",
7
+ "baseUrl": ".",
8
+ "allowJs": true,
9
+ "paths": {
10
+ "@keymanapp/common-types": ["../../../common/web/types/src/main"],
11
+ },
12
+
13
+ },
14
+ "include": [
15
+ "src/**/*.ts",
16
+ "src/import/kmcmplib/wasm-host.js"
17
+ ],
18
+ "references": [
19
+ { "path": "../../../common/web/keyman-version/tsconfig.esm.json" },
20
+ { "path": "../../../common/web/types/" },
21
+ ]
22
+ }