Skip to content
8 changes: 8 additions & 0 deletions src/Cli/Microsoft.DotNet.Cli.Utils/MSBuildArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,12 @@ public void ApplyPropertiesToRestore()
RestoreGlobalProperties = new(newdict);
}
}

internal string[]? GetResolvedTargets()
=> this switch
{
{ RequestedTargets: null or { Length: 0 } } => GetTargetResult,
{ GetTargetResult: null or { Length: 0 } } => RequestedTargets,
_ => [.. RequestedTargets.Union(GetTargetResult, StringComparer.OrdinalIgnoreCase)]
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../Microsoft.DotNet.Cli.CommandLine/Microsoft.DotNet.Cli.CommandLine.csproj" GlobalPropertiesToRemove="PublishDir" />
<ProjectReference Include="../Microsoft.DotNet.Cli.Utils/Microsoft.DotNet.Cli.Utils.csproj" GlobalPropertiesToRemove="PublishDir" />
<ProjectReference Include="../Microsoft.DotNet.Configurer/Microsoft.DotNet.Configurer.csproj" GlobalPropertiesToRemove="PublishDir" />
<ProjectReference Include="..\Microsoft.DotNet.Cli.CommandLine\Microsoft.DotNet.Cli.CommandLine.csproj" GlobalPropertiesToRemove="PublishDir" />
<ProjectReference Include="..\Microsoft.DotNet.Cli.Utils\Microsoft.DotNet.Cli.Utils.csproj" GlobalPropertiesToRemove="PublishDir" />
<ProjectReference Include="..\Microsoft.DotNet.Configurer\Microsoft.DotNet.Configurer.csproj" GlobalPropertiesToRemove="PublishDir" />
<ProjectReference Include="..\..\Microsoft.DotNet.ProjectTools\Microsoft.DotNet.ProjectTools.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Cli/Microsoft.TemplateEngine.Cli/TelemetryHelper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Utilities;
using Microsoft.TemplateEngine.Abstractions;
using Microsoft.TemplateEngine.Utils;

Expand Down
1 change: 1 addition & 0 deletions src/Cli/Microsoft.TemplateEngine.Cli/TemplateInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text.RegularExpressions;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Cli.Utils.Extensions;
using Microsoft.DotNet.Utilities;
using Microsoft.TemplateEngine.Abstractions;
using Microsoft.TemplateEngine.Abstractions.TemplatePackage;
using Microsoft.TemplateEngine.Cli.Commands;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#nullable disable

using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Utilities;

namespace Microsoft.DotNet.Cli.CommandFactory.CommandResolution;

Expand Down
7 changes: 3 additions & 4 deletions src/Cli/dotnet/Commands/Build/BuildCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ public static CommandBase FromParseResult(ParseResult parseResult, string? msbui
return CommandFactory.CreateVirtualOrPhysicalCommand(
BuildCommandParser.GetCommand(),
BuildCommandDefinition.SlnOrProjectOrFileArgument,
(msbuildArgs, appFilePath) => new VirtualProjectBuildingCommand(
configureVirtualCommand: (msbuildArgs, appFilePath) => new VirtualProjectBuildingCommand(
entryPointFileFullPath: Path.GetFullPath(appFilePath),
msbuildArgs: msbuildArgs
)
msbuildArgs: msbuildArgs)
{
NoRestore = noRestore,
NoCache = true,
},
(msbuildArgs, msbuildPath) => new RestoringCommand(
createPhysicalCommand: (msbuildArgs, msbuildPath) => new RestoringCommand(
msbuildArgs: msbuildArgs.CloneWithAdditionalArgs("-consoleloggerparameters:Summary"),
noRestore: noRestore,
msbuildPath: msbuildPath
Expand Down
4 changes: 2 additions & 2 deletions src/Cli/dotnet/Commands/Clean/CleanCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public static CommandBase FromParseResult(ParseResult result, string? msbuildPat
CleanCommandParser.GetCommand(),
CleanCommandDefinition.SlnOrProjectOrFileArgument,
static (msbuildArgs, appFilePath) => new VirtualProjectBuildingCommand(
entryPointFileFullPath: appFilePath,
msbuildArgs: msbuildArgs)
entryPointFileFullPath: appFilePath,
msbuildArgs: msbuildArgs)
{
NoBuild = false,
NoRestore = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.DotNet.Cli.Commands.Run;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Cli.Utils.Extensions;
using Microsoft.DotNet.ProjectTools;

namespace Microsoft.DotNet.Cli.Commands.Clean.FileBasedAppArtifacts;

Expand Down Expand Up @@ -60,7 +61,7 @@ public override int Execute()

private IEnumerable<DirectoryInfo> GetFoldersToRemove()
{
var directory = new DirectoryInfo(VirtualProjectBuildingCommand.GetTempSubdirectory());
var directory = new DirectoryInfo(VirtualProjectBuilder.GetTempSubdirectory());

if (!directory.Exists)
{
Expand All @@ -84,7 +85,7 @@ private IEnumerable<DirectoryInfo> GetFoldersToRemove()

private static FileInfo GetMetadataFile()
{
return new FileInfo(VirtualProjectBuildingCommand.GetTempSubpath(RunFileArtifactsMetadata.FilePath));
return new FileInfo(VirtualProjectBuilder.GetTempSubpath(RunFileArtifactsMetadata.FilePath));
}

private FileStream? OpenMetadataFile()
Expand Down
3 changes: 2 additions & 1 deletion src/Cli/dotnet/Commands/CommandFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Commands.Run;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ProjectTools;

namespace Microsoft.DotNet.Cli.Commands;

Expand All @@ -23,7 +24,7 @@ internal static CommandBase CreateVirtualOrPhysicalCommand(
var args = parseResult.GetValue(catchAllUserInputArgument) ?? [];
LoggerUtility.SeparateBinLogArguments(args, out var binLogArgs, out var nonBinLogArgs);
var forwardedArgs = parseResult.OptionValuesToBeForwarded(command);
if (nonBinLogArgs is [{ } arg] && VirtualProjectBuildingCommand.IsValidEntryPointPath(arg))
if (nonBinLogArgs is [{ } arg] && VirtualProjectBuilder.IsValidEntryPointPath(arg))
{
var msbuildArgs = MSBuildArgs.AnalyzeMSBuildArguments([.. forwardedArgs, .. binLogArgs],
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.DotNet.Cli.Telemetry;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Configurer;
using Microsoft.DotNet.Utilities;

namespace Microsoft.DotNet.Cli.Commands.Hidden.InternalReportInstallSuccess;

Expand Down
1 change: 1 addition & 0 deletions src/Cli/dotnet/Commands/MSBuild/MSBuildLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.DotNet.Cli.Telemetry;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Configurer;
using Microsoft.DotNet.Utilities;

namespace Microsoft.DotNet.Cli.Commands.MSBuild;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics;
using Microsoft.Build.Evaluation;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Utilities;
using Microsoft.Extensions.Logging;
using Microsoft.TemplateEngine.Abstractions;
using Microsoft.TemplateEngine.Utils;
Expand Down
7 changes: 4 additions & 3 deletions src/Cli/dotnet/Commands/Package/Add/PackageAddCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation;
using Microsoft.CodeAnalysis;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Commands.MSBuild;
using Microsoft.DotNet.Cli.Commands.NuGet;
using Microsoft.DotNet.Cli.Commands.Run;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Extensions;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.FileBasedPrograms;
using Microsoft.DotNet.ProjectTools;
using NuGet.ProjectModel;

namespace Microsoft.DotNet.Cli.Commands.Package.Add;
Expand All @@ -25,7 +25,7 @@ public override int Execute()
{
var (fileOrDirectory, allowedAppKinds) = PackageCommandDefinition.ProcessPathOptions(_parseResult);

if (allowedAppKinds.HasFlag(AppKinds.FileBased) && VirtualProjectBuildingCommand.IsValidEntryPointPath(fileOrDirectory))
if (allowedAppKinds.HasFlag(AppKinds.FileBased) && VirtualProjectBuilder.IsValidEntryPointPath(fileOrDirectory))
{
return ExecuteForFileBasedApp(fileOrDirectory);
}
Expand Down Expand Up @@ -192,6 +192,7 @@ private int ExecuteForFileBasedApp(string path)
NoCache = true,
NoBuild = true,
};

var projectCollection = new ProjectCollection();
var projectInstance = command.CreateProjectInstance(projectCollection);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.FileBasedPrograms;
using Microsoft.DotNet.ProjectTools;

namespace Microsoft.DotNet.Cli.Commands.Package.Remove;

Expand All @@ -24,7 +25,7 @@ public override int Execute()

var (fileOrDirectory, allowedAppKinds) = PackageCommandDefinition.ProcessPathOptions(_parseResult);

if (allowedAppKinds.HasFlag(AppKinds.FileBased) && VirtualProjectBuildingCommand.IsValidEntryPointPath(fileOrDirectory))
if (allowedAppKinds.HasFlag(AppKinds.FileBased) && VirtualProjectBuilder.IsValidEntryPointPath(fileOrDirectory))
{
return ExecuteForFileBasedApp(path: fileOrDirectory, packageId: packageToRemove);
}
Expand Down
40 changes: 17 additions & 23 deletions src/Cli/dotnet/Commands/Project/Convert/ProjectConvertCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.DotNet.Cli.Commands.Run;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.FileBasedPrograms;
using Microsoft.DotNet.ProjectTools;
using Microsoft.TemplateEngine.Cli.Commands;

namespace Microsoft.DotNet.Cli.Commands.Project.Convert;
Expand All @@ -23,31 +24,24 @@ public override int Execute()
{
// Check the entry point file path.
string file = Path.GetFullPath(_file);
if (!VirtualProjectBuildingCommand.IsValidEntryPointPath(file))
if (!VirtualProjectBuilder.IsValidEntryPointPath(file))
{
throw new GracefulException(CliCommandStrings.InvalidFilePath, file);
}

string targetDirectory = DetermineOutputDirectory(file);

// Find directives (this can fail, so do this before creating the target directory).
var sourceFile = SourceFile.Load(file);
var directives = FileLevelDirectiveHelpers.FindDirectives(sourceFile, reportAllErrors: !_force, VirtualProjectBuildingCommand.ThrowingReporter);

// Create a project instance for evaluation.
var projectCollection = new ProjectCollection();
var command = new VirtualProjectBuildingCommand(
entryPointFileFullPath: file,
msbuildArgs: MSBuildArgs.FromOtherArgs([]))
{
Directives = directives,
};
var projectInstance = command.CreateProjectInstance(projectCollection);

// Evaluate directives.
directives = VirtualProjectBuildingCommand.EvaluateDirectives(projectInstance, directives, sourceFile, VirtualProjectBuildingCommand.ThrowingReporter);
command.Directives = directives;
projectInstance = command.CreateProjectInstance(projectCollection);
var builder = new VirtualProjectBuilder(file, VirtualProjectBuildingCommand.TargetFrameworkVersion);

builder.CreateProjectInstance(
projectCollection,
VirtualProjectBuildingCommand.ThrowingReporter,
out var projectInstance,
out var evaluatedDirectives,
validateAllDirectives: !_force);

// Find other items to copy over, e.g., default Content items like JSON files in Web apps.
var includeItems = FindIncludedItems().ToList();
Expand All @@ -66,7 +60,7 @@ public override int Execute()
}
else
{
VirtualProjectBuildingCommand.RemoveDirectivesFromFile(directives, sourceFile.Text, targetFile);
VirtualProjectBuilder.RemoveDirectivesFromFile(evaluatedDirectives, builder.EntryPointSourceFile.Text, targetFile);
}

// Create project file.
Expand All @@ -79,9 +73,9 @@ public override int Execute()
{
using var stream = File.Open(projectFile, FileMode.Create, FileAccess.Write);
using var writer = new StreamWriter(stream, Encoding.UTF8);
VirtualProjectBuildingCommand.WriteProjectFile(writer, UpdateDirectives(directives), isVirtualProject: false,
VirtualProjectBuilder.WriteProjectFile(writer, UpdateDirectives(evaluatedDirectives), isVirtualProject: false,
userSecretsId: DetermineUserSecretsId(),
excludeDefaultProperties: FindDefaultPropertiesToExclude());
defaultProperties: GetDefaultProperties());
}

// Copy or move over included items.
Expand Down Expand Up @@ -230,14 +224,14 @@ ImmutableArray<CSharpDirective> UpdateDirectives(ImmutableArray<CSharpDirective>
return result.DrainToImmutable();
}

IEnumerable<string> FindDefaultPropertiesToExclude()
IEnumerable<(string name, string value)> GetDefaultProperties()
{
foreach (var (name, defaultValue) in VirtualProjectBuildingCommand.DefaultProperties)
foreach (var (name, defaultValue) in VirtualProjectBuilder.GetDefaultProperties(VirtualProjectBuildingCommand.TargetFrameworkVersion))
{
string projectValue = projectInstance.GetPropertyValue(name);
if (!string.Equals(projectValue, defaultValue, StringComparison.OrdinalIgnoreCase))
if (string.Equals(projectValue, defaultValue, StringComparison.OrdinalIgnoreCase))
{
yield return name;
yield return (name, defaultValue);
}
}
}
Expand Down
18 changes: 11 additions & 7 deletions src/Cli/dotnet/Commands/Run/Api/RunApiCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Text.Json.Serialization;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.FileBasedPrograms;
using Microsoft.DotNet.ProjectTools;

namespace Microsoft.DotNet.Cli.Commands.Run.Api;

Expand Down Expand Up @@ -66,10 +67,16 @@ public override RunApiOutput Execute()
{
var sourceFile = SourceFile.Load(EntryPointFileFullPath);
var directives = FileLevelDirectiveHelpers.FindDirectives(sourceFile, reportAllErrors: true, ErrorReporters.CreateCollectingReporter(out var diagnostics));
string artifactsPath = ArtifactsPath ?? VirtualProjectBuildingCommand.GetArtifactsPath(EntryPointFileFullPath);
string artifactsPath = ArtifactsPath ?? VirtualProjectBuilder.GetArtifactsPath(EntryPointFileFullPath);

var csprojWriter = new StringWriter();
VirtualProjectBuildingCommand.WriteProjectFile(csprojWriter, directives, isVirtualProject: true, targetFilePath: EntryPointFileFullPath, artifactsPath: artifactsPath);
VirtualProjectBuilder.WriteProjectFile(
csprojWriter,
directives,
VirtualProjectBuilder.GetDefaultProperties(VirtualProjectBuildingCommand.TargetFrameworkVersion),
isVirtualProject: true,
targetFilePath: EntryPointFileFullPath,
artifactsPath: artifactsPath);

return new RunApiOutput.Project
{
Expand All @@ -88,11 +95,8 @@ public override RunApiOutput Execute()
{
var msbuildArgs = MSBuildArgs.FromVerbosity(VerbosityOptions.quiet);
var buildCommand = new VirtualProjectBuildingCommand(
entryPointFileFullPath: EntryPointFileFullPath,
msbuildArgs: msbuildArgs)
{
CustomArtifactsPath = ArtifactsPath,
};
EntryPointFileFullPath, msbuildArgs, artifactsPath: ArtifactsPath);

buildCommand.MarkArtifactsFolderUsed();

var runCommand = new RunCommand(
Expand Down
7 changes: 4 additions & 3 deletions src/Cli/dotnet/Commands/Run/RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Microsoft.DotNet.Cli.Utils.Extensions;
using Microsoft.DotNet.FileBasedPrograms;
using Microsoft.DotNet.ProjectTools;
using Microsoft.DotNet.Utilities;

namespace Microsoft.DotNet.Cli.Commands.Run;

Expand Down Expand Up @@ -545,7 +546,7 @@ static void SetRootVariableName(ICommand command, string runtimeIdentifier, stri

static ICommand CreateCommandForCscBuiltProgram(string entryPointFileFullPath, string[] args)
{
var artifactsPath = VirtualProjectBuildingCommand.GetArtifactsPath(entryPointFileFullPath);
var artifactsPath = VirtualProjectBuilder.GetArtifactsPath(entryPointFileFullPath);
var exePath = Path.Join(artifactsPath, "bin", "debug", Path.GetFileNameWithoutExtension(entryPointFileFullPath) + FileNameSuffixes.CurrentPlatform.Exe);
var commandSpec = new CommandSpec(path: exePath, args: ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args));
var command = CommandFactoryUsingResolver.Create(commandSpec);
Expand Down Expand Up @@ -664,7 +665,7 @@ internal static void ThrowUnableToRunError(ProjectInstance project)

if (!readCodeFromStdin)
{
if (VirtualProjectBuildingCommand.IsValidEntryPointPath(arg))
if (VirtualProjectBuilder.IsValidEntryPointPath(arg))
{
arg = Path.GetFullPath(arg);
}
Expand Down Expand Up @@ -749,7 +750,7 @@ public static RunCommand FromParseResult(ParseResult parseResult)
// If '-' is specified as the input file, read all text from stdin into a temporary file and use that as the entry point.
// We create a new directory for each file so other files are not included in the compilation.
// We fail if the file already exists to avoid reusing the same file for multiple stdin runs (in case the random name is duplicate).
string directory = VirtualProjectBuildingCommand.GetTempSubpath(Path.GetRandomFileName());
string directory = VirtualProjectBuilder.GetTempSubpath(Path.GetRandomFileName());
VirtualProjectBuildingCommand.CreateTempSubdirectory(directory);
entryPointFilePath = Path.Join(directory, "app.cs");
using (var stdinStream = Console.OpenStandardInput())
Expand Down
1 change: 1 addition & 0 deletions src/Cli/dotnet/Commands/Run/RunTelemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.FileBasedPrograms;
using Microsoft.DotNet.ProjectTools;
using Microsoft.DotNet.Utilities;

namespace Microsoft.DotNet.Cli.Commands.Run;

Expand Down
Loading
Loading