Skip to content

Commit 0cfbf0c

Browse files
committed
Move workspace folder functions to separate file
1 parent bfead07 commit 0cfbf0c

File tree

24 files changed

+169
-162
lines changed

24 files changed

+169
-162
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { dirname, join } from "path";
2+
import { workspace, WorkspaceFolder } from "vscode";
3+
4+
/** Returns true if the specified workspace folder is on the file system. */
5+
export function isWorkspaceFolderOnDisk(
6+
workspaceFolder: WorkspaceFolder,
7+
): boolean {
8+
return workspaceFolder.uri.scheme === "file";
9+
}
10+
11+
/** Gets all active workspace folders that are on the filesystem. */
12+
export function getOnDiskWorkspaceFoldersObjects() {
13+
const workspaceFolders = workspace.workspaceFolders ?? [];
14+
return workspaceFolders.filter(isWorkspaceFolderOnDisk);
15+
}
16+
17+
/** Gets all active workspace folders that are on the filesystem. */
18+
export function getOnDiskWorkspaceFolders() {
19+
return getOnDiskWorkspaceFoldersObjects().map((folder) => folder.uri.fsPath);
20+
}
21+
22+
/** Check if folder is already present in workspace */
23+
export function isFolderAlreadyInWorkspace(folderName: string) {
24+
const workspaceFolders = workspace.workspaceFolders || [];
25+
26+
return !!workspaceFolders.find(
27+
(workspaceFolder) => workspaceFolder.name === folderName,
28+
);
29+
}
30+
31+
/**
32+
* Returns the path of the first folder in the workspace.
33+
* This is used to decide where to create skeleton QL packs.
34+
*
35+
* If the first folder is a QL pack, then the parent folder is returned.
36+
* This is because the vscode-codeql-starter repo contains a ql pack in
37+
* the first folder.
38+
*
39+
* This is a temporary workaround until we can retire the
40+
* vscode-codeql-starter repo.
41+
*/
42+
export function getFirstWorkspaceFolder() {
43+
const workspaceFolders = getOnDiskWorkspaceFolders();
44+
45+
if (!workspaceFolders || workspaceFolders.length === 0) {
46+
throw new Error("No workspace folders found");
47+
}
48+
49+
const firstFolderFsPath = workspaceFolders[0];
50+
51+
// For the vscode-codeql-starter repo, the first folder will be a ql pack
52+
// so we need to get the parent folder
53+
if (
54+
firstFolderFsPath.includes(
55+
join("vscode-codeql-starter", "codeql-custom-queries"),
56+
)
57+
) {
58+
// return the parent folder
59+
return dirname(firstFolderFsPath);
60+
} else {
61+
// if the first folder is not a ql pack, then we are in a normal workspace
62+
return firstFolderFsPath;
63+
}
64+
}

extensions/ql-vscode/src/data-extensions-editor/extension-pack-picker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import { dump as dumpYaml, load as loadYaml } from "js-yaml";
44
import { minimatch } from "minimatch";
55
import { CancellationToken, window } from "vscode";
66
import { CodeQLCliServer } from "../codeql-cli/cli";
7+
import { showAndLogErrorMessage } from "../helpers";
78
import {
89
getOnDiskWorkspaceFolders,
910
getOnDiskWorkspaceFoldersObjects,
10-
showAndLogErrorMessage,
11-
} from "../helpers";
11+
} from "../common/vscode/workspace-folders";
1212
import { ProgressCallback } from "../common/vscode/progress";
1313
import { DatabaseItem } from "../databases/local-databases";
1414
import { getQlPackPath, QLPACK_FILENAMES } from "../pure/ql";

extensions/ql-vscode/src/data-extensions-editor/external-api-usage-query.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ import { CoreCompletedQuery, QueryRunner } from "../query-server";
22
import { dir } from "tmp-promise";
33
import { writeFile } from "fs-extra";
44
import { dump as dumpYaml } from "js-yaml";
5-
import {
6-
getOnDiskWorkspaceFolders,
7-
showAndLogExceptionWithTelemetry,
8-
} from "../helpers";
5+
import { showAndLogExceptionWithTelemetry } from "../helpers";
6+
import { getOnDiskWorkspaceFolders } from "../common/vscode/workspace-folders";
97
import { TeeLogger } from "../common";
108
import { isQueryLanguage } from "../common/query-language";
119
import { CancellationToken } from "vscode";

extensions/ql-vscode/src/data-extensions-editor/generate-flow-model.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import { CodeQLCliServer } from "../codeql-cli/cli";
66
import { TeeLogger } from "../common";
77
import { extensiblePredicateDefinitions } from "./predicates";
88
import { ProgressCallback } from "../common/vscode/progress";
9-
import {
10-
getOnDiskWorkspaceFolders,
11-
showAndLogExceptionWithTelemetry,
12-
} from "../helpers";
9+
import { showAndLogExceptionWithTelemetry } from "../helpers";
10+
import { getOnDiskWorkspaceFolders } from "../common/vscode/workspace-folders";
1311
import {
1412
ModeledMethodType,
1513
ModeledMethodWithSignature,

extensions/ql-vscode/src/databases/local-databases/database-manager.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ import { join } from "path";
1414
import { FullDatabaseOptions } from "./database-options";
1515
import { DatabaseItemImpl } from "./database-item-impl";
1616
import {
17-
getFirstWorkspaceFolder,
18-
isFolderAlreadyInWorkspace,
1917
showAndLogExceptionWithTelemetry,
2018
showNeverAskAgainDialog,
2119
} from "../../helpers";
20+
import {
21+
getFirstWorkspaceFolder,
22+
isFolderAlreadyInWorkspace,
23+
} from "../../common/vscode/workspace-folders";
2224
import { isQueryLanguage } from "../../common/query-language";
2325
import { existsSync } from "fs";
2426
import { QlPackGenerator } from "../../qlpack-generator";

extensions/ql-vscode/src/databases/qlpack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { readFile } from "fs-extra";
66
import { getQlPackPath } from "../pure/ql";
77
import { CodeQLCliServer, QlpacksInfo } from "../codeql-cli/cli";
88
import { extLogger } from "../common";
9-
import { getOnDiskWorkspaceFolders } from "../helpers";
9+
import { getOnDiskWorkspaceFolders } from "../common/vscode/workspace-folders";
1010

1111
export interface QlPacksForLanguage {
1212
/** The name of the pack containing the dbscheme. */

extensions/ql-vscode/src/debugger/debug-configuration.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
DebugConfigurationProvider,
55
WorkspaceFolder,
66
} from "vscode";
7-
import { getOnDiskWorkspaceFolders, showAndLogErrorMessage } from "../helpers";
7+
import { showAndLogErrorMessage } from "../helpers";
8+
import { getOnDiskWorkspaceFolders } from "../common/vscode/workspace-folders";
89
import { LocalQueries } from "../local-queries";
910
import { getQuickEvalContext, validateQueryPath } from "../run-queries-shared";
1011
import * as CodeQLProtocol from "./debug-protocol";

extensions/ql-vscode/src/helpers.ts

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ensureDirSync, pathExists, ensureDir, writeFile } from "fs-extra";
2-
import { join, dirname } from "path";
2+
import { join } from "path";
33
import { dirSync } from "tmp-promise";
4-
import { Uri, window as Window, workspace, env, WorkspaceFolder } from "vscode";
4+
import { Uri, window as Window, workspace, env } from "vscode";
55
import { CodeQLCliServer } from "./codeql-cli/cli";
66
import { UserCancellationException } from "./common/vscode/progress";
77
import { extLogger, OutputChannelLogger } from "./common";
@@ -11,6 +11,7 @@ import { RedactableError } from "./pure/errors";
1111
import { isQueryLanguage, QueryLanguage } from "./common/query-language";
1212
import { isCodespacesTemplate } from "./config";
1313
import { AppCommandManager } from "./common/commands";
14+
import { getOnDiskWorkspaceFolders } from "./common/vscode/workspace-folders";
1415

1516
// Shared temporary folder for the extension.
1617
export const tmpDir = dirSync({
@@ -232,13 +233,6 @@ export async function showInformationMessageWithAction(
232233
return chosenItem === actionItem;
233234
}
234235

235-
/** Returns true if the specified workspace folder is on the file system. */
236-
export function isWorkspaceFolderOnDisk(
237-
workspaceFolder: WorkspaceFolder,
238-
): boolean {
239-
return workspaceFolder.uri.scheme === "file";
240-
}
241-
242236
/**
243237
* Opens a modal dialog for the user to make a choice between yes/no/never be asked again.
244238
*
@@ -279,26 +273,6 @@ export async function showNeverAskAgainDialog(
279273
return chosenItem?.title;
280274
}
281275

282-
/** Gets all active workspace folders that are on the filesystem. */
283-
export function getOnDiskWorkspaceFoldersObjects() {
284-
const workspaceFolders = workspace.workspaceFolders ?? [];
285-
return workspaceFolders.filter(isWorkspaceFolderOnDisk);
286-
}
287-
288-
/** Gets all active workspace folders that are on the filesystem. */
289-
export function getOnDiskWorkspaceFolders() {
290-
return getOnDiskWorkspaceFoldersObjects().map((folder) => folder.uri.fsPath);
291-
}
292-
293-
/** Check if folder is already present in workspace */
294-
export function isFolderAlreadyInWorkspace(folderName: string) {
295-
const workspaceFolders = workspace.workspaceFolders || [];
296-
297-
return !!workspaceFolders.find(
298-
(workspaceFolder) => workspaceFolder.name === folderName,
299-
);
300-
}
301-
302276
/** Check if the current workspace is the CodeTour and open the workspace folder.
303277
* Without this, we can't run the code tour correctly.
304278
**/
@@ -448,39 +422,3 @@ export async function createTimestampFile(storagePath: string) {
448422
await ensureDir(storagePath);
449423
await writeFile(timestampPath, Date.now().toString(), "utf8");
450424
}
451-
452-
/**
453-
* Returns the path of the first folder in the workspace.
454-
* This is used to decide where to create skeleton QL packs.
455-
*
456-
* If the first folder is a QL pack, then the parent folder is returned.
457-
* This is because the vscode-codeql-starter repo contains a ql pack in
458-
* the first folder.
459-
*
460-
* This is a temporary workaround until we can retire the
461-
* vscode-codeql-starter repo.
462-
*/
463-
464-
export function getFirstWorkspaceFolder() {
465-
const workspaceFolders = getOnDiskWorkspaceFolders();
466-
467-
if (!workspaceFolders || workspaceFolders.length === 0) {
468-
throw new Error("No workspace folders found");
469-
}
470-
471-
const firstFolderFsPath = workspaceFolders[0];
472-
473-
// For the vscode-codeql-starter repo, the first folder will be a ql pack
474-
// so we need to get the parent folder
475-
if (
476-
firstFolderFsPath.includes(
477-
join("vscode-codeql-starter", "codeql-custom-queries"),
478-
)
479-
) {
480-
// return the parent folder
481-
return dirname(firstFolderFsPath);
482-
} else {
483-
// if the first folder is not a ql pack, then we are in a normal workspace
484-
return firstFolderFsPath;
485-
}
486-
}

extensions/ql-vscode/src/language-support/contextual/query-resolver.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ import { dump } from "js-yaml";
33
import { file } from "tmp-promise";
44
import { basename, dirname, resolve } from "path";
55

6-
import {
7-
getOnDiskWorkspaceFolders,
8-
showAndLogExceptionWithTelemetry,
9-
} from "../../helpers";
6+
import { showAndLogExceptionWithTelemetry } from "../../helpers";
7+
import { getOnDiskWorkspaceFolders } from "../../common/vscode/workspace-folders";
108
import {
119
getPrimaryDbscheme,
1210
getQlPackForDbscheme,

extensions/ql-vscode/src/local-queries/local-queries.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ import { basename } from "path";
1919
import {
2020
createTimestampFile,
2121
findLanguage,
22-
getOnDiskWorkspaceFolders,
2322
showAndLogErrorMessage,
2423
showAndLogWarningMessage,
2524
showBinaryChoiceDialog,
2625
} from "../helpers";
26+
import { getOnDiskWorkspaceFolders } from "../common/vscode/workspace-folders";
2727
import { displayQuickQuery } from "./quick-query";
2828
import { CoreCompletedQuery, QueryRunner } from "../query-server";
2929
import { QueryHistoryManager } from "../query-history/query-history-manager";

0 commit comments

Comments
 (0)