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

@@ -7,6 +7,7 @@ import * as vscode from 'vscode';
import * as os from 'os';
import * as constants from './constants';
import { promises as fs } from 'fs';
import * as path from 'path';
/**
* Consolidates on the error message string
@@ -19,7 +20,8 @@ export function getErrorMessage(error: any): string {
/**
* removes any leading portion shared between the two URIs from outerUri.
* e.g. [@param innerUri: 'this\is'; @param outerUri: '\this\is\my\path'] => 'my\path'
* e.g. [@param innerUri: 'this\is'; @param outerUri: '\this\is\my\path'] => 'my\path' OR
* e.g. [@param innerUri: 'this\was'; @param outerUri: '\this\is\my\path'] => '..\my\path'
* @param innerUri the URI that will be cut away from the outer URI
* @param outerUri the URI that will have any shared beginning portion removed
*/
@@ -27,11 +29,22 @@ export function trimUri(innerUri: vscode.Uri, outerUri: vscode.Uri): string {
let innerParts = innerUri.path.split('/');
let outerParts = outerUri.path.split('/');
if (path.isAbsolute(outerUri.path)
&& innerParts.length > 0 && outerParts.length > 0
&& innerParts[0].toLowerCase() !== outerParts[0].toLowerCase()) {
throw new Error(constants.ousiderFolderPath);
}
while (innerParts.length > 0 && outerParts.length > 0 && innerParts[0].toLocaleLowerCase() === outerParts[0].toLocaleLowerCase()) {
innerParts = innerParts.slice(1);
outerParts = outerParts.slice(1);
}
while (innerParts.length > 1) {
outerParts.unshift(constants.RelativeOuterPath);
innerParts = innerParts.slice(1);
}
return outerParts.join('/');
}
@@ -68,24 +81,24 @@ export async function exists(path: string): Promise<boolean> {
* get quoted path to be used in any commandline argument
* @param filePath
*/
export function getSafePath(filePath: string): string {
export function getQuotedPath(filePath: string): string {
return (os.platform() === 'win32') ?
getSafeWindowsPath(filePath) :
getSafeNonWindowsPath(filePath);
getQuotedWindowsPath(filePath) :
getQuotedNonWindowsPath(filePath);
}
/**
* ensure that path with spaces are handles correctly
* ensure that path with spaces are handles correctly (return quoted path)
*/
export function getSafeWindowsPath(filePath: string): string {
function getQuotedWindowsPath(filePath: string): string {
filePath = filePath.split('\\').join('\\\\').split('"').join('');
return '"' + filePath + '"';
}
/**
* ensure that path with spaces are handles correctly
* ensure that path with spaces are handles correctly (return quoted path)
*/
export function getSafeNonWindowsPath(filePath: string): string {
function getQuotedNonWindowsPath(filePath: string): string {
filePath = filePath.split('\\').join('/').split('"').join('');
return '"' + filePath + '"';
}