mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-16 09:35:36 -05:00
Initial SQL Project tree viewlet in Explorer sidebar (#8639)
* Adding mock contents for tree * added open sqlproj dialog * reading files from directory * Added directory traversal * Adding tree sorting by folder vs file and label * Improved auto-unfolding of tree based on node type * replacing fs with fs.promise alias * added activation event for when workspace contains sqlproj files * Returning after displaying error
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
export class SqlDatabaseProjectItem {
|
||||
label: string;
|
||||
readonly isFolder: boolean;
|
||||
readonly parent?: SqlDatabaseProjectItem;
|
||||
children: SqlDatabaseProjectItem[] = [];
|
||||
|
||||
constructor(label: string, isFolder: boolean, parent?: SqlDatabaseProjectItem) {
|
||||
this.label = label;
|
||||
this.isFolder = isFolder;
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public createChild(label: string, isFolder: boolean): SqlDatabaseProjectItem {
|
||||
let child = new SqlDatabaseProjectItem(label, isFolder, this);
|
||||
this.children.push(child);
|
||||
|
||||
return child;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import { promises as fs } from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as constants from '../common/constants';
|
||||
|
||||
import { SqlDatabaseProjectItem } from './databaseProjectTreeItem';
|
||||
|
||||
export class SqlDatabaseProjectTreeViewProvider implements vscode.TreeDataProvider<SqlDatabaseProjectItem> {
|
||||
private _onDidChangeTreeData: vscode.EventEmitter<SqlDatabaseProjectItem | undefined> = new vscode.EventEmitter<SqlDatabaseProjectItem | undefined>();
|
||||
readonly onDidChangeTreeData: vscode.Event<SqlDatabaseProjectItem | undefined> = this._onDidChangeTreeData.event;
|
||||
|
||||
private roots: SqlDatabaseProjectItem[] = [];
|
||||
|
||||
constructor() {
|
||||
this.initialize();
|
||||
}
|
||||
|
||||
private initialize() {
|
||||
this.roots = [new SqlDatabaseProjectItem(constants.noOpenProjectMessage, false)];
|
||||
}
|
||||
|
||||
public getTreeItem(element: SqlDatabaseProjectItem): vscode.TreeItem {
|
||||
return {
|
||||
label: element.label,
|
||||
collapsibleState: element.parent === undefined
|
||||
? vscode.TreeItemCollapsibleState.Expanded
|
||||
: element.isFolder
|
||||
? vscode.TreeItemCollapsibleState.Collapsed
|
||||
: vscode.TreeItemCollapsibleState.None
|
||||
};
|
||||
}
|
||||
|
||||
public getChildren(element?: SqlDatabaseProjectItem): SqlDatabaseProjectItem[] {
|
||||
if (element === undefined) {
|
||||
return this.roots;
|
||||
}
|
||||
|
||||
return element.children;
|
||||
}
|
||||
|
||||
public async openProject(projectFiles: vscode.Uri[]) {
|
||||
if (projectFiles.length > 1) { // TODO: how to handle opening a folder with multiple .sqlproj files?
|
||||
vscode.window.showErrorMessage(constants.multipleSqlProjFiles);
|
||||
return;
|
||||
}
|
||||
|
||||
if (projectFiles.length === 0) {
|
||||
vscode.window.showErrorMessage(constants.noSqlProjFiles);
|
||||
return;
|
||||
}
|
||||
|
||||
let directoryPath = path.dirname(projectFiles[0].fsPath);
|
||||
console.log('Opening project directory: ' + directoryPath);
|
||||
|
||||
let newRoots: SqlDatabaseProjectItem[] = [];
|
||||
|
||||
newRoots.push(await this.constructDataSourcesTree(directoryPath));
|
||||
newRoots.push(await this.constructProjectTree(directoryPath));
|
||||
|
||||
this.roots = newRoots;
|
||||
this._onDidChangeTreeData.fire();
|
||||
}
|
||||
|
||||
private async constructProjectTree(directoryPath: string): Promise<SqlDatabaseProjectItem> {
|
||||
let projectsNode = await this.constructFileTreeNode(directoryPath, undefined);
|
||||
|
||||
projectsNode.label = constants.projectNodeName;
|
||||
|
||||
return projectsNode;
|
||||
}
|
||||
|
||||
private async constructFileTreeNode(entryPath: string, parentNode: SqlDatabaseProjectItem | undefined): Promise<SqlDatabaseProjectItem> {
|
||||
let stat = await fs.stat(entryPath);
|
||||
|
||||
let output = parentNode === undefined
|
||||
? new SqlDatabaseProjectItem(path.basename(entryPath), stat.isDirectory())
|
||||
: parentNode.createChild(path.basename(entryPath), stat.isDirectory());
|
||||
|
||||
if (stat.isDirectory()) {
|
||||
let contents = await fs.readdir(entryPath);
|
||||
|
||||
for (const entry of contents) {
|
||||
await this.constructFileTreeNode(path.join(entryPath, entry), output);
|
||||
}
|
||||
|
||||
// sort children so that folders come first, then alphabetical
|
||||
output.children.sort((a: SqlDatabaseProjectItem, b: SqlDatabaseProjectItem) => {
|
||||
if (a.isFolder && !b.isFolder) { return -1; }
|
||||
else if (!a.isFolder && b.isFolder) { return 1; }
|
||||
else { return a.label.localeCompare(b.label); }
|
||||
});
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
private async constructDataSourcesTree(directoryPath: string): Promise<SqlDatabaseProjectItem> {
|
||||
let dataSourceNode = new SqlDatabaseProjectItem(constants.dataSourcesNodeName, true);
|
||||
|
||||
let dataSourcesFilePath = path.join(directoryPath, constants.dataSourcesFileName);
|
||||
|
||||
try {
|
||||
let connections = await fs.readFile(dataSourcesFilePath, 'r');
|
||||
|
||||
// TODO: parse connections.json
|
||||
|
||||
dataSourceNode.createChild(constants.foundDataSourcesFile + connections.length, false);
|
||||
}
|
||||
catch {
|
||||
dataSourceNode.createChild(constants.noDataSourcesFile, false);
|
||||
}
|
||||
|
||||
return dataSourceNode;
|
||||
}
|
||||
}
|
||||
@@ -4,12 +4,21 @@
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import * as nls from 'vscode-nls';
|
||||
|
||||
import { SqlDatabaseProjectTreeViewProvider } from './databaseProjectTreeViewProvider';
|
||||
import { getErrorMessage } from '../common/utils';
|
||||
|
||||
const SQL_DATABASE_PROJECTS_VIEW_ID = 'sqlDatabaseProjectsView';
|
||||
|
||||
const localize = nls.loadMessageBundle();
|
||||
|
||||
/**
|
||||
* The main controller class that initializes the extension
|
||||
*/
|
||||
export default class MainController implements vscode.Disposable {
|
||||
protected _context: vscode.ExtensionContext;
|
||||
protected dbProjectTreeViewProvider: SqlDatabaseProjectTreeViewProvider = new SqlDatabaseProjectTreeViewProvider();
|
||||
|
||||
public constructor(context: vscode.ExtensionContext) {
|
||||
this._context = context;
|
||||
@@ -22,14 +31,36 @@ export default class MainController implements vscode.Disposable {
|
||||
public deactivate(): void {
|
||||
}
|
||||
|
||||
public activate(): Promise<boolean> {
|
||||
this.initializeDatabaseProjects();
|
||||
return Promise.resolve(true);
|
||||
public async activate(): Promise<void> {
|
||||
await this.initializeDatabaseProjects();
|
||||
}
|
||||
|
||||
private initializeDatabaseProjects(): void {
|
||||
vscode.commands.registerCommand('sqlDatabaseProjects.new', () => { console.log('new database project called'); });
|
||||
vscode.commands.registerCommand('sqlDatabaseProjects.open', () => { console.log('open database project called'); });
|
||||
private async initializeDatabaseProjects(): Promise<void> {
|
||||
// init commands
|
||||
vscode.commands.registerCommand('sqlDatabaseProjects.new', () => { console.log('"New Database Project" called.'); });
|
||||
vscode.commands.registerCommand('sqlDatabaseProjects.open', async () => { this.openProjectFolder(); });
|
||||
|
||||
// init view
|
||||
this.dbProjectTreeViewProvider = new SqlDatabaseProjectTreeViewProvider();
|
||||
|
||||
this.extensionContext.subscriptions.push(vscode.window.registerTreeDataProvider(SQL_DATABASE_PROJECTS_VIEW_ID, this.dbProjectTreeViewProvider));
|
||||
}
|
||||
|
||||
public async openProjectFolder(): Promise<void> {
|
||||
try {
|
||||
let filter: { [key: string]: string[] } = {};
|
||||
|
||||
filter[localize('sqlDatabaseProject', "SQL database project")] = ['sqlproj'];
|
||||
|
||||
let file = await vscode.window.showOpenDialog({ filters: filter });
|
||||
|
||||
if (file) {
|
||||
await this.dbProjectTreeViewProvider.openProject(file);
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
vscode.window.showErrorMessage(getErrorMessage(err));
|
||||
}
|
||||
}
|
||||
|
||||
public dispose(): void {
|
||||
|
||||
Reference in New Issue
Block a user