Skip to content

Commit 36295f2

Browse files
authored
Merge pull request #149 from dotnet/more-msbuild-hosting
2 parents af60b90 + 2928143 commit 36295f2

20 files changed

+285
-252
lines changed

Directory.Build.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
<ItemGroup>
33
<PackageReference Include="Nerdbank.GitVersioning" PrivateAssets="all" IncludeAssets="build; buildMultitargeting" />
44
<PackageReference Include="Microsoft.VisualStudioEng.MicroBuild.Core" PrivateAssets="all" />
5-
<PackageReference Include="Microsoft.Net.Compilers.Toolset" PrivateAssets="all" />
65
</ItemGroup>
76
</Project>

Directory.Packages.props

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@
33
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
44
</PropertyGroup>
55
<ItemGroup>
6+
<PackageVersion Include="coverlet.collector" Version="3.1.2" />
7+
<PackageVersion Include="GitHubActionsTestLogger" Version="2.0.1" />
8+
<PackageVersion Include="Microsoft.Build.Locator" Version="1.5.5" />
69
<PackageVersion Include="Microsoft.Build.Utilities.Core" Version="17.3.1" />
710
<PackageVersion Include="Microsoft.Build" Version="17.3.1" />
8-
<PackageVersion Include="Microsoft.Net.Compilers.Toolset" Version="4.4.0-4.final" />
911
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
1012
<PackageVersion Include="Microsoft.VisualStudioEng.MicroBuild.Core" Version="1.0.0" />
1113
<PackageVersion Include="MSTest.TestAdapter" Version="2.3.0-preview-20220810-02" />
1214
<PackageVersion Include="MSTest.TestFramework" Version="2.2.10" />
1315
<PackageVersion Include="Nerdbank.GitVersioning" Version="3.5.109" />
14-
<PackageVersion Include="coverlet.collector" Version="3.1.2" />
15-
<PackageVersion Include="GitHubActionsTestLogger" Version="2.0.1" />
1616
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
17-
<PackageVersion Include="Microsoft.Build.Locator" Version="1.5.5" />
18-
<PackageVersion Include="Valleysoft.DockerCredsProvider" Version="2.1.0" />
17+
<PackageVersion Include="Valleysoft.DockerCredsProvider" Version="2.1.0" />
1918
</ItemGroup>
2019
</Project>

Microsoft.NET.Build.Containers/ContainerHelpers.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public enum PortType
1818

1919
public record Port(int number, PortType type);
2020

21+
2122
public static class ContainerHelpers
2223
{
2324
public const string HostObjectUser = "SDK_CONTAINER_REGISTRY_UNAME";

Microsoft.NET.Build.Containers/CreateNewImageToolTask.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,4 @@ private string Quote(string path)
9696

9797
return $"\"{path}\"";
9898
}
99-
}
99+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace Microsoft.NET.Build.Containers;
2+
3+
public static class KnownStrings
4+
{
5+
public static class Properties
6+
{
7+
public static string ContainerBaseImage = nameof(ContainerBaseImage);
8+
public static string ContainerRegistry = nameof(ContainerRegistry);
9+
public static string ContainerImageName = nameof(ContainerImageName);
10+
public static string ContainerImageTag = nameof(ContainerImageTag);
11+
public static string ContainerImageTags = nameof(ContainerImageTags);
12+
public static string ContainerWorkingDirectory = nameof(ContainerWorkingDirectory);
13+
public static string ContainerEntrypoint = nameof(ContainerEntrypoint);
14+
public static string UseAppHost = nameof(UseAppHost);
15+
public static string ContainerLabel = nameof(ContainerLabel);
16+
public static string SelfContained = nameof(SelfContained);
17+
public static string ContainerPort = nameof(ContainerPort);
18+
public static string ContainerEnvironmentVariable = nameof(ContainerEnvironmentVariable);
19+
20+
public static string ComputeContainerConfig = nameof(ComputeContainerConfig);
21+
public static string AssemblyName = nameof(AssemblyName);
22+
public static string ContainerBaseRegistry = nameof(ContainerBaseRegistry);
23+
public static string ContainerBaseName = nameof(ContainerBaseName);
24+
public static string ContainerBaseTag = nameof(ContainerBaseTag);
25+
}
26+
27+
public static class ErrorCodes
28+
{
29+
public static string CONTAINER001 = nameof(CONTAINER001);
30+
public static string CONTAINER004 = nameof(CONTAINER004);
31+
public static string CONTAINER005 = nameof(CONTAINER005);
32+
}
33+
}

Microsoft.NET.Build.Containers/Microsoft.NET.Build.Containers.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<ItemGroup Condition="'$(TargetFramework)' == 'net472'">
3030
<Compile Remove="*.*" />
3131
<Compile Include="ReferenceParser.cs" />
32+
<Compile Include="KnownStrings.cs" />
3233
<Compile Include="ParseContainerProperties.cs" />
3334
<Compile Include="CreateNewImage.Interface.cs" />
3435
<Compile Include="CreateNewImageToolTask.cs" />

Microsoft.NET.Build.Containers/ParseContainerProperties.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,15 @@ public override bool Execute()
115115
else
116116
{
117117
validTags = Array.Empty<string>();
118-
Log.LogError(null, "CONTAINER003", "Container.InvalidTag", null, 0, 0, 0, 0, $"Invalid {nameof(ContainerImageTag)} provided: {{0}}. Image tags must be alphanumeric, underscore, hyphen, or period.", ContainerImageTag);
118+
Log.LogError(null, KnownStrings.ErrorCodes.CONTAINER004, "Container.InvalidTag", null, 0, 0, 0, 0, "Invalid {0} provided: {1}. Image tags must be alphanumeric, underscore, hyphen, or period.", nameof(ContainerImageTag), ContainerImageTag);
119119
}
120120
}
121121
else if (ContainerImageTags.Length != 0 && TryValidateTags(ContainerImageTags, out var valids, out var invalids))
122122
{
123123
validTags = valids;
124124
if (invalids.Any())
125125
{
126-
(string message, string args) = invalids switch
127-
{
128-
{ Length: 1 } => ($"Invalid {nameof(ContainerImageTags)} provided: {{0}}. {nameof(ContainerImageTags)} must be a semicolon-delimited list of valid image tags. Image tags must be alphanumeric, underscore, hyphen, or period.", invalids[0]),
129-
_ => ($"Invalid {nameof(ContainerImageTags)} provided: {{0}}. {nameof(ContainerImageTags)} must be a semicolon-delimited list of valid image tags. Image tags must be alphanumeric, underscore, hyphen, or period.", String.Join(", ", invalids))
130-
};
131-
Log.LogError(null, "CONTAINER003", "Container.InvalidTag", null, 0, 0, 0, 0, message, args);
126+
Log.LogError(null, KnownStrings.ErrorCodes.CONTAINER004, "Container.InvalidTag", null, 0, 0, 0, 0, "Invalid {0} provided: {1}. {0} must be a semicolon-delimited list of valid image tags. Image tags must be alphanumeric, underscore, hyphen, or period.", nameof(ContainerImageTags), String.Join(",", invalids));
132127
return !Log.HasLoggedErrors;
133128
}
134129
}
@@ -163,10 +158,10 @@ public override bool Execute()
163158

164159
try
165160
{
166-
if (!ContainerHelpers.NormalizeImageName(ContainerImageName, out string? normalizedImageName))
161+
if (!ContainerHelpers.NormalizeImageName(ContainerImageName, out var normalizedImageName))
167162
{
168-
Log.LogMessage(MessageImportance.High, $"'{ContainerImageName}' was not a valid container image name, it was normalized to {normalizedImageName}");
169-
NewContainerImageName = normalizedImageName ?? "";
163+
Log.LogMessage(null, KnownStrings.ErrorCodes.CONTAINER001, "Container.InvalidImageName", null, 0, 0, 0, 0, MessageImportance.High, "'{0}' was not a valid container image name, it was normalized to '{1}'", nameof(ContainerImageName), normalizedImageName);
164+
NewContainerImageName = normalizedImageName!; // known to be not null due to output of NormalizeImageName
170165
}
171166
else
172167
{
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Microsoft.Build.Framework;
2+
3+
namespace Test.Microsoft.NET.Build.Containers;
4+
5+
public class CapturingLogger : ILogger
6+
{
7+
public LoggerVerbosity Verbosity { get => LoggerVerbosity.Diagnostic; set { } }
8+
public string Parameters { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
9+
10+
private List<BuildMessageEventArgs> _messages = new();
11+
public IReadOnlyList<BuildMessageEventArgs> Messages {get { return _messages; } }
12+
13+
private List<BuildWarningEventArgs> _warnings = new();
14+
public IReadOnlyList<BuildWarningEventArgs> Warnings {get { return _warnings; } }
15+
16+
private List<BuildErrorEventArgs> _errors = new();
17+
public IReadOnlyList<BuildErrorEventArgs> Errors {get { return _errors; } }
18+
19+
public void Initialize(IEventSource eventSource)
20+
{
21+
eventSource.MessageRaised += (o, e) => _messages.Add(e);
22+
eventSource.WarningRaised += (o, e) => _warnings.Add(e);
23+
eventSource.ErrorRaised += (o, e) => _errors.Add(e);
24+
}
25+
26+
27+
public void Shutdown()
28+
{
29+
}
30+
}

Test.Microsoft.NET.Build.Containers.Filesystem/CreateNewImageTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void CreateNewImage_Baseline()
4444
CreateNewImage task = new CreateNewImage();
4545
task.BaseRegistry = "mcr.microsoft.com";
4646
task.BaseImageName = "dotnet/runtime";
47-
task.BaseImageTag = "6.0";
47+
task.BaseImageTag = "7.0";
4848

4949
task.OutputRegistry = "localhost:5010";
5050
task.PublishDirectory = Path.Combine(newProjectDir.FullName, "bin", "release", "net7.0");
@@ -91,15 +91,15 @@ public void ParseContainerProperties_EndToEnd()
9191
Assert.AreEqual(0, dotnetPublish.ExitCode);
9292

9393
ParseContainerProperties pcp = new ParseContainerProperties();
94-
pcp.FullyQualifiedBaseImageName = "mcr.microsoft.com/dotnet/runtime:6.0";
94+
pcp.FullyQualifiedBaseImageName = "mcr.microsoft.com/dotnet/runtime:7.0";
9595
pcp.ContainerRegistry = "localhost:5010";
9696
pcp.ContainerImageName = "dotnet/testimage";
97-
pcp.ContainerImageTags = new [] {"5.0", "latest"};
97+
pcp.ContainerImageTags = new [] { "5.0", "latest"};
9898

9999
Assert.IsTrue(pcp.Execute());
100100
Assert.AreEqual("mcr.microsoft.com", pcp.ParsedContainerRegistry);
101101
Assert.AreEqual("dotnet/runtime", pcp.ParsedContainerImage);
102-
Assert.AreEqual("6.0", pcp.ParsedContainerTag);
102+
Assert.AreEqual("7.0", pcp.ParsedContainerTag);
103103

104104
Assert.AreEqual("dotnet/testimage", pcp.NewContainerImageName);
105105
CollectionAssert.AreEquivalent(new []{ "5.0", "latest"}, pcp.NewContainerTags);

Test.Microsoft.NET.Build.Containers.Filesystem/CurrentFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ public static class CurrentFile
77
public static string Path([CallerFilePath] string file = "") => file;
88

99
public static string Relative(string relative, [CallerFilePath] string file = "") {
10-
return global::System.IO.Path.Combine(global::System.IO.Path.GetDirectoryName(file), relative);
10+
return global::System.IO.Path.Combine(global::System.IO.Path.GetDirectoryName(file)!, relative); // file known to be not-null due to the mechanics of CallerFilePath
1111
}
1212
}

0 commit comments

Comments
 (0)