-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add msbuild task to generate binary runtimeconfig format #49544
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1c1c6d1
Add msbuild task to generate binary runtimeconfig format
fanyang-mono 75f4076
Update property name due to naming conversion.
fanyang-mono 8ccb9c1
Fixed more formatting issue
fanyang-mono 47c0192
Fixed one more naming convention
fanyang-mono db366c9
Update src/tasks/RuntimeConfigParser/RuntimeConfigParser.cs
fanyang-mono 3a3a63a
Update src/tasks/RuntimeConfigParser/RuntimeConfigParser.cs
fanyang-mono 24553e3
Update src/tasks/RuntimeConfigParser/RuntimeConfigParser.cs
fanyang-mono fc22558
Fixed comments
fanyang-mono 4f6ac11
Update src/tasks/RuntimeConfigParser/RuntimeConfigParser.cs
fanyang-mono 218bca6
Fix error handling
fanyang-mono File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using System.Reflection.Metadata; | ||
| using Microsoft.Build.Framework; | ||
| using Microsoft.Build.Utilities; | ||
|
|
||
| public class RuntimeConfigParserTask : Task | ||
| { | ||
| /// <summary> | ||
| /// The path to runtimeconfig.json file. | ||
| /// </summary> | ||
| [Required] | ||
| public string RuntimeConfigFile { get; set; } = ""; | ||
|
|
||
| /// <summary> | ||
| /// The path to the output binary file. | ||
| /// </summary> | ||
| [Required] | ||
| public string OutputFile { get; set; } = ""; | ||
|
|
||
| /// <summary> | ||
| /// List of properties reserved for the host. | ||
| /// </summary> | ||
| public ITaskItem[] ReservedProperties { get; set; } = Array.Empty<ITaskItem>(); | ||
|
|
||
| public override bool Execute() | ||
| { | ||
| if (string.IsNullOrEmpty(RuntimeConfigFile)) | ||
| { | ||
| Log.LogError($"'{nameof(RuntimeConfigFile)}' is required."); | ||
| } | ||
|
|
||
| if (string.IsNullOrEmpty(OutputFile)) | ||
| { | ||
| Log.LogError($"'{nameof(OutputFile)}' is required."); | ||
| } | ||
|
|
||
| Dictionary<string, string> configProperties = ConvertInputToDictionary(RuntimeConfigFile); | ||
|
|
||
| if (ReservedProperties.Length != 0) | ||
| { | ||
| CheckDuplicateProperties(configProperties, ReservedProperties); | ||
| } | ||
|
|
||
| var blobBuilder = new BlobBuilder(); | ||
| ConvertDictionaryToBlob(configProperties, blobBuilder); | ||
|
|
||
| using var stream = File.OpenWrite(OutputFile); | ||
| blobBuilder.WriteContentTo(stream); | ||
|
|
||
| return !Log.HasLoggedErrors; | ||
| } | ||
|
|
||
| /// Reads a json file from the given path and extracts the "configProperties" key (assumed to be a string to string dictionary) | ||
| private Dictionary<string, string> ConvertInputToDictionary(string inputFilePath) | ||
| { | ||
| var options = new JsonSerializerOptions { | ||
| AllowTrailingCommas = true, | ||
| ReadCommentHandling = JsonCommentHandling.Skip, | ||
| }; | ||
|
|
||
| var jsonString = File.ReadAllText(inputFilePath); | ||
| var parsedJson = JsonSerializer.Deserialize<Root>(jsonString, options); | ||
|
|
||
| if (parsedJson == null) | ||
| { | ||
| throw new ArgumentException("Wasn't able to parse the json file successfully."); | ||
| } | ||
|
|
||
| return parsedJson.ConfigProperties; | ||
| } | ||
|
|
||
| /// Just write the dictionary out to a blob as a count followed by | ||
| /// a length-prefixed UTF8 encoding of each key and value | ||
| private void ConvertDictionaryToBlob(IReadOnlyDictionary<string, string> properties, BlobBuilder builder) | ||
| { | ||
| int count = properties.Count; | ||
|
|
||
| builder.WriteCompressedInteger(count); | ||
| foreach (var kvp in properties) | ||
| { | ||
| builder.WriteSerializedString(kvp.Key); | ||
| builder.WriteSerializedString(kvp.Value); | ||
| } | ||
| } | ||
|
|
||
| private void CheckDuplicateProperties(IReadOnlyDictionary<string, string> properties, ITaskItem[] keys) | ||
| { | ||
| foreach (var key in keys) | ||
| { | ||
| if (properties.ContainsKey(key.ItemSpec)) | ||
| { | ||
| throw new ArgumentException($"Property '{key}' can't be set by the user!"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public class Root | ||
| { | ||
| // the configProperties key | ||
| [JsonPropertyName("configProperties")] | ||
| public Dictionary<string, string> ConfigProperties { get; set; } = new Dictionary<string, string>(); | ||
| // everything other than configProperties | ||
| [JsonExtensionData] | ||
| public Dictionary<string, object> ExtensionData { get; set; } = new Dictionary<string, object>(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <TargetFramework>$(NetCoreAppToolCurrent)</TargetFramework> | ||
| <OutputType>Library</OutputType> | ||
| <TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
| <EnableDefaultCompileItems>false</EnableDefaultCompileItems> | ||
| <Nullable>enable</Nullable> | ||
| <NoWarn>$(NoWarn),CA1050</NoWarn> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Build" Version="$(RefOnlyMicrosoftBuildVersion)" /> | ||
| <PackageReference Include="Microsoft.Build.Framework" Version="$(RefOnlyMicrosoftBuildFrameworkVersion)" /> | ||
| <PackageReference Include="Microsoft.Build.Tasks.Core" Version="$(RefOnlyMicrosoftBuildTasksCoreVersion)" /> | ||
| <PackageReference Include="Microsoft.Build.Utilities.Core" Version="$(RefOnlyMicrosoftBuildUtilitiesCoreVersion)" /> | ||
| <PackageReference Include="System.Reflection.Metadata" Version="5.0.0" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <Compile Include="RuntimeConfigParser.cs" /> | ||
| </ItemGroup> | ||
|
|
||
| <Target Name="PublishBuilder" | ||
| AfterTargets="Build" | ||
| DependsOnTargets="Publish" /> | ||
|
|
||
| <Target Name="GetFilesToPackage" /> | ||
|
|
||
| </Project> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.