Skip to content

Commit

Permalink
Generate global ambient module for latest version
Browse files Browse the repository at this point in the history
  • Loading branch information
JumpLink committed Mar 3, 2024
1 parent ec345f2 commit cc6aed8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
- Create a Reporter to create a text or json file with warnings like unknown types, renaming, etc and a summary of all that (e.g. 5 type conflicts resolved, 8 unknown types of *gint)
- Update types for GJS v1.75.2, see https://gitlab.gnome.org/GNOME/gjs/-/commit/666755b3b09d765e43d415e76105b828517b5509

# Dev
- Generate global ambient module for latest version, e.g. 'gi://Gtk' or 'gi://GObject'

# 3.2.8
- Upgrade dependencies
- Update examples and removed deprecated function calls like `byteArray.toString()`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ declare module 'gi://<%= name %>?version=<%= version %>' {
}

<%# // Generate ambient module declarations Without version number if there are no conflicts or the target is an NPM package _%>
<%_ if(package || !dep.hasConflict(name, version)){ _%>
<%_ if(package && dep.isLatestVersion(pkg.namespace, pkg.version)){ _%>
declare module 'gi://<%= name %>' {
<%- moduleImportStr %>;
export default <%- girModule.importNamespace -%>;
Expand Down
42 changes: 33 additions & 9 deletions packages/lib/src/dependency-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export class DependencyManager {
return Object.values(this.cache)
}

getAllPackageNames(): string[] {
return Object.keys(this.cache)
}

/**
* Get the core dependencies
* @returns
Expand Down Expand Up @@ -130,6 +134,19 @@ export class DependencyManager {
return dependency
}

/**
* Get all dependencies with the given namespace
* @param namespace The namespace of the dependency
* @returns All dependencies with the given namespace
*/
list(namespace: string): Dependency[] {
const packageNames = this.all()
const candidates = packageNames.filter((dep) => {
return dep.namespace === namespace
})
return candidates
}

/**
* Get girModule for dependency
* @param girModules
Expand Down Expand Up @@ -165,10 +182,6 @@ export class DependencyManager {
return dependencies
}

getAllPackageNames(): string[] {
return Object.keys(this.cache)
}

/**
* Check if multiple dependencies with the given namespace exist in the cache
* @param namespace The namespace of the dependency
Expand All @@ -183,18 +196,29 @@ export class DependencyManager {
return candidates.length > 1
}

/**
* get the latest version of the dependency with the given namespace
* @param namespace The namespace of the dependency
* @returns The latest version of the dependency
*/
getLatestVersion(namespace: string): Dependency | undefined {
const candidates = this.list(namespace)
const latestVersion = candidates
.sort((a, b) => {
return a.version.localeCompare(b.version)
})
.pop()
return latestVersion
}

/**
* Check if the given version is the latest version of the dependency
* @param namespace The namespace of the dependency
* @param version The version of the dependency
* @returns
*/
isLatestVersion(namespace: string, version: string): boolean {
const hasConflict = this.hasConflict(namespace)
if (!hasConflict) {
return true
}
const latestVersion = this.find(namespace)
const latestVersion = this.getLatestVersion(namespace)
return latestVersion?.version === version
}

Expand Down

0 comments on commit cc6aed8

Please sign in to comment.