Skip to content

Commit 41e666c

Browse files
committed
Parse and use RootNamespace from project files
1 parent 88f6e04 commit 41e666c

File tree

3 files changed

+30
-8
lines changed
  • csharp
    • extractor/Semmle.Extraction.CSharp.DependencyFetching/SourceGenerators/DotnetSourceGeneratorWrapper
    • ql/integration-tests/all-platforms/standalone_resx

3 files changed

+30
-8
lines changed

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/SourceGenerators/DotnetSourceGeneratorWrapper/Resx.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5-
using System.Text;
5+
using System.Text.RegularExpressions;
66
using Semmle.Util.Logging;
77

88
namespace Semmle.Extraction.CSharp.DependencyFetching
99
{
10-
internal sealed class Resx : DotnetSourceGeneratorWrapper
10+
internal sealed partial class Resx : DotnetSourceGeneratorWrapper
1111
{
1212
protected override string FileType => "Resx";
1313

@@ -28,6 +28,24 @@ protected override void GenerateAnalyzerConfig(IEnumerable<string> resources, st
2828
sw.WriteLine("is_global = true");
2929

3030
var rootNamespace = Path.GetFileNameWithoutExtension(csprojFile);
31+
32+
var matches = File.ReadAllLines(csprojFile)
33+
.Select(line => RootNamespace().Match(line))
34+
.Where(match => match.Success)
35+
.Select(match => match.Groups[1].Value)
36+
.ToArray();
37+
38+
if (matches.Length == 1)
39+
{
40+
logger.LogDebug($"RootNamespace found in {csprojFile}: {matches[0]}");
41+
rootNamespace = matches[0];
42+
}
43+
else if (matches.Length > 1)
44+
{
45+
logger.LogDebug($"Multiple RootNamespace elements found in {csprojFile}. Using the first one.");
46+
rootNamespace = matches[0];
47+
}
48+
3149
sw.WriteLine($"build_property.RootNamespace = {rootNamespace}");
3250

3351
foreach (var f in resources.Select(f => f.Replace('\\', '/')))
@@ -36,5 +54,8 @@ protected override void GenerateAnalyzerConfig(IEnumerable<string> resources, st
3654
sw.WriteLine($"build_metadata.AdditionalFiles.EmitFormatMethods = true");
3755
}
3856
}
57+
58+
[GeneratedRegex(@"<RootNamespace>(.*)</RootNamespace>", RegexOptions.Compiled | RegexOptions.Singleline)]
59+
private static partial Regex RootNamespace();
3960
}
4061
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
| Program | Program.<Main>$ |
22
| Program | Program.Program |
3-
| resx.test | resx.test.Culture |
4-
| resx.test | resx.test.GetResourceString |
5-
| resx.test | resx.test.Key123 |
6-
| resx.test | resx.test.Key456 |
7-
| resx.test | resx.test.ResourceManager |
8-
| resx.test | resx.test.s_resourceManager |
3+
| Resx.Test1.Test2.test | Resx.Test1.Test2.test.Culture |
4+
| Resx.Test1.Test2.test | Resx.Test1.Test2.test.GetResourceString |
5+
| Resx.Test1.Test2.test | Resx.Test1.Test2.test.Key123 |
6+
| Resx.Test1.Test2.test | Resx.Test1.Test2.test.Key456 |
7+
| Resx.Test1.Test2.test | Resx.Test1.Test2.test.ResourceManager |
8+
| Resx.Test1.Test2.test | Resx.Test1.Test2.test.s_resourceManager |

csharp/ql/integration-tests/all-platforms/standalone_resx/resx.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<TargetFramework>net8.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
8+
<RootNamespace>Resx.Test1.Test2</RootNamespace>
89
</PropertyGroup>
910

1011
<Target Name="DeleteBinObjFolders" BeforeTargets="Clean">

0 commit comments

Comments
 (0)