Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Commit

Permalink
added RegEx and placeholder support for folder mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
mkloubert committed Jun 5, 2017
1 parent ee67bff commit c7e9387
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 39 deletions.
5 changes: 5 additions & 0 deletions src/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1581,6 +1581,11 @@ export interface DeployTargetWithPlugins {
* A folder mapping.
*/
export interface DeployTargetMapping {
/**
* Indicates if 'source' contains a regular expression
* instead of a static string.
*/
isRegEx?: boolean;
/**
* The source directory.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ async function showFilesInBrowsers(me: vs_deploy.Deployer,
remoteFile = Path.join(f.right.path, f.right.name);
remoteFile = deploy_helpers.replaceAllStrings(remoteFile, Path.sep, '/');

let relRemotePath = deploy_helpers.toRelativeTargetPath(remoteFile, target);
let relRemotePath = deploy_helpers.toRelativeTargetPathWithValues(remoteFile, target, me.getValues());
if (false !== relRemotePath) {
remoteFile = relRemotePath;
}
Expand Down
68 changes: 60 additions & 8 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ let nextHtmlDocId = -1;
* Applies values to an object.
*
* @param {T} obj The object to apply the values to.
* @param {deploy_values.ValueBase|deploy_values.ValueBase[]} values The values to apply.
* @param {deploy_contracts.ObjectWithNameAndValue|deploy_contracts.ObjectWithNameAndValue[]} values The values to apply.
* @param {boolean} [cloneObj] Clone object or not.
*
* @return {T} The object with the applied values.
*/
export function applyValues<T extends deploy_contracts.Applyable>(obj: T,
values: deploy_values.ValueBase | deploy_values.ValueBase[],
values: deploy_contracts.ObjectWithNameAndValue | deploy_contracts.ObjectWithNameAndValue[],
cloneObj = true): T {
values = asArray(values).filter(v => v);

Expand Down Expand Up @@ -2355,9 +2355,26 @@ export function toRelativePath(path: string, baseDir?: string): string | false {
* @param {deploy_contracts.DeployTarget} target The target.
* @param {string} [baseDir] The custom base / root directory to use.
*
* @return {string | false} The relative path or (false) if not possible.
* @return {string|false} The relative path or (false) if not possible.
*/
export function toRelativeTargetPath(path: string, target: deploy_contracts.DeployTarget, baseDir?: string): string | false {
return toRelativeTargetPathWithValues(path, target, [], baseDir);
}

/**
* Tries to convert a file path to a relative path
* by using the mappings of a target and placeholders / values.
*
* @param {string} path The path to convert.
* @param {deploy_contracts.DeployTarget} target The target.
* @param {deploy_contracts.ObjectWithNameAndValue|deploy_contracts.ObjectWithNameAndValue[]} values The values to use.
* @param {string} [baseDir] The custom base / root directory to use.
*
* @return {string|false} The relative path or (false) if not possible.
*/
export function toRelativeTargetPathWithValues(path: string, target: deploy_contracts.DeployTarget,
values: deploy_contracts.ObjectWithNameAndValue | deploy_contracts.ObjectWithNameAndValue[],
baseDir?: string): string | false {
let relativePath = toRelativePath(path, baseDir);
if (false === relativePath) {
return relativePath;
Expand All @@ -2381,14 +2398,49 @@ export function toRelativeTargetPath(path: string, target: deploy_contracts.Depl
for (let i = 0; i < allMappings.length; i++) {
let mapping = allMappings[i];

let source = normalizeDirPath(mapping.source);
let target = normalizeDirPath(mapping.target);
let sourceDir: string;
let targetDir = toStringSafe(mapping.target);

let doesMatch = false;
if (toBooleanSafe(mapping.isRegEx)) {
let r = new RegExp(toStringSafe(mapping.source), 'g');

let match = r.exec(relativePath);
if (match) {
sourceDir = match[0];

// RegEx matches
let matchValues: deploy_values.ValueBase[] = [];
for (let i = 0; i < match.length; i++) {
matchValues.push(new deploy_values.StaticValue({
name: '' + i,
value: match[i],
}));
}

sourceDir = deploy_values.replaceWithValues(values, sourceDir);
sourceDir = normalizeDirPath(sourceDir);

// apply RegEx matches to targetDir
targetDir = deploy_values.replaceWithValues(matchValues, targetDir);

doesMatch = true;
}
}
else {
sourceDir = deploy_values.replaceWithValues(values, mapping.source);
sourceDir = normalizeDirPath(sourceDir);

doesMatch = 0 === relativePath.indexOf(sourceDir);
}

targetDir = normalizeDirPath(targetDir);

if (0 === relativePath.indexOf(source)) {
if (doesMatch) {
// is matching => rebuild path

relativePath = Path.join(target,
relativePath.substr(source.length)); // remove the source prefix
relativePath = Path.join(targetDir,
relativePath.substr(sourceDir.length)); // remove the source prefix
break;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1668,7 +1668,7 @@ export abstract class ZipFileDeployPluginBase extends DeployPluginWithContextBas
completed(); // cancellation requested
}
else {
let relativePath = deploy_helpers.toRelativeTargetPath(file, target, opts.baseDirectory);
let relativePath = deploy_helpers.toRelativeTargetPathWithValues(file, target, me.context.values(), opts.baseDirectory);
if (false === relativePath) {
relativePath = file;
}
Expand Down Expand Up @@ -1736,7 +1736,7 @@ export abstract class ZipFileDeployPluginBase extends DeployPluginWithContextBas
let err: any;
try {
if (!hasCancelled) {
let relativePath = deploy_helpers.toRelativeTargetPath(file, target, opts.baseDirectory);
let relativePath = deploy_helpers.toRelativeTargetPathWithValues(file, target, me.context.values(), opts.baseDirectory);
if (false === relativePath) {
relativePath = file;
}
Expand Down Expand Up @@ -1889,7 +1889,7 @@ export abstract class ZipFileDeployPluginBase extends DeployPluginWithContextBas
let err: any;
try {
if (!hasCancelled) {
let relativePath = deploy_helpers.toRelativeTargetPath(file, target, opts.baseDirectory);
let relativePath = deploy_helpers.toRelativeTargetPathWithValues(file, target, me.context.values(), opts.baseDirectory);
if (false === relativePath) {
relativePath = file;
}
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class ApiPlugin extends deploy_objects.DeployPluginBase {
}

try {
let relativePath = deploy_helpers.toRelativeTargetPath(file, target, opts.baseDirectory);
let relativePath = deploy_helpers.toRelativeTargetPathWithValues(file, target, me.context.values(), opts.baseDirectory);
if (false === relativePath) {
completed(new Error(i18.t('relativePaths.couldNotResolve', file)));
return;
Expand Down Expand Up @@ -322,7 +322,7 @@ class ApiPlugin extends deploy_objects.DeployPluginBase {

let startRequest = () => {
try {
let relativePath = deploy_helpers.toRelativeTargetPath(file, target, opts.baseDirectory);
let relativePath = deploy_helpers.toRelativeTargetPathWithValues(file, target, me.context.values(), opts.baseDirectory);
if (false === relativePath) {
completed(new Error(i18.t('relativePaths.couldNotResolve', file)));
return;
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/azureblob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class AzureBlobPlugin extends deploy_objects.DeployPluginWithContextBase<AzureBl
}
else {
try {
let relativePath = deploy_helpers.toRelativeTargetPath(file, target, opts.baseDirectory);
let relativePath = deploy_helpers.toRelativeTargetPathWithValues(file, target, me.context.values(), opts.baseDirectory);
if (false === relativePath) {
completed(new Error(i18.t('relativePaths.couldNotResolve', file)));
return;
Expand Down Expand Up @@ -318,7 +318,7 @@ class AzureBlobPlugin extends deploy_objects.DeployPluginWithContextBase<AzureBl
}
else {
try {
let relativePath = deploy_helpers.toRelativeTargetPath(file, target, opts.baseDirectory);
let relativePath = deploy_helpers.toRelativeTargetPathWithValues(file, target, me.context.values(), opts.baseDirectory);
if (false === relativePath) {
completed(new Error(i18.t('relativePaths.couldNotResolve', file)));
return;
Expand Down Expand Up @@ -424,7 +424,7 @@ class AzureBlobPlugin extends deploy_objects.DeployPluginWithContextBase<AzureBl
}
else {
try {
let relativePath = deploy_helpers.toRelativeTargetPath(file, target, opts.baseDirectory);
let relativePath = deploy_helpers.toRelativeTargetPathWithValues(file, target, me.context.values(), opts.baseDirectory);
if (false === relativePath) {
completed(new Error(i18.t('relativePaths.couldNotResolve', file)));
return;
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/dropbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ class DropboxPlugin extends deploy_objects.DeployPluginWithContextBase<DropboxCo
completed(); // cancellation requested
}
else {
let relativeFilePath = deploy_helpers.toRelativeTargetPath(file, target, opts.baseDirectory);
let relativeFilePath = deploy_helpers.toRelativeTargetPathWithValues(file, target, me.context.values(), opts.baseDirectory);
if (false === relativeFilePath) {
completed(new Error(i18.t('relativePaths.couldNotResolve', file)));
return;
Expand Down Expand Up @@ -606,7 +606,7 @@ class DropboxPlugin extends deploy_objects.DeployPluginWithContextBase<DropboxCo
completed(null); // cancellation requested
}
else {
let relativeFilePath = deploy_helpers.toRelativeTargetPath(file, target, opts.baseDirectory);
let relativeFilePath = deploy_helpers.toRelativeTargetPathWithValues(file, target, me.context.values(), opts.baseDirectory);
if (false === relativeFilePath) {
completed(new Error(i18.t('relativePaths.couldNotResolve', file)));
return;
Expand Down Expand Up @@ -743,7 +743,7 @@ class DropboxPlugin extends deploy_objects.DeployPluginWithContextBase<DropboxCo
completed(null); // cancellation requested
}
else {
let relativeFilePath = deploy_helpers.toRelativeTargetPath(file, target, opts.baseDirectory);
let relativeFilePath = deploy_helpers.toRelativeTargetPathWithValues(file, target, me.context.values(), opts.baseDirectory);
if (false === relativeFilePath) {
completed(new Error(i18.t('relativePaths.couldNotResolve', file)));
return;
Expand Down
8 changes: 5 additions & 3 deletions src/plugins/ftp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,7 @@ class FtpPlugin extends deploy_objects.DeployPluginWithContextBase<FTPContext> {
completed(); // cancellation requested
}
else {
let relativeFilePath = deploy_helpers.toRelativeTargetPath(file, target, opts.baseDirectory);
let relativeFilePath = deploy_helpers.toRelativeTargetPathWithValues(file, target, me.context.values(), opts.baseDirectory);
if (false === relativeFilePath) {
completed(new Error(i18.t('relativePaths.couldNotResolve', file)));
return;
Expand Down Expand Up @@ -1214,7 +1214,7 @@ class FtpPlugin extends deploy_objects.DeployPluginWithContextBase<FTPContext> {
completed(null); // cancellation requested
}
else {
let relativeFilePath = deploy_helpers.toRelativeTargetPath(file, target, opts.baseDirectory);
let relativeFilePath = deploy_helpers.toRelativeTargetPathWithValues(file, target, me.context.values(), opts.baseDirectory);
if (false === relativeFilePath) {
completed(new Error(i18.t('relativePaths.couldNotResolve', file)));
return;
Expand Down Expand Up @@ -1263,11 +1263,13 @@ class FtpPlugin extends deploy_objects.DeployPluginWithContextBase<FTPContext> {

protected getFileInfoWithContext(ctx: FTPContext,
file: string, target: DeployTargetFTP, opts?: deploy_contracts.DeployFileOptions): Promise<deploy_contracts.FileInfo> {
let me = this;

return new Promise<deploy_contracts.FileInfo>((resolve, reject) => {
let completed = deploy_helpers.createSimplePromiseCompletedAction<deploy_contracts.FileInfo>(resolve, reject);

try {
let relativeFilePath = deploy_helpers.toRelativeTargetPath(file, target, opts.baseDirectory);
let relativeFilePath = deploy_helpers.toRelativeTargetPathWithValues(file, target, me.context.values(), opts.baseDirectory);
if (false === relativeFilePath) {
completed(new Error(i18.t('relativePaths.couldNotResolve', file)));
return;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class HttpPlugin extends deploy_objects.DeployPluginBase {
completed(); // cancellation requested
}
else {
let relativePath = deploy_helpers.toRelativeTargetPath(file, target, opts.baseDirectory);
let relativePath = deploy_helpers.toRelativeTargetPathWithValues(file, target, me.context.values(), opts.baseDirectory);
if (false === relativePath) {
completed(new Error(i18.t('relativePaths.couldNotResolve', file)));
return;
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class LocalPlugin extends deploy_objects.DeployPluginBase {
completed(); // cancellation requested
}
else {
let relativeTargetFilePath = deploy_helpers.toRelativeTargetPath(file, target, opts.baseDirectory);
let relativeTargetFilePath = deploy_helpers.toRelativeTargetPathWithValues(file, target, me.context.values(), opts.baseDirectory);
if (false === relativeTargetFilePath) {
completed(new Error(i18.t('relativePaths.couldNotResolve', file)));
return;
Expand Down Expand Up @@ -239,7 +239,7 @@ class LocalPlugin extends deploy_objects.DeployPluginBase {
completed(null); // cancellation requested
}
else {
let relativeTargetFilePath = deploy_helpers.toRelativeTargetPath(file, target, opts.baseDirectory);
let relativeTargetFilePath = deploy_helpers.toRelativeTargetPathWithValues(file, target, me.context.values(), opts.baseDirectory);
if (false === relativeTargetFilePath) {
completed(new Error(i18.t('relativePaths.couldNotResolve', file)));
return;
Expand Down Expand Up @@ -296,7 +296,7 @@ class LocalPlugin extends deploy_objects.DeployPluginBase {
opts = {};
}

let relativeTargetFilePath = deploy_helpers.toRelativeTargetPath(file, target, opts.baseDirectory);
let relativeTargetFilePath = deploy_helpers.toRelativeTargetPathWithValues(file, target, me.context.values(), opts.baseDirectory);
if (false === relativeTargetFilePath) {
throw new Error(i18.t('relativePaths.couldNotResolve', file));
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class RemotePlugin extends deploy_objects.DeployPluginWithContextBase<RemoteCont
}

try {
let relativePath = deploy_helpers.toRelativeTargetPath(file, target, opts.baseDirectory);
let relativePath = deploy_helpers.toRelativeTargetPathWithValues(file, target, me.context.values(), opts.baseDirectory);
if (false === relativePath) {
completed(new Error(i18.t('relativePaths.couldNotResolve', file)));
return;
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/s3bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class S3BucketPlugin extends deploy_objects.DeployPluginWithContextBase<S3Contex
}
else {
try {
let relativePath = deploy_helpers.toRelativeTargetPath(file, target, opts.baseDirectory);
let relativePath = deploy_helpers.toRelativeTargetPathWithValues(file, target, me.context.values(), opts.baseDirectory);
if (false === relativePath) {
completed(new Error(i18.t('relativePaths.couldNotResolve', file)));
return;
Expand Down Expand Up @@ -292,7 +292,7 @@ class S3BucketPlugin extends deploy_objects.DeployPluginWithContextBase<S3Contex
}
else {
try {
let relativePath = deploy_helpers.toRelativeTargetPath(file, target, opts.baseDirectory);
let relativePath = deploy_helpers.toRelativeTargetPathWithValues(file, target, me.context.values(), opts.baseDirectory);
if (false === relativePath) {
completed(new Error(i18.t('relativePaths.couldNotResolve', file)));
return;
Expand Down Expand Up @@ -369,7 +369,7 @@ class S3BucketPlugin extends deploy_objects.DeployPluginWithContextBase<S3Contex
};

try {
let relativePath = deploy_helpers.toRelativeTargetPath(file, target, opts.baseDirectory);
let relativePath = deploy_helpers.toRelativeTargetPathWithValues(file, target, me.context.values(), opts.baseDirectory);
if (false === relativePath) {
completed(new Error(i18.t('relativePaths.couldNotResolve', file)));
return;
Expand Down
Loading

0 comments on commit c7e9387

Please sign in to comment.