.sqlproj and datasources.json file parsing (#8921)

* Checkpoint

* 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

* PR feedback

* added activation event for when workspace contains sqlproj files

* Returning after displaying error

* Fixing linter errors

* Reworked tree

* Fixing missing grandchildren

* Correcting tree URI construction

* Refactoring to isolate tree item responsibilities from data model responsibilities

* project file parsing

* constructing tree from project files rather than filesystem

* Fixing double-initialization

* Changing projectEntry to take enum for file type

* Correct node type for project item

* Parsing datasources.json

* Child nodes for sql data source

* Localizing strings

* Checkpoint

* 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

* PR feedback

* added activation event for when workspace contains sqlproj files

* Returning after displaying error

* Fixing linter errors

* Reworked tree

* Fixing missing grandchildren

* Correcting tree URI construction

* Refactoring to isolate tree item responsibilities from data model responsibilities

* project file parsing

* constructing tree from project files rather than filesystem

* Fixing double-initialization

* Changing projectEntry to take enum for file type

* Correct node type for project item

* Parsing datasources.json

* Child nodes for sql data source

* Localizing strings

* missed file in merge

* changed extension method to helper

* cleanup

* Adding docstrings
This commit is contained in:
Benjin Dubishar
2020-02-24 12:11:41 -08:00
committed by GitHub
parent 933cfb21ef
commit 1a639f83c4
18 changed files with 718 additions and 152 deletions

View File

@@ -0,0 +1,71 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { promises as fs } from 'fs';
import * as constants from '../../common/constants';
import { SqlConnectionDataSource } from './sqlConnectionStringSource';
/**
* Abstract class for a datasource in a project
*/
export abstract class DataSource {
public name: string;
public abstract get type(): string;
public abstract get friendlyName(): string;
constructor(name: string) {
this.name = name;
}
}
/**
* parses the specified file to load DataSource objects
*/
export async function load(dataSourcesFilePath: string): Promise<DataSource[]> {
let fileContents;
try {
fileContents = await fs.readFile(dataSourcesFilePath);
}
catch (err) {
throw new Error(constants.noDataSourcesFile);
}
const rawJsonContents = JSON.parse(fileContents.toString());
if (rawJsonContents.version === undefined) {
throw new Error(constants.missingVersion);
}
const output: DataSource[] = [];
// TODO: do we have a construct for parsing version numbers?
switch (rawJsonContents.version) {
case '0.0.0':
const dataSources: DataSourceFileJson = rawJsonContents as DataSourceFileJson;
for (const source of dataSources.datasources) {
output.push(createDataSource(source));
}
break;
default:
throw new Error(constants.unrecognizedDataSourcesVersion + rawJsonContents.version);
}
return output;
}
/**
* Creates DataSource object from JSON
*/
function createDataSource(json: DataSourceJson): DataSource {
switch (json.type) {
case SqlConnectionDataSource.type:
return SqlConnectionDataSource.fromJson(json);
default:
throw new Error(constants.unknownDataSourceType + json.type);
}
}