2015-12-10 13:06:33 -08:00
|
|
|
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
|
|
|
|
|
|
|
2016-01-04 12:49:13 -08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2015-12-10 13:06:33 -08:00
|
|
|
|
using System.IO;
|
2016-01-04 12:49:13 -08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Xml.Linq;
|
2016-01-04 23:12:40 -08:00
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
2015-12-10 13:06:33 -08:00
|
|
|
|
using Microsoft.DotNet.ProjectModel;
|
2016-01-04 23:12:40 -08:00
|
|
|
|
using Microsoft.DotNet.ProjectModel.Compilation;
|
2016-01-04 12:49:13 -08:00
|
|
|
|
using Microsoft.DotNet.ProjectModel.Graph;
|
|
|
|
|
using Microsoft.DotNet.Tools.Common;
|
2015-12-10 13:06:33 -08:00
|
|
|
|
using NuGet.Frameworks;
|
|
|
|
|
|
2016-01-04 23:12:40 -08:00
|
|
|
|
namespace Microsoft.DotNet.Cli.Compiler.Common
|
2015-12-10 13:06:33 -08:00
|
|
|
|
{
|
2016-01-04 23:12:40 -08:00
|
|
|
|
public static class ProjectContextExtensions
|
2015-12-10 13:06:33 -08:00
|
|
|
|
{
|
|
|
|
|
public static string ProjectName(this ProjectContext context) => context.RootProject.Identity.Name;
|
|
|
|
|
|
|
|
|
|
public static string GetOutputPath(this ProjectContext context, string configuration, string currentOutputPath)
|
|
|
|
|
{
|
|
|
|
|
var outputPath = string.Empty;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(currentOutputPath))
|
|
|
|
|
{
|
|
|
|
|
outputPath = Path.Combine(
|
|
|
|
|
GetDefaultRootOutputPath(context, currentOutputPath),
|
|
|
|
|
Constants.BinDirectoryName,
|
|
|
|
|
configuration,
|
|
|
|
|
context.TargetFramework.GetTwoDigitShortFolderName());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
outputPath = currentOutputPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return outputPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string GetIntermediateOutputPath(this ProjectContext context, string configuration, string intermediateOutputValue, string currentOutputPath)
|
|
|
|
|
{
|
|
|
|
|
var intermediateOutputPath = string.Empty;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(intermediateOutputValue))
|
|
|
|
|
{
|
|
|
|
|
intermediateOutputPath = Path.Combine(
|
|
|
|
|
GetDefaultRootOutputPath(context, currentOutputPath),
|
|
|
|
|
Constants.ObjDirectoryName,
|
|
|
|
|
configuration,
|
|
|
|
|
context.TargetFramework.GetTwoDigitShortFolderName());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
intermediateOutputPath = intermediateOutputValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return intermediateOutputPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string GetDefaultRootOutputPath(ProjectContext context, string currentOutputPath)
|
|
|
|
|
{
|
2016-01-04 23:12:40 -08:00
|
|
|
|
var rootOutputPath = string.Empty;
|
2015-12-10 13:06:33 -08:00
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(currentOutputPath))
|
|
|
|
|
{
|
|
|
|
|
rootOutputPath = context.ProjectFile.ProjectDirectory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rootOutputPath;
|
|
|
|
|
}
|
2016-01-04 12:49:13 -08:00
|
|
|
|
|
2016-01-04 23:12:40 -08:00
|
|
|
|
public static void MakeCompilationOutputRunnable(this ProjectContext context, string outputPath, string configuration)
|
2016-01-04 12:49:13 -08:00
|
|
|
|
{
|
|
|
|
|
context
|
|
|
|
|
.ProjectFile
|
|
|
|
|
.Files
|
|
|
|
|
.GetContentFiles()
|
|
|
|
|
.StructuredCopyTo(context.ProjectDirectory, outputPath)
|
|
|
|
|
.RemoveAttribute(FileAttributes.ReadOnly);
|
|
|
|
|
|
|
|
|
|
var exporter = context.CreateExporter(configuration);
|
|
|
|
|
|
|
|
|
|
if (context.TargetFramework.IsDesktop())
|
|
|
|
|
{
|
|
|
|
|
exporter
|
|
|
|
|
.GetDependencies()
|
|
|
|
|
.SelectMany(e => e.RuntimeAssets())
|
|
|
|
|
.CopyTo(outputPath);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
exporter
|
|
|
|
|
.GetDependencies(LibraryType.Package)
|
|
|
|
|
.WriteDepsTo(Path.Combine(outputPath, context.ProjectFile.Name + FileNameSuffixes.Deps));
|
|
|
|
|
|
|
|
|
|
exporter.GetDependencies(LibraryType.Project)
|
|
|
|
|
.SelectMany(e => e.RuntimeAssets())
|
|
|
|
|
.CopyTo(outputPath);
|
|
|
|
|
|
2016-01-09 23:33:22 -08:00
|
|
|
|
CoreHost.CopyTo(outputPath, context.ProjectFile.Name + Constants.ExeSuffix);
|
2016-01-04 12:49:13 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<string> StructuredCopyTo(this IEnumerable<string> sourceFiles, string sourceDirectory, string targetDirectory)
|
|
|
|
|
{
|
|
|
|
|
if (sourceFiles == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(sourceFiles));
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-05 02:39:18 -08:00
|
|
|
|
sourceDirectory = EnsureTrailingSlash(sourceDirectory);
|
|
|
|
|
targetDirectory = EnsureTrailingSlash(targetDirectory);
|
|
|
|
|
|
2016-01-04 12:49:13 -08:00
|
|
|
|
var pathMap = sourceFiles
|
2016-01-05 02:39:18 -08:00
|
|
|
|
.ToDictionary(s => s,
|
|
|
|
|
s => Path.Combine(targetDirectory,
|
|
|
|
|
PathUtility.GetRelativePath(sourceDirectory, s)));
|
2016-01-04 12:49:13 -08:00
|
|
|
|
|
|
|
|
|
foreach (var targetDir in pathMap.Values
|
|
|
|
|
.Select(Path.GetDirectoryName)
|
|
|
|
|
.Distinct()
|
|
|
|
|
.Where(t => !Directory.Exists(t)))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(targetDir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var sourceFilePath in pathMap.Keys)
|
|
|
|
|
{
|
|
|
|
|
File.Copy(
|
|
|
|
|
sourceFilePath,
|
|
|
|
|
pathMap[sourceFilePath],
|
|
|
|
|
overwrite: true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pathMap.Values;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-05 02:39:18 -08:00
|
|
|
|
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;
|
|
|
|
|
}
|
2016-01-04 12:49:13 -08:00
|
|
|
|
|
|
|
|
|
private static IEnumerable<string> RemoveAttribute(this IEnumerable<string> files, FileAttributes attribute)
|
|
|
|
|
{
|
|
|
|
|
foreach (var file in files)
|
|
|
|
|
{
|
|
|
|
|
var fileAttributes = File.GetAttributes(file);
|
|
|
|
|
if ((fileAttributes & attribute) == attribute)
|
|
|
|
|
{
|
|
|
|
|
File.SetAttributes(file, fileAttributes & ~attribute);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return files;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-12 15:02:26 -08:00
|
|
|
|
public static void GenerateBindingRedirects(this ProjectContext context, LibraryExporter exporter, string outputName)
|
2016-01-04 12:49:13 -08:00
|
|
|
|
{
|
|
|
|
|
var existingConfig = new DirectoryInfo(context.ProjectDirectory)
|
|
|
|
|
.EnumerateFiles()
|
|
|
|
|
.FirstOrDefault(f => f.Name.Equals("app.config", StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
XDocument baseAppConfig = null;
|
|
|
|
|
|
|
|
|
|
if (existingConfig != null)
|
|
|
|
|
{
|
|
|
|
|
using (var fileStream = File.OpenRead(existingConfig.FullName))
|
|
|
|
|
{
|
|
|
|
|
baseAppConfig = XDocument.Load(fileStream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-04 23:12:40 -08:00
|
|
|
|
var appConfig = exporter.GetAllExports().GenerateBindingRedirects(baseAppConfig);
|
2016-01-04 12:49:13 -08:00
|
|
|
|
|
2016-01-05 01:01:46 -08:00
|
|
|
|
if (appConfig == null) { return; }
|
2016-01-04 12:49:13 -08:00
|
|
|
|
|
2016-01-12 15:02:26 -08:00
|
|
|
|
var path = outputName + ".config";
|
2016-01-04 12:49:13 -08:00
|
|
|
|
using (var stream = File.Create(path))
|
|
|
|
|
{
|
|
|
|
|
appConfig.Save(stream);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-10 13:06:33 -08:00
|
|
|
|
}
|
|
|
|
|
}
|