-
Notifications
You must be signed in to change notification settings - Fork 202
Allow import all database subfolders by selecting a folder #3797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
736dc46
Allow import all database subfolders by selecting a folder
reitowo 1ec341a
更新 CHANGELOG.md
reitowo fb6fac8
更新 package.json
reitowo 1e749ec
feat: move import folders out of original function, optimize logs
reitowo 955f8c8
feat: skip 0 folders
reitowo c84331e
fix: revert extra return type
reitowo 775e6dc
fix: unify naming
reitowo a93bf14
Apply suggestions from code review
reitowo 8170c46
feat: extract common logic
reitowo 5d4f75b
fix: testproj need to be with a dot
reitowo 1f6a7af
fix: unify descriptions.
reitowo 1b007c2
Apply suggestions from code review
reitowo 17a6076
fix: get error message
reitowo 9f1fd2c
fix: trim error
reitowo 5b854bc
fix: step message
reitowo 8b3add8
fix: remove title of importing status
reitowo e7e95e2
Clarify CHANGELOG.md
aeisenberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ import { | |
ThemeIcon, | ||
ThemeColor, | ||
workspace, | ||
FileType, | ||
} from "vscode"; | ||
import { pathExists, stat, readdir, remove } from "fs-extra"; | ||
|
||
|
@@ -36,6 +37,7 @@ import { | |
import { | ||
showAndLogExceptionWithTelemetry, | ||
showAndLogErrorMessage, | ||
showAndLogInformationMessage, | ||
} from "../common/logging"; | ||
import type { DatabaseFetcher } from "./database-fetcher"; | ||
import { asError, asyncFilter, getErrorMessage } from "../common/helpers-pure"; | ||
|
@@ -267,6 +269,8 @@ export class DatabaseUI extends DisposableObject { | |
"codeQL.getCurrentDatabase": this.handleGetCurrentDatabase.bind(this), | ||
"codeQL.chooseDatabaseFolder": | ||
this.handleChooseDatabaseFolderFromPalette.bind(this), | ||
"codeQL.chooseDatabaseFoldersParent": | ||
this.handleChooseDatabaseFoldersParentFromPalette.bind(this), | ||
"codeQL.chooseDatabaseArchive": | ||
this.handleChooseDatabaseArchiveFromPalette.bind(this), | ||
"codeQL.chooseDatabaseInternet": | ||
|
@@ -359,6 +363,12 @@ export class DatabaseUI extends DisposableObject { | |
); | ||
} | ||
|
||
private async handleChooseDatabaseFoldersParentFromPalette(): Promise<void> { | ||
return withProgress(async (progress) => { | ||
await this.chooseDatabasesParentFolder(progress); | ||
}); | ||
} | ||
Comment on lines
+367
to
+370
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aeisenberg Updated according to your suggestions with minor refine. I also removed the title of this progress bar, because otherwise the whole message will be too long and doesn't display the actual step message of each database. |
||
|
||
private async handleSetDefaultTourDatabase(): Promise<void> { | ||
return withProgress( | ||
async () => { | ||
|
@@ -957,26 +967,22 @@ export class DatabaseUI extends DisposableObject { | |
} | ||
|
||
/** | ||
* Ask the user for a database directory. Returns the chosen database, or `undefined` if the | ||
* operation was canceled. | ||
* Import database from uri. Returns the imported database, or `undefined` if the | ||
* operation was unsuccessful or canceled. | ||
*/ | ||
private async chooseAndSetDatabase( | ||
private async importDatabase( | ||
uri: Uri, | ||
byFolder: boolean, | ||
progress: ProgressCallback, | ||
): Promise<DatabaseItem | undefined> { | ||
const uri = await chooseDatabaseDir(byFolder); | ||
if (!uri) { | ||
return undefined; | ||
} | ||
|
||
if (byFolder && !uri.fsPath.endsWith("testproj")) { | ||
if (byFolder && !uri.fsPath.endsWith(".testproj")) { | ||
const fixedUri = await this.fixDbUri(uri); | ||
// we are selecting a database folder | ||
return await this.databaseManager.openDatabase(fixedUri, { | ||
type: "folder", | ||
}); | ||
} else { | ||
// we are selecting a database archive or a testproj. | ||
// we are selecting a database archive or a .testproj. | ||
// Unzip archives (if an archive) and copy into a workspace-controlled area | ||
// before importing. | ||
return await this.databaseFetcher.importLocalDatabase( | ||
|
@@ -986,6 +992,104 @@ export class DatabaseUI extends DisposableObject { | |
} | ||
} | ||
|
||
/** | ||
* Ask the user for a database directory. Returns the chosen database, or `undefined` if the | ||
* operation was canceled. | ||
*/ | ||
private async chooseAndSetDatabase( | ||
byFolder: boolean, | ||
progress: ProgressCallback, | ||
): Promise<DatabaseItem | undefined> { | ||
const uri = await chooseDatabaseDir(byFolder); | ||
if (!uri) { | ||
return undefined; | ||
} | ||
|
||
return await this.importDatabase(uri, byFolder, progress); | ||
} | ||
|
||
/** | ||
* Ask the user for a parent directory that contains all databases. | ||
* Returns all valid databases, or `undefined` if the operation was canceled. | ||
*/ | ||
private async chooseDatabasesParentFolder( | ||
progress: ProgressCallback, | ||
): Promise<DatabaseItem[] | undefined> { | ||
const uri = await chooseDatabaseDir(true); | ||
if (!uri) { | ||
return undefined; | ||
} | ||
|
||
const databases: DatabaseItem[] = []; | ||
const failures: string[] = []; | ||
const entries = await workspace.fs.readDirectory(uri); | ||
const validFileTypes = [FileType.File, FileType.Directory]; | ||
|
||
for (const [index, entry] of entries.entries()) { | ||
progress({ | ||
step: index + 1, | ||
maxStep: entries.length, | ||
message: `Importing '${entry[0]}'`, | ||
}); | ||
|
||
const subProgress: ProgressCallback = (p) => { | ||
progress({ | ||
step: index + 1, | ||
maxStep: entries.length, | ||
message: `Importing '${entry[0]}': (${p.step}/${p.maxStep}) ${p.message}`, | ||
}); | ||
}; | ||
|
||
if (!validFileTypes.includes(entry[1])) { | ||
void this.app.logger.log( | ||
`Skipping import for '${entry}', invalid file type: ${entry[1]}`, | ||
); | ||
continue; | ||
} | ||
|
||
try { | ||
const databaseUri = Uri.joinPath(uri, entry[0]); | ||
void this.app.logger.log(`Importing from ${databaseUri}`); | ||
|
||
const database = await this.importDatabase( | ||
databaseUri, | ||
entry[1] === FileType.Directory, | ||
subProgress, | ||
); | ||
if (database) { | ||
databases.push(database); | ||
} else { | ||
failures.push(entry[0]); | ||
} | ||
} catch (e) { | ||
failures.push(`${entry[0]}: ${getErrorMessage(e)}`.trim()); | ||
} | ||
} | ||
|
||
if (failures.length) { | ||
void showAndLogErrorMessage( | ||
this.app.logger, | ||
`Failed to import ${failures.length} database(s), successfully imported ${databases.length} database(s).`, | ||
{ | ||
fullMessage: `Failed to import ${failures.length} database(s), successfully imported ${databases.length} database(s).\nFailed databases:\n - ${failures.join("\n - ")}`, | ||
}, | ||
); | ||
} else if (databases.length === 0) { | ||
void showAndLogErrorMessage( | ||
this.app.logger, | ||
`No database folder to import.`, | ||
); | ||
return undefined; | ||
} else { | ||
void showAndLogInformationMessage( | ||
this.app.logger, | ||
`Successfully imported ${databases.length} database(s).`, | ||
); | ||
} | ||
|
||
return databases; | ||
} | ||
|
||
/** | ||
* Perform some heuristics to ensure a proper database location is chosen. | ||
* | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.