Skip to content

Commit 7e3224d

Browse files
PatMyronkddejong
andauthored
Autocompletion / links on hover for Registry types (#200)
* creating JSON patch for JSON schema based on registry schema * overwriting template schema file with patched template schema Co-authored-by: Kevin DeJong <[email protected]>
1 parent a5edc94 commit 7e3224d

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

schema/resource-patch-stub.json

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
[
2+
{
3+
"op":"add",
4+
"path":"/definitions/RESOURCE_TYPE",
5+
"value":{
6+
"type":"object",
7+
"properties":{
8+
"DependsOn":{
9+
"type":[
10+
"string",
11+
"array"
12+
],
13+
"items":{
14+
"type":"string"
15+
}
16+
},
17+
"Metadata":{
18+
"description":"https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-metadata.html",
19+
"type":"object"
20+
},
21+
"DeletionPolicy":{
22+
"description":"https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-deletionpolicy.html",
23+
"type":"string",
24+
"enum":[
25+
"Delete",
26+
"Retain",
27+
"Snapshot"
28+
]
29+
},
30+
"UpdateReplacePolicy":{
31+
"description":"https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-updatereplacepolicy.html",
32+
"type":"string",
33+
"enum":[
34+
"Delete",
35+
"Retain",
36+
"Snapshot"
37+
]
38+
},
39+
"CreationPolicy":{
40+
"description":"https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-creationpolicy.html",
41+
"type":"object"
42+
},
43+
"UpdatePolicy":{
44+
"description":"https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-updatepolicy.html",
45+
"type":"object"
46+
},
47+
"Type":{
48+
"type":"string",
49+
"enum":[
50+
"RESOURCE_TYPE"
51+
]
52+
},
53+
"Properties": "RESOURCE_SCHEMA"
54+
},
55+
"required":[
56+
"Type"
57+
],
58+
"additionalProperties":false
59+
}
60+
},
61+
{
62+
"op":"add",
63+
"path":"/definitions/resources/patternProperties/^[a-zA-Z0-9]{1,255}$/oneOf/-",
64+
"value":{
65+
"$ref":"#/definitions/RESOURCE_TYPE"
66+
}
67+
}
68+
]

server/package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
},
1515
"dependencies": {
1616
"commander": "^3.0.2",
17+
"fast-json-patch": "^3.1.0",
1718
"vscode-languageserver": "^5.2.1",
1819
"vscode-uri": "^2.1.1"
1920
},

server/src/server.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import {
2525
import { URI } from 'vscode-uri';
2626

2727
import { spawn } from "child_process";
28+
import { readdirSync, readFileSync, writeFileSync } from 'fs';
29+
import { applyPatch } from 'fast-json-patch';
2830

2931
const program = new Command('cfn-lsp')
3032
.allowUnknownOption()
@@ -157,6 +159,17 @@ function isCloudFormation(template: string, filename: string): Boolean {
157159
return false;
158160
}
159161

162+
function patchTemplateSchema(registrySchemaDirectory: string) {
163+
const stub = readFileSync(__dirname + '/../../schema/resource-patch-stub.json', 'utf8');
164+
let templateSchema = JSON.parse(readFileSync(__dirname + '/../../schema/all-spec.json', 'utf8'));
165+
for (const schemaFile of readdirSync(registrySchemaDirectory)) {
166+
const registrySchema = readFileSync(registrySchemaDirectory + schemaFile, 'utf8');
167+
const patch = JSON.parse(stub.replace(/RESOURCE_TYPE/g, JSON.parse(registrySchema)['typeName']).replace(/"RESOURCE_SCHEMA"/g, registrySchema));
168+
templateSchema = applyPatch(templateSchema, patch).newDocument;
169+
}
170+
writeFileSync(__dirname + '/../../schema/all-spec.json', JSON.stringify(templateSchema));
171+
}
172+
160173
function runLinter(document: TextDocument): void {
161174
let uri = document.uri;
162175

@@ -174,6 +187,14 @@ function runLinter(document: TextDocument): void {
174187
let build_graph = isPreviewing[uri];
175188

176189
if (is_cfn) {
190+
if (Path.includes(' --registry-schemas ') || Path.includes(' -s ')) {
191+
for (const segment of Path.split('-')) {
192+
if (segment.startsWith('schemas ') || segment.startsWith('s ')) {
193+
patchTemplateSchema(segment.split(' ')[1] + "/");
194+
}
195+
}
196+
}
197+
177198
let args = ['--format', 'json'];
178199
if (build_graph) {
179200
args.push('--build-graph');

0 commit comments

Comments
 (0)