mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-19 17:22:48 -05:00
Import project from database (#10326)
* Initial changes for Import database as new project * Functionally complete code * Initial changes for Import database as new project * Functionally complete code * Resolved conflicts with latest changes. Also did some code refactoring. * Addressed comments. Added unit tests. * Addressed comments * Moved ExtractTarget enum from azdata to mssql * Addressed comments * Fixed indentation in project templates
This commit is contained in:
18
extensions/sql-database-projects/src/models/api/import.ts
Normal file
18
extensions/sql-database-projects/src/models/api/import.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { ExtractTarget } from '../../../../mssql';
|
||||
|
||||
/**
|
||||
* Data model to communicate for Import API
|
||||
*/
|
||||
export interface ImportDataModel {
|
||||
serverId: string;
|
||||
database: string;
|
||||
projName: string;
|
||||
filePath: string;
|
||||
version: string;
|
||||
extractTarget: ExtractTarget;
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
import * as path from 'path';
|
||||
import * as xmldom from 'xmldom';
|
||||
import * as constants from '../common/constants';
|
||||
import * as utils from '../common/utils';
|
||||
|
||||
import { Uri } from 'vscode';
|
||||
import { promises as fs } from 'fs';
|
||||
@@ -60,7 +61,12 @@ export class Project {
|
||||
*/
|
||||
public async addFolderItem(relativeFolderPath: string): Promise<ProjectEntry> {
|
||||
const absoluteFolderPath = path.join(this.projectFolderPath, relativeFolderPath);
|
||||
await fs.mkdir(absoluteFolderPath, { recursive: true });
|
||||
|
||||
//If folder doesn't exist, create it
|
||||
let exists = await utils.exists(absoluteFolderPath);
|
||||
if (!exists) {
|
||||
await fs.mkdir(absoluteFolderPath, { recursive: true });
|
||||
}
|
||||
|
||||
const folderEntry = this.createProjectEntry(relativeFolderPath, EntryType.Folder);
|
||||
this.files.push(folderEntry);
|
||||
@@ -70,14 +76,23 @@ export class Project {
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a file to disk, adds that file to the project, and writes it to disk
|
||||
* Writes a file to disk if contents are provided, adds that file to the project, and writes it to disk
|
||||
* @param relativeFilePath Relative path of the file
|
||||
* @param contents Contents to be written to the new file
|
||||
*/
|
||||
public async addScriptItem(relativeFilePath: string, contents: string): Promise<ProjectEntry> {
|
||||
public async addScriptItem(relativeFilePath: string, contents?: string): Promise<ProjectEntry> {
|
||||
const absoluteFilePath = path.join(this.projectFolderPath, relativeFilePath);
|
||||
await fs.mkdir(path.dirname(absoluteFilePath), { recursive: true });
|
||||
await fs.writeFile(absoluteFilePath, contents);
|
||||
|
||||
if (contents) {
|
||||
await fs.mkdir(path.dirname(absoluteFilePath), { recursive: true });
|
||||
await fs.writeFile(absoluteFilePath, contents);
|
||||
}
|
||||
|
||||
//Check that file actually exists
|
||||
let exists = await utils.exists(absoluteFilePath);
|
||||
if (!exists) {
|
||||
throw new Error(constants.noFileExist(absoluteFilePath));
|
||||
}
|
||||
|
||||
const fileEntry = this.createProjectEntry(relativeFilePath, EntryType.File);
|
||||
this.files.push(fileEntry);
|
||||
@@ -145,6 +160,29 @@ export class Project {
|
||||
|
||||
await fs.writeFile(this.projectFilePath, xml);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the list of sql files and directories to the project, and saves the project file
|
||||
* @param absolutePath Absolute path of the folder
|
||||
*/
|
||||
public async addToProject(list: string[]): Promise<void> {
|
||||
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
let file: string = list[i];
|
||||
const relativePath = utils.trimChars(utils.trimUri(Uri.file(this.projectFilePath), Uri.file(file)), '/');
|
||||
|
||||
if (relativePath.length > 0) {
|
||||
let fileStat = await fs.stat(file);
|
||||
|
||||
if (fileStat.isFile() && file.toLowerCase().endsWith(constants.sqlFileExtension)) {
|
||||
await this.addScriptItem(relativePath);
|
||||
}
|
||||
else if (fileStat.isDirectory()) {
|
||||
await this.addFolderItem(relativePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user