Skip to content

Commit

Permalink
Add DABs code completion for includes
Browse files Browse the repository at this point in the history
  • Loading branch information
fjakobs committed Jul 25, 2023
1 parent 23a501e commit 9b8faec
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 15 deletions.
3 changes: 2 additions & 1 deletion packages/databricks-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,8 @@
"ansi-to-html": "^0.7.2",
"bcryptjs": "^2.4.3",
"triple-beam": "^1.4.1",
"winston": "^3.10.0"
"winston": "^3.10.0",
"yaml": "^2.3.1"
},
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^1.0.2",
Expand Down
67 changes: 55 additions & 12 deletions packages/databricks-vscode/src/bundle/GenerateBundle.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {CliWrapper} from "../cli/CliWrapper";
import {extensions, Uri} from "vscode";
import path from "node:path";
import {workspace, RelativePattern, GlobPattern, Disposable} from "vscode";
import YAML from "yaml";

export async function generateBundleSchema(cli: CliWrapper) {
export async function generateBundleSchema(
cli: CliWrapper
): Promise<Disposable> {
// get freshly generated bundle schema
const bundleSchema = await cli.getBundleSchema();

Expand All @@ -12,6 +15,51 @@ export async function generateBundleSchema(cli: CliWrapper) {
// URI for bundle root config json schema
const rootConfigSchemaUri = `${dabsUriScheme}:///root.json`;

const folder = workspace.workspaceFolders?.[0];
const configFilePattern = new RelativePattern(
folder!,
"{databricks,bundle}.{yml,yaml}"
);

// file watcher on all YAML files
const watcher = workspace.createFileSystemWatcher("**/*.{yml,yaml}");
watcher.onDidChange(async () => {
await updateFileGlobs();
});

let configFiles = new Set<string>();
await updateFileGlobs();

async function updateFileGlobs() {
const fileGlobs: GlobPattern[] = [configFilePattern];

// find all YAML files that are included in the root config file
for (const configFile of await workspace.findFiles(configFilePattern)) {
try {
const fileContents = await workspace.fs.readFile(configFile);
const config = YAML.parse(fileContents.toString());
if (config.include) {
fileGlobs.push(
...config.include.map(
(g: string) => new RelativePattern(folder!, g)
)
);
}
} catch (e) {
// ignore errors
}
}

// expand globs to find all config files
const newConfigFiles = new Set<string>();
for (const glob of fileGlobs) {
for (const file of await workspace.findFiles(glob)) {
newConfigFiles.add(file.path);
}
}
configFiles = newConfigFiles;
}

const extensionYaml = extensions.getExtension("redhat.vscode-yaml");
if (extensionYaml) {
const redHatYamlSchemaApi = await extensionYaml.activate();
Expand All @@ -21,16 +69,9 @@ export async function generateBundleSchema(cli: CliWrapper) {
redHatYamlSchemaApi.registerContributor(
"dabs",
(resource: string) => {
const validFileNames: string[] = [
"databricks.yml",
"databricks.yaml",
"bundle.yml",
"bundle.yaml",
];
for (const name of validFileNames) {
if (path.basename(resource) === name) {
return rootConfigSchemaUri;
}
const resourceUri = Uri.parse(resource);
if (configFiles.has(resourceUri.path)) {
return rootConfigSchemaUri;
}
return undefined;
},
Expand All @@ -43,4 +84,6 @@ export async function generateBundleSchema(cli: CliWrapper) {
}
);
}

return watcher;
}
6 changes: 4 additions & 2 deletions packages/databricks-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,12 +537,14 @@ export async function activate(

// generate a json schema for bundle root and load a custom provider into
// redhat.vscode-yaml extension to validate bundle config files with this schema
generateBundleSchema(cli).catch((e) => {
try {
context.subscriptions.push(await generateBundleSchema(cli));
} catch (e) {
NamedLogger.getOrCreate("Extension").error(
"Failed to load bundle schema: ",
e
);
});
}

connectionManager.login(false).catch((e) => {
NamedLogger.getOrCreate(Loggers.Extension).error("Login error", e);
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3715,6 +3715,7 @@ __metadata:
wdio-video-reporter: ^4.0.3
wdio-vscode-service: ^5.2.0
winston: ^3.10.0
yaml: ^2.3.1
yargs: ^17.7.2
languageName: unknown
linkType: soft
Expand Down Expand Up @@ -11198,6 +11199,13 @@ __metadata:
languageName: node
linkType: hard

"yaml@npm:^2.3.1":
version: 2.3.1
resolution: "yaml@npm:2.3.1"
checksum: 2c7bc9a7cd4c9f40d3b0b0a98e370781b68b8b7c4515720869aced2b00d92f5da1762b4ffa947f9e795d6cd6b19f410bd4d15fdd38aca7bd96df59bd9486fb54
languageName: node
linkType: hard

"yargs-parser@npm:20.2.4":
version: 20.2.4
resolution: "yargs-parser@npm:20.2.4"
Expand Down

0 comments on commit 9b8faec

Please sign in to comment.