Skip to content

Commit 8263b54

Browse files
authored
Spectre.Console repo fixes (#248)
1 parent 8f12bdc commit 8263b54

File tree

8 files changed

+140
-0
lines changed

8 files changed

+140
-0
lines changed

Directory.Build.props

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
<PatchesDir>$(ProjectDir)patches/</PatchesDir>
3131
<!-- if we're not currently building, Visual Studio will still set this -->
3232
<SDK_VERSION Condition="'$(SDK_VERSION)' == ''">$(NETCoreSdkVersion)</SDK_VERSION>
33+
<XPlatTasksDir>$(MSBuildThisFileDirectory)eng/tasks/Microsoft.DotNet.SourceBuild.Tasks.XPlat/</XPlatTasksDir>
34+
<XPlatTasksBinDir>$(XPlatTasksDir)bin/$(Configuration)/</XPlatTasksBinDir>
35+
<XPlatSourceBuildTasksAssembly>$(XPlatTasksBinDir)Microsoft.DotNet.SourceBuild.Tasks.XPlat.dll</XPlatSourceBuildTasksAssembly>
36+
<SdkReferenceDir>$(DotNetCliToolDir)sdk/$(SDK_VERSION)/</SdkReferenceDir>
3337
</PropertyGroup>
3438

3539
<!--

build.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,11 @@ while [[ -h $source ]]; do
1313
done
1414

1515
scriptroot="$( cd -P "$( dirname "$source" )" && pwd )"
16+
17+
sdkLine=$(grep -m 1 'dotnet' "$scriptroot/global.json")
18+
sdkPattern="\"dotnet\" *: *\"(.*)\""
19+
if [[ $sdkLine =~ $sdkPattern ]]; then
20+
export SDK_VERSION=${BASH_REMATCH[1]}
21+
fi
22+
1623
"$scriptroot/eng/common/build.sh" --build --restore "$@"

eng/Build.props

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<Project>
22

33
<ItemGroup>
4+
<ProjectToBuild Include="$(RepoRoot)eng\tasks\Microsoft.DotNet.SourceBuild.Tasks.XPlat\*.csproj">
5+
<BuildInParallel>false</BuildInParallel>
6+
</ProjectToBuild>
47
<ProjectToBuild Include="$(RepoRoot)repo-projects\*.proj" />
58
</ItemGroup>
69

eng/tasks/Directory.Build.props

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project>
2+
<Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Build.props, $(MSBuildThisFileDirectory)..))" />
3+
4+
<PropertyGroup>
5+
<!-- Build all tasks as AnyCPU to match NuGet DLLs in the SDK: avoid warnings. -->
6+
<Platform>AnyCPU</Platform>
7+
<!--
8+
Do not import the Arcade SDK for the local tooling projects. This lets us
9+
build them with just the .NET SDK, simplifying the build.
10+
-->
11+
<SkipArcadeSdkImport>true</SkipArcadeSdkImport>
12+
</PropertyGroup>
13+
14+
<!--
15+
Use some assemblies from the SDK, instead of package references. This ensures they match what's
16+
found when the task is loaded by the SDK's MSBuild.
17+
-->
18+
<ItemGroup>
19+
<SdkAssembly Include="$(SdkReferenceDir)Newtonsoft.Json.dll" />
20+
21+
<SdkAssemblyReference
22+
Include="@(SdkAssembly -> '%(FileName)')"
23+
HintPath="$(SdkReferenceDir)%(Identity).dll" />
24+
</ItemGroup>
25+
26+
</Project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<OutputPath>$(XPlatTasksBinDir)</OutputPath>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.Build">
10+
<Version>15.7.179</Version>
11+
</PackageReference>
12+
<PackageReference Include="Microsoft.Build.Framework">
13+
<Version>15.7.179</Version>
14+
</PackageReference>
15+
<PackageReference Include="Microsoft.Build.Tasks.Core">
16+
<Version>15.7.179</Version>
17+
</PackageReference>
18+
<PackageReference Include="Microsoft.Build.Utilities.Core">
19+
<Version>15.7.179</Version>
20+
</PackageReference>
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<Reference Include="@(SdkAssemblyReference)" />
25+
</ItemGroup>
26+
27+
</Project>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.IO;
7+
using System.Linq;
8+
using Newtonsoft.Json;
9+
using Newtonsoft.Json.Linq;
10+
using Microsoft.Build.Framework;
11+
using Microsoft.Build.Utilities;
12+
13+
namespace Microsoft.DotNet.Build.Tasks
14+
{
15+
// Takes a path to a path to a json file and a
16+
// string that represents a dotted path to an attribute
17+
// and updates that attribute with the new value provided.
18+
public class UpdateJson : Task
19+
{
20+
[Required]
21+
public string JsonFilePath { get; set; }
22+
23+
[Required]
24+
public string PathToAttribute { get; set; }
25+
26+
[Required]
27+
public string NewAttributeValue { get; set; }
28+
29+
public bool SkipUpdateIfMissingKey { get; set; }
30+
31+
public override bool Execute()
32+
{
33+
JObject jsonObj = JObject.Parse(File.ReadAllText(JsonFilePath));
34+
35+
string[] escapedPathToAttributeParts = PathToAttribute.Replace("\\.", "\x1F").Split('.');
36+
for (int i = 0; i < escapedPathToAttributeParts.Length; ++i)
37+
{
38+
escapedPathToAttributeParts[i] = escapedPathToAttributeParts[i].Replace("\x1F", ".");
39+
}
40+
UpdateAttribute(jsonObj, escapedPathToAttributeParts, NewAttributeValue);
41+
42+
File.WriteAllText(JsonFilePath, jsonObj.ToString());
43+
return true;
44+
}
45+
46+
private void UpdateAttribute(JToken jsonObj, string[] path, string newValue)
47+
{
48+
string pathItem = path[0];
49+
if (jsonObj[pathItem] == null)
50+
{
51+
string message = $"Path item [{nameof(PathToAttribute)}] not found in json file.";
52+
if (SkipUpdateIfMissingKey)
53+
{
54+
Log.LogMessage(MessageImportance.Low, $"Skipping update: {message} {pathItem}");
55+
return;
56+
}
57+
throw new ArgumentException(message, pathItem);
58+
}
59+
60+
if (path.Length == 1)
61+
{
62+
jsonObj[pathItem] = newValue;
63+
return;
64+
}
65+
66+
UpdateAttribute(jsonObj[pathItem], path.Skip(1).ToArray(), newValue);
67+
}
68+
}
69+
}

repo-projects/Directory.Build.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
</PropertyGroup>
99

1010
<UsingTask TaskName="Microsoft.DotNet.Arcade.Sdk.SourceBuild.AddSourceToNuGetConfig" AssemblyFile="$(ArcadeSdkBuildTasksAssembly)" />
11+
<UsingTask TaskName="UpdateJson" AssemblyFile="$(XPlatSourceBuildTasksAssembly)" />
1112

1213
<Target Name="BuildRepoReferences" Condition="'@(RepositoryReference)' != '' and '$(SkipRepoReferences)' != 'true'">
1314
<Message Importance="High" Text="Building dependencies [@(RepositoryReference)] needed by '$(RepositoryName)'." />

repo-projects/spectre-console.proj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
<PropertyGroup>
55
<PackagesOutput>$(ProjectDirectory)/src/Spectre.Console/bin/$(Configuration)/</PackagesOutput>
66
<SpectreConsolePackageVersion>0.48.0</SpectreConsolePackageVersion>
7+
8+
<!-- Defining this ensures global.json gets rewritten to target the current SDK version -->
9+
<GlobalJsonFile>$(ProjectDirectory)/global.json</GlobalJsonFile>
710
</PropertyGroup>
811

912
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />

0 commit comments

Comments
 (0)