Skip to content

Commit

Permalink
refactor: split library into smaller files (#1)
Browse files Browse the repository at this point in the history
* refactor(generator): split library into smaller files

* refactor(grafanaplane): split library into smaller files

* docs: fix import statements

* chore: Makefile targets and use long import path

* chore: ensure jsonnetfile.lock.json is up-to-date
  • Loading branch information
Duologic authored Sep 24, 2024
1 parent 46743f8 commit ef90361
Show file tree
Hide file tree
Showing 410 changed files with 78,234 additions and 78,105 deletions.
File renamed without changes.
17 changes: 10 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ LIBRARY_VERSION:=0.0.1
PROVIDER_VERSION:=0.18.0
JSONNET_BIN:=jrsonnet

grafanaplane: grafanaplane/raw.libsonnet grafanaplane/compositions.libsonnet
VENDOR_DEPTHS:=$(shell find generator/vendor -type f)

grafanaplane/raw.libsonnet: generator/main.libsonnet generator/namespaced.libsonnet generator/crds.yaml generator/vendor
$(JSONNET_BIN) -S -J generator/vendor -A 'version=$(LIBRARY_VERSION)-$(PROVIDER_VERSION)' generator/main.libsonnet | jsonnetfmt - > grafanaplane/raw.libsonnet

grafanaplane/compositions.libsonnet: generator/compositions.libsonnet generator/namespaced.libsonnet generator/crds.yaml generator/vendor
$(JSONNET_BIN) -J generator/vendor generator/compositions.libsonnet | jsonnetfmt - > grafanaplane/compositions.libsonnet
grafanaplane: grafanaplane/main.libsonnet grafanaplane/compositions.libsonnet

generator/crds.yaml:
cd generator && \
curl -sLO https://github.com/grafana/crossplane-provider-grafana/releases/download/v$(PROVIDER_VERSION)/crds.yaml

docs: grafanaplane
grafanaplane/main.libsonnet: generator/main.libsonnet generator/namespaced.libsonnet generator/crds.yaml $(VENDOR_DEPTHS)
FILES=$$($(JSONNET_BIN) -S -c -m grafanaplane -J generator/vendor -A 'version=$(LIBRARY_VERSION)-$(PROVIDER_VERSION)' generator/main.libsonnet) && \
xargs -n1 jsonnetfmt -i <<< "$${FILES}"

grafanaplane/compositions.libsonnet: generator/compositions.libsonnet generator/namespaced.libsonnet generator/crds.yaml $(VENDOR_DEPTHS)
$(JSONNET_BIN) -J generator/vendor generator/compositions.libsonnet | jsonnetfmt - > grafanaplane/compositions.libsonnet

docs: $(shell find grafanaplane/ -type f)
@rm -rf docs/
@$(JSONNET_BIN) \
-J generator/vendor \
Expand Down
5 changes: 3 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Jsonnet library providing a namespaced set of compositions/XRDs for the Grafana
The compositions/XRDs can be imported like this:

```jsonnet
local compositions = import "github.com/Duologic/grafana-crossplane-libsonnet/grafanaplane/compositions.libsonnet"
local compositions = import 'github.com/Duologic/grafana-crossplane-libsonnet/grafanaplane/compositions.libsonnet';
[
# Each composition has a `definition` and `composition` key
Expand All @@ -30,10 +30,11 @@ jb install github.com/Duologic/grafana-crossplane-libsonnet/[email protected]
## Usage

```jsonnet
local grafanaplane = import "github.com/Duologic/grafana-crossplane-libsonnet/grafanaplane/main.libsonnet"
local grafanaplane = import 'github.com/Duologic/grafana-crossplane-libsonnet/grafanaplane/main.libsonnet';
```



## Subpackages

* [alerting](alerting/index.md)
Expand Down
11 changes: 10 additions & 1 deletion generator/jsonnetfile.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
},
"version": "master"
},
{
"source": {
"git": {
"remote": "https://github.com/Duologic/jsonnet-parser.git",
"subdir": ""
}
},
"version": "master"
},
{
"source": {
"git": {
Expand All @@ -20,5 +29,5 @@
"version": "master"
}
],
"legacyImports": true
"legacyImports": false
}
18 changes: 14 additions & 4 deletions generator/jsonnetfile.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@
"subdir": ""
}
},
"version": "c26e17ea5dbddf389e5e65217b679044eb9d627d",
"sum": "esRceW/DxhdnRea+DZTNJg6SGnl/Hb+vX+hi+0o8jOI="
"version": "f9fd49989b40a2706af64cb1896856fa20aaa4da",
"sum": "IM+5xV6Mat3VD2AUUGt5fYyCComJyVspXH+Tdz+I3Nc="
},
{
"source": {
"git": {
"remote": "https://github.com/Duologic/jsonnet-parser.git",
"subdir": ""
}
},
"version": "4f3ca583ccfa4f6833abe75428677a005bc6b486",
"sum": "0N0SeOSZeD1DaVp7Avj9WpTRVt3tdPrAjxZ0qWr/xrU="
},
{
"source": {
Expand All @@ -18,8 +28,8 @@
"subdir": ""
}
},
"version": "a771663cb9594cd10b30815933fe55554e6a3755",
"sum": "k/SJ+F+jjdg+dBMKczPEjcYCEuQ6v0BROdXKdXOCi9o="
"version": "f810e234468906b2d05b091ae1b5f8c6047090a4",
"sum": "Q1BAaS0EIkBXdOOGXfQrnJlo1wvlVd2JTRIRUhfxTj0="
},
{
"source": {
Expand Down
145 changes: 104 additions & 41 deletions generator/main.libsonnet
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
local d = import './vendor/github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';
local parser = import 'github.com/Duologic/jsonnet-parser/parser.libsonnet';
local a = import 'github.com/crdsonnet/astsonnet/main.libsonnet';
local autils = import 'github.com/crdsonnet/astsonnet/utils.libsonnet';
local helpers = import 'github.com/crdsonnet/crdsonnet/crdsonnet/helpers.libsonnet';
local crdsonnet = import 'github.com/crdsonnet/crdsonnet/crdsonnet/main.libsonnet';
local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';

local processor = crdsonnet.processor.new('ast');

Expand Down Expand Up @@ -51,45 +52,56 @@ local mergeDocstring(group, version, name, obj, help='') =
obj,
]);

local packageDocString(version='main') =
a.object.new([
a.field.new(
a.string.new('#'),
a.literal.new(
std.manifestJsonEx(
d.package.new(
'grafanaplane',
'github.com/Duologic/grafana-crossplane-libsonnet/grafanaplane',
|||
Jsonnet library providing a namespaced set of compositions/XRDs for the Grafana Crossplane provider. The compositions, XRDs and the library for creating the XRD objects is generated.
The compositions/XRDs can be imported like this:
```jsonnet
local compositions = import "github.com/Duologic/grafana-crossplane-libsonnet/grafanaplane/compositions.libsonnet"
[
# Each composition has a `definition` and `composition` key
compositions.oss.v1alpha1.folder.composition,
compositions.oss.v1alpha1.folder.definition,
# When using Tanka, then providing the higher level objects is also possible
compositions.cloud.v1alpha1.stack, # a composition/XRD pair
compositions.oss, # whole group of composition/XRD pairs
]
```
The library in `main.libsonnet` can be used to build objects for these XRDs.
|||,
'main.libsonnet',
version
)
, ' ', ''
),
local packageDocStringField(version='main') =
a.field.new(
a.string.new('#'),
parser.new(
std.manifestJsonEx(
d.package.new(
'grafanaplane',
'github.com/Duologic/grafana-crossplane-libsonnet/grafanaplane',
|||
Jsonnet library providing a namespaced set of compositions/XRDs for the Grafana Crossplane provider. The compositions, XRDs and the library for creating the XRD objects is generated.
The compositions/XRDs can be imported like this:
```jsonnet
local compositions = import 'github.com/Duologic/grafana-crossplane-libsonnet/grafanaplane/compositions.libsonnet';
[
# Each composition has a `definition` and `composition` key
compositions.oss.v1alpha1.folder.composition,
compositions.oss.v1alpha1.folder.definition,
# When using Tanka, then providing the higher level objects is also possible
compositions.cloud.v1alpha1.stack, # a composition/XRD pair
compositions.oss, # whole group of composition/XRD pairs
]
```
The library in `main.libsonnet` can be used to build objects for these XRDs.
|||,
'main.libsonnet',
version
)
+ d.package.withUsageTemplate(
@"local %(name)s = import '%(import)s';"
)
, ' ', ''
),
),
]);
).parse()
+ {
members: std.map(
function(member)
if member.fieldname.string == 'help'
then member + { expr+: { textblock: true } }
else member,
super.members,
),
}
,
);

local ast =
std.foldl(
Expand All @@ -107,6 +119,57 @@ local ast =
a.object.withMembers([]),
);

function(version='main')
autils.deepMergeObjects([ast, packageDocString(version)]).toString()
local splitIntoFiles(objast, sub='', depth=1, maxDepth=5) =
local subdir = if sub != '' then sub + '/' else '';
std.foldl(
function(acc, member)
if member.type == 'field'
&& member.expr.type == 'object'
&& !std.startsWith(member.fieldname.string, '#')
then
acc
+ {
[subdir + 'main.libsonnet']+:
a.object.withMembersMixin([
member
+ a.field.withExpr(
if depth != maxDepth
then a.import_statement.new('./' + member.fieldname.string + '/main.libsonnet')
else a.import_statement.new('./' + member.fieldname.string + '.libsonnet')
),
]),
}
+ (if depth != maxDepth
then splitIntoFiles(member.expr, subdir + member.fieldname.string, depth + 1)
else {
[subdir + member.fieldname.string + '.libsonnet']: member.expr,
})
else
acc
+ {
[subdir + 'main.libsonnet']+:
a.object.withMembersMixin([member]),
}
,
objast.members,
{
[subdir + 'main.libsonnet']:
a.object.new([]),
}
);

function(version='main')
local files = splitIntoFiles(ast);
{
[file.key]: file.value.toString()
for file in std.objectKeysValues(files)
}
+ {
'main.libsonnet':
(
files['main.libsonnet']
+ a.object.withMembersMixin(
[packageDocStringField(version)]
)
).toString(),
}
2 changes: 1 addition & 1 deletion generator/namespaced.libsonnet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local cngen = import 'crossplane-namespaced-libsonnet/main.jsonnet';
local cngen = import 'github.com/Duologic/crossplane-namespaced-libsonnet/main.jsonnet';

local crds =
std.filter(
Expand Down
2 changes: 0 additions & 2 deletions grafanaplane/.gitignore

This file was deleted.

4 changes: 4 additions & 0 deletions grafanaplane/alerting/main.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
'#': { help: '', name: 'alerting' },
v1alpha1+: import './v1alpha1/main.libsonnet',
}
34 changes: 34 additions & 0 deletions grafanaplane/alerting/v1alpha1/contactPoint/main.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
'#': { help: '', name: 'contactPoint' },
'#new': { 'function': { args: [{ default: null, enums: null, name: 'name', type: 'string' }], help: '`new` creates a new instance' } },
new(name):
self.withApiVersion()
+ self.withKind()
+ self.metadata.withName(name),
'#withApiVersion': { 'function': { args: [], help: '' } },
withApiVersion(): {
apiVersion: 'alerting.grafana.crossplane.io.namespaced/v1alpha1',
},
'#withKind': { 'function': { args: [], help: '' } },
withKind(): {
kind: 'ContactPoint',
},
'#withMetadata': { 'function': { args: [{ default: null, enums: null, name: 'value', type: ['object'] }], help: 'ObjectMeta is metadata that all persisted resources must have, which includes all objects users must create.' } },
withMetadata(value): {
metadata: value,
},
'#withMetadataMixin': { 'function': { args: [{ default: null, enums: null, name: 'value', type: ['object'] }], help: 'ObjectMeta is metadata that all persisted resources must have, which includes all objects users must create.' } },
withMetadataMixin(value): {
metadata+: value,
},
metadata+: import './metadata/main.libsonnet',
'#withSpec': { 'function': { args: [{ default: null, enums: null, name: 'value', type: ['object'] }], help: '' } },
withSpec(value): {
spec: value,
},
'#withSpecMixin': { 'function': { args: [{ default: null, enums: null, name: 'value', type: ['object'] }], help: '' } },
withSpecMixin(value): {
spec+: value,
},
spec+: import './spec/main.libsonnet',
}
Loading

0 comments on commit ef90361

Please sign in to comment.