Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle self-contained apps #194

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Buildalyzer/Construction/IProjectFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public interface IProjectFile
{
bool ContainsPackageReferences { get; }
bool IsMultiTargeted { get; }
bool IsSelfContained { get; }
IReadOnlyList<IPackageReference> PackageReferences { get; }
string Path { get; }
string Name { get; }
Expand Down
2 changes: 2 additions & 0 deletions src/Buildalyzer/Construction/ProjectFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ internal ProjectFile(string path)
/// </remarks>
public bool IsMultiTargeted => _projectElement.GetDescendants(ProjectFileNames.TargetFrameworks).Any();

public bool IsSelfContained => _projectElement.GetDescendants(ProjectFileNames.SelfContained).Any();

/// <summary>
/// Whether the project file contains <c>PackageReference</c> items.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/Buildalyzer/Construction/ProjectFileNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public static class ProjectFileNames
public const string Import = nameof(Import);
public const string PackageReference = nameof(PackageReference);
public const string ToolsVersion = nameof(ToolsVersion);
public const string SelfContained = nameof(SelfContained);
public const string LanguageTargets = nameof(LanguageTargets);

// Attributes
Expand Down
35 changes: 14 additions & 21 deletions src/Buildalyzer/Environment/BuildEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ public sealed class BuildEnvironment
/// </remarks>
public bool DesignTime { get; }

/// <summary>
/// Indicates that the app is self-contained.
/// </summary>
/// <remarks>
/// See https://docs.microsoft.com/en-us/dotnet/core/deploying/.
/// </remarks>
public bool SelfContained { get; }

/// <summary>
/// Runs the restore target prior to any other targets using the MSBuild <c>restore</c> switch.
/// </summary>
Expand All @@ -54,6 +62,7 @@ public sealed class BuildEnvironment
public BuildEnvironment(
bool designTime,
bool restore,
bool isSelfContained,
string[] targetsToBuild,
string msBuildExePath,
string dotnetExePath,
Expand Down Expand Up @@ -101,7 +110,11 @@ public BuildEnvironment(
_globalProperties.Add(MsBuildProperties.AddModules, "false");
_globalProperties.Add(MsBuildProperties.UseCommonOutputDirectory, "true"); // This is used in a condition to prevent copying in _CopyFilesMarkedCopyLocal
_globalProperties.Add(MsBuildProperties.GeneratePackageOnBuild, "false"); // Prevent NuGet.Build.Tasks.Pack.targets from running the pack targets (since we didn't build anything)
_globalProperties.Add(MsBuildProperties.UseAppHost, "false"); // Prevent creation of native host executable https://docs.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#useapphost

if (!isSelfContained)
{
_globalProperties.Add(MsBuildProperties.UseAppHost, "false"); // Prevent creation of native host executable https://docs.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#useapphost
}
}
_additionalGlobalProperties = CopyItems(_globalProperties, additionalGlobalProperties);

Expand All @@ -124,25 +137,5 @@ private Dictionary<string, string> CopyItems(Dictionary<string, string> destinat
}
return null;
}

/// <summary>
/// Clones the build environment with a different set of build targets.
/// </summary>
/// <param name="targets">
/// The targets that should be used to build the project.
/// Specifying an empty array indicates that the <see cref="ProjectAnalyzer"/> should
/// return a <see cref="Microsoft.Build.Execution.ProjectInstance"/> without building the project.
/// </param>
/// <returns>A new build environment with the specified targets.</returns>
public BuildEnvironment WithTargetsToBuild(params string[] targets) =>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the thinking behind removing this clone method - was it a mistake or am I missing the reason it needed to come out?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this method needs to be otherwise adjusted in the light of the changes above. Which is doable but looks like it's not used anywhere... Moreover, I tracked the git history and looks like it wasn't used even once added (in a quite a big commit) so I though it was some temp convenience at some point not needed currently anymore.

new BuildEnvironment(
DesignTime,
Restore,
targets,
MsBuildExePath,
DotnetExePath,
Arguments,
_additionalGlobalProperties,
_additionalEnvironmentVariables);
}
}
2 changes: 2 additions & 0 deletions src/Buildalyzer/Environment/EnvironmentFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ private BuildEnvironment CreateCoreEnvironment(EnvironmentOptions options)
return new BuildEnvironment(
options.DesignTime,
options.Restore,
_projectFile.IsSelfContained,
options.TargetsToBuild.ToArray(),
msBuildExePath,
options.DotnetExePath,
Expand Down Expand Up @@ -149,6 +150,7 @@ private BuildEnvironment CreateFrameworkEnvironment(EnvironmentOptions options)
return new BuildEnvironment(
options.DesignTime,
options.Restore,
_projectFile.IsSelfContained,
options.TargetsToBuild.ToArray(),
msBuildExePath,
options.DotnetExePath,
Expand Down
18 changes: 18 additions & 0 deletions tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,24 @@ public void BuildsProject(
results.ShouldAllBe(x => x.Succeeded, log.ToString());
}

[Test]
public void HandlesSelfContainedApp()
psfinaki marked this conversation as resolved.
Show resolved Hide resolved
{
string projectFile = @"SelfContainedApp\SelfContainedApp.csproj";

// Given
StringWriter log = new StringWriter();
IProjectAnalyzer analyzer = GetProjectAnalyzer(projectFile, log);

// When
DeleteProjectDirectory(projectFile, "obj");
DeleteProjectDirectory(projectFile, "bin");
IAnalyzerResults results = analyzer.Build();

// Then
results.Single().SourceFiles.Count().ShouldBeGreaterThan(0);
}

[Test]
public void GetsSourceFiles(
[ValueSource(nameof(Preferences))] EnvironmentPreference preference,
Expand Down
2 changes: 2 additions & 0 deletions tests/projects/SelfContainedApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
12 changes: 12 additions & 0 deletions tests/projects/SelfContainedApp/SelfContainedApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>

</Project>
6 changes: 6 additions & 0 deletions tests/projects/TestProjects.sln
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SdkNet5Project", "SdkNet5Pr
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SdkNet6Project", "SdkNet6Project\SdkNet6Project.csproj", "{54D4DF7E-CD98-4F46-961A-A006C5C1D03C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SelfContainedApp", "SelfContainedApp\SelfContainedApp.csproj", "{2605A734-42F5-4255-92E9-F438E0CDB8FE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -130,6 +132,10 @@ Global
{54D4DF7E-CD98-4F46-961A-A006C5C1D03C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{54D4DF7E-CD98-4F46-961A-A006C5C1D03C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{54D4DF7E-CD98-4F46-961A-A006C5C1D03C}.Release|Any CPU.Build.0 = Release|Any CPU
{2605A734-42F5-4255-92E9-F438E0CDB8FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2605A734-42F5-4255-92E9-F438E0CDB8FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2605A734-42F5-4255-92E9-F438E0CDB8FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2605A734-42F5-4255-92E9-F438E0CDB8FE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down