Skip to content

Commit f356219

Browse files
Make DABs autocomplete detect new bundle files (#922)
## Changes * Fixes a big in #892, which caused the bundle autocomplete to not recognise a new file. * Bundle autocomplete now updates monitored files when * there is a change to the bundle root * a bundle file is created * a bundle file is deleted ## Tests * manual
1 parent 559911c commit f356219

File tree

4 files changed

+50
-23
lines changed

4 files changed

+50
-23
lines changed

packages/databricks-vscode/src/bundle/BundleFileSet.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,13 @@ export class BundleFileSet {
5656
return undefined;
5757
}
5858
const bundle = await parseBundleYaml(Uri.file(rootFile.fsPath));
59-
const includedFilesGlob =
60-
bundle?.include === undefined || bundle?.include.length === 0
61-
? undefined
62-
: `{${bundle.include?.join(",")}}`;
63-
64-
return includedFilesGlob;
59+
if (bundle?.include === undefined || bundle?.include.length === 0) {
60+
return undefined;
61+
}
62+
if (bundle?.include.length === 1) {
63+
return bundle.include[0];
64+
}
65+
return `{${bundle.include.join(",")}}`;
6566
}
6667

6768
async getIncludedFiles() {

packages/databricks-vscode/src/bundle/bundleAutocompleteProvider.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export async function registerBundleAutocompleteProvider(
1616
const dabsUriScheme = "dabs";
1717

1818
// URI for bundle root config json schema
19-
const rootConfigSchemaUri = `${dabsUriScheme}:///root.json`;
19+
const rootConfigSchemaUri = `${dabsUriScheme}:///databricks-asset-bundles.json`;
2020

2121
const extensionYaml = extensions.getExtension("redhat.vscode-yaml");
2222
if (extensionYaml) {
@@ -28,14 +28,21 @@ export async function registerBundleAutocompleteProvider(
2828
context.subscriptions.push(
2929
bundleWatcher.onDidChangeRootFile(async () => {
3030
bundleFileList = await bundleFileSet.allFiles();
31+
}),
32+
bundleWatcher.onDidCreate(async (e) => {
33+
bundleFileList.push(e);
34+
}),
35+
bundleWatcher.onDidDelete(async (e) => {
36+
bundleFileList.push(e);
3137
})
3238
);
3339
redHatYamlSchemaApi.registerContributor(
3440
"dabs",
3541
(resource: string) => {
42+
const resourceUri = Uri.parse(resource);
3643
if (
3744
bundleFileList.find(
38-
(i) => i.fsPath === Uri.parse(resource).fsPath
45+
(i) => i.fsPath === resourceUri.fsPath
3946
) !== undefined
4047
) {
4148
return rootConfigSchemaUri;

packages/databricks-vscode/src/extension.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,7 @@ export async function activate(
535535
);
536536

537537
const bundleFileSet = new BundleFileSet(workspace.workspaceFolders[0].uri);
538-
const bundleFileWatcher = new BundleWatcher(
539-
workspace.workspaceFolders[0].uri,
540-
bundleFileSet
541-
);
538+
const bundleFileWatcher = new BundleWatcher(bundleFileSet);
542539
context.subscriptions.push(bundleFileWatcher);
543540

544541
// generate a json schema for bundle root and load a custom provider into

packages/databricks-vscode/src/file-managers/BundleWatcher.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ export class BundleWatcher implements Disposable {
1212
private readonly _onDidChangeRootFile = new EventEmitter<void>();
1313
public readonly onDidChangeRootFile = this._onDidChangeRootFile.event;
1414

15+
private readonly _onDidCreate = new EventEmitter<Uri>();
16+
public readonly onDidCreate = this._onDidCreate.event;
17+
18+
private readonly _onDidDelete = new EventEmitter<Uri>();
19+
public readonly onDidDelete = this._onDidDelete.event;
20+
1521
private bundleFileSet: WithMutex<BundleFileSet>;
1622

17-
constructor(
18-
private readonly workspaceRoot: Uri,
19-
bundleFileSet: BundleFileSet
20-
) {
23+
constructor(bundleFileSet: BundleFileSet) {
2124
this.bundleFileSet = new WithMutex(bundleFileSet);
2225
const yamlWatcher = workspace.createFileSystemWatcher(
2326
this.bundleFileSet.value.getAbsolutePath(
@@ -27,21 +30,40 @@ export class BundleWatcher implements Disposable {
2730

2831
this.disposables.push(
2932
yamlWatcher,
30-
yamlWatcher.onDidCreate(this.yamlFileChangeHandler, this),
31-
yamlWatcher.onDidChange(this.yamlFileChangeHandler, this),
32-
yamlWatcher.onDidDelete(this.yamlFileChangeHandler, this)
33+
yamlWatcher.onDidCreate((e) => {
34+
this.yamlFileChangeHandler(e, "CREATE");
35+
}),
36+
yamlWatcher.onDidChange((e) => {
37+
this.yamlFileChangeHandler(e, "CHANGE");
38+
}),
39+
yamlWatcher.onDidDelete((e) => {
40+
this.yamlFileChangeHandler(e, "DELETE");
41+
})
3342
);
3443
}
3544

36-
private async yamlFileChangeHandler(e: Uri) {
37-
if (await this.bundleFileSet.value.isBundleFile(e)) {
38-
await this.bundleFileSet.value.invalidateMergedBundleCache();
39-
this._onDidChange.fire();
45+
private async yamlFileChangeHandler(
46+
e: Uri,
47+
type: "CREATE" | "CHANGE" | "DELETE"
48+
) {
49+
if (!(await this.bundleFileSet.value.isBundleFile(e))) {
50+
return;
4051
}
52+
53+
await this.bundleFileSet.value.invalidateMergedBundleCache();
54+
this._onDidChange.fire();
4155
// to provide additional granularity, we also fire an event when the root bundle file changes
4256
if (this.bundleFileSet.value.isRootBundleFile(e)) {
4357
this._onDidChangeRootFile.fire();
4458
}
59+
switch (type) {
60+
case "CREATE":
61+
this._onDidCreate.fire(e);
62+
break;
63+
case "DELETE":
64+
this._onDidDelete.fire(e);
65+
break;
66+
}
4567
}
4668

4769
dispose() {

0 commit comments

Comments
 (0)