-
Notifications
You must be signed in to change notification settings - Fork 38
Auto gen update #166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
greggman
wants to merge
3
commits into
gpuweb:main
Choose a base branch
from
greggman:auto-gen-update
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Auto gen update #166
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import {spawnSync} from 'node:child_process'; | ||
import fs from 'node:fs'; | ||
|
||
export function execute(cmd, args, options) { | ||
const { error, status } = spawnSync(cmd, args, {...options || {}, shell: true, stdio: 'inherit'}); | ||
if (error) { | ||
throw new(Error); | ||
} | ||
if (status !== 0) { | ||
throw new Error(`${cmd} exited with status code: ${status}`); | ||
} | ||
} | ||
|
||
/** | ||
* Transform generated to something closer to what we want | ||
* so we have less manual work to do. | ||
*/ | ||
function fixupGenerated(filename) { | ||
let s = fs.readFileSync(filename, { encoding: 'utf-8' }); | ||
|
||
// replace ' | null>' with ' null | undefined>' | ||
// replace ' | null,' with ' null | undefined,' | ||
s = s.replace(/ \| null(>|,)/g, ' | null | undefined$1'); | ||
|
||
// replace Array<x> with Iterable<x> | ||
s = s.replace(/\bArray<(.*?)>/g, 'Iterable<$1>'); | ||
|
||
// add new(): never; | ||
s = s.replace(/(\ndeclare\svar\s\w+:\s\{\n\s+prototype:.*?\n)\};/g, '$1 new(): never;\n};'); | ||
|
||
// replace : GPUExtent3D -> : GPUExtent3DStrict | ||
s = s.replace(/: GPUExtent3D\b/g, ': GPUExtent3DStrict'); | ||
|
||
// replace AllowSharedBufferSource -> BufferSource | SharedArrayBuffer | ||
s = s.replace(/AllowSharedBufferSource/g, 'BufferSource | SharedArrayBuffer'); | ||
|
||
// replace Promise<... | undefined> with Promise<...> | ||
s = s.replace(/( Promise<[^>]*?)\s+\|\s+undefined\s*>/g, '$1>'); | ||
|
||
// fix constants | ||
const constants = [ | ||
'GPUBufferUsage', | ||
'GPUColorWrite', | ||
'GPUMapMode', | ||
'GPUShaderStage', | ||
'GPUTextureUsage', | ||
]; | ||
const constantRE = new RegExp(`interface (${constants.join('|')}) \\{`, 'g'); | ||
s = s.replace(constantRE, 'declare var $1: {'); | ||
|
||
// fix constructables | ||
const constructables = [ | ||
'GPUInternalError', | ||
'GPUPipelineError', | ||
'GPUOutOfMemoryError', | ||
'GPUUncapturedErrorEvent', | ||
'GPUValidationError', | ||
]; | ||
const constructableRE = new RegExp(`(\\ndeclare var (${constructables.join('|')}):\\s{\\n[\\s\\S]*?\\snew\\s\\([^;]*?\\));\\n};`, 'g'); | ||
s = s.replace(constructableRE, '$1: $2;\n};'); | ||
|
||
fs.writeFileSync(filename, s); | ||
} | ||
|
||
execute('./node_modules/.bin/bikeshed-to-ts', ['--in', './gpuweb/spec/index.bs', '--out', './generated/index.d.ts', '--forceGlobal', '--nominal']) | ||
fixupGenerated('./generated/index.d.ts'); | ||
execute('./node_modules/.bin/prettier', ['-w', 'generated/index.d.ts']); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most (but not all) of these things would ideally be changed in the upstream generator. That repo is external but it was created for webgpu so we can change it freely.
Since some of these do make sense to keep here, it's not a big deal, since we need this script anyway. I'm fine with accepting it as is.