Generating native code interfaces from TypeScript
Перейти к файлу
ʇuǝɯןooɟ 80fe719667
Enable renaming non-predefined types (#80)
* Enable renaming non-predefined types

* fix lint

Co-authored-by: Kelvin Mo <fumo@microsoft.com>
2021-11-05 12:54:59 +08:00
.github/workflows Remove custom tag in release pipeline (#75) 2021-08-31 16:05:23 +08:00
bin Rename ts-codegen to ts-gyb (#57) 2021-07-29 17:34:01 +08:00
demo Enable renaming non-predefined types (#80) 2021-11-05 12:54:59 +08:00
documentation Support generating a single file (#76) 2021-09-13 16:36:25 +08:00
example-templates Generate module documentation and fix basic demo (#55) 2021-07-29 06:26:56 +00:00
src Enable renaming non-predefined types (#80) 2021-11-05 12:54:59 +08:00
test Migrate to use TypeChecker if possible (#72) 2021-08-31 12:18:05 +08:00
.eslintrc.js Handle errors and add more unit test cases (#30) 2021-07-27 16:09:05 +08:00
.gitignore Demo project: Mini Editor (#31) 2021-07-27 16:58:09 +08:00
CODE_OF_CONDUCT.md Initial CODE_OF_CONDUCT.md commit 2020-12-01 00:52:40 -08:00
LICENSE Initial LICENSE commit 2020-12-01 00:52:42 -08:00
README.md Support generating a single file (#76) 2021-09-13 16:36:25 +08:00
SECURITY.md Initial SECURITY.md commit 2020-12-01 00:52:46 -08:00
package-lock.json Bump version to 0.6.1 (#78) 2021-10-13 14:28:42 +08:00
package.json Bump version to 0.6.1 (#78) 2021-10-13 14:28:42 +08:00
prettier.config.js Initialize project 2020-12-09 13:58:26 +08:00
tsconfig.json Rename ts-codegen to ts-gyb (#57) 2021-07-29 17:34:01 +08:00

README.md

ts-gyb

GitHub Workflow Status npm

ts-gyb is a multi-purpose code generation tool based on TypeScript interfaces. It was initially designed for generating boilerplate interfacing code between web and mobile platforms in hybrid apps. With custom templates, it can generate code for any use from TypeScript.

GYB: Generate Your Boilerplate. Inspired by swift/gyb.py.

Features

  • Generate production ready and well documented code from TypeScript code
  • Support most of TypeScript syntax
  • Fully customizable template makes the generated code fit perfectly into any project

Installation

To use ts-gyb with an existing project managed by npm, it is recommended to install ts-gyb as a dev dependency:

npm install --save-dev ts-gyb

You can also install ts-gyb globally:

npm install --global ts-gyb

Get Started

1. Define TypeScript interfaces

To generate code for a TypeScript interface, add the JSDoc tag @shouldExport true to documentation of the interface.

/**
 * @shouldExport true
 */
interface EditorSelection {
  getSelectionPath(): SelectionPath | null;

  // Function can have only one argument and it must be an object
  setSelection(args: { selectionPath: SelectionPath }): void;

  // Destructuring assignment is allowed
  setSelectionAtElement({ id, removeElement }: { id: string, removeElement?: boolean }): void;
}

ts-gyb only handles method members like methodName(): ReturnType;. If a method needs to take in parameters, it must define one object argument. The type of this object can either be an interface or an object literal. For more information on how to write interfaces for ts-gyb, please refer to TypeScript Interface Guide.

2. Provide templates

ts-gyb generates code from mustache templates. At least two templates are needed:

  • Module template: used to generate a file for every TypeScript interface
  • Custom type template: used to generate the file that hosts all TypeScript types found in method parameters or return types

For generating boilerplate interfacing code between web and mobile platforms, a good starting point is the example templates. You can copy the templates to your project and modify them according to your project's needs.

Please refer to Template Guide for all available variables, and mustache Manual for mustache template syntax.

3. Create a configuration file

Create a json configuration file in your project:

{
  "parsing": {
    "source": {
      "default": ["path/to/interfaces.ts"]
    }
  },
  "rendering": {
    "swift": {
      "templates": {
        "default": "path/to/module-interface.mustache"
      },
      "outputPath": {
        "default": "path/to/output/directory"
      },
      "namedTypesTemplatePath": "path/to/named-types.mustache",
      "namedTypesOutputPath": "path/to/output/directory/SharedTypes.swift"
    }
  }
}

All paths are relative to the configuration file. For all supported options in the configuration file, please refer to Configuration Reference.

4. Run ts-gyb

npx ts-gyb --config path/to/config.json

Or if ts-gyb is installed globally:

ts-gyb --config path/to/config.json

Generated code can be found at the output directory specified in the configuration file.

Demos

mini-editor contains an iOS and an Android rich text editing app. Their editors are powered by the same TypeScript web project.

The web part provides some rich text formatting operations that can be invoked from native code (Kotlin/Swift). The operations are defined in IEditor.ts. ts-gyb generates EditorBridge.swift from that TypeScript interface.

Documentation

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Trademarks

This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.