From 06b30103ae06f621fc8e9e638f74fad14c42d005 Mon Sep 17 00:00:00 2001 From: Pascal Garber Date: Fri, 19 Jul 2024 15:34:07 +0200 Subject: [PATCH] Generate index.d.ts files types without package.json support --- DEVELOPMENT.md | 49 ++++++------------- packages/cli/src/generation-handler.ts | 10 ++-- packages/generator-base/src/generator.ts | 2 +- .../src/html-doc-generator.ts | 2 +- .../src/type-definition-generator.ts | 31 +++++++++++- .../generator-typescript/templates/gi.d.ts | 7 +++ .../templates/index-locally.d.ts | 7 +++ .../generator-typescript/templates/index.d.ts | 5 +- tests/types-locally/main.ts | 6 +-- types | 2 +- 10 files changed, 72 insertions(+), 49 deletions(-) create mode 100644 packages/generator-typescript/templates/gi.d.ts create mode 100644 packages/generator-typescript/templates/index-locally.d.ts diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index 236d1e292..db193e8fb 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -68,28 +68,19 @@ Install the dependencies and build: ```bash yarn install -yarn run build +yarn build ``` Generate example type definitions: ```bash -yarn run test:girs:local:gtk4 +yarn build:types:packages:gtk4 ``` ## Gir XML Format See [gobject-introspection/docs/gir-1.2.rnc](https://gitlab.gnome.org/GNOME/gobject-introspection/-/blob/master/docs/gir-1.2.rnc) for type definitions. -## Debugging - -It is strongly recommended to use the debugger. For Visual Studio Code there are some predefined launch configurations for it. -The VSCode launch.json configuration is intended that Node was installed via [NVM](https://github.com/nvm-sh/nvm) and yarn was installed globally using npm. - -Take a look at the following video, to see how the debugging process looks like: - -https://user-images.githubusercontent.com/1073989/150143112-1c0a2dab-3d4c-4472-818f-8404a620cc53.mp4 - ## Yarn We are using [Yarn](https://yarnpkg.com/) and its [workspace feature](https://yarnpkg.com/features/workspaces). @@ -100,30 +91,12 @@ We have created some of them for development. npm install -g yarn ``` -### Validate +## Validate To validate all your generated type definition files in this project run ```bash -yarn run validate:types:local -``` - -To validate only your type definition files for GJS - -```bash -yarn run validate:types:gjs -``` - -To validate only your type definition files for node-gtk - -```bash -yarn run validate:types:node -``` - -You can also validate a single type definition file like this - -```bash -yarn run validate -- ./@types/gobject-2.0.d.ts +yarn validate:types:packages ``` ## Test @@ -141,9 +114,19 @@ git submodule update --init Now you can run the test with ```bash -yarn run test:girs:local +yarn test:girs:packages ``` +## Update gir files + +To update the gir files we have introduced a new cli command `copy`, you can run it with our default settings as follows: + +```bash +yarn copy:girs +``` + +This copies the latest gir file found on your machine into this repository, so that we can ensure that all developers can use the same gir files and that we always use the latest versions if possible. + # FAQ Problem: I get the following error: @@ -156,7 +139,7 @@ Solution: ```bash sudo sysctl -w vm.max_map_count=262144 -NODE_OPTIONS=--max-old-space-size=25600 yarn run ... +NODE_OPTIONS=--max-old-space-size=25600 yarn ... ``` # Related Projects diff --git a/packages/cli/src/generation-handler.ts b/packages/cli/src/generation-handler.ts index bf940510e..cd16e2d2c 100644 --- a/packages/cli/src/generation-handler.ts +++ b/packages/cli/src/generation-handler.ts @@ -10,7 +10,7 @@ import { } from '@ts-for-gir/lib' import { GeneratorType, Generator } from '@ts-for-gir/generator-base' import { TypeDefinitionGenerator } from '@ts-for-gir/generator-typescript' -// import { HtmlDocGenerator } from '@ts-for-gir/generator-html-doc' +import { HtmlDocGenerator } from '@ts-for-gir/generator-html-doc' import type { OptionsGeneration, NSRegistry } from '@ts-for-gir/lib' @@ -28,9 +28,9 @@ export class GenerationHandler { case GeneratorType.TYPES: this.generator = new TypeDefinitionGenerator(config) break - // case GeneratorType.HTML_DOC: - // this.generator = new HtmlDocGenerator(config) - // break + case GeneratorType.HTML_DOC: + this.generator = new HtmlDocGenerator(config) + break default: throw new Error('Unknown Generator') } @@ -64,7 +64,7 @@ export class GenerationHandler { await this.generator.generate(registry, girModule) } - await this.generator.finish(registry) + await this.generator.finish(registry, girModules) this.log.success(GENERATING_TYPES_DONE) } diff --git a/packages/generator-base/src/generator.ts b/packages/generator-base/src/generator.ts index ce13cb8f6..ed77c6a57 100644 --- a/packages/generator-base/src/generator.ts +++ b/packages/generator-base/src/generator.ts @@ -3,5 +3,5 @@ import type { GirModule, NSRegistry } from '@ts-for-gir/lib' export interface Generator { start(registry: NSRegistry): Promise generate(registry: NSRegistry, module: GirModule): Promise - finish(registry: NSRegistry): Promise + finish(registry: NSRegistry, girModules: GirModule[]): Promise } diff --git a/packages/generator-html-doc/src/html-doc-generator.ts b/packages/generator-html-doc/src/html-doc-generator.ts index 168ab3dbc..c4ca981b0 100644 --- a/packages/generator-html-doc/src/html-doc-generator.ts +++ b/packages/generator-html-doc/src/html-doc-generator.ts @@ -21,7 +21,7 @@ export class HtmlDocGenerator implements Generator { throw new Error('Method not implemented.') } - finish(_registry: NSRegistry): Promise { + finish(_registry: NSRegistry, _girModules: GirModule[]): Promise { throw new Error('Method not implemented.') } } diff --git a/packages/generator-typescript/src/type-definition-generator.ts b/packages/generator-typescript/src/type-definition-generator.ts index d7242ca58..90fec27fc 100644 --- a/packages/generator-typescript/src/type-definition-generator.ts +++ b/packages/generator-typescript/src/type-definition-generator.ts @@ -101,6 +101,30 @@ export class TypeDefinitionGenerator implements Generator { } } + async exportAllModules(girModules: GirModule[]) { + const { config, dependencyManager } = this + + if (config.package) { + throw new Error('Export all modules is not implemented for package.json mode') + } + + // TODO: Print to stdout + if (!config.outdir) return + + const templateProcessor = new TemplateProcessor( + { + registry: dependencyManager, + girModules, + }, + 'index', + dependencyManager.all(), + config, + ) + + await templateProcessor.create('gi.d.ts', config.outdir, 'gi.d.ts') + await templateProcessor.create('index-locally.d.ts', config.outdir, 'index.d.ts') + } + public async generate(registry: NSRegistry, module: GirModule) { const moduleGenerator = new ModuleGenerator(module, this.config) await moduleGenerator.exportModule(registry, module) @@ -112,8 +136,13 @@ export class TypeDefinitionGenerator implements Generator { } } - public async finish() { + public async finish(_registry: NSRegistry, girModules: GirModule[]) { // GJS internal stuff await this.exportGjs() + + // index file for all modules + if (!this.config.package) { + await this.exportAllModules(girModules) + } } } diff --git a/packages/generator-typescript/templates/gi.d.ts b/packages/generator-typescript/templates/gi.d.ts new file mode 100644 index 000000000..9d761c140 --- /dev/null +++ b/packages/generator-typescript/templates/gi.d.ts @@ -0,0 +1,7 @@ +/** + * This file exports all GIR module type definitions. + */ + +<%_ for (const girModule of girModules) { -%> +import './<%= girModule.importName %>.d.ts'; +<%_ } -%> \ No newline at end of file diff --git a/packages/generator-typescript/templates/index-locally.d.ts b/packages/generator-typescript/templates/index-locally.d.ts new file mode 100644 index 000000000..32b69a445 --- /dev/null +++ b/packages/generator-typescript/templates/index-locally.d.ts @@ -0,0 +1,7 @@ +/** + * This file exports all type definitions. + */ + +import './gjs.d.ts'; +import './dom.d.ts'; +import './gi.d.ts'; \ No newline at end of file diff --git a/packages/generator-typescript/templates/index.d.ts b/packages/generator-typescript/templates/index.d.ts index 3e6a75628..600abc11d 100644 --- a/packages/generator-typescript/templates/index.d.ts +++ b/packages/generator-typescript/templates/index.d.ts @@ -1,9 +1,10 @@ -<%# This EJS template is used for the generated index.js file of each GIR module like Gtk-4.0, GObject-2.0, ... %> -/* +/** * Type Definitions for Gjs (https://gjs.guide/) * * These type definitions are automatically generated, do not edit them by hand. * If you found a bug fix it in `<%= APP_NAME %>` or create a bug report on <%= APP_SOURCE %> + * + * This template is used to generate the index.d.ts file of each GIR module like Gtk-4.0, GObject-2.0, ... */ import './<%= importName %>-ambient.d.ts'; diff --git a/tests/types-locally/main.ts b/tests/types-locally/main.ts index 1480c5ea3..84e78595b 100644 --- a/tests/types-locally/main.ts +++ b/tests/types-locally/main.ts @@ -1,8 +1,4 @@ -import './@types/gjs.d.ts' -import './@types/dom.d.ts' -import './@types/gtk-2.0.d.ts' -import './@types/gtk-3.0.d.ts' -import './@types/gtk-4.0.d.ts' +import './@types/index.d.ts' import Gettext from 'gettext' console.log(Gettext.textdomain) diff --git a/types b/types index 0284b43c9..028a18433 160000 --- a/types +++ b/types @@ -1 +1 @@ -Subproject commit 0284b43c9c9992b3bf1cc9445b1145008d18683e +Subproject commit 028a18433db2873a4d4314c5de686dc55b5b96b2