Feature/outer paths for project (#11445)

* allow relative paths in project file outside of project folder

* Adding some tests

* Adding error string to loc strings

* Fixed test

* fix error message

* PR comments and some more fixes
This commit is contained in:
Udeesha Gautam
2020-07-22 19:28:03 -07:00
committed by GitHub
parent efc8182954
commit 196b3752a9
13 changed files with 112 additions and 36 deletions

View File

@@ -9,6 +9,7 @@ import { BaseProjectTreeItem } from './baseTreeItem';
import { ProjectRootTreeItem } from './projectTreeItem';
import { Project } from '../project';
import { DatabaseProjectItemType } from '../../common/constants';
import * as utils from '../../common/utils';
/**
* Node representing a folder in a project
@@ -44,7 +45,7 @@ export class FileNode extends BaseProjectTreeItem {
public fileSystemUri: vscode.Uri;
constructor(filePath: vscode.Uri, parent: FolderNode | ProjectRootTreeItem) {
super(fsPathToProjectUri(filePath, parent.root as ProjectRootTreeItem), parent);
super(fsPathToProjectUri(filePath, parent.root as ProjectRootTreeItem, true), parent);
this.fileSystemUri = filePath;
}
@@ -87,15 +88,18 @@ export function sortFileFolderNodes(a: (FolderNode | FileNode), b: (FolderNode |
/**
* Converts a full filesystem URI to a project-relative URI that's compatible with the project tree
*/
function fsPathToProjectUri(fileSystemUri: vscode.Uri, projectNode: ProjectRootTreeItem): vscode.Uri {
function fsPathToProjectUri(fileSystemUri: vscode.Uri, projectNode: ProjectRootTreeItem, isFile?: boolean): vscode.Uri {
const projBaseDir = projectNode.project.projectFolderPath;
let localUri = '';
if (fileSystemUri.fsPath.startsWith(projBaseDir)) {
localUri = fileSystemUri.fsPath.substring(projBaseDir.length);
}
else {
throw new Error(`Project (${projBaseDir}) pointing to file outside of directory (${fileSystemUri.fsPath})`);
else if (isFile) {
// if file is outside the folder add add at top level in tree
// this is not true for folders otherwise the outside files will not be directly inside the top level
let parts = utils.getPlatformSafeFileEntryPath(fileSystemUri.fsPath).split('/');
localUri = parts[parts.length - 1];
}
return vscode.Uri.file(path.join(projectNode.uri.path, localUri));

View File

@@ -11,7 +11,7 @@ import * as fileTree from './fileFolderTreeItem';
import { Project, ProjectEntry, EntryType } from '../project';
import * as utils from '../../common/utils';
import { DatabaseReferencesTreeItem } from './databaseReferencesTreeItem';
import { DatabaseProjectItemType } from '../../common/constants';
import { DatabaseProjectItemType, RelativeOuterPath } from '../../common/constants';
/**
* TreeNode root that represents an entire project
@@ -21,11 +21,13 @@ export class ProjectRootTreeItem extends BaseProjectTreeItem {
databaseReferencesNode: DatabaseReferencesTreeItem;
fileChildren: { [childName: string]: (fileTree.FolderNode | fileTree.FileNode) } = {};
project: Project;
fileSystemUri: vscode.Uri;
constructor(project: Project) {
super(vscode.Uri.parse(path.basename(project.projectFilePath)), undefined);
this.project = project;
this.fileSystemUri = vscode.Uri.file(project.projectFilePath);
this.dataSourceNode = new DataSourcesTreeItem(this);
this.databaseReferencesNode = new DatabaseReferencesTreeItem(this);
@@ -51,6 +53,10 @@ export class ProjectRootTreeItem extends BaseProjectTreeItem {
*/
private construct() {
for (const entry of this.project.files) {
if (entry.type !== EntryType.File && entry.relativePath.startsWith(RelativeOuterPath)) {
continue;
}
const parentNode = this.getEntryParentNode(entry);
if (Object.keys(parentNode.fileChildren).includes(path.basename(entry.fsUri.path))) {
@@ -84,6 +90,10 @@ export class ProjectRootTreeItem extends BaseProjectTreeItem {
return this; // if nothing left after trimming the entry itself, must been root
}
if (relativePathParts[0] === RelativeOuterPath) {
return this;
}
let current: fileTree.FolderNode | ProjectRootTreeItem = this;
for (const part of relativePathParts) {