Skip to content

Commit

Permalink
Provide extension methods for path operations
Browse files Browse the repository at this point in the history
  • Loading branch information
sibille committed Dec 21, 2018
1 parent 60cbd76 commit 7c185ae
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 124 deletions.
1 change: 1 addition & 0 deletions code/src/Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
<Compile Include="Extensions\DateTimeExtensions.cs" />
<Compile Include="Extensions\DictionaryExtensions.cs" />
<Compile Include="Extensions\ICreationPathExtensions.cs" />
<Compile Include="Extensions\PathExtensions.cs" />
<Compile Include="Extensions\StringExtensions.cs" />
<Compile Include="Extensions\TaskExtensions.cs" />
<Compile Include="Extensions\VersionExtensions.cs" />
Expand Down
62 changes: 62 additions & 0 deletions code/src/Core/Extensions/PathExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Templates.Core.Gen;

namespace Microsoft.Templates.Core.Extensions
{
public static class PathExtensions
{
public static string GetPathRelativeToGenerationParentPath(this string filePath)
{
var generationParentDirectory = Directory.GetParent(GenContext.Current.GenerationOutputPath).FullName;
return GetRelativePath(filePath, generationParentDirectory);
}

public static string GetPathRelativeToGenerationPath(this string filePath)
{
var generationParentDirectory = GenContext.Current.GenerationOutputPath;
return GetRelativePath(filePath, generationParentDirectory);
}

public static string GetPathRelativeToDestinationParentPath(this string filePath)
{
var generationParentDirectory = Directory.GetParent(GenContext.Current.DestinationPath).FullName;
return GetRelativePath(filePath, generationParentDirectory);
}

public static string GetDestinationPath(this string filePath)
{
var parentGenerationOutputPath = Directory.GetParent(GenContext.Current.GenerationOutputPath).FullName;
var parentDestinationPath = Directory.GetParent(GenContext.Current.DestinationPath).FullName;

return filePath.Replace(parentGenerationOutputPath, parentDestinationPath);
}

public static string GetGenerationPath(this string filePath)
{
var parentGenerationOutputPath = Directory.GetParent(GenContext.Current.GenerationOutputPath).FullName;
var parentDestinationPath = Directory.GetParent(GenContext.Current.DestinationPath).FullName;

return filePath.Replace(parentDestinationPath, parentGenerationOutputPath);
}

private static string GetRelativePath(this string filePath, string folderName)
{
if (filePath.Contains(folderName))
{
return filePath.Replace(folderName + Path.DirectorySeparatorChar, string.Empty);
}

// TODO: Should this throw an exception?
return filePath;
}
}
}
6 changes: 4 additions & 2 deletions code/src/Core/Gen/NewItemGenController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Threading.Tasks;
using Microsoft.TemplateEngine.Edge.Template;
using Microsoft.Templates.Core.Diagnostics;
using Microsoft.Templates.Core.Extensions;
using Microsoft.Templates.Core.PostActions;
using Microsoft.Templates.Core.PostActions.Catalog.Merge;
using Microsoft.Templates.Core.Resources;
Expand Down Expand Up @@ -148,11 +149,12 @@ private TempGenerationResult CompareTempGenerationWithProject()

foreach (var file in files)
{
var destFilePath = file.Replace(parentGenerationOutputPath, parentDestinationPath);
var fileName = file.Replace(parentGenerationOutputPath + Path.DirectorySeparatorChar, string.Empty);
var destFilePath = file.GetDestinationPath();
var fileName = file.GetPathRelativeToGenerationParentPath();

var projectFileName = Path.GetFullPath(Path.Combine(parentDestinationPath, fileName));

// TODO: is projectFilename and destfilename the same?
if (File.Exists(projectFileName))
{
if (GenContext.Current.MergeFilesFromProject.ContainsKey(fileName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Templates.Core.Extensions;
using Microsoft.Templates.Core.Gen;

namespace Microsoft.Templates.Core.PostActions.Catalog.Merge
Expand All @@ -26,11 +27,10 @@ public GenerateMergeInfoPostAction(string relatedTemplate, string config)

internal override void ExecuteInternal()
{
var parentGenerationOutputPath = Directory.GetParent(GenContext.Current.GenerationOutputPath).FullName;
var postAction = File.ReadAllText(Config).AsUserFriendlyPostAction();
var sourceFile = Regex.Replace(Config, PostactionRegex, ".");
var mergeType = GetMergeType();
var relFilePath = sourceFile.Replace(parentGenerationOutputPath + Path.DirectorySeparatorChar, string.Empty);
var relFilePath = sourceFile.GetPathRelativeToGenerationParentPath();

if (GenContext.Current.MergeFilesFromProject.ContainsKey(relFilePath))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Templates.Core.Extensions;
using Microsoft.Templates.Core.Gen;

namespace Microsoft.Templates.Core.PostActions.Catalog.Merge
Expand Down Expand Up @@ -36,36 +37,20 @@ internal override void ExecuteInternal()

private void GetFileFromProject()
{
var parentGenerationOutputPath = Directory.GetParent(GenContext.Current.GenerationOutputPath).FullName;
var destinationParentPath = Directory.GetParent(GenContext.Current.DestinationPath).FullName;

var filePath = GetMergeFileFromDirectory(Path.GetDirectoryName(Config.Replace(parentGenerationOutputPath, destinationParentPath)));
var relFilePath = GetRelativePath(filePath, destinationParentPath + Path.DirectorySeparatorChar);
var filePath = GetMergeFileFromDirectory(Path.GetDirectoryName(Config.GetDestinationPath()));
var relFilePath = filePath.GetPathRelativeToDestinationParentPath();

if (!GenContext.Current.MergeFilesFromProject.ContainsKey(relFilePath))
{
GenContext.Current.MergeFilesFromProject.Add(relFilePath, new List<MergeInfo>());
if (File.Exists(filePath))
{
var destFile = filePath.Replace(destinationParentPath, parentGenerationOutputPath);
var destFile = filePath.GetGenerationPath();
File.Copy(filePath, destFile, true);
}
}
}

private string GetRelativePath(string filePath, string rootPath)
{
var index = filePath.IndexOf(rootPath, StringComparison.OrdinalIgnoreCase);
if (index == 0)
{
return filePath.Remove(0, rootPath.Length);
}
else
{
return filePath;
}
}

private bool CheckLocalMergeFileAvailable()
{
var filePath = GetMergeFileFromDirectory(Path.GetDirectoryName(Config));
Expand Down
4 changes: 2 additions & 2 deletions code/src/Core/PostActions/Catalog/Merge/MergeConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ namespace Microsoft.Templates.Core.PostActions.Catalog.Merge
public class MergeConfiguration
{
public const string Suffix = "postaction";
public const string NewSuffix = "failedpostaction";
public const string FailedPostactionSuffix = "failedpostaction";
public const string SearchReplaceSuffix = "searchreplace";

public const string PostactionRegex = @"(\$\S*)?(_" + Suffix + "|_g" + Suffix + @")\.";
public const string PostactionAndSearchReplaceRegex = @"(\$\S*)?(_" + Suffix + "|_" + SearchReplaceSuffix + "|_g" + Suffix + @")\.";

public const string FailedPostactionRegex = @"(\$\S*)?(_" + NewSuffix + "|_g" + NewSuffix + @")(\d)?\.";
public const string FailedPostactionRegex = @"(\$\S*)?(_" + FailedPostactionSuffix + "|_g" + FailedPostactionSuffix + @")(\d)?\.";

public const string Extension = "_" + Suffix + ".";
public const string SearchReplaceExtension = "_" + SearchReplaceSuffix + ".";
Expand Down
64 changes: 22 additions & 42 deletions code/src/Core/PostActions/Catalog/Merge/MergePostAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Templates.Core.Extensions;
using Microsoft.Templates.Core.Gen;

using Microsoft.Templates.Core.Resources;
Expand All @@ -29,12 +29,15 @@ internal override void ExecuteInternal()
{
if (Config.FailOnError )
{
throw new FileNotFoundException(string.Format(StringRes.MergeFileNotFoundExceptionMessage, Config.FilePath, RelatedTemplate));
var errorMessage = string.Format(StringRes.MergeFileNotFoundExceptionMessage, Config.FilePath, RelatedTemplate);
throw new FileNotFoundException(errorMessage);
}
else
{
AddFailedMergePostActionsFileNotFound(originalFilePath);
File.Delete(Config.FilePath);
var relativeFilePath = originalFilePath.GetPathRelativeToGenerationParentPath();
var errorMessage = string.Format(StringRes.FailedMergePostActionFileNotFound, relativeFilePath, RelatedTemplate);

HandleFailedMergePostActions(relativeFilePath, MergeFailureType.FileNotFound, MergeConfiguration.Suffix, errorMessage);
return;
}
}
Expand All @@ -52,7 +55,11 @@ internal override void ExecuteInternal()
}
else
{
AddFailedMergePostActionsAddLineNotFound(originalFilePath, errorLine);
var relativeFilePath = originalFilePath.GetPathRelativeToGenerationParentPath();
var errorMessage = string.Format(StringRes.FailedMergePostActionLineNotFound, errorLine.Trim(), relativeFilePath, RelatedTemplate);

HandleFailedMergePostActions(relativeFilePath, MergeFailureType.LineNotFound, MergeConfiguration.Suffix, errorMessage);
return;
}
}
else
Expand All @@ -64,49 +71,22 @@ internal override void ExecuteInternal()
File.Delete(Config.FilePath);
}

protected void AddFailedMergePostActions(string originalFilePath, MergeFailureType mergeFailureType, string description)
public void HandleFailedMergePostActions(string originalFileRelativePath, MergeFailureType mergeFailureType, string suffix, string errorMessage)
{
var sourceFileName = GetRelativePath(originalFilePath);
var postactionFileName = GetRelativePath(Config.FilePath);

var failedFileName = GetFailedPostActionFileName();
GenContext.Current.FailedMergePostActions.Add(new FailedMergePostActionInfo(sourceFileName, Config.FilePath, GetRelativePath(failedFileName), failedFileName, description, mergeFailureType));
File.Copy(Config.FilePath, failedFileName, true);
}

protected string GetRelativePath(string path)
{
var parentGenerationOutputPath = Directory.GetParent(GenContext.Current.GenerationOutputPath).FullName;
return path.Replace(parentGenerationOutputPath + Path.DirectorySeparatorChar, string.Empty);
}

private void AddFailedMergePostActionsFileNotFound(string originalFilePath)
{
var description = string.Format(StringRes.FailedMergePostActionFileNotFound, GetRelativePath(originalFilePath), RelatedTemplate);
AddFailedMergePostActions(originalFilePath, MergeFailureType.FileNotFound, description);
}

private void AddFailedMergePostActionsAddLineNotFound(string originalFilePath, string errorLine)
{
var description = string.Format(StringRes.FailedMergePostActionLineNotFound, errorLine.Trim(), GetRelativePath(originalFilePath), RelatedTemplate);
AddFailedMergePostActions(originalFilePath, MergeFailureType.LineNotFound, description);
}

private string GetFailedPostActionFileName()
{
var splittedFileName = Path.GetFileName(Config.FilePath).Split('.');
splittedFileName[0] = splittedFileName[0].Replace(MergeConfiguration.Suffix, MergeConfiguration.NewSuffix);
var folder = Path.GetDirectoryName(Config.FilePath);
var extension = Path.GetExtension(Config.FilePath);

var validator = new List<Validator>
{
new FileExistsValidator(Path.GetDirectoryName(Config.FilePath)),
};

splittedFileName[0] = Naming.Infer(splittedFileName[0], validator);
var newFileName = string.Join(".", splittedFileName);
return Path.Combine(folder, newFileName);
// Change filename from .postaction to .failedpostaction, .failedpostaction1,...
var splittedFileName = Path.GetFileName(Config.FilePath).Split('.');
splittedFileName[0] = Naming.Infer(splittedFileName[0].Replace(suffix, MergeConfiguration.FailedPostactionSuffix), validator);
var failedFileName = Path.Combine(Path.GetDirectoryName(Config.FilePath), string.Join(".", splittedFileName));

Fs.SafeMoveFile(Config.FilePath, failedFileName);

// Add info to context
GenContext.Current.FailedMergePostActions.Add(new FailedMergePostActionInfo(originalFileRelativePath, Config.FilePath, failedFileName.GetPathRelativeToGenerationParentPath(), failedFileName, errorMessage, mergeFailureType));
}

private string GetFilePath()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

using Microsoft.Templates.Core.Extensions;
using Microsoft.Templates.Core.Gen;
using Microsoft.Templates.Core.Resources;

Expand All @@ -32,12 +31,13 @@ public MergeResourceDictionaryPostAction(string relatedTemplate, MergeConfigurat

internal override void ExecuteInternal()
{
string originalFilePath = GetFilePath();
string originalFilePath = Regex.Replace(Config.FilePath, MergeConfiguration.PostactionRegex, ".");
if (!File.Exists(originalFilePath))
{
// If original file does not exist, rename the postaction file, add it to projectitems and app.xamls mergedictionary
File.Copy(Config.FilePath, originalFilePath);
GenContext.Current.ProjectInfo.ProjectItems.Add(originalFilePath.Replace(GenContext.Current.GenerationOutputPath, GenContext.Current.DestinationPath));
AddToMergeDictionary(originalFilePath);
GenContext.Current.ProjectInfo.ProjectItems.Add(originalFilePath.GetDestinationPath());
AddToAppMergeDictionary(originalFilePath);
}
else
{
Expand All @@ -49,7 +49,7 @@ internal override void ExecuteInternal()
var sourceNode = sourceRoot.Elements().FirstOrDefault(e => GetKey(e) == GetKey(node));
if (sourceNode == null)
{
AddNodeToSource(sourceRoot, node);
AddNode(sourceRoot, node);
}
else
{
Expand All @@ -62,8 +62,8 @@ internal override void ExecuteInternal()
}
else
{
AddFailedMergePostActions(originalFilePath, MergeFailureType.KeyAlreadyDefined, errorMessage);
File.Delete(Config.FilePath);
var relativeFilePath = originalFilePath.GetPathRelativeToGenerationParentPath();
HandleFailedMergePostActions(relativeFilePath, MergeFailureType.KeyAlreadyDefined, MergeConfiguration.Suffix, errorMessage);
return;
}
}
Expand All @@ -81,15 +81,16 @@ internal override void ExecuteInternal()
File.Delete(Config.FilePath);
}

private void AddToMergeDictionary(string originalFilePath)
private void AddToAppMergeDictionary(string originalFilePath)
{
var relPath = originalFilePath.Replace(GenContext.Current.GenerationOutputPath, string.Empty).Replace(@"\", @"/");
// Write postaction to include this file to mergedictionary
var relPath = originalFilePath.GetPathRelativeToGenerationPath().Replace(@"\", @"/");
var postactionContent = MergeDictionaryPattern.Replace("{filePath}", relPath);
var mergeDictionaryName = Path.GetFileNameWithoutExtension(originalFilePath);
File.WriteAllText(GenContext.Current.GenerationOutputPath + $"/App${mergeDictionaryName}_gpostaction.xaml", postactionContent);
}

private static void AddNodeToSource(XElement sourceRoot, XElement node)
private static void AddNode(XElement sourceRoot, XElement node)
{
if (node.PreviousNode != null && node.PreviousNode.NodeType == XmlNodeType.Comment)
{
Expand All @@ -109,10 +110,5 @@ private IEnumerable<XElement> GetNodesToMerge(XElement rootNode)
{
return rootNode.Descendants().Where(e => e.Attributes().Any(a => a.Name.LocalName == "Key"));
}

private string GetFilePath()
{
return Regex.Replace(Config.FilePath, MergeConfiguration.PostactionRegex, ".");
}
}
}
Loading

0 comments on commit 7c185ae

Please sign in to comment.