Publish content files

This commit is contained in:
Victor Hurdugaci 2016-01-08 16:20:39 -08:00
parent 6803bfd1ac
commit fafa49845e
2 changed files with 52 additions and 7 deletions

View file

@ -12,11 +12,12 @@ namespace Microsoft.DotNet.ProjectModel.Files
public class ProjectFilesCollection
{
public static readonly string[] DefaultCompileBuiltInPatterns = new[] { @"**/*.cs" };
public static readonly string[] DefaultPublishExcludePatterns = new[] { @"obj/**/*.*", @"bin/**/*.*", @"**/.*/**", @"**/global.json", @"**/project.json", @"**/project.lock.json" };
public static readonly string[] DefaultPreprocessPatterns = new[] { @"compiler/preprocess/**/*.cs" };
public static readonly string[] DefaultSharedPatterns = new[] { @"compiler/shared/**/*.cs" };
public static readonly string[] DefaultResourcesBuiltInPatterns = new[] { @"compiler/resources/**/*", "**/*.resx" };
public static readonly string[] DefaultContentsBuiltInPatterns = new[] { @"**/*" };
public static readonly string[] DefaultPublishExcludePatterns = new string[0];
public static readonly string[] DefaultContentsBuiltInPatterns = new string[0];
public static readonly string[] DefaultBuiltInExcludePatterns = new[] { "bin/**", "obj/**", "**/*.xproj", "packages/**" };
@ -73,11 +74,7 @@ namespace Microsoft.DotNet.ProjectModel.Files
.ExcludeGroup(_preprocessPatternsGroup)
.ExcludeGroup(_resourcePatternsGroup);
_contentPatternsGroup = PatternGroup.Build(_rawProject, _projectDirectory, _projectFilePath, "content", additionalIncluding: contentBuiltIns, additionalExcluding: excludePatterns.Concat(_publishExcludePatterns))
.ExcludeGroup(_compilePatternsGroup)
.ExcludeGroup(_preprocessPatternsGroup)
.ExcludeGroup(_sharedPatternsGroup)
.ExcludeGroup(_resourcePatternsGroup);
_contentPatternsGroup = PatternGroup.Build(_rawProject, _projectDirectory, _projectFilePath, "content", additionalIncluding: contentBuiltIns, additionalExcluding: _publishExcludePatterns);
_namedResources = NamedResourceReader.ReadNamedResources(_rawProject, _projectFilePath);

View file

@ -10,6 +10,7 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.DotNet.Tools.Common;
namespace Microsoft.DotNet.Tools.Publish
{
@ -128,6 +129,8 @@ namespace Microsoft.DotNet.Tools.Publish
PublishFiles(export.NativeLibraries, outputPath, nativeSubdirectories);
}
CopyContents(context, outputPath);
// Publish a host if this is an application
if (options.EmitEntryPoint.GetValueOrDefault())
{
@ -250,5 +253,50 @@ namespace Microsoft.DotNet.Tools.Publish
}
}
}
private static void CopyContents(ProjectContext context, string outputPath)
{
var contentFiles = context.ProjectFile.Files.GetContentFiles();
Copy(contentFiles, context.ProjectDirectory, outputPath);
}
private static void Copy(IEnumerable<string> contentFiles, string sourceDirectory, string targetDirectory)
{
if (contentFiles == null)
{
throw new ArgumentNullException(nameof(contentFiles));
}
sourceDirectory = PathUtility.EnsureTrailingSlash(sourceDirectory);
targetDirectory = PathUtility.EnsureTrailingSlash(targetDirectory);
foreach (var contentFilePath in contentFiles)
{
Reporter.Verbose.WriteLine($"Publishing {contentFilePath.Green().Bold()} ...");
var fileName = Path.GetFileName(contentFilePath);
var targetFilePath = contentFilePath.Replace(sourceDirectory, targetDirectory);
var targetFileParentFolder = Path.GetDirectoryName(targetFilePath);
// Create directory before copying a file
if (!Directory.Exists(targetFileParentFolder))
{
Directory.CreateDirectory(targetFileParentFolder);
}
File.Copy(
contentFilePath,
targetFilePath,
overwrite: true);
// clear read-only bit if set
var fileAttributes = File.GetAttributes(targetFilePath);
if ((fileAttributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
File.SetAttributes(targetFilePath, fileAttributes & ~FileAttributes.ReadOnly);
}
}
}
}
}