Skip to content

Commit 9b6c8b6

Browse files
jpinkney-awsJadenSimon
andauthored
feat(schemas): use less statusbar space #2709
Problem: - SAM/CFN schema paths take up a lot of space on the status bar. Solution: - Set a title for the SAM/CFN schemas so VSCode-YAML will use that on the status bar instead of the full file path. * fix(schema): remove `onSuccess` callback * fix(schema): catch cache key update failure * fix(schemas): make updating more robust * fix(schema): Add additional error information to users when schema fails to download and content is already cached Co-authored-by: JadenSimon <[email protected]>
1 parent 505816d commit 9b6c8b6

File tree

4 files changed

+57
-44
lines changed

4 files changed

+57
-44
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Feature",
3+
"description": "Decrease the amount of space SAM/CFN schemas take on the status bar"
4+
}

src/shared/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export const INSIGHTS_TIMESTAMP_FORMAT = 'YYYY-MM-DDTHH:mm:ss.SSSZ'
9797
* URI scheme for CloudWatch Logs Virtual Documents
9898
*/
9999
export const CLOUDWATCH_LOGS_SCHEME = 'awsCloudWatchLogs'
100+
export const AWS_SCHEME = 'aws'
100101

101102
export const LAMBDA_PACKAGE_TYPE_IMAGE = 'Image'
102103

src/shared/extensions/yaml.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { VSCODE_EXTENSION_ID } from '../extensions'
99
import { getLogger } from '../logger/logger'
1010
import { getIdeProperties } from '../extensionUtilities'
1111
import { activateExtension } from '../utilities/vsCodeUtils'
12+
import { AWS_SCHEME } from '../constants'
1213

1314
// sourced from https://github.com/redhat-developer/vscode-yaml/blob/3d82d61ea63d3e3a9848fe6b432f8f1f452c1bec/src/schema-extension-api.ts
1415
// removed everything that is not currently being used
@@ -21,8 +22,6 @@ interface YamlExtensionApi {
2122
): boolean
2223
}
2324

24-
const AWS_SCHEME = 'aws'
25-
2625
function applyScheme(scheme: string, path: vscode.Uri): vscode.Uri {
2726
return path.with({ scheme })
2827
}

src/shared/schemas.ts

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { mkdirSync, writeFileSync } from 'fs'
7-
import * as path from 'path'
86
import * as vscode from 'vscode'
97
import globals from './extensionGlobals'
108
import { activateYamlExtension, YamlExtension } from './extensions/yaml'
11-
import * as filesystemUtilities from './filesystemUtilities'
129
import * as pathutil from '../shared/utilities/pathUtils'
1310
import { getLogger } from './logger'
1411
import { FileResourceFetcher } from './resourcefetcher/fileResourceFetcher'
@@ -17,8 +14,12 @@ import { Settings } from './settings'
1714
import { once } from './utilities/functionUtils'
1815
import { normalizeSeparator } from './utilities/pathUtils'
1916
import { Any, ArrayConstructor } from './utilities/typeConstructors'
17+
import { AWS_SCHEME } from './constants'
18+
import { writeFile } from 'fs-extra'
19+
import { SystemUtilities } from './systemUtilities'
2020

2121
const GOFORMATION_MANIFEST_URL = 'https://api.github.com/repos/awslabs/goformation/releases/latest'
22+
const SCHEMA_PREFIX = `${AWS_SCHEME}://`
2223

2324
export type Schemas = { [key: string]: vscode.Uri }
2425
export type SchemaType = 'yaml' | 'json'
@@ -139,29 +140,28 @@ export class SchemaService {
139140
*/
140141
export async function getDefaultSchemas(extensionContext: vscode.ExtensionContext): Promise<Schemas | undefined> {
141142
try {
142-
// Convert the paths to URIs which is what the YAML extension expects
143-
const cfnSchemaUri = vscode.Uri.file(
144-
normalizeSeparator(path.join(extensionContext.globalStorageUri.fsPath, 'cloudformation.schema.json'))
145-
)
146-
const samSchemaUri = vscode.Uri.file(
147-
normalizeSeparator(path.join(extensionContext.globalStorageUri.fsPath, 'sam.schema.json'))
148-
)
143+
const cfnSchemaUri = vscode.Uri.joinPath(extensionContext.globalStorageUri, 'cloudformation.schema.json')
144+
const samSchemaUri = vscode.Uri.joinPath(extensionContext.globalStorageUri, 'sam.schema.json')
145+
149146
const goformationSchemaVersion = await getPropertyFromJsonUrl(GOFORMATION_MANIFEST_URL, 'tag_name')
150147

151-
await getRemoteOrCachedFile({
152-
filepath: cfnSchemaUri.fsPath,
148+
await updateSchemaFromRemote({
149+
destination: cfnSchemaUri,
153150
version: goformationSchemaVersion,
154151
url: `https://raw.githubusercontent.com/awslabs/goformation/${goformationSchemaVersion}/schema/cloudformation.schema.json`,
155152
cacheKey: 'cfnSchemaVersion',
156153
extensionContext,
154+
title: SCHEMA_PREFIX + 'cloudformation.schema.json',
157155
})
158-
await getRemoteOrCachedFile({
159-
filepath: samSchemaUri.fsPath,
156+
await updateSchemaFromRemote({
157+
destination: samSchemaUri,
160158
version: goformationSchemaVersion,
161159
url: `https://raw.githubusercontent.com/awslabs/goformation/${goformationSchemaVersion}/schema/sam.schema.json`,
162160
cacheKey: 'samSchemaVersion',
163161
extensionContext,
162+
title: SCHEMA_PREFIX + 'sam.schema.json',
164163
})
164+
165165
return {
166166
cfn: cfnSchemaUri,
167167
sam: samSchemaUri,
@@ -183,43 +183,52 @@ export async function getDefaultSchemas(extensionContext: vscode.ExtensionContex
183183
* @param params.cacheKey Cache key to check version against
184184
* @param params.extensionContext VSCode extension context
185185
*/
186-
export async function getRemoteOrCachedFile(params: {
187-
filepath: string
186+
export async function updateSchemaFromRemote(params: {
187+
destination: vscode.Uri
188188
version?: string
189189
url: string
190190
cacheKey: string
191191
extensionContext: vscode.ExtensionContext
192-
}): Promise<string> {
193-
const dir = path.parse(params.filepath).dir
194-
if (!(await filesystemUtilities.fileExists(dir))) {
195-
mkdirSync(dir, { recursive: true })
196-
}
197-
192+
title: string
193+
}): Promise<void> {
198194
const cachedVersion = params.extensionContext.globalState.get<string>(params.cacheKey)
199195
const outdated = params.version && params.version !== cachedVersion
200-
if (!outdated) {
201-
// Check that the cached file actually can be fetched. Else we might
202-
// never update the cache.
203-
const fileFetcher = new FileResourceFetcher(params.filepath)
204-
const content = await fileFetcher.get()
205-
if (content) {
206-
return content
207-
}
196+
197+
// Check that the cached file actually can be fetched. Else we might
198+
// never update the cache.
199+
const fileFetcher = new FileResourceFetcher(params.destination.fsPath)
200+
const cachedContent = await fileFetcher.get()
201+
202+
if (!outdated && cachedContent) {
203+
return
208204
}
209205

210-
const httpFetcher = new HttpResourceFetcher(params.url, {
211-
showUrl: true,
212-
// updates curr version
213-
onSuccess: contents => {
214-
writeFileSync(params.filepath, contents)
215-
params.extensionContext.globalState.update(params.cacheKey, params.version)
216-
},
217-
})
218-
const content = await httpFetcher.get()
219-
if (!content) {
220-
throw new Error(`failed to resolve schema: ${params.filepath}`)
206+
try {
207+
const httpFetcher = new HttpResourceFetcher(params.url, { showUrl: true })
208+
const content = await httpFetcher.get()
209+
210+
if (!content) {
211+
throw new Error(`failed to resolve schema: ${params.destination}`)
212+
}
213+
214+
const parsedFile = { ...JSON.parse(content), title: params.title }
215+
const dir = vscode.Uri.joinPath(params.destination, '..')
216+
await SystemUtilities.createDirectory(dir)
217+
await writeFile(params.destination.fsPath, JSON.stringify(parsedFile))
218+
await params.extensionContext.globalState.update(params.cacheKey, params.version).then(undefined, err => {
219+
getLogger().warn(`schemas: failed to update cache key for "${params.title}": ${err?.message}`)
220+
})
221+
} catch (err) {
222+
if (cachedContent) {
223+
getLogger().warn(
224+
`schemas: failed to fetch the latest version for "${params.title}": ${
225+
(err as Error).message
226+
}. Using cached schema instead.`
227+
)
228+
} else {
229+
throw err
230+
}
221231
}
222-
return content
223232
}
224233

225234
/**

0 commit comments

Comments
 (0)