Skip to content

Commit

Permalink
Make DABs autocomplete detect new bundle files (#922)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
kartikgupta-db authored Oct 31, 2023
1 parent 559911c commit f356219
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 23 deletions.
13 changes: 7 additions & 6 deletions packages/databricks-vscode/src/bundle/BundleFileSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ export class BundleFileSet {
return undefined;
}
const bundle = await parseBundleYaml(Uri.file(rootFile.fsPath));
const includedFilesGlob =
bundle?.include === undefined || bundle?.include.length === 0
? undefined
: `{${bundle.include?.join(",")}}`;

return includedFilesGlob;
if (bundle?.include === undefined || bundle?.include.length === 0) {
return undefined;
}
if (bundle?.include.length === 1) {
return bundle.include[0];
}
return `{${bundle.include.join(",")}}`;
}

async getIncludedFiles() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function registerBundleAutocompleteProvider(
const dabsUriScheme = "dabs";

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

const extensionYaml = extensions.getExtension("redhat.vscode-yaml");
if (extensionYaml) {
Expand All @@ -28,14 +28,21 @@ export async function registerBundleAutocompleteProvider(
context.subscriptions.push(
bundleWatcher.onDidChangeRootFile(async () => {
bundleFileList = await bundleFileSet.allFiles();
}),
bundleWatcher.onDidCreate(async (e) => {
bundleFileList.push(e);
}),
bundleWatcher.onDidDelete(async (e) => {
bundleFileList.push(e);
})
);
redHatYamlSchemaApi.registerContributor(
"dabs",
(resource: string) => {
const resourceUri = Uri.parse(resource);
if (
bundleFileList.find(
(i) => i.fsPath === Uri.parse(resource).fsPath
(i) => i.fsPath === resourceUri.fsPath
) !== undefined
) {
return rootConfigSchemaUri;
Expand Down
5 changes: 1 addition & 4 deletions packages/databricks-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,10 +535,7 @@ export async function activate(
);

const bundleFileSet = new BundleFileSet(workspace.workspaceFolders[0].uri);
const bundleFileWatcher = new BundleWatcher(
workspace.workspaceFolders[0].uri,
bundleFileSet
);
const bundleFileWatcher = new BundleWatcher(bundleFileSet);
context.subscriptions.push(bundleFileWatcher);

// generate a json schema for bundle root and load a custom provider into
Expand Down
44 changes: 33 additions & 11 deletions packages/databricks-vscode/src/file-managers/BundleWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ export class BundleWatcher implements Disposable {
private readonly _onDidChangeRootFile = new EventEmitter<void>();
public readonly onDidChangeRootFile = this._onDidChangeRootFile.event;

private readonly _onDidCreate = new EventEmitter<Uri>();
public readonly onDidCreate = this._onDidCreate.event;

private readonly _onDidDelete = new EventEmitter<Uri>();
public readonly onDidDelete = this._onDidDelete.event;

private bundleFileSet: WithMutex<BundleFileSet>;

constructor(
private readonly workspaceRoot: Uri,
bundleFileSet: BundleFileSet
) {
constructor(bundleFileSet: BundleFileSet) {
this.bundleFileSet = new WithMutex(bundleFileSet);
const yamlWatcher = workspace.createFileSystemWatcher(
this.bundleFileSet.value.getAbsolutePath(
Expand All @@ -27,21 +30,40 @@ export class BundleWatcher implements Disposable {

this.disposables.push(
yamlWatcher,
yamlWatcher.onDidCreate(this.yamlFileChangeHandler, this),
yamlWatcher.onDidChange(this.yamlFileChangeHandler, this),
yamlWatcher.onDidDelete(this.yamlFileChangeHandler, this)
yamlWatcher.onDidCreate((e) => {
this.yamlFileChangeHandler(e, "CREATE");
}),
yamlWatcher.onDidChange((e) => {
this.yamlFileChangeHandler(e, "CHANGE");
}),
yamlWatcher.onDidDelete((e) => {
this.yamlFileChangeHandler(e, "DELETE");
})
);
}

private async yamlFileChangeHandler(e: Uri) {
if (await this.bundleFileSet.value.isBundleFile(e)) {
await this.bundleFileSet.value.invalidateMergedBundleCache();
this._onDidChange.fire();
private async yamlFileChangeHandler(
e: Uri,
type: "CREATE" | "CHANGE" | "DELETE"
) {
if (!(await this.bundleFileSet.value.isBundleFile(e))) {
return;
}

await this.bundleFileSet.value.invalidateMergedBundleCache();
this._onDidChange.fire();
// to provide additional granularity, we also fire an event when the root bundle file changes
if (this.bundleFileSet.value.isRootBundleFile(e)) {
this._onDidChangeRootFile.fire();
}
switch (type) {
case "CREATE":
this._onDidCreate.fire(e);
break;
case "DELETE":
this._onDidDelete.fire(e);
break;
}
}

dispose() {
Expand Down

0 comments on commit f356219

Please sign in to comment.