Skip to content

Commit

Permalink
Merge pull request dotnet#149 from dotnet/more-msbuild-hosting
Browse files Browse the repository at this point in the history
  • Loading branch information
baronfel authored Jan 16, 2023
2 parents 592c814 + b54b9e5 commit cfe6559
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 21 deletions.
1 change: 1 addition & 0 deletions Microsoft.NET.Build.Containers/ContainerHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public enum PortType

public record Port(int number, PortType type);


public static class ContainerHelpers
{
public const string HostObjectUser = "SDK_CONTAINER_REGISTRY_UNAME";
Expand Down
2 changes: 1 addition & 1 deletion Microsoft.NET.Build.Containers/CreateNewImageToolTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,4 @@ private string Quote(string path)

return $"\"{path}\"";
}
}
}
33 changes: 33 additions & 0 deletions Microsoft.NET.Build.Containers/KnownStrings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Microsoft.NET.Build.Containers;

public static class KnownStrings
{
public static class Properties
{
public static string ContainerBaseImage = nameof(ContainerBaseImage);
public static string ContainerRegistry = nameof(ContainerRegistry);
public static string ContainerImageName = nameof(ContainerImageName);
public static string ContainerImageTag = nameof(ContainerImageTag);
public static string ContainerImageTags = nameof(ContainerImageTags);
public static string ContainerWorkingDirectory = nameof(ContainerWorkingDirectory);
public static string ContainerEntrypoint = nameof(ContainerEntrypoint);
public static string UseAppHost = nameof(UseAppHost);
public static string ContainerLabel = nameof(ContainerLabel);
public static string SelfContained = nameof(SelfContained);
public static string ContainerPort = nameof(ContainerPort);
public static string ContainerEnvironmentVariable = nameof(ContainerEnvironmentVariable);

public static string ComputeContainerConfig = nameof(ComputeContainerConfig);
public static string AssemblyName = nameof(AssemblyName);
public static string ContainerBaseRegistry = nameof(ContainerBaseRegistry);
public static string ContainerBaseName = nameof(ContainerBaseName);
public static string ContainerBaseTag = nameof(ContainerBaseTag);
}

public static class ErrorCodes
{
public static string CONTAINER001 = nameof(CONTAINER001);
public static string CONTAINER004 = nameof(CONTAINER004);
public static string CONTAINER005 = nameof(CONTAINER005);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<ItemGroup Condition="'$(TargetFramework)' == 'net472'">
<Compile Remove="*.*" />
<Compile Include="ReferenceParser.cs" />
<Compile Include="KnownStrings.cs" />
<Compile Include="ParseContainerProperties.cs" />
<Compile Include="CreateNewImage.Interface.cs" />
<Compile Include="CreateNewImageToolTask.cs" />
Expand Down
15 changes: 5 additions & 10 deletions Microsoft.NET.Build.Containers/ParseContainerProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,15 @@ public override bool Execute()
else
{
validTags = Array.Empty<string>();
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);
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);
}
}
else if (ContainerImageTags.Length != 0 && TryValidateTags(ContainerImageTags, out var valids, out var invalids))
{
validTags = valids;
if (invalids.Any())
{
(string message, string args) = invalids switch
{
{ 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]),
_ => ($"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))
};
Log.LogError(null, "CONTAINER003", "Container.InvalidTag", null, 0, 0, 0, 0, message, args);
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));
return !Log.HasLoggedErrors;
}
}
Expand Down Expand Up @@ -163,10 +158,10 @@ public override bool Execute()

try
{
if (!ContainerHelpers.NormalizeImageName(ContainerImageName, out string? normalizedImageName))
if (!ContainerHelpers.NormalizeImageName(ContainerImageName, out var normalizedImageName))
{
Log.LogMessage(MessageImportance.High, $"'{ContainerImageName}' was not a valid container image name, it was normalized to {normalizedImageName}");
NewContainerImageName = normalizedImageName ?? "";
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);
NewContainerImageName = normalizedImageName!; // known to be not null due to output of NormalizeImageName
}
else
{
Expand Down
16 changes: 8 additions & 8 deletions packaging/build/Microsoft.NET.Build.Containers.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
<PropertyGroup>
<!-- A flag representing this package existing in a project. -->
<SDKContainerSupportEnabled>true</SDKContainerSupportEnabled>
<taskFoldername>tasks</taskFoldername>
<taskFramework Condition="'$(MSBuildRuntimeType)' == 'Core'">net7.0</taskFramework>
<taskFramework Condition="'$(MSBuildRuntimeType)' == 'Full'">net472</taskFramework>
<ContainerTaskFolderName>tasks</ContainerTaskFolderName>
<ContainerTaskFramework Condition="'$(MSBuildRuntimeType)' == 'Core'">net7.0</ContainerTaskFramework>
<ContainerTaskFramework Condition="'$(MSBuildRuntimeType)' == 'Full'">net472</ContainerTaskFramework>
<ContainerizeFolderName>containerize</ContainerizeFolderName>
<!--The folder where the custom task will be present. It points to inside the nuget package. -->
<CustomTasksFolder>$(MSBuildThisFileDirectory)..\$(taskFoldername)\$(taskFramework)\</CustomTasksFolder>
<ContainerCustomTasksFolder>$(MSBuildThisFileDirectory)..\$(ContainerTaskFolderName)\$(ContainerTaskFramework)\</ContainerCustomTasksFolder>
<ContainerizeFolder>$(MSBuildThisFileDirectory)..\$(ContainerizeFolderName)\</ContainerizeFolder>
<!--Reference to the assembly which contains the MSBuild Task-->
<CustomTasksAssembly Condition="'$(CustomTasksAssembly)' == ''">$(CustomTasksFolder)$(MSBuildThisFileName).dll</CustomTasksAssembly>
<ContainerCustomTasksAssembly Condition="'$(ContainerCustomTasksAssembly)' == ''">$(ContainerCustomTasksFolder)$(MSBuildThisFileName).dll</ContainerCustomTasksAssembly>
</PropertyGroup>

<!--Register our custom task-->
<UsingTask TaskName="$(MSBuildThisFileName).Tasks.ParseContainerProperties" AssemblyFile="$(CustomTasksAssembly)"/>
<UsingTask TaskName="$(MSBuildThisFileName).Tasks.CreateNewImage" AssemblyFile="$(CustomTasksAssembly)"/>
</Project>
<UsingTask TaskName="$(MSBuildThisFileName).Tasks.CreateNewImage" AssemblyFile="$(ContainerCustomTasksAssembly)"/>
<UsingTask TaskName="$(MSBuildThisFileName).Tasks.ParseContainerProperties" AssemblyFile="$(ContainerCustomTasksAssembly)"/>
</Project>
2 changes: 1 addition & 1 deletion packaging/build/Microsoft.NET.Build.Containers.targets
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<ContainerImageName Condition="'$(ContainerImageName)' == ''">$(AssemblyName)</ContainerImageName>
<!-- Only default a tag name if no tag names at all are provided -->
<ContainerImageTag Condition="'$(ContainerImageTag)' == '' and '$(ContainerImageTags)' == ''">$(Version)</ContainerImageTag>
<ContainerImageTag Condition="'$(AutoGenerateImageTag)' == 'true'">$([System.DateTime]::UtcNow.ToString('yyyyMMddhhmmss'))</ContainerImageTag>
<ContainerImageTag Condition="'$(AutoGenerateImageTag)' == 'true' and '$(ContainerImageTags)' == ''">$([System.DateTime]::UtcNow.ToString('yyyyMMddhhmmss'))</ContainerImageTag>
<ContainerWorkingDirectory Condition="'$(ContainerWorkingDirectory)' == ''">/app</ContainerWorkingDirectory>
<!-- Could be semicolon-delimited -->
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion packaging/package.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@
<Copy SourceFiles="$(OutDir)../Microsoft.NET.Build.Containers.$(Version).nupkg"
DestinationFiles="../Test.Microsoft.NET.Build.Containers.Filesystem/package/Microsoft.NET.Build.Containers.$(Version).nupkg" />
</Target>
</Project>
</Project>

0 comments on commit cfe6559

Please sign in to comment.