Skip to content

Commit

Permalink
lib: Unify dependency manager and registry and fix example builds
Browse files Browse the repository at this point in the history
  • Loading branch information
ewlsh committed Mar 11, 2024
1 parent 7e670a7 commit 5c5475c
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 19 deletions.
2 changes: 1 addition & 1 deletion examples/gjs/glib-2-spawn-command/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"watch": "yarn build:app --watch",
"build": "yarn build:types && yarn build:app",
"build:app": "yarn clear:ts && webpack --env production",
"build:types": "yarn ts-for-gir generate GLib-2.0 --noNamespace -t cjs",
"build:types": "yarn ts-for-gir generate GLib-2.0 --noNamespace",
"clear:types": "rm -rf ./@types",
"clear:ts": "rm -rf ./dist",
"clear": "yarn clear:ts && yarn clear:types",
Expand Down
2 changes: 1 addition & 1 deletion examples/gjs/gtk-3-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"watch": "yarn build:app --watch",
"build": "yarn build:types && yarn build:app",
"build:app": "yarn clear:ts && webpack --env production",
"build:types": "yarn ts-for-gir generate Gtk-3.0 -e gjs --noNamespace -t cjs",
"build:types": "yarn ts-for-gir generate Gtk-3.0 --noNamespace",
"clear:types": "rm -rf ./@types",
"clear:ts": "rm -rf ./dist",
"clear": "yarn clear:ts && yarn clear:types",
Expand Down
2 changes: 1 addition & 1 deletion examples/gjs/gtk-3-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"build": "yarn build:types && yarn build:app",
"build:app": "yarn clear:app && webpack --env production",
"build:types": "yarn ts-for-gir generate Gtk-3.0 GtkSource-3.0 -e gjs --noNamespace -t cjs",
"build:types": "yarn ts-for-gir generate Gtk-3.0 GtkSource-3.0 --noNamespace",
"clear:types": "rm -rf ./@types",
"clear:app": "rm -rf ./dist",
"clear": "yarn clear:app && yarn clear:types",
Expand Down
2 changes: 1 addition & 1 deletion examples/gjs/gtk-3-hello/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"build": "yarn build:types && yarn build:app",
"build:app": "yarn clear:app && webpack --env production",
"build:types": "yarn ts-for-gir generate Gtk-3.0 GtkSource-3.0 -e gjs --noNamespace -t cjs",
"build:types": "yarn ts-for-gir generate Gtk-3.0 GtkSource-3.0 --noNamespace",
"clear:types": "rm -rf ./@types",
"clear:app": "rm -rf ./dist",
"clear": "yarn clear:app && yarn clear:types",
Expand Down
2 changes: 1 addition & 1 deletion examples/gjs/timers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build:types": "yarn ts-for-gir generate Gtk-4.0 -e gjs --noNamespace -t cjs",
"build:types": "yarn ts-for-gir generate Gtk-4.0 --noNamespace",
"build:app": "yarn node esbuild.js",
"build": "yarn build:types && yarn build:app",
"start:app": "gjs dist/main.js",
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@
},
"workspaces": [
"examples/*/*",
"packages/*",
"types/*"
"packages/*"
],
"packageManager": "[email protected]"
}
2 changes: 1 addition & 1 deletion packages/cli/src/commands/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const handler = async (args: ConfigFlags) => {
return Logger.error(ERROR_NO_MODULES_FOUND(config.girDirectories))
}
const tsForGir = new GenerationHandler(generateConfig, GeneratorType.HTML_DOC)
const registry = moduleLoader.dependencyManager.registry
const registry = moduleLoader.dependencyManager

await tsForGir.start(
Array.from(keep).map((girModuleResolvedBy) => girModuleResolvedBy.module),
Expand Down
7 changes: 4 additions & 3 deletions packages/cli/src/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@ const handler = async (args: ConfigFlags) => {
config.ignore || [],
config.ignoreVersionConflicts,
)

if (keep.length === 0) {
return Logger.error(ERROR_NO_MODULES_FOUND(config.girDirectories))
}

const tsForGir = new GenerationHandler(generateConfig, GeneratorType.TYPES)

const girModules = Array.from(keep).map((girModuleResolvedBy) => girModuleResolvedBy.module)
// const girModulesGrouped = Object.values(grouped)

moduleLoader.dependencyManager.registry.registerFormatter('dts', new TypeScriptFormatter())
await tsForGir.start(girModules, moduleLoader.dependencyManager.registry)
moduleLoader.dependencyManager.registerFormatter('dts', new TypeScriptFormatter())
await tsForGir.start(girModules, moduleLoader.dependencyManager)
}

const examples: ReadonlyArray<[string, string?]> = [
Expand Down
22 changes: 19 additions & 3 deletions packages/cli/src/module-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ export class ModuleLoader {
this.log.log(`Parsing ${dependency.path}...`)
const fileContents = await readFile(dependency.path, 'utf8')
const result = parser.parseGir(fileContents)
const girModule = GirModule.load(result, this.config, this.dependencyManager.registry)
const girModule = GirModule.load(result, this.config, this.dependencyManager)
// Figure out transitive module dependencies
this.extendDependencyMapByGirModule(girModule)
return girModule
Expand Down Expand Up @@ -548,9 +548,25 @@ export class ModuleLoader {
ignore: string[] = [],
doNotAskForVersionOnConflict = true,
): Promise<{ keep: GirModuleResolvedBy[]; grouped: GirModulesGroupedMap; ignore: string[]; failed: Set<string> }> {
const foundPackageNames = await this.findPackageNames(packageNames, ignore)
const foundPackageNames = await this.findPackageNames([...packageNames], ignore)
// Always require these because GJS does...
const GLib = this.dependencyManager.get('GLib', '2.0')
const Gio = this.dependencyManager.get('Gio', '2.0')
const GObject = this.dependencyManager.get('GObject', '2.0')

const dependencies = this.packageNamesToDependencies(foundPackageNames)
const { loaded, failed } = await this.loadGirModules(dependencies, ignore)

const { loaded, failed } = await this.loadGirModules(
[
GLib,
Gio,
GObject,
...dependencies.filter(
(dep) => dep.namespace !== 'GLib' && dep.namespace !== 'Gio' && dep.namespace !== 'GObject',
),
],
ignore,
)
let keep: GirModuleResolvedBy[] = []
if (doNotAskForVersionOnConflict) {
keep = loaded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class ModuleGenerator extends FormatGenerator<string[]> {
importName: girModule.importName,
girModule,
pkgData,
registry: this.dependencyManager.registry,
registry: this.dependencyManager,
},
girModule.packageName,
girModule.transitiveDependencies,
Expand Down Expand Up @@ -1336,7 +1336,7 @@ class ModuleGenerator extends FormatGenerator<string[]> {

const target = `${girModule.importName}.d.ts`

const formatter = this.dependencyManager.registry.getFormatter('dts')
const formatter = this.dependencyManager.getFormatter('dts')

let contents!: string
try {
Expand Down
1 change: 1 addition & 0 deletions packages/generator-typescript/templates/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ _%>
"default": "./cairo.js"
},
<%_ } _%>
<%_ if (entryPointName === 'gjs') { _%>
"./ambient": {
"types": "./ambient.d.ts",
"default": "./ambient.js"
Expand Down
5 changes: 3 additions & 2 deletions packages/lib/src/dependency-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import type { Dependency, GenerateConfig, GirInclude } from './types/index.js'
import type { GirModule } from './gir-module.js'
import { GirNSRegistry } from './registry.js'

export class DependencyManager {
export class DependencyManager extends GirNSRegistry {
protected log: Logger
protected transformation: Transformation
registry = new GirNSRegistry()

cache: { [packageName: string]: Dependency } = {}

static instance?: DependencyManager

protected constructor(protected readonly config: GenerateConfig) {
super()

this.transformation = new Transformation(config)
this.log = new Logger(config.verbose, 'DependencyManager')
}
Expand Down
1 change: 0 additions & 1 deletion types
Submodule types deleted from 1ab7cd

0 comments on commit 5c5475c

Please sign in to comment.