dotnet-installer/src/Microsoft.DotNet.Compiler.Common/Executable.cs

148 lines
4.9 KiB
C#
Raw Normal View History

// 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.
using System;
2016-02-09 12:01:52 -08:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Microsoft.DotNet.Cli.Compiler.Common;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Files;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Compilation;
using Microsoft.DotNet.ProjectModel.Graph;
using NuGet.Frameworks;
namespace Microsoft.Dotnet.Cli.Compiler.Common
{
public class Executable
{
private readonly ProjectContext _context;
2016-02-17 10:08:27 -08:00
private readonly LibraryExporter _exporter;
2016-02-03 10:57:25 -08:00
private readonly OutputPaths _outputPaths;
2016-02-17 10:08:27 -08:00
private readonly string _runtimeOutputPath;
private readonly string _intermediateOutputPath;
2016-02-03 10:57:25 -08:00
public Executable(ProjectContext context, OutputPaths outputPaths, LibraryExporter exporter)
{
_context = context;
2016-02-03 10:57:25 -08:00
_outputPaths = outputPaths;
2016-02-17 10:08:27 -08:00
_runtimeOutputPath = outputPaths.RuntimeOutputPath;
_intermediateOutputPath = outputPaths.IntermediateOutputDirectoryPath;
2016-02-03 10:57:25 -08:00
_exporter = exporter;
}
2016-02-03 10:57:25 -08:00
public void MakeCompilationOutputRunnable()
{
2016-02-09 10:51:03 -08:00
if (string.IsNullOrEmpty(_context.RuntimeIdentifier))
{
throw new InvalidOperationException($"Can not make output runnable for framework {_context.TargetFramework}, because it doesn't have runtime target");
}
2016-02-17 10:08:27 -08:00
CopyContentFiles();
ExportRuntimeAssets();
}
2016-02-17 10:08:27 -08:00
private void ExportRuntimeAssets()
{
if (_context.TargetFramework.IsDesktop())
{
2016-02-17 10:08:27 -08:00
MakeCompilationOutputRunnableForFullFramework();
}
else
{
2016-02-17 10:08:27 -08:00
MakeCompilationOutputRunnableForCoreCLR();
}
2016-02-03 10:57:25 -08:00
}
2016-02-17 10:08:27 -08:00
private void MakeCompilationOutputRunnableForFullFramework()
{
2016-02-17 10:08:27 -08:00
var dependencies = _exporter.GetDependencies();
CopyAssemblies(dependencies);
CopyAssets(dependencies);
2016-02-03 10:57:25 -08:00
GenerateBindingRedirects(_exporter);
}
2016-02-17 10:08:27 -08:00
private void MakeCompilationOutputRunnableForCoreCLR()
{
2016-02-17 10:08:27 -08:00
WriteDepsFileAndCopyProjectDependencies(_exporter);
// TODO: Pick a host based on the RID
2016-02-17 10:08:27 -08:00
CoreHost.CopyTo(_runtimeOutputPath, _context.ProjectFile.Name + Constants.ExeSuffix);
}
2016-02-17 10:08:27 -08:00
private void CopyContentFiles()
{
var contentFiles = new ContentFiles(_context);
2016-02-17 10:08:27 -08:00
contentFiles.StructuredCopyTo(_runtimeOutputPath);
}
private void CopyAssemblies(IEnumerable<LibraryExport> libraryExports)
{
foreach (var libraryExport in libraryExports)
{
libraryExport.RuntimeAssemblies.CopyTo(_runtimeOutputPath);
libraryExport.NativeLibraries.CopyTo(_runtimeOutputPath);
}
}
2016-02-17 10:08:27 -08:00
private void CopyAssets(IEnumerable<LibraryExport> libraryExports)
{
2016-02-10 10:07:22 -08:00
foreach (var libraryExport in libraryExports)
{
2016-02-17 10:08:27 -08:00
libraryExport.RuntimeAssets.StructuredCopyTo(
_runtimeOutputPath,
_intermediateOutputPath);
2016-02-10 10:07:22 -08:00
}
}
2016-02-17 10:08:27 -08:00
private void WriteDepsFileAndCopyProjectDependencies(LibraryExporter exporter)
{
exporter
.GetDependencies(LibraryType.Package)
2016-02-17 10:08:27 -08:00
.WriteDepsTo(Path.Combine(_runtimeOutputPath, _context.ProjectFile.Name + FileNameSuffixes.Deps));
2016-02-16 15:30:39 -08:00
var projectExports = exporter.GetDependencies(LibraryType.Project);
2016-02-17 10:08:27 -08:00
CopyAssemblies(projectExports);
CopyAssets(projectExports);
2016-02-09 12:01:52 -08:00
2016-02-17 10:08:27 -08:00
var packageExports = exporter.GetDependencies(LibraryType.Package);
CopyAssets(packageExports);
2016-02-03 10:57:25 -08:00
}
2016-02-03 10:57:25 -08:00
public void GenerateBindingRedirects(LibraryExporter exporter)
{
2016-02-03 10:57:25 -08:00
var outputName = _outputPaths.RuntimeFiles.Assembly;
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);
}
}
var appConfig = exporter.GetAllExports().GenerateBindingRedirects(baseAppConfig);
if (appConfig == null) { return; }
var path = outputName + ".config";
using (var stream = File.Create(path))
{
appConfig.Save(stream);
}
}
}
}