-
Notifications
You must be signed in to change notification settings - Fork 1
/
configuration.ts
145 lines (129 loc) · 5.83 KB
/
configuration.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
import * as vscode from 'vscode';
import { Uri } from 'vscode';
import { getCurrentFolder, showAndLogErrorMessage, showAndLogWarningMessage } from './helpers';
import { existsSync, mkdirSync } from 'fs';
export let SUPPORT_LANGUAGES = ['cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby'];
export let OUTPUT_FOLDER = 'codeql-agent-results';
export let DOCKER_CONTAINER_NAME = 'codeql-agent-docker';
/**
* Displays file selection dialog. Expects the user to choose a
* database directory, which should be the parent directory of a
* directory of the form `db-[language]`, for example, `db-cpp`.
*
* XXX: no validation is done other than checking the directory name
* to make sure it really is a database directory.
*/
export async function chooseProjectFolder(byFolder: boolean = true): Promise<vscode.Uri | undefined> {
const chosen = await vscode.window.showOpenDialog({
openLabel: byFolder ? 'Choose Database folder' : 'Choose Database archive',
canSelectFiles: !byFolder,
canSelectFolders: byFolder,
canSelectMany: false,
// eslint-disable-next-line @typescript-eslint/naming-convention
filters: byFolder ? {} : { Archives: ['zip'] },
});
return getFirst(chosen);
}
/** Gets the first element in the given list, if any, or undefined if the list is empty or undefined. */
function getFirst(list: vscode.Uri[] | undefined): vscode.Uri | undefined {
if (list === undefined || list.length === 0) {
return undefined;
} else {
return list[0];
}
}
class ProjectConfiguration {
sourcePath: string | undefined = undefined;
outputPath: string | undefined = undefined;
overwrite: boolean = true;
language: string | undefined = undefined;
dockerPath: string = 'docker';
async setSourcePath(sourcePath: Uri | undefined): Promise<boolean> {
if (sourcePath === undefined) {
this.sourcePath = undefined;
return true;
}
if (!existsSync(sourcePath.path)) {
showAndLogErrorMessage("Invalid source path or source path does not exists.");
return false;
} else {
this.sourcePath = sourcePath.path;
return true;
}
}
async getSourcePath(): Promise<string> {
if (this.sourcePath === undefined || this.sourcePath === '') {
this.sourcePath = await getCurrentFolder();
}
if (this.sourcePath === '' || !existsSync(this.sourcePath)) {
let error = new Error(`Invalid source path or source path does not exists: ` + this.sourcePath);
showAndLogErrorMessage(`${error.name}: ${error.message}`);
throw error;
}
return this.sourcePath;
}
async getLanguage(): Promise<string | undefined> {
let configLanguage: string | undefined = vscode.workspace.getConfiguration().get('codeql-agent.project.language');
if (configLanguage === "Auto detect") {
return undefined;
} else { return configLanguage; };
}
async getJavaVersion(): Promise<string | undefined> {
let javaVersion: string | undefined = vscode.workspace.getConfiguration().get('codeql-agent.project.javaVersion');
if (javaVersion === "Auto") {
return undefined;
} else { return javaVersion; };
}
async getCommand(): Promise<string | undefined> {
let configCommand: string | undefined = vscode.workspace.getConfiguration().get('codeql-agent.project.command');
if (configCommand === undefined || configCommand === "") {
return undefined;
} else { return configCommand; };
}
async getOutputPath(): Promise<string> {
this.outputPath = vscode.workspace.getConfiguration().get('codeql-agent.project.outputPath');
if (this.outputPath === undefined || this.outputPath === '') {
this.outputPath = `${await getCurrentFolder()}/${OUTPUT_FOLDER}`;
if (!existsSync(this.outputPath)){
mkdirSync(this.outputPath);
}
}
if (this.outputPath === '' || !existsSync(this.outputPath)) {
let error = new Error(`Invalid output path or output path does not exists: ` + this.outputPath);
showAndLogErrorMessage(`${error.name}: ${error.message}`);
throw error;
}
return this.outputPath;
}
async getSARIFResultPath(): Promise<string> {
let outputFolder = await this.getOutputPath();
return outputFolder + "/issues.sarif"
}
async getOverwriteFlag(): Promise<boolean> {
let configOverwriteFlag: boolean | undefined = vscode.workspace.getConfiguration().get('codeql-agent.project.overwriteFlag');
if (configOverwriteFlag === undefined) { return false; };
return configOverwriteFlag;
}
async getSaveCache(): Promise<boolean> {
let configSaveCache: boolean | undefined = vscode.workspace.getConfiguration().get('codeql-agent.project.saveCache');
if (configSaveCache === undefined) { return false; };
return configSaveCache;
}
async getThreads(): Promise<string> {
let configThreads: string | undefined = vscode.workspace.getConfiguration().get('codeql-agent.project.threads');
if (configThreads === undefined) { return "0"; };
return configThreads;
}
async getDockerPath(): Promise<string> {
let configDockerPath: string | undefined = vscode.workspace.getConfiguration().get('codeql-agent.cli.dockerExecutablePath');
if (configDockerPath !== undefined && configDockerPath !== '') {
if (existsSync(configDockerPath)) {
this.dockerPath = configDockerPath;
} else {
showAndLogErrorMessage("Can not found Docker executable path.");
}
}
return this.dockerPath;
}
}
export let projectConfiguration = new ProjectConfiguration();