From 6edac85dceb45d6116ad004252a1b1828e8ce7a0 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Wed, 27 Jan 2016 20:35:43 -0800 Subject: [PATCH] Fixed copying output assets to the publish directory --- .../CommandResolver.cs | 34 ++++------ .../LibraryExporterExtensions.cs | 10 +-- .../Compilation/LibraryExport.cs | 45 +++++++------ .../Compilation/LibraryExporter.cs | 63 +++++++++---------- .../OutputPathCalculator.cs | 34 ++++++++-- .../Program.cs | 34 +++++----- .../PublishCommand.cs | 9 +++ 7 files changed, 128 insertions(+), 101 deletions(-) diff --git a/src/Microsoft.DotNet.Cli.Utils/CommandResolver.cs b/src/Microsoft.DotNet.Cli.Utils/CommandResolver.cs index 6931dfe39..5c8ff6655 100644 --- a/src/Microsoft.DotNet.Cli.Utils/CommandResolver.cs +++ b/src/Microsoft.DotNet.Cli.Utils/CommandResolver.cs @@ -24,16 +24,16 @@ namespace Microsoft.DotNet.Cli.Utils private static CommandSpec ResolveFromPath(string commandName, IEnumerable args, bool useComSpec = false) { var commandPath = Env.GetCommandPath(commandName); - return commandPath == null - ? null + return commandPath == null + ? null : CreateCommandSpecPreferringExe(commandName, args, commandPath, CommandResolutionStrategy.Path, useComSpec); } private static CommandSpec ResolveFromAppBase(string commandName, IEnumerable args, bool useComSpec = false) { var commandPath = Env.GetCommandPathFromAppBase(AppContext.BaseDirectory, commandName); - return commandPath == null - ? null + return commandPath == null + ? null : CreateCommandSpecPreferringExe(commandName, args, commandPath, CommandResolutionStrategy.BaseDirectory, useComSpec); } @@ -50,7 +50,7 @@ namespace Microsoft.DotNet.Cli.Utils var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args); return new CommandSpec(commandName, escapedArgs, CommandResolutionStrategy.Path); } - + } return null; @@ -69,7 +69,7 @@ namespace Microsoft.DotNet.Cli.Utils if (commandPackage == null) return null; - var depsPath = GetDepsPath(projectContext, Constants.DefaultConfiguration); + var depsPath = projectContext.GetOutputPathCalculator().GetDepsPath(Constants.DefaultConfiguration); return ConfigureCommandFromPackage(commandName, args, commandPackage, projectContext, depsPath, useComSpec); } @@ -90,7 +90,7 @@ namespace Microsoft.DotNet.Cli.Utils private static PackageDescription GetCommandPackage(ProjectContext projectContext, string commandName) { return projectContext.LibraryManager.GetLibraries() - .Where(l => l.GetType() == typeof (PackageDescription)) + .Where(l => l.GetType() == typeof(PackageDescription)) .Select(l => l as PackageDescription) .FirstOrDefault(p => p.Library.Files .Select(Path.GetFileName) @@ -201,21 +201,13 @@ namespace Microsoft.DotNet.Cli.Utils var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args); return new CommandSpec(fileName, escapedArgs, CommandResolutionStrategy.NugetPackage); } - - - } - - private static string GetDepsPath(ProjectContext context, string buildConfiguration) - { - return Path.Combine(context.GetOutputPathCalculator().GetOutputDirectoryPath(buildConfiguration), - context.ProjectFile.Name + FileNameSuffixes.Deps); } private static CommandSpec CreateCommandSpecPreferringExe( - string commandName, - IEnumerable args, + string commandName, + IEnumerable args, string commandPath, - CommandResolutionStrategy resolutionStrategy, + CommandResolutionStrategy resolutionStrategy, bool useComSpec = false) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && @@ -226,7 +218,7 @@ namespace Microsoft.DotNet.Cli.Utils // Use cmd if we can't find an exe if (preferredCommandPath == null) { - useComSpec = true; + useComSpec = true; } else { @@ -246,8 +238,8 @@ namespace Microsoft.DotNet.Cli.Utils } private static CommandSpec CreateComSpecCommandSpec( - string command, - IEnumerable args, + string command, + IEnumerable args, CommandResolutionStrategy resolutionStrategy) { // To prevent Command Not Found, comspec gets passed in as diff --git a/src/Microsoft.DotNet.Compiler.Common/LibraryExporterExtensions.cs b/src/Microsoft.DotNet.Compiler.Common/LibraryExporterExtensions.cs index 3a30203c2..c5291bb75 100644 --- a/src/Microsoft.DotNet.Compiler.Common/LibraryExporterExtensions.cs +++ b/src/Microsoft.DotNet.Compiler.Common/LibraryExporterExtensions.cs @@ -32,16 +32,18 @@ namespace Microsoft.DotNet.Cli.Compiler.Common })); } - internal static IEnumerable RuntimeAssets(this LibraryExport export) + internal static IEnumerable RuntimeAssets(this LibraryExport export) { - return export.RuntimeAssemblies.Union(export.NativeLibraries); + return export.RuntimeAssemblies.Union(export.NativeLibraries) + .Select(e => e.ResolvedPath) + .Union(export.RuntimeAssets); } - internal static void CopyTo(this IEnumerable assets, string destinationPath) + internal static void CopyTo(this IEnumerable assets, string destinationPath) { foreach (var asset in assets) { - File.Copy(asset.ResolvedPath, Path.Combine(destinationPath, Path.GetFileName(asset.ResolvedPath)), + File.Copy(asset, Path.Combine(destinationPath, Path.GetFileName(asset)), overwrite: true); } } diff --git a/src/Microsoft.DotNet.ProjectModel/Compilation/LibraryExport.cs b/src/Microsoft.DotNet.ProjectModel/Compilation/LibraryExport.cs index 25158a436..b39e383e3 100644 --- a/src/Microsoft.DotNet.ProjectModel/Compilation/LibraryExport.cs +++ b/src/Microsoft.DotNet.ProjectModel/Compilation/LibraryExport.cs @@ -19,6 +19,11 @@ namespace Microsoft.DotNet.ProjectModel.Compilation /// public IEnumerable RuntimeAssemblies { get; } + /// + /// Non assembly runtime assets. + /// + public IEnumerable RuntimeAssets { get; } + /// /// Gets a list of fully-qualified paths to native binaries required to run /// @@ -33,26 +38,28 @@ namespace Microsoft.DotNet.ProjectModel.Compilation /// Gets a list of fully-qualified paths to source code file references /// public IEnumerable SourceReferences { get; } - - /// - /// Get a list of analyzers provided by this export. - /// - public IEnumerable AnalyzerReferences { get; } - public LibraryExport(LibraryDescription library, - IEnumerable compileAssemblies, - IEnumerable sourceReferences, - IEnumerable runtimeAssemblies, - IEnumerable nativeLibraries, - IEnumerable analyzers) - { - Library = library; - CompilationAssemblies = compileAssemblies; - SourceReferences = sourceReferences; - RuntimeAssemblies = runtimeAssemblies; - NativeLibraries = nativeLibraries; - AnalyzerReferences = analyzers; - } + /// + /// Get a list of analyzers provided by this export. + /// + public IEnumerable AnalyzerReferences { get; } + + public LibraryExport(LibraryDescription library, + IEnumerable compileAssemblies, + IEnumerable sourceReferences, + IEnumerable runtimeAssemblies, + IEnumerable runtimeAssets, + IEnumerable nativeLibraries, + IEnumerable analyzers) + { + Library = library; + CompilationAssemblies = compileAssemblies; + SourceReferences = sourceReferences; + RuntimeAssemblies = runtimeAssemblies; + RuntimeAssets = runtimeAssets; + NativeLibraries = nativeLibraries; + AnalyzerReferences = analyzers; + } private string DebuggerDisplay => Library.Identity.ToString(); } diff --git a/src/Microsoft.DotNet.ProjectModel/Compilation/LibraryExporter.cs b/src/Microsoft.DotNet.ProjectModel/Compilation/LibraryExporter.cs index ba6deabfa..98de24b31 100644 --- a/src/Microsoft.DotNet.ProjectModel/Compilation/LibraryExporter.cs +++ b/src/Microsoft.DotNet.ProjectModel/Compilation/LibraryExporter.cs @@ -81,7 +81,7 @@ namespace Microsoft.DotNet.ProjectModel.Compilation var sourceReferences = new List(); var analyzerReferences = new List(); var libraryExport = GetExport(library); - + // We need to filter out source references from non-root libraries, // so we rebuild the library export @@ -100,8 +100,13 @@ namespace Microsoft.DotNet.ProjectModel.Compilation analyzerReferences.AddRange(libraryExport.AnalyzerReferences); } - yield return new LibraryExport(library, compilationAssemblies, sourceReferences, - libraryExport.RuntimeAssemblies, libraryExport.NativeLibraries, analyzerReferences); + yield return new LibraryExport(library, + compilationAssemblies, + sourceReferences, + libraryExport.RuntimeAssemblies, + libraryExport.RuntimeAssets, + libraryExport.NativeLibraries, + analyzerReferences); } } @@ -142,17 +147,17 @@ namespace Microsoft.DotNet.ProjectModel.Compilation { sourceReferences.Add(sharedSource); } - + var analyzers = GetAnalyzerReferences(package); return new LibraryExport(package, compileAssemblies, - sourceReferences, runtimeAssemblies, nativeLibraries, analyzers); + sourceReferences, runtimeAssemblies, Array.Empty(), nativeLibraries, analyzers); } private LibraryExport ExportProject(ProjectDescription project) { var compileAssemblies = new List(); - var runtimeAssemblies = new List(); + var runtimeAssets = new List(); var sourceReferences = new List(); if (!string.IsNullOrEmpty(project.TargetFrameworkInfo?.AssemblyPath)) @@ -160,38 +165,29 @@ namespace Microsoft.DotNet.ProjectModel.Compilation // Project specifies a pre-compiled binary. We're done! var assemblyPath = ResolvePath(project.Project, _configuration, project.TargetFrameworkInfo.AssemblyPath); var pdbPath = Path.ChangeExtension(assemblyPath, "pdb"); - + var compileAsset = new LibraryAsset( project.Project.Name, null, Path.GetFullPath(Path.Combine(project.Project.ProjectDirectory, assemblyPath))); - var pdbAsset = new LibraryAsset( - project.Project.Name + "/pdb", - null, - Path.GetFullPath(Path.Combine(project.Project.ProjectDirectory, pdbPath))); - compileAssemblies.Add(compileAsset); - - runtimeAssemblies.Add(compileAsset); - runtimeAssemblies.Add(pdbAsset); + runtimeAssets.Add(pdbPath); } else { var outputCalculator = project.GetOutputPathCalculator(); var assemblyPath = outputCalculator.GetAssemblyPath(_configuration); compileAssemblies.Add(new LibraryAsset(project.Identity.Name, null, assemblyPath)); - + foreach (var path in outputCalculator.GetBuildOutputs(_configuration)) { - var assetName = project.Identity.Name; - if (Path.GetFileNameWithoutExtension(path) != project.Identity.Name) + if (string.Equals(assemblyPath, path)) { - assetName += "/" + Path.GetExtension(path).Substring(1); + continue; } - // We're going to call this asset - runtimeAssemblies.Add(new LibraryAsset(assetName, null, path)); + runtimeAssets.Add(path); } } @@ -205,7 +201,7 @@ namespace Microsoft.DotNet.ProjectModel.Compilation // just the same as compileAssemblies and nativeLibraries are empty // Also no support for analyzer projects return new LibraryExport(project, compileAssemblies, sourceReferences, - runtimeAssemblies, Array.Empty(), Array.Empty()); + compileAssemblies, runtimeAssets, Array.Empty(), Array.Empty()); } private static string ResolvePath(Project project, string configuration, string path) @@ -233,6 +229,7 @@ namespace Microsoft.DotNet.ProjectModel.Compilation new[] { new LibraryAsset(library.Identity.Name, library.Path, library.Path) }, Array.Empty(), Array.Empty(), + Array.Empty(), Array.Empty(), Array.Empty()); } @@ -245,7 +242,7 @@ namespace Microsoft.DotNet.ProjectModel.Compilation .Where(path => path.StartsWith("shared" + Path.DirectorySeparatorChar)) .Select(path => Path.Combine(package.Path, path)); } - + private IEnumerable GetAnalyzerReferences(PackageDescription package) { var analyzers = package @@ -253,16 +250,16 @@ namespace Microsoft.DotNet.ProjectModel.Compilation .Files .Where(path => path.StartsWith("analyzers" + Path.DirectorySeparatorChar) && path.EndsWith(".dll")); - + var analyzerRefs = new List(); // See https://docs.nuget.org/create/analyzers-conventions for the analyzer // NuGet specification foreach (var analyzer in analyzers) { var specifiers = analyzer.Split(Path.DirectorySeparatorChar); - + var assemblyPath = Path.Combine(package.Path, analyzer); - + // $/analyzers/{Framework Name}{Version}/{Supported Architecture}/{Supported Programming Language}/{Analyzer}.dll switch (specifiers.Length) { @@ -275,7 +272,7 @@ namespace Microsoft.DotNet.ProjectModel.Compilation runtimeIdentifier: null )); break; - + // $/analyzers/{framework}/{analyzer}.dll case 3: analyzerRefs.Add(new AnalyzerReference( @@ -285,7 +282,7 @@ namespace Microsoft.DotNet.ProjectModel.Compilation runtimeIdentifier: null )); break; - + // $/analyzers/{framework}/{language}/{analyzer}.dll case 4: analyzerRefs.Add(new AnalyzerReference( @@ -295,7 +292,7 @@ namespace Microsoft.DotNet.ProjectModel.Compilation runtimeIdentifier: null )); break; - + // $/analyzers/{framework}/{runtime}/{language}/{analyzer}.dll case 5: analyzerRefs.Add(new AnalyzerReference( @@ -305,10 +302,10 @@ namespace Microsoft.DotNet.ProjectModel.Compilation runtimeIdentifier: specifiers[2] )); break; - - // Anything less than 2 specifiers or more than 4 is - // illegal according to the specification and will be - // ignored + + // Anything less than 2 specifiers or more than 4 is + // illegal according to the specification and will be + // ignored } } return analyzerRefs; diff --git a/src/Microsoft.DotNet.ProjectModel/OutputPathCalculator.cs b/src/Microsoft.DotNet.ProjectModel/OutputPathCalculator.cs index 59537141b..b57ede72f 100644 --- a/src/Microsoft.DotNet.ProjectModel/OutputPathCalculator.cs +++ b/src/Microsoft.DotNet.ProjectModel/OutputPathCalculator.cs @@ -1,6 +1,7 @@ // 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; using System.Collections.Generic; using System.IO; using System.Runtime.InteropServices; @@ -74,7 +75,7 @@ namespace Microsoft.DotNet.ProjectModel return intermediateOutputPath; } - + public string GetAssemblyPath(string buildConfiguration) { var compilationOptions = _project.GetCompilerOptions(_framework, buildConfiguration); @@ -89,22 +90,43 @@ namespace Microsoft.DotNet.ProjectModel GetOutputDirectoryPath(buildConfiguration), _project.Name + outputExtension); } - + public IEnumerable GetBuildOutputs(string buildConfiguration) { var assemblyPath = GetAssemblyPath(buildConfiguration); - + yield return assemblyPath; yield return Path.ChangeExtension(assemblyPath, "pdb"); var compilationOptions = _project.GetCompilerOptions(_framework, buildConfiguration); - + if (compilationOptions.GenerateXmlDocumentation == true) { yield return Path.ChangeExtension(assemblyPath, "xml"); } + + // This should only exist in desktop framework + var configFile = assemblyPath + ".config"; + + if (File.Exists(configFile)) + { + yield return configFile; + } + + // Deps file + var depsFile = GetDepsPath(buildConfiguration); + + if (File.Exists(depsFile)) + { + yield return depsFile; + } } - + + public string GetDepsPath(string buildConfiguration) + { + return Path.Combine(GetOutputDirectoryPath(buildConfiguration), _project.Name + FileNameSuffixes.Deps); + } + public string GetExecutablePath(string buildConfiguration) { var extension = FileNameSuffixes.CurrentPlatform.Exe; @@ -115,7 +137,7 @@ namespace Microsoft.DotNet.ProjectModel { extension = FileNameSuffixes.DotNet.Exe; } - + return Path.Combine( GetOutputDirectoryPath(buildConfiguration), _project.Name + extension); diff --git a/src/Microsoft.DotNet.Tools.Compiler/Program.cs b/src/Microsoft.DotNet.Tools.Compiler/Program.cs index 24ab5509a..7c5ea08e2 100644 --- a/src/Microsoft.DotNet.Tools.Compiler/Program.cs +++ b/src/Microsoft.DotNet.Tools.Compiler/Program.cs @@ -57,7 +57,7 @@ namespace Microsoft.DotNet.Tools.Compiler } private static bool CompileNative( - ProjectContext context, + ProjectContext context, CompilerCommandApp args) { var outputPathCalculator = context.GetOutputPathCalculator(args.OutputValue); @@ -71,7 +71,7 @@ namespace Microsoft.DotNet.Tools.Compiler var compilationOptions = context.ProjectFile.GetCompilerOptions(context.TargetFramework, args.ConfigValue); var managedOutput = outputPathCalculator.GetAssemblyPath(args.ConfigValue); - + var nativeArgs = new List(); // Input Assembly @@ -82,8 +82,8 @@ namespace Microsoft.DotNet.Tools.Compiler { nativeArgs.Add("--ilcargs"); nativeArgs.Add($"{args.IlcArgsValue}"); - } - + } + // ILC Path if (!string.IsNullOrWhiteSpace(args.IlcPathValue)) { @@ -106,7 +106,7 @@ namespace Microsoft.DotNet.Tools.Compiler } // CodeGen Mode - if(args.IsCppModeValue) + if (args.IsCppModeValue) { nativeArgs.Add("--mode"); nativeArgs.Add("cpp"); @@ -138,7 +138,7 @@ namespace Microsoft.DotNet.Tools.Compiler // Output Path nativeArgs.Add("--output"); - nativeArgs.Add($"{nativeOutputPath}"); + nativeArgs.Add($"{nativeOutputPath}"); // Write Response File var rsp = Path.Combine(nativeIntermediateOutputPath, $"dotnet-compile-native.{context.ProjectFile.Name}.rsp"); @@ -288,7 +288,7 @@ namespace Microsoft.DotNet.Tools.Compiler }; RunScripts(context, ScriptNames.PreCompile, contextVariables); - var result = Command.Create($"dotnet-compile-{compilerName}", new [] {"@" + $"{rsp}" }) + var result = Command.Create($"dotnet-compile-{compilerName}", new[] { "@" + $"{rsp}" }) .OnErrorLine(line => { var diagnostic = ParseDiagnostic(context.ProjectDirectory, line); @@ -344,12 +344,16 @@ namespace Microsoft.DotNet.Tools.Compiler generateBindingRedirects = true; var projectContext = ProjectContext.Create(context.ProjectDirectory, context.TargetFramework, - new[] { PlatformServices.Default.Runtime.GetLegacyRestoreRuntimeIdentifier()}); + new[] { PlatformServices.Default.Runtime.GetLegacyRestoreRuntimeIdentifier() }); - projectContext - .CreateExporter(args.ConfigValue) - .GetDependencies(LibraryType.Package) - .WriteDepsTo(Path.Combine(outputPath, projectContext.ProjectFile.Name + FileNameSuffixes.Deps)); + // Don't generate a deps file if we're on desktop + if (!context.TargetFramework.IsDesktop()) + { + projectContext + .CreateExporter(args.ConfigValue) + .GetDependencies(LibraryType.Package) + .WriteDepsTo(Path.Combine(outputPath, projectContext.ProjectFile.Name + FileNameSuffixes.Deps)); + } } if (generateBindingRedirects && context.TargetFramework.IsDesktop()) @@ -372,12 +376,6 @@ namespace Microsoft.DotNet.Tools.Compiler .Execute(); } } - - private static void CopyExport(string outputPath, LibraryExport export) - { - CopyFiles(export.RuntimeAssemblies, outputPath); - CopyFiles(export.NativeLibraries, outputPath); - } private static bool PrintSummary(List diagnostics, Stopwatch sw, bool success = true) { diff --git a/src/Microsoft.DotNet.Tools.Publish/PublishCommand.cs b/src/Microsoft.DotNet.Tools.Publish/PublishCommand.cs index c030360d0..50a6280ec 100644 --- a/src/Microsoft.DotNet.Tools.Publish/PublishCommand.cs +++ b/src/Microsoft.DotNet.Tools.Publish/PublishCommand.cs @@ -132,6 +132,7 @@ namespace Microsoft.DotNet.Tools.Publish PublishFiles(export.RuntimeAssemblies, outputPath, nativeSubdirectories: false); PublishFiles(export.NativeLibraries, outputPath, nativeSubdirectories); + PublishFiles(export.RuntimeAssets, outputPath); } CopyContents(context, outputPath); @@ -174,6 +175,14 @@ namespace Microsoft.DotNet.Tools.Publish return 0; } + private static void PublishFiles(IEnumerable files, string outputPath) + { + foreach (var file in files) + { + var targetPath = Path.Combine(outputPath, Path.GetFileName(file)); + File.Copy(file, targetPath, overwrite: true); + } + } private static void PublishFiles(IEnumerable files, string outputPath, bool nativeSubdirectories) {