Skip to content

Commit 75518a5

Browse files
committed
Ensure source folders are zipped
Zips source folders of databases when they are added. Only if the databases are fully controlled by VS Code. Fixes #479
1 parent 4beead5 commit 75518a5

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

extensions/ql-vscode/src/databaseFetcher.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fetch, { Response } from 'node-fetch';
22
import * as unzipper from 'unzipper';
3+
import { zip } from 'zip-a-folder';
34
import {
45
Uri,
56
ProgressOptions,
@@ -184,9 +185,9 @@ async function databaseArchiveFetcher(
184185
progressCallback?: ProgressCallback
185186
): Promise<DatabaseItem> {
186187
progressCallback?.({
187-
maxStep: 3,
188188
message: 'Getting database',
189189
step: 1,
190+
maxStep: 4,
190191
});
191192
if (!storagePath) {
192193
throw new Error('No storage path specified.');
@@ -201,9 +202,9 @@ async function databaseArchiveFetcher(
201202
}
202203

203204
progressCallback?.({
204-
maxStep: 3,
205205
message: 'Opening database',
206206
step: 3,
207+
maxStep: 4,
207208
});
208209

209210
// find the path to the database. The actual database might be in a sub-folder
@@ -213,6 +214,13 @@ async function databaseArchiveFetcher(
213214
'codeql-database.yml'
214215
);
215216
if (dbPath) {
217+
progressCallback?.({
218+
message: 'Validating and fixing source location',
219+
step: 4,
220+
maxStep: 4,
221+
});
222+
await ensureZippedSourceLocation(dbPath);
223+
216224
const item = await databasesManager.openDatabase(Uri.file(dbPath));
217225
databasesManager.setCurrentDatabaseItem(item);
218226
return item;
@@ -436,3 +444,24 @@ async function promptForLanguage(
436444
}
437445
);
438446
}
447+
448+
/**
449+
* Databases created by the old odasa tool will not have a zipped
450+
* source location. However, this extension works better if sources
451+
* are zipped.
452+
*
453+
* This function ensures that the source location is zipped. If the
454+
* `src` folder exists and the `src.zip` file does not, the `src`
455+
* folder will be zipped and then deleted.
456+
*
457+
* @param databasePath The full path to the unzipped database
458+
*/
459+
async function ensureZippedSourceLocation(databasePath: string): Promise<void> {
460+
const srcFolderPath = path.join(databasePath, 'src');
461+
const srcZipPath = srcFolderPath + '.zip';
462+
463+
if ((await fs.pathExists(srcFolderPath)) && !(await fs.pathExists(srcZipPath))) {
464+
await zip(srcFolderPath, srcZipPath);
465+
await fs.remove(srcFolderPath);
466+
}
467+
}

0 commit comments

Comments
 (0)