Adding content copying to publish #51

This commit is contained in:
John Luo 2015-10-16 17:41:32 -07:00
parent 1056577f02
commit 0606e26221
2 changed files with 73 additions and 8 deletions

View file

@ -127,6 +127,8 @@ namespace Microsoft.DotNet.Tools.Publish
{
exitCode = PublishForUnix(context, outputPath);
}
CopyContents(context, outputPath);
Reporter.Output.WriteLine($"Published to {outputPath}".Green().Bold());
return exitCode;
@ -220,9 +222,74 @@ exec ""$DIR/corerun"" ""$DIR/{context.ProjectFile.Name}.exe"" $*";
var outputExe = Path.Combine(outputPath, context.ProjectFile.Name + Constants.ExeSuffix);
File.Copy(coreConsole, outputExe, overwrite: true);
return 0;
}
private static void CopyContents(ProjectContext context, string outputPath)
{
var sourceFiles = context.ProjectFile.Files.GetFilesForBundling();
Copy(sourceFiles, context.ProjectDirectory, outputPath);
}
private static void Copy(IEnumerable<string> sourceFiles, string sourceDirectory, string targetDirectory)
{
if (sourceFiles == null)
{
throw new ArgumentNullException(nameof(sourceFiles));
}
sourceDirectory = EnsureTrailingSlash(sourceDirectory);
targetDirectory = EnsureTrailingSlash(targetDirectory);
foreach (var sourceFilePath in sourceFiles)
{
var fileName = Path.GetFileName(sourceFilePath);
var targetFilePath = sourceFilePath.Replace(sourceDirectory, targetDirectory);
var targetFileParentFolder = Path.GetDirectoryName(targetFilePath);
// Create directory before copying a file
if (!Directory.Exists(targetFileParentFolder))
{
Directory.CreateDirectory(targetFileParentFolder);
}
File.Copy(
sourceFilePath,
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);
}
}
}
private static string EnsureTrailingSlash(string path)
{
return EnsureTrailingCharacter(path, Path.DirectorySeparatorChar);
}
private static string EnsureTrailingCharacter(string path, char trailingCharacter)
{
if (path == null)
{
throw new ArgumentNullException(nameof(path));
}
// if the path is empty, we want to return the original string instead of a single trailing character.
if (path.Length == 0 || path[path.Length - 1] == trailingCharacter)
{
return path;
}
return path + trailingCharacter;
}
private static void PublishFiles(IEnumerable<string> files, string outputPath)
{
foreach (var file in files)