Skip to content

Commit 3065f1e

Browse files
authored
Add CommandLineResource API (#78679)
1 parent 540285c commit 3065f1e

File tree

8 files changed

+356
-289
lines changed

8 files changed

+356
-289
lines changed

src/Compilers/CSharp/Portable/CommandLine/CSharpCommandLineParser.cs

Lines changed: 38 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ internal sealed override CommandLineArguments CommonParse(IEnumerable<string> ar
9797
bool? delaySignSetting = null;
9898
string? keyFileSetting = null;
9999
string? keyContainerSetting = null;
100-
List<ResourceDescription> managedResources = new List<ResourceDescription>();
100+
List<CommandLineResource> managedResources = new List<CommandLineResource>();
101101
List<CommandLineSourceFile> sourceFiles = new List<CommandLineSourceFile>();
102102
List<CommandLineSourceFile> additionalFiles = new List<CommandLineSourceFile>();
103103
var analyzerConfigPaths = ArrayBuilder<string>.GetInstance();
@@ -742,8 +742,7 @@ internal sealed override CommandLineArguments CommonParse(IEnumerable<string> ar
742742
break; // Dev11 reports unrecognized option
743743
}
744744

745-
var embeddedResource = ParseResourceDescription(arg, valueMemory.Value, baseDirectory, diagnostics, embedded: true);
746-
if (embeddedResource != null)
745+
if (TryParseResourceDescription(arg, valueMemory.Value, baseDirectory, diagnostics, isEmbedded: true, out var embeddedResource))
747746
{
748747
managedResources.Add(embeddedResource);
749748
resourcesOrModulesSpecified = true;
@@ -758,8 +757,7 @@ internal sealed override CommandLineArguments CommonParse(IEnumerable<string> ar
758757
break; // Dev11 reports unrecognized option
759758
}
760759

761-
var linkedResource = ParseResourceDescription(arg, valueMemory.Value, baseDirectory, diagnostics, embedded: false);
762-
if (linkedResource != null)
760+
if (TryParseResourceDescription(arg, valueMemory.Value, baseDirectory, diagnostics, isEmbedded: false, out var linkedResource))
763761
{
764762
managedResources.Add(linkedResource);
765763
resourcesOrModulesSpecified = true;
@@ -1580,7 +1578,7 @@ internal sealed override CommandLineArguments CommonParse(IEnumerable<string> ar
15801578
DisplayHelp = displayHelp,
15811579
DisplayVersion = displayVersion,
15821580
DisplayLangVersions = displayLangVersions,
1583-
ManifestResources = managedResources.AsImmutable(),
1581+
ManifestResourceArguments = managedResources.AsImmutable(),
15841582
CompilationOptions = options,
15851583
ParseOptions = parseOptions,
15861584
EmitOptions = emitOptions,
@@ -2015,78 +2013,51 @@ private static IEnumerable<InstrumentationKind> ParseInstrumentationKinds(string
20152013
}
20162014
}
20172015

2018-
internal static ResourceDescription? ParseResourceDescription(
2019-
string arg,
2020-
string resourceDescriptor,
2021-
string? baseDirectory,
2022-
IList<Diagnostic> diagnostics,
2023-
bool embedded) =>
2024-
ParseResourceDescription(arg, resourceDescriptor.AsMemory(), baseDirectory, diagnostics, embedded);
2025-
2026-
internal static ResourceDescription? ParseResourceDescription(
2027-
string arg,
2016+
internal static bool TryParseResourceDescription(
2017+
string argName,
20282018
ReadOnlyMemory<char> resourceDescriptor,
20292019
string? baseDirectory,
20302020
IList<Diagnostic> diagnostics,
2031-
bool embedded)
2021+
bool isEmbedded,
2022+
out CommandLineResource resource)
20322023
{
2033-
string? filePath;
2034-
string? fullPath;
2035-
string? fileName;
2036-
string? resourceName;
2037-
string? accessibility;
2038-
2039-
ParseResourceDescription(
2024+
if (!TryParseResourceDescription(
20402025
resourceDescriptor,
20412026
baseDirectory,
2042-
false,
2043-
out filePath,
2044-
out fullPath,
2045-
out fileName,
2046-
out resourceName,
2047-
out accessibility);
2048-
2049-
bool isPublic;
2050-
if (accessibility == null)
2051-
{
2052-
// If no accessibility is given, we default to "public".
2053-
// NOTE: Dev10 distinguishes between null and empty/whitespace-only.
2054-
isPublic = true;
2055-
}
2056-
else if (string.Equals(accessibility, "public", StringComparison.OrdinalIgnoreCase))
2027+
skipLeadingSeparators: false,
2028+
allowEmptyAccessibility: false,
2029+
out var filePath,
2030+
out var fullPath,
2031+
out var fileName,
2032+
out var resourceName,
2033+
out var isPublic,
2034+
out var rawAccessibility))
20572035
{
2058-
isPublic = true;
2059-
}
2060-
else if (string.Equals(accessibility, "private", StringComparison.OrdinalIgnoreCase))
2061-
{
2062-
isPublic = false;
2063-
}
2064-
else
2065-
{
2066-
AddDiagnostic(diagnostics, ErrorCode.ERR_BadResourceVis, accessibility);
2067-
return null;
2068-
}
2036+
if (isPublic == null)
2037+
{
2038+
AddDiagnostic(diagnostics, ErrorCode.ERR_BadResourceVis, rawAccessibility ?? "");
2039+
}
2040+
else if (RoslynString.IsNullOrWhiteSpace(filePath))
2041+
{
2042+
AddDiagnostic(diagnostics, ErrorCode.ERR_NoFileSpec, argName);
2043+
}
2044+
else
2045+
{
2046+
Debug.Assert(!PathUtilities.IsValidFilePath(fullPath));
2047+
AddDiagnostic(diagnostics, ErrorCode.FTL_InvalidInputFileName, filePath);
2048+
}
20692049

2070-
if (RoslynString.IsNullOrWhiteSpace(filePath))
2071-
{
2072-
AddDiagnostic(diagnostics, ErrorCode.ERR_NoFileSpec, arg);
2073-
return null;
2050+
resource = default;
2051+
return false;
20742052
}
2075-
Debug.Assert(!resourceName.IsEmpty()); // see ParseResourceDescription's check on filePath
20762053

2077-
if (!PathUtilities.IsValidFilePath(fullPath))
2078-
{
2079-
AddDiagnostic(diagnostics, ErrorCode.FTL_InvalidInputFileName, filePath);
2080-
return null;
2081-
}
2054+
resource = new CommandLineResource(
2055+
resourceName: resourceName,
2056+
fullPath: fullPath,
2057+
linkedResourceFileName: isEmbedded ? null : fileName,
2058+
isPublic.Value);
20822059

2083-
Func<Stream> dataProvider = () =>
2084-
{
2085-
// Use FileShare.ReadWrite because the file could be opened by the current process.
2086-
// For example, it is an XML doc file produced by the build.
2087-
return new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
2088-
};
2089-
return new ResourceDescription(resourceName, fileName, dataProvider, isPublic, embedded, checkArgs: false);
2060+
return true;
20902061
}
20912062

20922063
private static void ParseWarnings(ReadOnlyMemory<char> value, ArrayBuilder<string> ids)

0 commit comments

Comments
 (0)