All files / src/types/kpj keyman-developer-project.ts

56.8% Statements 121/213
81.25% Branches 13/16
43.47% Functions 10/23
56.8% Lines 121/213

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 2141x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x     5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x                                                               5x 5x     5x 5x     5x 5x       5x 5x                                 5x 5x         5x 5x     5x 5x                                   5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 6x 6x 6x 6x 8x 2x 2x 2x 2x 8x   8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 63x 14x 14x 63x 6x 6x 63x 63x 63x 63x 63x 63x 63x 63x 1x 1x 1x 1x 5x     5x     5x 5x 5x 1x 1x         1x 1x          
//
// Version 1.0 and 2.0 of Keyman Developer Project .kpj file
//
 
import { KeymanFileTypes } from '@keymanapp/common-types';
import { CompilerAsyncCallbacks, CompilerPathCallbacks } from "../../compiler-callbacks.js";
 
export class KeymanDeveloperProject {
  options: KeymanDeveloperProjectOptions;
  files: KeymanDeveloperProjectFile[];
  projectPath: string = '';
  readonly projectFile: KeymanDeveloperProjectFile;
 
  get projectFilename() {
    return this._projectFilename;
  }
 
  constructor(
    private _projectFilename: string,
    version: KeymanDeveloperProjectVersion,
    protected callbacks: CompilerAsyncCallbacks
  ) {
    this.projectPath = this.callbacks.path.dirname(this._projectFilename);
    this.options = new KeymanDeveloperProjectOptions(version);
    this.files = [];
    this.projectFile = new KeymanDeveloperProjectFile20(_projectFilename, this.callbacks.path);
  }
  /**
   * Adds .kmn, .xml, .kps to project based on options.sourcePath
   * @param projectFilename Full path to project.kpj (even if the file doesn't exist)
   */
  async populateFiles(): Promise<boolean> {
    if(this.options.version != '2.0') {
      throw new Error('populateFiles can only be called on a v2.0 project');
    }
    const sourcePath = this.resolveProjectPath(this.options.sourcePath);
    if(!await this.callbacks.fsAsync.exists(sourcePath)) {
      return false;
    }
    const files = await this.callbacks.fsAsync.readdir(sourcePath);
    for(const file of files.filter(file => file.type == 'file')) {
      const fullPath = this.callbacks.path.join(sourcePath, file.filename);
      if(KeymanFileTypes.filenameIs(file.filename, KeymanFileTypes.Source.LdmlKeyboard)) {
        try {
          const data = await this.callbacks.fsAsync.readFile(fullPath);
          const text = new TextDecoder().decode(data);
          if(!text?.match(/<keyboard3/)) {
            // Skip this .xml because we assume it isn't really a keyboard .xml
            continue;
          }
        } catch(e) {
          // We'll just silently skip this file because we were not able to load it,
          // so let's hope it wasn't a real LDML keyboard XML :-)
          continue;
        }
      }
      if(KeymanFileTypes.sourceTypeFromFilename(file.filename) !== null) {
        const file = new KeymanDeveloperProjectFile20(fullPath, this.callbacks.path);
        this.files.push(file);
      }
    }
    return this.files.length > 0;
  }
 
  public isKeyboardProject() {
    return !!this.files.find(file => file.fileType == KeymanFileTypes.Source.KeymanKeyboard || file.fileType == KeymanFileTypes.Source.LdmlKeyboard);
  }
 
  public isLexicalModelProject() {
    return !!this.files.find(file => file.fileType == KeymanFileTypes.Source.Model);
  }
 
  private resolveProjectPath(p: string): string {
    // Replace placeholders in the target path
    return p.replace('$PROJECTPATH', this.projectPath);
  }
 
  resolveBuildPath(): string {
    // Roughly corresponds to Delphi TProject.GetTargetFileName
    let p = this.options.version == '1.0' ?
      this.options.buildPath || '$SOURCEPATH' :
      this.options.buildPath;

    // Replace placeholders in the target path
    if(this.options.version == '1.0') {
      // TODO: do we need to support $VERSION?
      // $SOURCEPATH only supported in 1.0 projects
      p = p.replace('$SOURCEPATH', this.callbacks.path.dirname(this._projectFilename));
    }

    p = this.resolveProjectPath(p);

    return p;
  }
 
  getOutputFilePath(type: KeymanFileTypes.Binary) {
    const p = this.resolveBuildPath();
    const f = this.callbacks.path.basename(this._projectFilename, KeymanFileTypes.Source.Project) + type;
    return this.callbacks.path.normalize(this.callbacks.path.join(p, f));
  }
 
  resolveInputFilePath(file: KeymanDeveloperProjectFile): string {
    return this.callbacks.fsAsync.resolveFilename(this._projectFilename, file.filePath);
  }
 
  resolveOutputFilePath(file: KeymanDeveloperProjectFile, sourceExt: string, targetExt: string): string {
    // Roughly corresponds to Delphi TProject.GetTargetFileName
    let p = this.options.version == '1.0' ?
      this.options.buildPath || '$SOURCEPATH' :
      this.options.buildPath;

    // Replace placeholders in the target path
    if(this.options.version == '1.0') {
      // TODO: do we need to support $VERSION?
      // $SOURCEPATH only supported in 1.0 projects
      p = p.replace('$SOURCEPATH', this.callbacks.path.dirname(this.resolveInputFilePath(file)));
    }

    p = this.resolveProjectPath(p);

    const f = file.filename.replace(new RegExp(`\\${sourceExt}$`, 'i'), targetExt);
    return this.callbacks.path.normalize(this.callbacks.path.join(p, f));
  }
 
};
 
export enum KeymanDeveloperProjectType {
  Keyboard,
  LexicalModel
};
 
export type KeymanDeveloperProjectVersion =
  "1.0" |   // Keyman Developer <17.0: All files referenced in .kpj
  "2.0";    // Keyman Developer 17.0+: Files in sub-folders implicitly included
 
export class KeymanDeveloperProjectOptions {
  buildPath: string;
  sourcePath: string;
  compilerWarningsAsErrors: boolean = false;
  warnDeprecatedCode: boolean = true;
  checkFilenameConventions: boolean = false;  // missing option defaults to False
  /**
   * Skip building .keyboard_info and .model_info files, for example in
   * unit tests or for legacy keyboards
   */
  skipMetadataFiles: boolean;
  projectType: KeymanDeveloperProjectType = KeymanDeveloperProjectType.Keyboard;
  readonly version: KeymanDeveloperProjectVersion;
  constructor(version: KeymanDeveloperProjectVersion) {
    this.version = version;
    switch(version) {
      case "1.0":
        this.buildPath = '';
        this.sourcePath = '';
        this.skipMetadataFiles = true;
        break;
      case "2.0":
        this.buildPath = '$PROJECTPATH/build';
        this.sourcePath = '$PROJECTPATH/source';
        this.skipMetadataFiles = false;
        break;
      default:
        throw new Error('Invalid version');
    }
  }
};
 
export interface KeymanDeveloperProjectFile {
  get filename(): string;
  get fileType(): string;
  readonly filePath: string;
};
 
export class KeymanDeveloperProjectFile10 implements KeymanDeveloperProjectFile {
  get filename(): string {
    return this.path.basename(this.filePath);
  }
  get fileType(): string {
    return KeymanFileTypes.fromFilename(this.filename);
  }
  details: KeymanDeveloperProjectFileDetail_Kmn & KeymanDeveloperProjectFileDetail_Kps; // 1.0 only
  childFiles: KeymanDeveloperProjectFile[]; // 1.0 only
 
  constructor(public readonly id: string, public readonly filePath: string, public readonly fileVersion:string, private path: CompilerPathCallbacks) {
    this.details = {};
    this.childFiles = [];
  }
};
 
export type KeymanDeveloperProjectFileType20 = KeymanFileTypes.Source;
 
export class KeymanDeveloperProjectFile20 implements KeymanDeveloperProjectFile {
  get filename(): string {
    return this.path.basename(this.filePath);
  }
  get fileType() {
    return KeymanFileTypes.fromFilename(this.filename);
  }
  constructor(public readonly filePath: string, private path: CompilerPathCallbacks) {
  }
};
 
export class KeymanDeveloperProjectFileDetail_Kps {
  name?: string;
  copyright?: string;
  version?: string;
};
 
export class KeymanDeveloperProjectFileDetail_Kmn {
  name?: string;
  copyright?: string;
  message?: string;
};