Repository navigation
fix: remove hardcoded OpenCLI schema versions peppered throughout repo #26
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
Merged
Merged
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
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
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
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
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
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
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
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
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
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
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
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
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
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,145 @@ | ||
| // Command genschema derives generated constants and synchronized schema copies | ||
| // from the repository's canonical spec.schema.json. | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "flag" | ||
| "fmt" | ||
| "go/format" | ||
| "io/fs" | ||
| "os" | ||
| "path/filepath" | ||
| "regexp" | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| type schema struct { | ||
| ID string `json:"$id"` | ||
| Properties struct { | ||
| OpenCLIVersion struct { | ||
| Enum []string `json:"enum"` | ||
| } `json:"opencliVersion"` | ||
| } `json:"properties"` | ||
| } | ||
|
|
||
| func main() { | ||
| schemaPath := flag.String("schema", "spec.schema.json", "path to the canonical schema") | ||
| goOutput := flag.String("out", "schema_version_gen.go", "path to the generated Go file") | ||
| updateDocs := flag.Bool("update-documents", false, "update checked-in document and documentation versions") | ||
| documentRoot := flag.String("document-root", ".", "root directory to scan when updating documents") | ||
| flag.Parse() | ||
|
|
||
| version, err := readVersion(*schemaPath) | ||
| if err != nil { | ||
| fail(err) | ||
| } | ||
|
|
||
| contents := fmt.Appendf(nil, `// Code generated by genschema; DO NOT EDIT. | ||
|
|
||
| package spec | ||
|
|
||
| // SchemaVersion is the version declared by the canonical OpenCLI schema. | ||
| const SchemaVersion = %s | ||
| `, strconv.Quote(version)) | ||
| contents, err = format.Source(contents) | ||
| if err != nil { | ||
| fail(fmt.Errorf("format generated Go source: %w", err)) | ||
| } | ||
| if err := os.WriteFile(*goOutput, contents, 0644); err != nil { | ||
| fail(fmt.Errorf("write %s: %w", *goOutput, err)) | ||
| } | ||
| if *updateDocs { | ||
| if err := updateDocumentVersions(*documentRoot, version); err != nil { | ||
| fail(err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func readVersion(path string) (string, error) { | ||
| contents, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return "", fmt.Errorf("read schema: %w", err) | ||
| } | ||
|
|
||
| var document schema | ||
| if err := json.Unmarshal(contents, &document); err != nil { | ||
| return "", fmt.Errorf("parse schema: %w", err) | ||
| } | ||
|
|
||
| versions := document.Properties.OpenCLIVersion.Enum | ||
|
|
||
| if len(versions) < 1 || versions[len(versions)-1] == "" { | ||
| return "", fmt.Errorf("schema properties.opencliVersion.enum must contain at least one non-empty string") | ||
| } | ||
| // take most recent version (versions will be in ascending order in the schema) | ||
| version := versions[len(versions)-1] | ||
|
|
||
| const schemaSuffix = "/spec.schema.json" | ||
| if !strings.HasSuffix(document.ID, schemaSuffix) { | ||
| return "", fmt.Errorf("schema $id must end with %q", schemaSuffix) | ||
| } | ||
| idWithoutSuffix := strings.TrimSuffix(document.ID, schemaSuffix) | ||
| idVersion := idWithoutSuffix[strings.LastIndex(idWithoutSuffix, "/")+1:] | ||
| if idVersion != version { | ||
| return "", fmt.Errorf("schema $id version %q does not match opencliVersion enum %q", idVersion, version) | ||
| } | ||
|
|
||
| return version, nil | ||
| } | ||
|
|
||
| var ( | ||
| quotedVersionPattern = regexp.MustCompile(`(opencliVersion["']?\s*:\s*["'])([^"']+)(["'])`) | ||
| bareVersionPattern = regexp.MustCompile(`(opencliVersion\s*:\s*)([0-9][^[:space:]#,}]+)`) | ||
| ) | ||
|
|
||
| func updateDocumentVersions(root, version string) error { | ||
| return filepath.WalkDir(root, func(path string, entry fs.DirEntry, err error) error { | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if entry.IsDir() { | ||
| switch entry.Name() { | ||
| case ".git", ".next", ".scratch", "build", "dist", "node_modules", "out", "scratch": | ||
| return filepath.SkipDir | ||
| } | ||
| return nil | ||
| } | ||
| if entry.Name() == "spec.schema.json" { | ||
| return nil | ||
| } | ||
|
|
||
| extension := strings.ToLower(filepath.Ext(path)) | ||
| switch extension { | ||
| case ".json", ".md", ".txt", ".yaml", ".yml": | ||
| default: | ||
| return nil | ||
| } | ||
|
|
||
| contents, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| updated := quotedVersionPattern.ReplaceAll(contents, []byte(`${1}`+version+`${3}`)) | ||
| updated = bareVersionPattern.ReplaceAll(updated, []byte(`${1}`+version)) | ||
| if string(updated) == string(contents) { | ||
| return nil | ||
| } | ||
|
|
||
| info, err := entry.Info() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if err := os.WriteFile(path, updated, info.Mode().Perm()); err != nil { | ||
| return fmt.Errorf("update %s: %w", path, err) | ||
| } | ||
| fmt.Println("updated", path) | ||
| return nil | ||
| }) | ||
| } | ||
|
|
||
| func fail(err error) { | ||
| fmt.Fprintln(os.Stderr, err) | ||
| os.Exit(1) | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.