Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Output/
*.suo
*.user
build.bat
/.vs/MSBuild.NugetContentRestore/v15/sqlite3/storage.ide
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,99 @@
using System.IO;
using System.Linq;

using Microsoft.Build.Evaluation;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using MSBuild.NugetContentRestore.Tasks.Utilities;

namespace MSBuild.NugetContentRestore.Tasks.Extensions
{
public static class DirectoryExtensions
{
private static string CurrentProjectPath { get; set; }
private static Project CurrentProject { get; set; }

public static void CopyTo(this DirectoryInfo source, string destination, bool recursive, Wildcard[] ignorePatterns, bool smartRestore)
public static void CopyTo(this DirectoryInfo source, string destination, bool recursive, Wildcard[] ignorePatterns, bool smartRestore, string projectPath, TaskLoggingHelper log)
{
if (source == null) throw new ArgumentNullException("source");
if (destination == null) throw new ArgumentNullException("destination");
if (!source.Exists) throw new DirectoryNotFoundException("Source directory not found: " + source.FullName);

var target = new DirectoryInfo(destination);
string targetFile;
FileInfo targetFileInfo;

if (!target.Exists) target.Create();


foreach (var file in source.GetFiles())
{
if (ignorePatterns.Any(p => p.IsMatch(file.Name))) continue;

targetFile = Path.Combine(target.FullName, file.Name);
var targetFile = Path.Combine(target.FullName, file.Name);

if (smartRestore)
{
// perform additional checks to see if we really need to copy the file
targetFileInfo = new FileInfo(targetFile);
var targetFileInfo = new FileInfo(targetFile);

// if target exists and source/target have same write time, skip copying this file (don't worry about file contents - turn off SmartRestore if wanting that)
if (targetFileInfo.Exists && file.LastWriteTime.Equals(targetFileInfo.LastWriteTime)) continue;
}


file.CopyTo(targetFile, true);
if (string.Equals(file.Extension, ".pp", StringComparison.InvariantCultureIgnoreCase))
{
targetFile = targetFile.Substring(0, targetFile.Length - ".pp".Length);
log.LogMessage(MessageImportance.Low, "NugetContentRestore :: Processing Source Code Transformation for file '{0}' in project '{1}'", targetFile, projectPath);
ProccessSourceCodeTransformation(projectPath, file.FullName, targetFile, log);
}
else {
file.CopyTo(targetFile, true);
}
}

// Exit Condition
if (!recursive) return;

foreach (var directory in source.GetDirectories())
CopyTo(directory, Path.Combine(target.FullName, directory.Name), recursive, ignorePatterns, smartRestore);
CopyTo(directory, Path.Combine(target.FullName, directory.Name), true, ignorePatterns, smartRestore, projectPath, log);
}

private static void ProccessSourceCodeTransformation(string projectPath, string sourcePath, string targetPath, TaskLoggingHelper log)
{
if (!string.Equals(CurrentProjectPath, projectPath, StringComparison.InvariantCultureIgnoreCase))
{
CurrentProjectPath = projectPath;
CurrentProject = new Project(CurrentProjectPath);
}

var source = File.ReadAllLines(sourcePath);
ReplaceProjectPropertyTokens(source, log);
File.WriteAllLines(targetPath, source);
}

private static void ReplaceProjectPropertyTokens(string[] source, TaskLoggingHelper log)
{
for (var i = 0; i < source.Length; i++)
{
var splitLine = source[i].Split('$');
if (splitLine.Length <= 2)
{
continue;
}

for (var j = 1; j < splitLine.Length; j += 2)
{
var token = splitLine[j];
if (token.Contains(" "))
{
// Not a valid token...
j--;
continue;
}
var value = CurrentProject.GetPropertyValue(token);
log.LogMessage(MessageImportance.Low, "NugetContentRestore :: Replacing token '{0}' with '{1}'", token, value);
splitLine[j] = value;
}
source[i] = string.Join(string.Empty, splitLine);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Build" />
<Reference Include="Microsoft.Build.Framework" />
<Reference Include="Microsoft.Build.Utilities.v4.0" />
<Reference Include="System" />
Expand Down
21 changes: 5 additions & 16 deletions MSBuild.NugetContentRestore.Tasks/NugetContentRestoreTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class NugetContentRestoreTask : Task

#region Private Members

private readonly string[] _folders = new[] { "Scripts", "Images", "fonts", "content" };
private readonly string[] _ignoreFilePatterns = new[] { "*.transform", "*.install.xdt", "*.pp" };
private readonly string[] _folders = new[] { "App_Packages", "Scripts", "Images", "fonts", "content" };
private readonly string[] _ignoreFilePatterns = new[] { "*.transform", "*.install.xdt" };

private string _configFileFullPath;
#endregion
Expand Down Expand Up @@ -91,26 +91,15 @@ public override bool Execute()
var ignoreFilePatterns = from p in ignoreFilePatternsArray
select new Wildcard(p, RegexOptions.IgnoreCase);

// Restore Package Content for predefined folders (_folders)
// Restore Package Content for predefined folders (Folders) and additional folders (AdditionalFolder)
var filePatterns = ignoreFilePatterns as Wildcard[] ?? ignoreFilePatterns.ToArray();
foreach (var folder in _folders)
foreach (var folder in Folders.Union(AdditionalFolders ?? new string[0] ))
{
var sourceFolderInfo = new DirectoryInfo(Path.Combine(packageContentsFullPath, folder));
if (!sourceFolderInfo.Exists) continue;

Log.LogMessage(MessageImportance.High, "NugetContentRestore :: {0} :: {1} :: Restoring content files", package.FolderName, folder);
sourceFolderInfo.CopyTo(Path.Combine(ProjectDir, folder), true, filePatterns.ToArray(), EnableSmartRestore);
}

// Restore Package Content for additional folders (AdditionalFolder)
if (AdditionalFolders == null) continue;
foreach (var folder in AdditionalFolders)
{
var sourceFolderInfo = new DirectoryInfo(Path.Combine(packageContentsFullPath, folder));
if (!sourceFolderInfo.Exists) continue;

Log.LogMessage(MessageImportance.High, "NugetContentRestore :: {0} :: {1} :: Restoring content files", package.FolderName, folder);
sourceFolderInfo.CopyTo(Path.Combine(ProjectDir, folder), true, filePatterns.ToArray(), EnableSmartRestore);
sourceFolderInfo.CopyTo(Path.Combine(ProjectDir, folder), true, filePatterns.ToArray(), EnableSmartRestore, BuildEngine3.ProjectFileOfTaskNode, Log);
}
}

Expand Down