Skip to content

Commit a219732

Browse files
authored
Update test command to use global.json (#50673)
1 parent 05080d2 commit a219732

File tree

169 files changed

+529
-1231
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

169 files changed

+529
-1231
lines changed

Directory.Packages.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftExtensionsLoggingAbstractionsVersion)" />
6060
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="$(MicrosoftExtensionsLoggingConsoleVersion)" />
6161
<PackageVersion Include="Microsoft.Extensions.ObjectPool" Version="$(MicrosoftExtensionsObjectPoolPackageVersion)" />
62-
<PackageVersion Include="Microsoft.Extensions.Configuration.Ini" Version="$(MicrosoftExtensionsConfigurationIniVersion)" />
6362
<PackageVersion Include="Microsoft.FSharp.Compiler" Version="$(MicrosoftFSharpCompilerPackageVersion)" />
6463
<PackageVersion Include="Microsoft.Net.Compilers.Toolset.Framework" Version="$(MicrosoftNetCompilersToolsetFrameworkPackageVersion)" />
6564
<PackageVersion Include="Microsoft.Management.Infrastructure" Version="3.0.0" />

src/Cli/Microsoft.TemplateEngine.Cli/Components.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public static class Components
1515
(typeof(IPostActionProcessor), new InstructionDisplayPostActionProcessor()),
1616
(typeof(IPostActionProcessor), new ProcessStartPostActionProcessor()),
1717
(typeof(IPostActionProcessor), new AddJsonPropertyPostActionProcessor()),
18-
(typeof(IPostActionProcessor), new CreateOrUpdateDotnetConfigPostActionProcessor()),
1918
};
2019
}
2120
}

src/Cli/Microsoft.TemplateEngine.Cli/LocalizableStrings.Designer.cs

Lines changed: 0 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/Microsoft.TemplateEngine.Cli/LocalizableStrings.resx

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -953,23 +953,4 @@ The header is followed by the list of parameters and their errors (might be seve
953953
<data name="PostAction_ModifyJson_Verbose_AttemptingToFindJsonFile" xml:space="preserve">
954954
<value>Attempting to find json file '{0}' in '{1}'</value>
955955
</data>
956-
<data name="PostAction_DotnetConfig_Error_ArgumentNotConfigured" xml:space="preserve">
957-
<value>Post action argument '{0}' is mandatory, but not configured.</value>
958-
</data>
959-
<data name="PostAction_CreateDotnetConfig_Succeeded" xml:space="preserve">
960-
<value>Successfully created 'dotnet.config' file.</value>
961-
<comment>{Locked="dotnet.config"}</comment>
962-
</data>
963-
<data name="PostAction_CreateDotnetConfig_CreatedNewSection" xml:space="preserve">
964-
<value>Created new section in 'dotnet.config' file</value>
965-
<comment>{Locked="dotnet.config"}</comment>
966-
</data>
967-
<data name="PostAction_CreateDotnetConfig_ValueAlreadyExist" xml:space="preserve">
968-
<value>The required value in 'dotnet.config' is already set.</value>
969-
<comment>{Locked="dotnet.config"}</comment>
970-
</data>
971-
<data name="PostAction_CreateDotnetConfig_ManuallyUpdate" xml:space="preserve">
972-
<value>Updating existing values in 'dotnet.config' is not yet supported. Please, manually update 'dotnet.config' to have '{0}' under section '{1}'.</value>
973-
<comment>{Locked="dotnet.config"}</comment>
974-
</data>
975-
</root>
956+
</root>

src/Cli/Microsoft.TemplateEngine.Cli/Microsoft.TemplateEngine.Cli.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
<PackageReference Include="Microsoft.TemplateEngine.Edge" />
1717
<PackageReference Include="Microsoft.TemplateSearch.Common" />
1818
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
19-
<PackageReference Include="Microsoft.Extensions.Configuration.Ini" />
2019
<PackageReference Include="Wcwidth.Sources" ExcludeAssets="all" GeneratePathProperty="true" />
2120
<PackageReference Include="System.CommandLine" />
2221
</ItemGroup>

src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ internal class AddJsonPropertyPostActionProcessor : PostActionProcessorBase
1919
private const string ParentPropertyPathArgument = "parentPropertyPath";
2020
private const string NewJsonPropertyNameArgument = "newJsonPropertyName";
2121
private const string NewJsonPropertyValueArgument = "newJsonPropertyValue";
22+
private const string DetectRepoRootForFileCreation = "detectRepositoryRootForFileCreation";
2223

2324
private static readonly JsonSerializerOptions SerializerOptions = new()
2425
{
@@ -36,6 +37,43 @@ internal class AddJsonPropertyPostActionProcessor : PostActionProcessorBase
3637

3738
internal static Guid ActionProcessorId { get; } = new Guid("695A3659-EB40-4FF5-A6A6-C9C4E629FCB0");
3839

40+
internal static string GetRootDirectory(IPhysicalFileSystem fileSystem, string outputBasePath)
41+
{
42+
string? currentDirectory = outputBasePath;
43+
string? directoryWithSln = null;
44+
while (currentDirectory is not null)
45+
{
46+
if (fileSystem.FileExists(Path.Combine(currentDirectory, "global.json")) ||
47+
fileSystem.FileExists(Path.Combine(currentDirectory, ".git")) ||
48+
fileSystem.DirectoryExists(Path.Combine(currentDirectory, ".git")))
49+
{
50+
// If we found global.json or .git, we immediately return the directory as the repo root.
51+
// We won't go up any further.
52+
return currentDirectory;
53+
}
54+
55+
// DirectoryExists here should always be true in practice, but for the way tests are mocking the file system, it's not.
56+
// The check was added to prevent test failures similar to:
57+
// System.IO.DirectoryNotFoundException : Could not find a part of the path '/Users/runner/work/1/s/artifacts/bin/Microsoft.TemplateEngine.Cli.UnitTests/Release/sandbox'.
58+
// We get to this exception when doing `EnumerateFiles` on a directory that was virtually created in memory (not really available on disk).
59+
// EnumerateFiles tries to access the physical file system, which then fails.
60+
if (fileSystem.DirectoryExists(currentDirectory) &&
61+
(fileSystem.EnumerateFiles(currentDirectory, "*.sln", SearchOption.TopDirectoryOnly).Any() ||
62+
fileSystem.EnumerateFiles(currentDirectory, "*.slnx", SearchOption.TopDirectoryOnly).Any()))
63+
{
64+
directoryWithSln = currentDirectory;
65+
}
66+
67+
currentDirectory = Directory.GetParent(currentDirectory)?.FullName;
68+
}
69+
70+
// If we reach here, that means we didn't find .git or global.json.
71+
// So, we return the directory where we found a .sln/.slnx file, if any.
72+
// Note that when we keep track of directoryWithSln, we keep updating it from sln/slnx from parent directories, if found.
73+
// This means that if there are multiple .sln/.slnx files in the parent directories, we will return the top-most one.
74+
return directoryWithSln ?? outputBasePath;
75+
}
76+
3977
protected override bool ProcessInternal(
4078
IEngineEnvironmentSettings environment,
4179
IPostAction action,
@@ -65,7 +103,13 @@ protected override bool ProcessInternal(
65103
return false;
66104
}
67105

68-
string newJsonFilePath = Path.Combine(outputBasePath, jsonFileName);
106+
if (!bool.TryParse(action.Args.GetValueOrDefault(DetectRepoRootForFileCreation, "false"), out bool detectRepoRoot))
107+
{
108+
Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_ArgumentNotBoolean, DetectRepoRootForFileCreation));
109+
return false;
110+
}
111+
112+
string newJsonFilePath = Path.Combine(detectRepoRoot ? GetRootDirectory(environment.Host.FileSystem, outputBasePath) : outputBasePath, jsonFileName);
69113
environment.Host.FileSystem.WriteAllText(newJsonFilePath, "{}");
70114
jsonFiles = new List<string> { newJsonFilePath };
71115
}

src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/CreateOrUpdateDotnetConfigPostActionProcessor.cs

Lines changed: 0 additions & 131 deletions
This file was deleted.

src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.cs.xlf

Lines changed: 0 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)