Skip to content

Commit b87667a

Browse files
committed
chore(developer): cleanup kmc-keyboard-info interface
Moves responsibility for loading .kps into kmc-keyboard-info, away from the caller, and removes other fields with kmc-keyboard-info can calculate by itself. Have not moved project parsing into kmc-keyboard-info, because that's a bigger job, as currently that is mostly happening within kmc itself. A project for a future version I think.
1 parent 0e382ba commit b87667a

File tree

3 files changed

+40
-59
lines changed

3 files changed

+40
-59
lines changed

developer/src/kmc-keyboard-info/src/index.ts

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ import { KeymanFileTypes, CompilerCallbacks, KmpJsonFile, KmxFileReader, KMX, Ke
99
import { KeyboardInfoCompilerMessages } from "./messages.js";
1010
import langtags from "./imports/langtags.js";
1111
import { validateMITLicense } from "./validate-mit-license.js";
12+
import { KmpCompiler } from "@keymanapp/kmc-package";
1213

1314
const regionNames = new Intl.DisplayNames(['en'], { type: "region" });
1415
const scriptNames = new Intl.DisplayNames(['en'], { type: "script" });
1516
const langtagsByTag = {};
1617

18+
const HelpRoot = 'https://help.keyman.com/keyboard/';
19+
1720
/**
1821
* Build a dictionary of language tags from langtags.json
1922
*/
@@ -37,20 +40,11 @@ function init(): void {
3740
}
3841

3942
export interface KeyboardInfoSources {
40-
/** The identifier for the keyboard */
41-
keyboard_id: string;
42-
43-
/** The data from the .kps file, transformed to kmp.json */
44-
kmpJsonData: KmpJsonFile.KmpJsonFile;
45-
46-
/** The path in the keymanapp/keyboards repo where this keyboard may be found (optional) */
47-
sourcePath?: string;
48-
49-
/** The full URL to the keyboard help, starting with https://help.keyman.com/keyboard/ (optional) */
50-
helpLink?: string;
43+
/** The path in the keymanapp/keyboards repo where this keyboard may be found */
44+
sourcePath: string;
5145

5246
/** The compiled keyboard filename and relative path (.js only) */
53-
keyboardFilenameJs?: string;
47+
jsFilename?: string;
5448

5549
/** The compiled package filename and relative path (.kmp) */
5650
kmpFilename: string;
@@ -78,7 +72,16 @@ export class KeyboardInfoCompiler {
7872
sources: KeyboardInfoSources
7973
): Uint8Array {
8074

81-
// TODO: work from .kpj and nothing else as input
75+
// TODO(lowpri): work from .kpj and nothing else as input. Blocked because
76+
// .kpj work is largely in kmc at present, so that would need to move to
77+
// a separate module.
78+
79+
const kmpCompiler = new KmpCompiler(this.callbacks);
80+
const kmpJsonData = kmpCompiler.transformKpsToKmpObject(sources.kpsFilename);
81+
if(!kmpJsonData) {
82+
// Errors will have been emitted by KmpCompiler
83+
return null;
84+
}
8285

8386
if(!sources.kmpFilename) {
8487
// We can't build any metadata without a .kmp file
@@ -90,8 +93,8 @@ export class KeyboardInfoCompiler {
9093

9194
let jsFile: string = null;
9295

93-
if(sources.keyboardFilenameJs) {
94-
jsFile = this.loadJsFile(sources.keyboardFilenameJs);
96+
if(sources.jsFilename) {
97+
jsFile = this.loadJsFile(sources.jsFilename);
9598
if(!jsFile) {
9699
return null;
97100
}
@@ -100,7 +103,7 @@ export class KeyboardInfoCompiler {
100103
const kmxFiles: {
101104
filename: string,
102105
data: KMX.KEYBOARD
103-
}[] = this.loadKmxFiles(sources.kpsFilename, sources.kmpJsonData);
106+
}[] = this.loadKmxFiles(sources.kpsFilename, kmpJsonData);
104107

105108
//
106109
// Build .keyboard_info file
@@ -109,16 +112,16 @@ export class KeyboardInfoCompiler {
109112
//
110113

111114
keyboard_info.id = this.callbacks.path.basename(sources.kmpFilename, '.kmp');
112-
keyboard_info.name = sources.kmpJsonData.info.name.description;
115+
keyboard_info.name = kmpJsonData.info.name.description;
113116

114117
// License
115118

116-
if(!sources.kmpJsonData.options?.licenseFile) {
119+
if(!kmpJsonData.options?.licenseFile) {
117120
this.callbacks.reportMessage(KeyboardInfoCompilerMessages.Error_NoLicenseFound());
118121
return null;
119122
}
120123

121-
if(!this.isLicenseMIT(this.callbacks.resolveFilename(sources.kpsFilename, sources.kmpJsonData.options.licenseFile))) {
124+
if(!this.isLicenseMIT(this.callbacks.resolveFilename(sources.kpsFilename, kmpJsonData.options.licenseFile))) {
122125
return null;
123126
}
124127

@@ -132,7 +135,7 @@ export class KeyboardInfoCompiler {
132135

133136
// author
134137

135-
const author = sources.kmpJsonData.info.author;
138+
const author = kmpJsonData.info.author;
136139
if(author?.description || author?.url) {
137140
keyboard_info.authorName = author.description;
138141

@@ -151,15 +154,15 @@ export class KeyboardInfoCompiler {
151154

152155
// description
153156

154-
if(sources.kmpJsonData.info.description?.description) {
155-
keyboard_info.description = sources.kmpJsonData.info.description?.description.trim();
157+
if(kmpJsonData.info.description?.description) {
158+
keyboard_info.description = kmpJsonData.info.description?.description.trim();
156159
}
157160

158161
// extract the language identifiers from the language metadata arrays for
159162
// each of the keyboards in the kmp.json file, and merge into a single array
160163
// of identifiers in the .keyboard_info file.
161164

162-
this.fillLanguages(keyboard_info, sources.kmpJsonData);
165+
this.fillLanguages(keyboard_info, kmpJsonData);
163166

164167
// TODO: use: TZ=UTC0 git log -1 --no-merges --date=format:%Y-%m-%dT%H:%M:%SZ --format=%ad
165168
keyboard_info.lastModifiedDate = (new Date).toISOString();
@@ -173,19 +176,19 @@ export class KeyboardInfoCompiler {
173176
return null;
174177
}
175178

176-
if(sources.keyboardFilenameJs) {
177-
keyboard_info.jsFilename = this.callbacks.path.basename(sources.keyboardFilenameJs);
179+
if(sources.jsFilename) {
180+
keyboard_info.jsFilename = this.callbacks.path.basename(sources.jsFilename);
178181
// Always overwrite with actual file size
179-
keyboard_info.jsFileSize = this.callbacks.fileSize(sources.keyboardFilenameJs);
182+
keyboard_info.jsFileSize = this.callbacks.fileSize(sources.jsFilename);
180183
if(keyboard_info.jsFileSize === undefined) {
181-
this.callbacks.reportMessage(KeyboardInfoCompilerMessages.Error_FileDoesNotExist({filename:sources.keyboardFilenameJs}));
184+
this.callbacks.reportMessage(KeyboardInfoCompilerMessages.Error_FileDoesNotExist({filename:sources.jsFilename}));
182185
return null;
183186
}
184187
}
185188

186189
const includes = new Set<KeyboardInfoFileIncludes>();
187190
keyboard_info.packageIncludes = [];
188-
for(const file of sources.kmpJsonData.files) {
191+
for(const file of kmpJsonData.files) {
189192
if(file.name.match(/\.(otf|ttf|ttc)$/)) {
190193
includes.add('fonts');
191194
} else if(file.name.match(/welcome\.htm$/)) {
@@ -198,7 +201,7 @@ export class KeyboardInfoCompiler {
198201
}
199202
keyboard_info.packageIncludes = [...includes];
200203

201-
keyboard_info.version = sources.kmpJsonData.info.version.description;
204+
keyboard_info.version = kmpJsonData.info.version.description;
202205

203206
let minVersion = minKeymanVersion;
204207
const m = jsFile?.match(/this.KMINVER\s*=\s*(['"])(.*?)\1/);
@@ -235,7 +238,7 @@ export class KeyboardInfoCompiler {
235238
// and if the .js is in the package, that it is mobile native as well,
236239
// because the targets metadata is not available in the .js.
237240
platforms.add('mobileWeb').add('desktopWeb');
238-
if(sources.kmpJsonData.files.find(file => file.name.match(/\.js$/))) {
241+
if(kmpJsonData.files.find(file => file.name.match(/\.js$/))) {
239242
platforms.add('android').add('ios');
240243
}
241244
}
@@ -262,15 +265,12 @@ export class KeyboardInfoCompiler {
262265

263266
keyboard_info.minKeymanVersion = minVersion;
264267
keyboard_info.sourcePath = sources.sourcePath;
265-
266-
if(sources.helpLink) {
267-
keyboard_info.helpLink = sources.helpLink;
268-
}
268+
keyboard_info.helpLink = HelpRoot + keyboard_info.id;
269269

270270
// Related packages
271-
if(sources.kmpJsonData.relatedPackages?.length) {
271+
if(kmpJsonData.relatedPackages?.length) {
272272
keyboard_info.related = {};
273-
for(const p of sources.kmpJsonData.relatedPackages) {
273+
for(const p of kmpJsonData.relatedPackages) {
274274
keyboard_info.related[p.id] = {
275275
deprecates: p.relationship == 'deprecates'
276276
};

developer/src/kmc-keyboard-info/test/test-keyboard-info-compiler.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import 'mocha';
44
import { TestCompilerCallbacks } from '@keymanapp/developer-test-helpers';
55
import { makePathToFixture } from './helpers/index.js';
66
import { KeyboardInfoCompiler } from '../src/index.js';
7-
import { KmpCompiler } from '@keymanapp/kmc-package';
87

98
const callbacks = new TestCompilerCallbacks();
109

@@ -19,18 +18,12 @@ describe('keyboard-info-compiler', function () {
1918
const kmpFilename = makePathToFixture('khmer_angkor', 'build', 'khmer_angkor.kmp');
2019
const buildKeyboardInfoFilename = makePathToFixture('khmer_angkor', 'build', 'khmer_angkor.keyboard_info');
2120

22-
const kmpCompiler = new KmpCompiler(callbacks);
23-
const kmpJsonData = kmpCompiler.transformKpsToKmpObject(kpsFilename);
24-
2521
const compiler = new KeyboardInfoCompiler(callbacks);
2622
const data = compiler.writeMergedKeyboardInfoFile({
2723
kmpFilename,
28-
kmpJsonData,
29-
keyboard_id: 'khmer_angkor',
3024
sourcePath: 'release/k/khmer_angkor',
3125
kpsFilename,
32-
helpLink: 'https://help.keyman.com/keyboard/khmer_angkor',
33-
keyboardFilenameJs: jsFilename,
26+
jsFilename: jsFilename,
3427
});
3528
if(data == null) {
3629
callbacks.printMessages();

developer/src/kmc/src/commands/buildClasses/BuildKeyboardInfo.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ import { CompilerCallbacks, CompilerOptions, KeymanDeveloperProject, KeymanFileT
44
import { KeyboardInfoCompiler } from '@keymanapp/kmc-keyboard-info';
55
import { loadProject } from '../../util/projectLoader.js';
66
import { InfrastructureMessages } from '../../messages/infrastructureMessages.js';
7-
import { KmpCompiler } from '@keymanapp/kmc-package';
87
import { calculateSourcePath } from '../../util/calculateSourcePath.js';
98

10-
const HelpRoot = 'https://help.keyman.com/keyboard/';
11-
129
export class BuildKeyboardInfo extends BuildActivity {
1310
public get name(): string { return 'Keyboard metadata'; }
1411
public get sourceExtension(): KeymanFileTypes.Source { return KeymanFileTypes.Source.KeyboardInfo; }
@@ -44,23 +41,14 @@ export class BuildKeyboardInfo extends BuildActivity {
4441
return false;
4542
}
4643

47-
let kmpCompiler = new KmpCompiler(callbacks);
48-
let kmpJsonData = kmpCompiler.transformKpsToKmpObject(project.resolveInputFilePath(kps));
49-
if(!kmpJsonData) {
50-
// Errors will have been emitted by KmpCompiler
51-
return false;
52-
}
5344

54-
const keyboardFileNameJs = project.resolveOutputFilePath(keyboard, KeymanFileTypes.Source.KeymanKeyboard, KeymanFileTypes.Binary.WebKeyboard);
55-
const keyboard_id = callbacks.path.basename(metadata.filename, KeymanFileTypes.Source.KeyboardInfo);
45+
const jsFilename = project.resolveOutputFilePath(keyboard, KeymanFileTypes.Source.KeymanKeyboard, KeymanFileTypes.Binary.WebKeyboard);
46+
5647
const compiler = new KeyboardInfoCompiler(callbacks);
5748
const data = compiler.writeMergedKeyboardInfoFile({
58-
keyboard_id,
5949
kmpFilename: project.resolveOutputFilePath(kps, KeymanFileTypes.Source.Package, KeymanFileTypes.Binary.Package),
60-
kmpJsonData,
6150
kpsFilename: project.resolveInputFilePath(kps),
62-
helpLink: HelpRoot + keyboard_id,
63-
keyboardFilenameJs: fs.existsSync(keyboardFileNameJs) ? keyboardFileNameJs : undefined,
51+
jsFilename: fs.existsSync(jsFilename) ? jsFilename : undefined,
6452
sourcePath: calculateSourcePath(infile)
6553
});
6654

0 commit comments

Comments
 (0)