Skip to content

Commit

Permalink
Annotate MSBuildFileSetResult with data contract attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Nov 18, 2022
1 parent f7afe08 commit 1374e63
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 12 deletions.
20 changes: 20 additions & 0 deletions src/BuiltInTools/DotNetWatchTasks/Utilities/IsExternalInit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// Copied from:
// https://github.com/dotnet/runtime/blob/218ef0f7776c2c20f6c594e3475b80f1fe2d7d08/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/IsExternalInit.cs

using System.ComponentModel;

namespace System.Runtime.CompilerServices
{
/// <summary>
/// Reserved to be used by the compiler for tracking metadata.
/// This class should not be used by developers in source code.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
internal static class IsExternalInit
{
}
}
40 changes: 28 additions & 12 deletions src/BuiltInTools/dotnet-watch/Internal/MSBuildFileSetResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,55 @@

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Microsoft.DotNet.Watcher.Internal
{
[DataContract]
internal sealed class MSBuildFileSetResult
{
public string RunCommand { get; set; }
[DataMember]
public string RunCommand { get; init; }

public string RunArguments { get; set; }
[DataMember]
public string RunArguments { get; init; }

public string RunWorkingDirectory { get; set; }
[DataMember]
public string RunWorkingDirectory { get; init; }

public bool IsNetCoreApp { get; set; }
[DataMember]
public bool IsNetCoreApp { get; init; }

public string TargetFrameworkVersion { get; set; }
[DataMember]
public string TargetFrameworkVersion { get; init; }

public string RuntimeIdentifier { get; set; }
[DataMember]
public string RuntimeIdentifier { get; init; }

public string DefaultAppHostRuntimeIdentifier { get; set; }
[DataMember]
public string DefaultAppHostRuntimeIdentifier { get; init; }

public Dictionary<string, ProjectItems> Projects { get; set; }
[DataMember]
public Dictionary<string, ProjectItems> Projects { get; init; }
}

[DataContract]
internal sealed class ProjectItems
{
public List<string> Files { get; set; } = new();
[DataMember]
public List<string> Files { get; init; } = new();

public List<StaticFileItem> StaticFiles { get; set; } = new();
[DataMember]
public List<StaticFileItem> StaticFiles { get; init; } = new();
}

[DataContract]
internal sealed class StaticFileItem
{
public string FilePath { get; set; }
[DataMember]
public string FilePath { get; init; }

public string StaticWebAssetPath { get; set; }
[DataMember]
public string StaticWebAssetPath { get; init; }
}
}
49 changes: 49 additions & 0 deletions src/Tests/dotnet-watch.Tests/FileSetSerializerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.DotNet.Watcher.Internal;
using Xunit;

namespace Microsoft.DotNet.Watcher.Tools;

public class FileSetSerializerTests
{
[Fact]
public async Task Serialize()
{
var fileSetResult = new MSBuildFileSetResult()
{
IsNetCoreApp = true,
TargetFrameworkVersion = "net5.0",
RuntimeIdentifier = "win-arm64",
DefaultAppHostRuntimeIdentifier = "",
RunCommand = "run",
RunArguments = "args",
RunWorkingDirectory = "dir",
Projects = new() { { "proj", new() { Files = new() { "a.cs", "b.cs" }, StaticFiles = new() { new() { FilePath = "path1", StaticWebAssetPath = "path2" } } } } }
};

using var stream = new MemoryStream();

var serializer = new DataContractJsonSerializer(fileSetResult.GetType(), new DataContractJsonSerializerSettings
{
UseSimpleDictionaryFormat = true,
});

using (var writer = JsonReaderWriterFactory.CreateJsonWriter(stream, Encoding.UTF8, ownsStream: false, indent: true))
{
serializer.WriteObject(writer, fileSetResult);
}

stream.Position = 0;

await JsonSerializer.DeserializeAsync<MSBuildFileSetResult>(stream, cancellationToken: CancellationToken.None);
}
}
1 change: 1 addition & 0 deletions src/Tests/dotnet-watch.Tests/dotnet-watch.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>$(ToolsetTargetFramework)</TargetFramework>
<StrongNameKeyId>MicrosoftAspNetCore</StrongNameKeyId>
<RootNamespace>Microsoft.DotNet.Watcher.Tools</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 1374e63

Please sign in to comment.