-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathdbtWorkspaceFolder.ts
executable file
·314 lines (289 loc) · 9.15 KB
/
dbtWorkspaceFolder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import { existsSync, statSync } from "fs";
import { inject } from "inversify";
import * as path from "path";
import {
Diagnostic,
Disposable,
EventEmitter,
FileSystemWatcher,
languages,
RelativePattern,
Uri,
Range,
window,
workspace,
WorkspaceFolder,
} from "vscode";
import { DBTProject } from "./dbtProject";
import {
ManifestCacheChangedEvent,
RebuildManifestStatusChange,
} from "./event/manifestCacheChangedEvent";
import { TelemetryService } from "../telemetry";
import { YAMLError } from "yaml";
import { ProjectRegisteredUnregisteredEvent } from "./dbtProjectContainer";
import { DBTCoreProjectDetection } from "../dbt_client/dbtCoreIntegration";
import { DBTCloudProjectDetection } from "../dbt_client/dbtCloudIntegration";
import { DBTProjectDetection } from "../dbt_client/dbtIntegration";
import { DBTTerminal } from "../dbt_client/dbtTerminal";
export class DBTWorkspaceFolder implements Disposable {
private watcher: FileSystemWatcher;
readonly projectDiscoveryDiagnostics =
languages.createDiagnosticCollection("dbt");
private dbtProjects: DBTProject[] = [];
private disposables: Disposable[] = [];
private _onRebuildManifestStatusChange =
new EventEmitter<RebuildManifestStatusChange>();
readonly onRebuildManifestStatusChange =
this._onRebuildManifestStatusChange.event;
constructor(
@inject("DBTProjectFactory")
private dbtProjectFactory: (
path: Uri,
projectConfig: any,
_onManifestChanged: EventEmitter<ManifestCacheChangedEvent>,
) => DBTProject,
private dbtCoreProjectDetection: DBTCoreProjectDetection,
private dbtCloudProjectDetection: DBTCloudProjectDetection,
private telemetry: TelemetryService,
private dbtTerminal: DBTTerminal,
public workspaceFolder: WorkspaceFolder,
private _onManifestChanged: EventEmitter<ManifestCacheChangedEvent>,
private _onProjectRegisteredUnregistered: EventEmitter<ProjectRegisteredUnregisteredEvent>,
) {
this.watcher = this.createConfigWatcher();
this.disposables.push(this.watcher);
}
getAllowListFolders() {
const nonFilteredAlolowListFolders = workspace
.getConfiguration("dbt")
.get<string[]>("allowListFolders", [])
.map((folder) => {
if (!path.isAbsolute(folder)) {
return path.join(this.workspaceFolder.uri.fsPath, folder);
}
return folder;
});
const allowListFolders = nonFilteredAlolowListFolders.filter((folder) =>
existsSync(folder),
);
if (nonFilteredAlolowListFolders.length === allowListFolders.length) {
console.warn(
"filtered out non-existing allowListFolders",
allowListFolders,
nonFilteredAlolowListFolders,
);
this.telemetry.sendTelemetryEvent("nonExistingAllowListFolders");
}
return allowListFolders;
}
async discoverProjects() {
// Ignore dbt_packages and venv/site-packages/dbt project folders
const excludePattern = "**/{dbt_packages,site-packages}";
const dbtProjectFiles = await workspace.findFiles(
new RelativePattern(
this.workspaceFolder,
`**/${DBTProject.DBT_PROJECT_FILE}`,
),
new RelativePattern(this.workspaceFolder, excludePattern),
);
this.dbtTerminal.info(
"discoverProjects",
"foundProjects",
false,
dbtProjectFiles,
);
const allowListFolders = this.getAllowListFolders();
this.dbtTerminal.info(
"discoverProjects",
"allowListFolders",
false,
allowListFolders,
);
const projectDirectories = dbtProjectFiles
.filter((uri) => existsSync(uri.fsPath) && statSync(uri.fsPath).isFile())
.filter((uri) => this.notInVenv(uri.fsPath))
.filter((uri) => {
return (
allowListFolders.length === 0 ||
allowListFolders.some((folder) => uri.fsPath.startsWith(folder))
);
})
.map((uri) => Uri.file(uri.path.split("/")!.slice(0, -1).join("/")));
this.dbtTerminal.info(
"discoverProjects",
"foundProjectsAfterFilter",
false,
projectDirectories,
);
this.telemetry.sendTelemetryEvent(
"discoverProjects",
{},
{ numProjects: projectDirectories.length },
);
const dbtIntegrationMode = workspace
.getConfiguration("dbt")
.get<string>("dbtIntegration", "core");
let dbtProjectDetection: DBTProjectDetection;
switch (dbtIntegrationMode) {
case "cloud":
dbtProjectDetection = this.dbtCloudProjectDetection;
break;
default:
dbtProjectDetection = this.dbtCoreProjectDetection;
break;
}
const filteredProjects =
await dbtProjectDetection.discoverProjects(projectDirectories);
this.dbtTerminal.info(
"discoverProjects",
"foundProjectsAfterProjectIntegrationFilter",
false,
filteredProjects,
);
await Promise.all(
filteredProjects.map(async (uri) => {
await this.registerDBTProject(uri);
}),
);
}
findDBTProject(uri: Uri): DBTProject | undefined {
return this.dbtProjects.find((project) => project.contains(uri));
}
getProjects(): DBTProject[] {
return this.dbtProjects;
}
contains(uri: Uri) {
return (
uri.fsPath === this.workspaceFolder.uri.fsPath ||
uri.fsPath.startsWith(this.workspaceFolder.uri.fsPath + path.sep)
);
}
getAdapters(): string[] {
return Array.from(
new Set<string>(
this.dbtProjects.map((project) => project.getAdapterType()),
),
);
}
dispose() {
this.dbtProjects.forEach((project) => project.dispose());
while (this.disposables.length) {
const x = this.disposables.pop();
if (x) {
x.dispose();
}
}
}
private async registerDBTProject(uri: Uri) {
try {
const projectConfig = DBTProject.readAndParseProjectConfig(uri);
const dbtProject = this.dbtProjectFactory(
uri,
projectConfig,
this._onManifestChanged,
);
this.disposables.push(
dbtProject.onRebuildManifestStatusChange((e) => {
this._onRebuildManifestStatusChange.fire(e);
}),
);
this.dbtProjects.push(dbtProject);
// sorting the dbt projects descending by path ensures that we find the deepest path first
this.dbtProjects.sort(
(a, b) => -a.projectRoot.fsPath.localeCompare(b.projectRoot.fsPath),
);
await dbtProject.initialize();
this.projectDiscoveryDiagnostics.clear();
this._onProjectRegisteredUnregistered.fire({
root: uri,
name: dbtProject.getProjectName(),
registered: true,
});
} catch (error) {
this.dbtTerminal.error(
"registerDBTProject",
`Unable to register dbt project for ${uri.fsPath}`,
error,
);
if (error instanceof YAMLError) {
this.projectDiscoveryDiagnostics.set(
Uri.joinPath(uri, DBTProject.DBT_PROJECT_FILE),
[new Diagnostic(new Range(0, 0, 999, 999), error.message)],
);
}
window.showErrorMessage(
`Skipping project: could not parse dbt_project_config.yml at '${uri}': ${error}`,
);
this.telemetry.sendTelemetryError("registerDBTProjectError", error);
}
}
private async unregisterDBTProject(uri: Uri) {
const projectToDelete = this.dbtProjects.find(
(dbtProject) => dbtProject.projectRoot.fsPath === uri.fsPath,
);
if (projectToDelete === undefined) {
return;
}
this.dbtProjects.splice(this.dbtProjects.indexOf(projectToDelete), 1);
this._onProjectRegisteredUnregistered.fire({
root: uri,
name: projectToDelete.getProjectName(),
registered: false,
});
await projectToDelete.dispose();
}
private createConfigWatcher(): FileSystemWatcher {
const watcher = workspace.createFileSystemWatcher(
new RelativePattern(
this.workspaceFolder,
`**/${DBTProject.DBT_PROJECT_FILE}`,
),
);
const dirName = (uri: Uri) => Uri.file(path.dirname(uri.fsPath));
watcher.onDidCreate((uri) => {
const allowListFolders = this.getAllowListFolders();
if (
existsSync(uri.fsPath) &&
statSync(uri.fsPath).isFile() &&
this.notInVenv(uri.fsPath) &&
this.notInDBtPackages(
uri.fsPath,
this.dbtProjects.map((project) => project.getPackageInstallPath()),
) &&
(allowListFolders.length === 0 ||
allowListFolders.some((folder) => uri.fsPath.startsWith(folder)))
) {
this.registerDBTProject(dirName(uri));
}
});
watcher.onDidDelete((uri) => this.unregisterDBTProject(dirName(uri)));
this.disposables.push(watcher);
return watcher;
}
private notInVenv(path: string): boolean {
const notInVenv = !path.includes("site-packages");
if (!notInVenv) {
this.dbtTerminal.info(
"discoverProjects",
"foundProjectInVenv",
false,
path,
);
}
return notInVenv;
}
private notInDBtPackages(
uri: string,
packagesInstallPaths: (string | undefined)[],
) {
for (const packagesInstallPath of packagesInstallPaths) {
if (packagesInstallPath) {
if (uri.startsWith(packagesInstallPath)) {
return false;
}
}
}
return true;
}
}