Skip to content
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

Kindsys: Introduce Kind framework #56492

Merged
merged 92 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
c8747d0
Update thema to latest
Oct 9, 2022
94ad641
Deal with s/Library/*Runtime/
Oct 9, 2022
f9e426c
Commit new, working results of codegen
Oct 10, 2022
eb9ada5
We like pointers now
Oct 10, 2022
743188f
Always take runtime arg for NewBase()
Oct 10, 2022
a52e7bc
Sketchy handwavy pass at entity meta framework
Oct 6, 2022
930378a
Little nibbles
Oct 6, 2022
049729c
Update pkg/framework/coremodel/entityframework.cue
Oct 6, 2022
151898d
Move file into new framework location
Oct 8, 2022
b785f6e
Introduce loaders, Go code
Oct 9, 2022
add66f4
Complete rename to kind
Oct 9, 2022
017ad27
Flesh out framework, add svg/dashboard examples
Oct 10, 2022
f9382a7
Cruft removal
Oct 11, 2022
c0aa83f
Remove generated kind go files from gitignore
Oct 11, 2022
34815a8
Merge branch 'main' into sdboyer/entity-framework-skeleton
Oct 11, 2022
fb42532
Refine maturity concept, add SlotKind
Oct 13, 2022
d8cc45f
Update embed and go deps
Oct 18, 2022
350b2db
Export PrefixWithGrafanaCUE
Oct 18, 2022
f8bf8f2
Make the loader actually work, holy crap
Oct 18, 2022
f40576f
Many small tweaks to type.cue
Oct 18, 2022
7974159
Add Apache 2 licensing exceptions for kinds
Oct 18, 2022
a7b41c7
Add new kinds dir, start of generator
Oct 19, 2022
ade745e
Roll back to earlier oapi-codegen
Oct 19, 2022
2a2c0b8
Introduce new grafana-specific CUE loaders
Oct 19, 2022
5a439d6
Introduce new tidy code generators framework
Oct 19, 2022
8a8c7bc
Catch up kind framework with tinkering
Oct 19, 2022
0e1abd2
Add slices for the generators
Oct 19, 2022
331a273
Add write/verify step to main generator
Oct 19, 2022
559732b
Many renames
Oct 19, 2022
ac4242a
Split up kind framework cue files
Oct 19, 2022
0b3a472
Use kind.Decl within generated kinds
Oct 19, 2022
94f8c7a
Create kind.SomeDecl wrapper type to cache lineages
Oct 19, 2022
017ca89
Better names again
Oct 19, 2022
867824f
Get one generated implemented, hopefully
Oct 19, 2022
73f37ee
Copy dashboard schema into new kind.cue
Oct 19, 2022
e90e729
Small fixes to make the initial gen work
Oct 19, 2022
1b68684
Put svg kind in its new home
Oct 19, 2022
9392d3e
Add generated Go dashboard type
Oct 19, 2022
6ac1b84
More renames and cleanups
Oct 20, 2022
a6eed25
Add base kind registry and generator
Oct 20, 2022
25f1da6
Stop blacklisting *_gen.go files
Oct 20, 2022
77a18d2
Change codegen output directories
Oct 20, 2022
c8b672f
Rename pkg/framework/kind to pkg/kindsys
Oct 20, 2022
a88a855
Add core structured kind generator
Oct 20, 2022
241ac5a
Add plural and machine names to kind spec
Oct 20, 2022
bbc5e81
Copy playlist over to kind system
Oct 20, 2022
b039eed
Consolidate kindsys files
Oct 20, 2022
953164a
Add raw kind generator
Oct 20, 2022
3da2d99
Merge remote-tracking branch 'origin/main' into sdboyer/entity-framew…
Oct 20, 2022
f7e26ac
Update CODEOWNERS for kind framework
Oct 20, 2022
94e71f9
Touch up comments a bit
Oct 20, 2022
01e2c0d
More docs tweaks
Oct 20, 2022
63d4274
Remove generated types to reduce noise for review
Oct 20, 2022
a2189f4
Split each generator into its own file
Oct 21, 2022
48939ce
Rename Slot kind to Composable kind
Oct 31, 2022
e7711ef
Add handwavy types for customkind loading
Oct 31, 2022
20bdae6
Guard against init calls to framework loader
Oct 31, 2022
407869d
First pass at doc on extending the kind system
Oct 31, 2022
6d6879d
Improve attribute example in docs
Oct 31, 2022
e8f4583
Fix wire imports
Nov 1, 2022
25ce57b
Add basic TS types generator
Nov 2, 2022
5060445
Fix composable kind category def
Nov 2, 2022
5270262
No need for a separate file with generate directive
Nov 2, 2022
60b7e61
Catch dashboard schema up
Nov 3, 2022
27d32c6
Rename generator types to something saner and generic
Nov 3, 2022
d86a26c
Make version configurable in ts/go generators
Nov 3, 2022
ed058e0
Add CommonMeta to ease property access
Nov 3, 2022
154be26
Add kindsys prop indicating whether lineage is group
Nov 3, 2022
b65cf35
Put all kind categories back in a single file
Nov 3, 2022
245f89b
Finish with kindsys group props
Nov 3, 2022
2195032
Refactor maturity progression per discussion
Nov 3, 2022
611c744
Convert ts veneer index generator to modular system
Nov 3, 2022
8c24e05
Move over to new jennywrites framework
Nov 6, 2022
63094fa
Strip down old coremodel generator
Nov 6, 2022
4645f17
Use public version of jennywrites
Nov 6, 2022
8521735
Pull latest thema
Nov 7, 2022
93589c4
Commit generated Go types
Nov 7, 2022
3173f69
Add header injection postprocessor
Nov 8, 2022
fe0c86c
Move sdboyer/jennywrites to grafana/codejen
Nov 8, 2022
af2e1fa
Merge remote-tracking branch 'origin/main' into sdboyer/entity-framew…
Nov 8, 2022
9380df7
Tweak header output
Nov 9, 2022
f2f7e25
Remove dashboard and playlist coremodels
Nov 9, 2022
764fe84
Fix up backend dashboards devenv test
Nov 9, 2022
6b5cbe9
Fix TS import patterns to new gen filename
Nov 9, 2022
2202b7a
Update internal imports, remove coremodel registry
Nov 9, 2022
16d2d2d
Fix compilation errors, wire generation
Nov 9, 2022
0c80697
Export and replace the prefix dropper
Nov 9, 2022
aa32a56
More Go struct and field name changes
Nov 10, 2022
812957f
Last name fixes, hopefully
Nov 10, 2022
e9982e0
Fix lint errors
Nov 10, 2022
f92b893
Last lint error
Nov 10, 2022
62587e1
Merge remote-tracking branch 'origin/main' into sdboyer/entity-framew…
Nov 10, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,15 @@ lerna.json @grafana/frontend-ops
# Grafana Partnerships Team
/pkg/infra/httpclient/httpclientprovider/sigv4_middleware.go @grafana/grafana-partnerships-team

# Schema framework and code generation
# Kind system and code generation
embed.go @grafana/grafana-as-code
/kinds/ @grafana/grafana-as-code
/pkg/codegen @grafana/grafana-as-code
/pkg/framework/coremodel @grafana/grafana-as-code
/pkg/kindsys @grafana/grafana-as-code
/pkg/kinds/*/*_gen.go @grafana/grafana-as-code
/pkg/registry/corekind @grafana/grafana-as-code
/public/app/plugins/*gen.go @grafana/grafana-as-code

# Specific core kinds
/kinds/raw/ @grafana/grafana-edge-squad
/kinds/structured/dashboard @grafana/dashboards-squad
10 changes: 2 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,8 @@ compilation-stats.json
# auto generated frontend docs
/docs/sources/packages_api

# auto generated Go files
*_gen.go
!pkg/services/featuremgmt/toggles_gen.go
!pkg/coremodel/**/*_gen.go
!pkg/framework/**/*_gen.go
!pkg/plugins/pfs/**/*_gen.go
!public/app/plugins/**/*_gen.go
!pkg/services/publicdashboards/*_gen.go
# wire generated files
**/wire_gen.go

# Auto-generated internationalization files
public/locales/_build/
Expand Down
7 changes: 4 additions & 3 deletions LICENSING.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ packages/grafana-toolkit/
packages/grafana-ui/
packages/jaeger-ui-components/
packaging/
pkg/coremodel/
pkg/framework/coremodel/
kinds/
pkg/kinds/
pkg/kindsys/
pkg/registry/corekind/
grafana-mixin/
cue/
public/app/plugins/datasource/tempo
public/img/icons/solid/
public/img/icons/unicons/
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ openapi3-gen: swagger-api-spec ## Generates OpenApi 3 specs from the Swagger 2 a
##@ Building
gen-cue: ## Do all CUE/Thema code generation
@echo "generate code from .cue files"
go generate ./kinds/gen.go
go generate ./pkg/framework/coremodel
go generate ./public/app/plugins

Expand Down
2 changes: 1 addition & 1 deletion embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import (

// CueSchemaFS embeds all schema-related CUE files in the Grafana project.
//
//go:embed cue.mod/module.cue packages/grafana-schema/src/schema/*.cue public/app/plugins/*/*/*.cue public/app/plugins/*/*/plugin.json pkg/framework/coremodel/*.cue
//go:embed cue.mod/module.cue kinds/*/*.cue kinds/*/*/*.cue packages/grafana-schema/src/schema/*.cue public/app/plugins/*/*/*.cue public/app/plugins/*/*/plugin.json pkg/framework/coremodel/*.cue pkg/kindsys/*.cue
var CueSchemaFS embed.FS
10 changes: 7 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ require (
github.com/grafana/grafana-aws-sdk v0.11.0
github.com/grafana/grafana-azure-sdk-go v1.3.1
github.com/grafana/grafana-plugin-sdk-go v0.142.0
github.com/grafana/thema v0.0.0-20220929145912-2c7c4a7bb20b
github.com/grafana/thema v0.0.0-20221107225215-00ad2949c7bc
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/hashicorp/go-hclog v1.0.0
github.com/hashicorp/go-plugin v1.4.3
Expand Down Expand Up @@ -110,7 +110,7 @@ require (
golang.org/x/exp v0.0.0-20220613132600-b0d781184e0d
golang.org/x/net v0.0.0-20220909164309-bea034e7d591 // indirect
golang.org/x/oauth2 v0.0.0-20220630143837-2104d58473e0
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
golang.org/x/sync v0.1.0
golang.org/x/time v0.0.0-20220609170525-579cf78fd858
golang.org/x/tools v0.1.12
gonum.org/v1/gonum v0.11.0
Expand Down Expand Up @@ -250,11 +250,13 @@ require (
github.com/bufbuild/connect-go v1.0.0
github.com/dlmiddlecote/sqlstats v1.0.2
github.com/drone/drone-cli v1.6.1
github.com/getkin/kin-openapi v0.94.0
github.com/getkin/kin-openapi v0.103.0
github.com/golang-migrate/migrate/v4 v4.7.0
github.com/google/go-github/v45 v45.2.0
github.com/grafana/codejen v0.0.2
github.com/grafana/dskit v0.0.0-20211011144203-3a88ec0b675f
github.com/jmoiron/sqlx v1.3.5
github.com/kr/pretty v0.3.0
github.com/matryer/is v1.4.0
github.com/parca-dev/parca v0.12.1
github.com/urfave/cli v1.22.9
Expand Down Expand Up @@ -285,11 +287,13 @@ require (
github.com/gosimple/unidecode v1.0.1 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/memberlist v0.4.0 // indirect
github.com/invopop/yaml v0.1.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-ieproxy v0.0.3 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/segmentio/asm v1.1.4 // indirect
go.starlark.net v0.0.0-20221020143700-22309ac47eac // indirect
Expand Down
15 changes: 11 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -847,8 +847,9 @@ github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo
github.com/gdamore/tcell v1.3.0/go.mod h1:Hjvr+Ofd+gLglo7RYKxxnzCBmev3BzsS67MebKS4zMM=
github.com/getkin/kin-openapi v0.53.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4=
github.com/getkin/kin-openapi v0.61.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4=
github.com/getkin/kin-openapi v0.94.0 h1:bAxg2vxgnHHHoeefVdmGbR+oxtJlcv5HsJJa3qmAHuo=
github.com/getkin/kin-openapi v0.94.0/go.mod h1:LWZfzOd7PRy8GJ1dJ6mCU6tNdSfOwRac1BUPam4aw6Q=
github.com/getkin/kin-openapi v0.103.0 h1:F5wAtaQvPWxKCAYZ69LgHAThgu16p4u41VQtbn1U8LA=
github.com/getkin/kin-openapi v0.103.0/go.mod h1:w4lRPHiyOdwGbOkLIyk+P0qCwlu7TXPCHD/64nSXzgE=
github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
github.com/getsentry/sentry-go v0.13.0 h1:20dgTiUSfxRB/EhMPtxcL9ZEbM1ZdR+W/7f7NWD+xWo=
github.com/getsentry/sentry-go v0.13.0/go.mod h1:EOsfu5ZdvKPfeHYV6pTVQnsjfp30+XA7//UooKNumH0=
Expand Down Expand Up @@ -1347,6 +1348,8 @@ github.com/gosimple/slug v1.12.0 h1:xzuhj7G7cGtd34NXnW/yF0l+AGNfWqwgh/IXgFy7dnc=
github.com/gosimple/slug v1.12.0/go.mod h1:UiRaFH+GEilHstLUmcBgWcI42viBN7mAb818JrYOeFQ=
github.com/gosimple/unidecode v1.0.1 h1:hZzFTMMqSswvf0LBJZCZgThIZrpDHFXux9KeGmn6T/o=
github.com/gosimple/unidecode v1.0.1/go.mod h1:CP0Cr1Y1kogOtx0bJblKzsVWrqYaqfNOnHzpgWw4Awc=
github.com/grafana/codejen v0.0.2 h1:Ssp27X7SOnYxaPUTByW/6201tNV5Q60l1BSF+s3lRP8=
github.com/grafana/codejen v0.0.2/go.mod h1:zmwwM/DRyQB7pfuBjTWII3CWtxcXh8LTwAYGfDfpR6s=
github.com/grafana/cuetsy v0.1.1 h1:+1jaDDYCpvKlcOWJgBRbkc5+VZIClCEn5mbI+4PLZqM=
github.com/grafana/cuetsy v0.1.1/go.mod h1:4KWkUOslwvRTpEv7wdQG0jDFTuJmU+0L9x0h4kWxa2A=
github.com/grafana/dskit v0.0.0-20211011144203-3a88ec0b675f h1:FvvSVEbnGeM2bUivGmsiXTi8URJyBU7TcFEEoRe5wWI=
Expand All @@ -1366,8 +1369,8 @@ github.com/grafana/prometheus-alertmanager v0.24.1-0.20221012142027-823cd9150293
github.com/grafana/prometheus-alertmanager v0.24.1-0.20221012142027-823cd9150293/go.mod h1:HVHqK+BVPa/tmL8EMhLCCrPt2a1GdJpEyxr5hgur2UI=
github.com/grafana/saml v0.4.9-0.20220727151557-61cd9c9353fc h1:1PY8n+rXuBNr3r1JQhoytWDCpc+pq+BibxV0SZv+Cr4=
github.com/grafana/saml v0.4.9-0.20220727151557-61cd9c9353fc/go.mod h1:9Zh6dWPtB3MSzTRt8fIFH60Z351QQ+s7hCU3J/tTlA4=
github.com/grafana/thema v0.0.0-20220929145912-2c7c4a7bb20b h1:OEGzlaj04LE6Eq7aGMOh0bCplGW5rXNeSSSwgamPBEY=
github.com/grafana/thema v0.0.0-20220929145912-2c7c4a7bb20b/go.mod h1:i3/NX50sNrwsPSAQAj56ckjQTb4biaYG/6y+zyKgpb0=
github.com/grafana/thema v0.0.0-20221107225215-00ad2949c7bc h1:Icv777/PBaqhLmbSBSDaajDl424cbmh5ee77Du2rUFE=
github.com/grafana/thema v0.0.0-20221107225215-00ad2949c7bc/go.mod h1:wnIJykzNiNVANl6g/Z4nkXxoMqaaH1LoG0IPNW++BEk=
github.com/grafana/xorm v0.8.3-0.20220614223926-2fcda7565af6 h1:I9dh1MXGX0wGyxdV/Sl7+ugnki4Dfsy8lv2s5Yf887o=
github.com/grafana/xorm v0.8.3-0.20220614223926-2fcda7565af6/go.mod h1:ZkJLEYLoVyg7amJK/5r779bHyzs2AU8f8VMiP6BM7uY=
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
Expand Down Expand Up @@ -1552,6 +1555,8 @@ github.com/influxdata/roaring v0.4.13-0.20180809181101-fc520f41fab6/go.mod h1:bS
github.com/influxdata/tdigest v0.0.0-20181121200506-bf2b5ad3c0a9/go.mod h1:Js0mqiSBE6Ffsg94weZZ2c+v/ciT8QRHFOap7EKDrR0=
github.com/influxdata/tdigest v0.0.2-0.20210216194612-fc98d27c9e8b/go.mod h1:Z0kXnxzbTC2qrx4NaIzYkE1k66+6oEDQTvL95hQFh5Y=
github.com/influxdata/usage-client v0.0.0-20160829180054-6d3895376368/go.mod h1:Wbbw6tYNvwa5dlB6304Sd+82Z3f7PmVZHVKU637d4po=
github.com/invopop/yaml v0.1.0 h1:YW3WGUoJEXYfzWBjn00zIlrw7brGVD0fUKRYDPAPhrc=
github.com/invopop/yaml v0.1.0/go.mod h1:2XuRLgs/ouIrW3XNzuNj7J3Nvu/Dig5MXvbCEdiBN3Q=
github.com/j-keck/arping v0.0.0-20160618110441-2cf9dc699c56/go.mod h1:ymszkNOg6tORTn+6F6j+Jc8TOr5osrynvN6ivFWZ2GA=
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
Expand Down Expand Up @@ -2888,8 +2893,9 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20170830134202-bb24a47a89ea/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -3376,6 +3382,7 @@ gopkg.in/yaml.v3 v3.0.0-20200603094226-e3079894b1e8/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
Expand Down
157 changes: 157 additions & 0 deletions kinds/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
//go:build ignore
// +build ignore

//go:generate go run gen.go

package main

import (
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"

"cuelang.org/go/cue/errors"
"github.com/grafana/codejen"
"github.com/grafana/grafana/pkg/codegen"
"github.com/grafana/grafana/pkg/cuectx"
"github.com/grafana/grafana/pkg/kindsys"
)

const sep = string(filepath.Separator)

func main() {
if len(os.Args) > 1 {
fmt.Fprintf(os.Stderr, "plugin thema code generator does not currently accept any arguments\n, got %q", os.Args)
os.Exit(1)
}

// Core kinds composite code generator. Produces all generated code in
// grafana/grafana that derives from raw and structured core kinds.
coreKindsGen := codejen.JennyListWithNamer[*codegen.DeclForGen](func(decl *codegen.DeclForGen) string {
return decl.Meta.Common().MachineName
})

// All the jennies that comprise the core kinds generator pipeline
coreKindsGen.Append(
codegen.GoTypesJenny(kindsys.GoCoreKindParentPath, nil),
codegen.CoreStructuredKindJenny(kindsys.GoCoreKindParentPath, nil),
codegen.RawKindJenny(kindsys.GoCoreKindParentPath, nil),
codegen.BaseCoreRegistryJenny(filepath.Join("pkg", "registry", "corekind"), kindsys.GoCoreKindParentPath),
codegen.TSTypesJenny(kindsys.TSCoreKindParentPath, &codegen.TSTypesGeneratorConfig{
GenDirName: func(decl *codegen.DeclForGen) string {
// FIXME this hardcodes always generating to experimental dir. OK for now, but need generator fanout
return filepath.Join(decl.Meta.Common().MachineName, "x")
},
}),
codegen.TSVeneerIndexJenny(filepath.Join("packages", "grafana-schema", "src")),
)

coreKindsGen.AddPostprocessors(codegen.SlashHeaderMapper("kinds/gen.go"))

cwd, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "could not get working directory: %s", err)
os.Exit(1)
}
grootp := strings.Split(cwd, sep)
groot := filepath.Join(sep, filepath.Join(grootp[:len(grootp)-1]...))

rt := cuectx.GrafanaThemaRuntime()
var all []*codegen.DeclForGen

// structured kinddirs first
f := os.DirFS(filepath.Join(groot, kindsys.CoreStructuredDeclParentPath))
kinddirs := elsedie(fs.ReadDir(f, "."))("error reading structured fs root directory")
for _, ent := range kinddirs {
if !ent.IsDir() {
continue
}
rel := filepath.Join(kindsys.CoreStructuredDeclParentPath, ent.Name())
decl, err := kindsys.LoadCoreKind[kindsys.CoreStructuredMeta](rel, rt.Context(), nil)
if err != nil {
die(fmt.Errorf("%s is not a valid kind: %s", rel, errors.Details(err, nil)))
}
if decl.Meta.MachineName != ent.Name() {
die(fmt.Errorf("%s: kind's machine name (%s) must equal parent dir name (%s)", rel, decl.Meta.Name, ent.Name()))
}

all = append(all, elsedie(codegen.ForGen(rt, decl.Some()))(rel))
}

// now raw kinddirs
f = os.DirFS(filepath.Join(groot, kindsys.RawDeclParentPath))
kinddirs = elsedie(fs.ReadDir(f, "."))("error reading raw fs root directory")
for _, ent := range kinddirs {
if !ent.IsDir() {
continue
}
rel := filepath.Join(kindsys.RawDeclParentPath, ent.Name())
decl, err := kindsys.LoadCoreKind[kindsys.RawMeta](rel, rt.Context(), nil)
if err != nil {
die(fmt.Errorf("%s is not a valid kind: %s", rel, errors.Details(err, nil)))
}
if decl.Meta.MachineName != ent.Name() {
die(fmt.Errorf("%s: kind's machine name (%s) must equal parent dir name (%s)", rel, decl.Meta.Name, ent.Name()))
}
dfg, _ := codegen.ForGen(nil, decl.Some())
all = append(all, dfg)
}

sort.Slice(all, func(i, j int) bool {
return nameFor(all[i].Meta) < nameFor(all[j].Meta)
})

jfs, err := coreKindsGen.GenerateFS(all)
if err != nil {
die(fmt.Errorf("core kinddirs codegen failed: %w", err))
}
// for _, f := range jfs.AsFiles() {
// fmt.Println(filepath.Join(groot, f.RelativePath))
// }

if _, set := os.LookupEnv("CODEGEN_VERIFY"); set {
if err = jfs.Verify(context.Background(), groot); err != nil {
die(fmt.Errorf("generated code is out of sync with inputs:\n%s\nrun `make gen-cue` to regenerate", err))
}
} else if err = jfs.Write(context.Background(), groot); err != nil {
die(fmt.Errorf("error while writing generated code to disk:\n%s", err))
}
}

func nameFor(m kindsys.SomeKindMeta) string {
switch x := m.(type) {
case kindsys.RawMeta:
return x.Name
case kindsys.CoreStructuredMeta:
return x.Name
case kindsys.CustomStructuredMeta:
return x.Name
case kindsys.ComposableMeta:
return x.Name
default:
// unreachable so long as all the possibilities in KindMetas have switch branches
panic("unreachable")
}
}

func elsedie[T any](t T, err error) func(msg string) T {
if err != nil {
return func(msg string) T {
fmt.Fprintf(os.Stderr, "%s: %s\n", msg, err)
os.Exit(1)
return t
}
}
return func(msg string) T {
return t
}
}

func die(err error) {
fmt.Fprint(os.Stderr, err, "\n")
os.Exit(1)
}
7 changes: 7 additions & 0 deletions kinds/raw/constraint.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package kind

import "github.com/grafana/grafana/pkg/kindsys"

// In each child directory, the set of .cue files with 'package kind'
// must be an instance of kindsys.#Raw - a declaration of a raw kind.
kindsys.#Raw
4 changes: 4 additions & 0 deletions kinds/raw/svg/svg_kind.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package kind

name: "SVG"
extensions: ["svg"]
8 changes: 8 additions & 0 deletions kinds/structured/constraint.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package kind

import "github.com/grafana/grafana/pkg/kindsys"

// In each child directory, the set of .cue files with 'package kind'
// must be an instance of kindsys.#CoreStructured - a declaration of a
// structured kind.
kindsys.#CoreStructured
Loading