Skip to content

Commit

Permalink
Generate index.d.ts files types without package.json support
Browse files Browse the repository at this point in the history
  • Loading branch information
JumpLink committed Jul 19, 2024
1 parent 744469f commit 06b3010
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 49 deletions.
49 changes: 16 additions & 33 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/src/generation-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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')
}
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/generator-base/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import type { GirModule, NSRegistry } from '@ts-for-gir/lib'
export interface Generator {
start(registry: NSRegistry): Promise<void>
generate(registry: NSRegistry, module: GirModule): Promise<void>
finish(registry: NSRegistry): Promise<void>
finish(registry: NSRegistry, girModules: GirModule[]): Promise<void>
}
2 changes: 1 addition & 1 deletion packages/generator-html-doc/src/html-doc-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class HtmlDocGenerator implements Generator {
throw new Error('Method not implemented.')
}

finish(_registry: NSRegistry): Promise<void> {
finish(_registry: NSRegistry, _girModules: GirModule[]): Promise<void> {
throw new Error('Method not implemented.')
}
}
31 changes: 30 additions & 1 deletion packages/generator-typescript/src/type-definition-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}
}
7 changes: 7 additions & 0 deletions packages/generator-typescript/templates/gi.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* This file exports all GIR module type definitions.
*/

<%_ for (const girModule of girModules) { -%>
import './<%= girModule.importName %>.d.ts';
<%_ } -%>
7 changes: 7 additions & 0 deletions packages/generator-typescript/templates/index-locally.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* This file exports all type definitions.
*/

import './gjs.d.ts';
import './dom.d.ts';
import './gi.d.ts';
5 changes: 3 additions & 2 deletions packages/generator-typescript/templates/index.d.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
6 changes: 1 addition & 5 deletions tests/types-locally/main.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion types
Submodule types updated 664 files

0 comments on commit 06b3010

Please sign in to comment.