Skip to content

Commit 1a86c64

Browse files
committed
Fix some issues
1 parent fd76c51 commit 1a86c64

File tree

5 files changed

+109
-52
lines changed

5 files changed

+109
-52
lines changed

src/Cli/Microsoft.DotNet.FileBasedPrograms/AppDirectiveHelpers.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
using Microsoft.CodeAnalysis.CSharp;
1717
using Microsoft.CodeAnalysis.CSharp.Syntax;
1818
using Microsoft.CodeAnalysis.Text;
19-
using Microsoft.DotNet.Cli;
19+
20+
#if !FILE_BASED_PROGRAMS_SOURCE_PACKAGE_GRACEFUL_EXCEPTION
21+
using Microsoft.DotNet.Cli.Utils;
22+
#endif
23+
2024
using Roslyn.Utilities;
2125

2226
namespace Microsoft.DotNet.FileBasedPrograms;
@@ -477,10 +481,10 @@ public sealed class Project(in ParseInfo info) : Named(info)
477481
}
478482
else if (!File.Exists(resolvedProjectPath))
479483
{
480-
throw new GracefulExceptionDELETEME(FileBasedProgramsResources.CouldNotFindProjectOrDirectory, resolvedProjectPath);
484+
throw new GracefulException(FileBasedProgramsResources.CouldNotFindProjectOrDirectory, resolvedProjectPath);
481485
}
482486
}
483-
catch (GracefulExceptionDELETEME e)
487+
catch (GracefulException e)
484488
{
485489
context.Diagnostics.AddError(context.SourceFile, context.Info.Span, string.Format(FileBasedProgramsResources.InvalidProjectDirective, e.Message), e);
486490
}
@@ -500,25 +504,25 @@ public static FileInfo GetProjectFileFromDirectory(string projectDirectory)
500504
}
501505
catch (ArgumentException)
502506
{
503-
throw new GracefulExceptionDELETEME(FileBasedProgramsResources.CouldNotFindProjectOrDirectory, projectDirectory);
507+
throw new GracefulException(FileBasedProgramsResources.CouldNotFindProjectOrDirectory, projectDirectory);
504508
}
505509

506510
if (!dir.Exists)
507511
{
508-
throw new GracefulExceptionDELETEME(FileBasedProgramsResources.CouldNotFindProjectOrDirectory, projectDirectory);
512+
throw new GracefulException(FileBasedProgramsResources.CouldNotFindProjectOrDirectory, projectDirectory);
509513
}
510514

511515
FileInfo[] files = dir.GetFiles("*proj");
512516
if (files.Length == 0)
513517
{
514-
throw new GracefulExceptionDELETEME(
518+
throw new GracefulException(
515519
FileBasedProgramsResources.CouldNotFindAnyProjectInDirectory,
516520
projectDirectory);
517521
}
518522

519523
if (files.Length > 1)
520524
{
521-
throw new GracefulExceptionDELETEME(FileBasedProgramsResources.MoreThanOneProjectInDirectory, projectDirectory);
525+
throw new GracefulException(FileBasedProgramsResources.MoreThanOneProjectInDirectory, projectDirectory);
522526
}
523527

524528
return files.First();
@@ -579,7 +583,7 @@ internal readonly struct DiagnosticBag
579583
public bool IgnoreDiagnostics { get; private init; }
580584

581585
/// <summary>
582-
/// If <see langword="null"/> and <see cref="IgnoreDiagnostics"/> is <see langword="false"/>, the first diagnostic is thrown as <see cref="GracefulExceptionDELETEME"/>.
586+
/// If <see langword="null"/> and <see cref="IgnoreDiagnostics"/> is <see langword="false"/>, the first diagnostic is thrown as <see cref="GracefulException"/>.
583587
/// </summary>
584588
public ImmutableArray<SimpleDiagnostic>.Builder? Builder { get; private init; }
585589

@@ -596,7 +600,7 @@ public void AddError(SourceFile sourceFile, TextSpan span, string message, Excep
596600
}
597601
else if (!IgnoreDiagnostics)
598602
{
599-
throw new GracefulExceptionDELETEME($"{sourceFile.GetLocationString(span)}: {FileBasedProgramsResources.DirectiveError}: {message}", inner);
603+
throw new GracefulException($"{sourceFile.GetLocationString(span)}: {FileBasedProgramsResources.DirectiveError}: {message}", inner);
600604
}
601605
}
602606

src/Cli/Microsoft.DotNet.FileBasedPrograms/ExternalHelpers.cs

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,60 +8,47 @@
88

99
namespace Microsoft.DotNet.FileBasedPrograms;
1010

11-
#if NET
12-
11+
/// <summary>
12+
/// When targeting netstandard2.0, the user of the source package must "implement" certain methods by declaring members in this type.
1313
partial class ExternalHelpers
1414
{
15-
public static int CombineHashCodes(int value1, int value2) => HashCode.Combine(value1, value2);
16-
public static string GetRelativePath(string relativeTo, string path) => Path.GetRelativePath(relativeTo, path);
17-
}
15+
public static partial int CombineHashCodes(int value1, int value2);
16+
public static partial string GetRelativePath(string relativeTo, string path);
1817

19-
#else
18+
#if NET
19+
public static partial int CombineHashCodes(int value1, int value2)
20+
=> HashCode.Combine(value1, value2);
2021

21-
/// <summary>
22-
/// When targeting netstandard2.0, the user of the source package must "implement" certain methods by declaring members in this type.
23-
/// </summary>
24-
partial class ExternalHelpers;
22+
public static partial string GetRelativePath(string relativeTo, string path)
23+
=> Path.GetRelativePath(relativeTo, path);
2524

26-
static class Stubs
27-
{
28-
extension(ExternalHelpers)
29-
{
30-
#if !FILE_BASED_PROGRAMS_SOURCE_PACKAGE_BUILD
31-
[Obsolete("Please declare and implement this method in `Microsoft.DotNet.FileBasedPrograms.ExternalHelpers`.", error: true)]
32-
#endif
33-
public static int CombineHashCodes(int value1, int value2)
34-
=> throw new NotImplementedException("Please declare and implement this method in `Microsoft.DotNet.FileBasedPrograms.ExternalHelpers`.");
25+
#elif FILE_BASED_PROGRAMS_SOURCE_PACKAGE_BUILD
26+
public static partial int CombineHashCodes(int value1, int value2)
27+
=> throw new NotImplementedException("Please declare and implement this method in `Microsoft.DotNet.FileBasedPrograms.ExternalHelpers`.");
3528

36-
#if !FILE_BASED_PROGRAMS_SOURCE_PACKAGE_BUILD
37-
[Obsolete("Please declare and implement this method in `Microsoft.DotNet.FileBasedPrograms.ExternalHelpers`.", error: true)]
38-
#endif
39-
public static string GetRelativePath(string relativeTo, string path)
40-
=> throw new NotImplementedException("Please declare and implement this method in `Microsoft.DotNet.FileBasedPrograms.ExternalHelpers`.");
41-
}
42-
}
29+
public static partial string GetRelativePath(string relativeTo, string path)
30+
=> throw new NotImplementedException("Please declare and implement this method in `Microsoft.DotNet.FileBasedPrograms.ExternalHelpers`.");
4331

4432
#endif
33+
}
4534

46-
// TODO: this is tricky. dotnet cli includes this type via ProjectReference to Cli.Utils.
47-
// But it's not reasonable to reference that project from Roslyn.
48-
// We should probably avoid using this type in the source package.
49-
// But currently there is a significant behavioral need and number of callers who are catching GracefulException.
50-
internal class GracefulExceptionDELETEME : Exception
35+
#if FILE_BASED_PROGRAMS_SOURCE_PACKAGE_GRACEFUL_EXCEPTION
36+
internal class GracefulException : Exception
5137
{
52-
public GracefulExceptionDELETEME()
38+
public GracefulException()
5339
{
5440
}
5541

56-
public GracefulExceptionDELETEME(string? message) : base(message)
42+
public GracefulException(string? message) : base(message)
5743
{
5844
}
5945

60-
public GracefulExceptionDELETEME(string? message, string? todo2) : base(message)
46+
public GracefulException(string? message, string? todo2) : base(message)
6147
{
6248
}
6349

64-
public GracefulExceptionDELETEME(string? message, Exception? innerException) : base(message, innerException)
50+
public GracefulException(string? message, Exception? innerException) : base(message, innerException)
6551
{
6652
}
67-
}
53+
}
54+
#endif
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
4+
// TODO: consider if this extra copy of the file can be avoided.
5+
// It's really not that much code, so, if we can't solve the reuse issue, we can live with that.
6+
#nullable enable
7+
using System;
8+
9+
namespace Microsoft.DotNet.FileBasedPrograms
10+
{
11+
/// <summary>
12+
/// Internal utilities copied from microsoft/MSBuild repo.
13+
/// </summary>
14+
class MSBuildUtilities
15+
{
16+
/// <summary>
17+
/// Converts a string to a bool. We consider "true/false", "on/off", and
18+
/// "yes/no" to be valid boolean representations in the XML.
19+
/// Modified from its original version to not throw, but return a default value.
20+
/// </summary>
21+
/// <param name="parameterValue">The string to convert.</param>
22+
/// <returns>Boolean true or false, corresponding to the string.</returns>
23+
internal static bool ConvertStringToBool(string? parameterValue, bool defaultValue = false)
24+
{
25+
if (string.IsNullOrEmpty(parameterValue))
26+
{
27+
return defaultValue;
28+
}
29+
else if (ValidBooleanTrue(parameterValue))
30+
{
31+
return true;
32+
}
33+
else if (ValidBooleanFalse(parameterValue))
34+
{
35+
return false;
36+
}
37+
else
38+
{
39+
// Unsupported boolean representation.
40+
return defaultValue;
41+
}
42+
}
43+
44+
/// <summary>
45+
/// Returns true if the string represents a valid MSBuild boolean true value,
46+
/// such as "on", "!false", "yes"
47+
/// </summary>
48+
private static bool ValidBooleanTrue(string? parameterValue)
49+
{
50+
return ((string.Compare(parameterValue, "true", StringComparison.OrdinalIgnoreCase) == 0) ||
51+
(string.Compare(parameterValue, "on", StringComparison.OrdinalIgnoreCase) == 0) ||
52+
(string.Compare(parameterValue, "yes", StringComparison.OrdinalIgnoreCase) == 0) ||
53+
(string.Compare(parameterValue, "!false", StringComparison.OrdinalIgnoreCase) == 0) ||
54+
(string.Compare(parameterValue, "!off", StringComparison.OrdinalIgnoreCase) == 0) ||
55+
(string.Compare(parameterValue, "!no", StringComparison.OrdinalIgnoreCase) == 0));
56+
}
57+
58+
/// <summary>
59+
/// Returns true if the string represents a valid MSBuild boolean false value,
60+
/// such as "!on" "off" "no" "!true"
61+
/// </summary>
62+
private static bool ValidBooleanFalse(string? parameterValue)
63+
{
64+
return ((string.Compare(parameterValue, "false", StringComparison.OrdinalIgnoreCase) == 0) ||
65+
(string.Compare(parameterValue, "off", StringComparison.OrdinalIgnoreCase) == 0) ||
66+
(string.Compare(parameterValue, "no", StringComparison.OrdinalIgnoreCase) == 0) ||
67+
(string.Compare(parameterValue, "!true", StringComparison.OrdinalIgnoreCase) == 0) ||
68+
(string.Compare(parameterValue, "!on", StringComparison.OrdinalIgnoreCase) == 0) ||
69+
(string.Compare(parameterValue, "!yes", StringComparison.OrdinalIgnoreCase) == 0));
70+
}
71+
}
72+
}

src/Cli/Microsoft.DotNet.FileBasedPrograms/Microsoft.DotNet.FileBasedPrograms.Package.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
<!-- Remove once https://github.com/NuGet/Home/issues/8583 is fixed -->
2020
<NoWarn>$(NoWarn);NU5128</NoWarn>
2121
<EnableDefaultItems>false</EnableDefaultItems>
22-
<DefineConstants>$(DefineConstants);FILE_BASED_PROGRAMS_SOURCE_PACKAGE_BUILD</DefineConstants>
23-
22+
<DefineConstants>$(DefineConstants);FILE_BASED_PROGRAMS_SOURCE_PACKAGE_BUILD;FILE_BASED_PROGRAMS_SOURCE_PACKAGE_GRACEFUL_EXCEPTION</DefineConstants>
2423
<!-- Files must individually enable nullable in source packages -->
2524
<Nullable>disable</Nullable>
2625
</PropertyGroup>

src/Cli/Microsoft.DotNet.FileBasedPrograms/Microsoft.DotNet.FileBasedPrograms.projitems

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010
</PropertyGroup>
1111
<ItemGroup>
1212
<Compile Include="$(MSBuildThisFileDirectory)**\*.cs" />
13-
<!--
14-
TODO: this file isn't getting included in the source package.
15-
Is it possible/desirable to include random files from different parts of the repo tree in the package?
16-
-->
17-
<Compile Include="$(RepoRoot)src\Common\MSBuildUtilities.cs" LinkBase="Common" />
1813
<EmbeddedResource Include="$(MSBuildThisFileDirectory)\FileBasedProgramsResources.resx" LinkBase="Resources" GenerateSource="true" Namespace="Microsoft.DotNet.FileBasedPrograms" />
1914
</ItemGroup>
2015
</Project>

0 commit comments

Comments
 (0)