Skip to content

Commit

Permalink
chore(developer): refactor KeymanDeveloperProjectFile
Browse files Browse the repository at this point in the history
Turns KeymanDeveloperProjectFile into an interface, and removes
redundant data in filename and fileType fields, calculating these from
filePath instead. Maintains separate KeymanDeveloperProjectFile10 and
KeymanDeveloperProjectFile20 classes implementing the base interface for
now, although that may be a target for future consolidation (lowpri).
  • Loading branch information
mcdurdin committed Aug 2, 2023
1 parent 48c0f09 commit d8e03e0
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 35 deletions.
46 changes: 22 additions & 24 deletions common/web/types/src/kpj/keyman-developer-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ export class KeymanDeveloperProject {
}

public isKeyboardProject() {
return !!this.files.find(file => file.getFileType() == KeymanFileTypes.Source.KeymanKeyboard || file.getFileType() == KeymanFileTypes.Source.LdmlKeyboard);
return !!this.files.find(file => file.fileType == KeymanFileTypes.Source.KeymanKeyboard || file.fileType == KeymanFileTypes.Source.LdmlKeyboard);
}

public isLexicalModelProject() {
return !!this.files.find(file => file.getFileType() == KeymanFileTypes.Source.Model);
return !!this.files.find(file => file.fileType == KeymanFileTypes.Source.Model);
}

public addMetadataFile() {
Expand Down Expand Up @@ -127,41 +127,39 @@ export class KeymanDeveloperProjectOptions {
}
};

export type KeymanDeveloperProjectFile = KeymanDeveloperProjectFile10 | KeymanDeveloperProjectFile20;

export class KeymanDeveloperProjectFile10 {
readonly id: string; // 1.0 only
readonly filename: string;
export interface KeymanDeveloperProjectFile {
get filename(): string;
get fileType(): string;
readonly filePath: string;
readonly fileVersion: string; // 1.0 only
};

export class KeymanDeveloperProjectFile10 implements KeymanDeveloperProjectFile {
get filename(): string {
return this.callbacks.path.basename(this.filePath);
}
get fileType(): string {
return KeymanFileTypes.sourceTypeFromFilename(this.filename);
}
details: KeymanDeveloperProjectFileDetail_Kmn & KeymanDeveloperProjectFileDetail_Kps; // 1.0 only
childFiles: KeymanDeveloperProjectFile[]; // 1.0 only
constructor(id: string, filename: string, filePath: string, fileVersion:string) {

constructor(public readonly id: string, public readonly filePath: string, public readonly fileVersion:string, private readonly callbacks: CompilerCallbacks) {
this.details = {};
this.childFiles = [];
this.id = id;
this.filename = filename;
this.filePath = filePath;
this.fileVersion = fileVersion;
// we now ignore the fileType on load
}
getFileType() {
return KeymanFileTypes.sourceTypeFromFilename(this.filename);
}
};

export type KeymanDeveloperProjectFileType20 = KeymanFileTypes.Source;

export class KeymanDeveloperProjectFile20 {
readonly filename: string;
readonly filePath: string;
constructor(filePath: string, private callbacks: CompilerCallbacks) {
this.filename = this.callbacks.path.basename(filePath);
this.filePath = filePath;
export class KeymanDeveloperProjectFile20 implements KeymanDeveloperProjectFile {
get filename(): string {
return this.callbacks.path.basename(this.filePath);
}
getFileType() {
get fileType() {
return KeymanFileTypes.sourceTypeFromFilename(this.filename);
}
constructor(public readonly filePath: string, private readonly callbacks: CompilerCallbacks) {
}
};

export class KeymanDeveloperProjectFileDetail_Kps {
Expand Down
4 changes: 2 additions & 2 deletions common/web/types/src/kpj/kpj-file-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ export class KPJFileReader {
for (let sourceFile of project.Files?.File) {
let file: KeymanDeveloperProjectFile10 = new KeymanDeveloperProjectFile10(
sourceFile.ID || '',
sourceFile.Filename || '',
(sourceFile.Filepath || '').replace(/\\/g, '/'),
sourceFile.FileVersion || ''
sourceFile.FileVersion || '',
this.callbacks
);
if (sourceFile.Details) {
file.details.copyright = sourceFile.Details.Copyright;
Expand Down
8 changes: 4 additions & 4 deletions common/web/types/test/kpj/test-kpj-file-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('kpj-file-reader', function () {
assert.equal(f.filename, 'khmer_angkor.kmn');
assert.equal(f.filePath, 'source/khmer_angkor.kmn');
assert.equal(f.fileVersion, '1.3');
assert.equal(f.getFileType(), '.kmn');
assert.equal(f.fileType, '.kmn');
assert.equal(f.details.name, 'Khmer Angkor');
assert.equal(f.details.copyright, '© 2015-2022 SIL International');
assert.equal(f.details.message, 'More than just a Khmer Unicode keyboard.');
Expand All @@ -94,7 +94,7 @@ describe('kpj-file-reader', function () {
assert.equal(f.filename, 'khmer_angkor.kps');
assert.equal(f.filePath, 'source/khmer_angkor.kps');
assert.equal(f.fileVersion, '');
assert.equal(f.getFileType(), '.kps');
assert.equal(f.fileType, '.kps');
assert.equal(f.details.name, 'Khmer Angkor');
assert.isUndefined(f.details.message);
assert.equal(f.details.copyright, '© 2015-2022 SIL International');
Expand All @@ -107,7 +107,7 @@ describe('kpj-file-reader', function () {
assert.equal(f.filename, 'khmer_angkor.ico');
assert.equal(f.filePath, 'source/khmer_angkor.ico');
assert.equal(f.fileVersion, '');
assert.equal(f.getFileType(), '.ico');
assert.equal(f.fileType, '.ico');
assert.isEmpty(f.details);
assert.lengthOf(f.childFiles, 0);

Expand All @@ -117,7 +117,7 @@ describe('kpj-file-reader', function () {
assert.equal(f.filename, 'khmer_angkor.js');
assert.equal(f.filePath, 'source/../build/khmer_angkor.js');
assert.equal(f.fileVersion, '');
assert.equal(f.getFileType(), '.js');
assert.equal(f.fileType, '.js');
assert.isEmpty(f.details);
assert.lengthOf(f.childFiles, 0);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class BuildKeyboardInfo extends BuildActivity {
}

function findProjectFile(callbacks: CompilerCallbacks, project: KeymanDeveloperProject, ext: KeymanFileTypes.Source) {
const file = project.files.find(file => file.getFileType() == ext);
const file = project.files.find(file => file.fileType == ext);
if(!file) {
callbacks.reportMessage(InfrastructureMessages.Error_FileTypeNotFound({ext}));
}
Expand Down
6 changes: 3 additions & 3 deletions developer/src/kmc/src/commands/buildClasses/BuildModelInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ export class BuildModelInfo extends BuildActivity {
return false;
}

const metadata = project.files.find(file => file.getFileType() == KeymanFileTypes.Source.ModelInfo);
const metadata = project.files.find(file => file.fileType == KeymanFileTypes.Source.ModelInfo);
if(!metadata) {
callbacks.reportMessage(InfrastructureMessages.Error_FileTypeNotFound({ext: KeymanFileTypes.Source.ModelInfo}));
return false;
}

const model = project.files.find(file => file.getFileType() == KeymanFileTypes.Source.Model);
const model = project.files.find(file => file.fileType == KeymanFileTypes.Source.Model);
if(!model) {
callbacks.reportMessage(InfrastructureMessages.Error_FileTypeNotFound({ext: KeymanFileTypes.Source.Model}));
return false;
}

const kps = project.files.find(file => file.getFileType() == KeymanFileTypes.Source.Package);
const kps = project.files.find(file => file.fileType == KeymanFileTypes.Source.Package);
if(!kps) {
callbacks.reportMessage(InfrastructureMessages.Error_FileTypeNotFound({ext: KeymanFileTypes.Source.Package}));
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class ProjectBuilder {
async buildProjectTargets(activity: BuildActivity): Promise<boolean> {
let result = true;
for(let file of this.project.files) {
if(file.getFileType().toLowerCase() == activity.sourceExtension) {
if(file.fileType.toLowerCase() == activity.sourceExtension) {
result = await this.buildTarget(file, activity) && result;
}
}
Expand Down

0 comments on commit d8e03e0

Please sign in to comment.