Skip to content

Commit 735b341

Browse files
committed
Merge branch 'main' into redsun82/kotlin
2 parents 8c705ad + f57e0cb commit 735b341

90 files changed

Lines changed: 1178 additions & 1489 deletions

File tree

Some content is hidden

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

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/AssemblyLookupLocation.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private void AddReferenceDirectory(List<string> dllsToIndex, ILogger logger)
4343
}
4444
else
4545
{
46-
logger.LogInfo($"AssemblyLookupLocation: Skipping {dll.FullName}.");
46+
logger.LogDebug($"AssemblyLookupLocation: Skipping {dll.FullName}.");
4747
}
4848
}
4949
}
@@ -68,19 +68,19 @@ public List<string> GetDlls(ILogger logger)
6868
}
6969
else
7070
{
71-
logger.LogInfo($"AssemblyLookupLocation: Skipping {path}.");
71+
logger.LogDebug($"AssemblyLookupLocation: Skipping {path}.");
7272
}
7373
return dllsToIndex;
7474
}
7575

7676
if (Directory.Exists(path))
7777
{
78-
logger.LogInfo($"AssemblyLookupLocation: Finding reference DLLs in {path}...");
78+
logger.LogDebug($"AssemblyLookupLocation: Finding reference DLLs in {path}...");
7979
AddReferenceDirectory(dllsToIndex, logger);
8080
}
8181
else
8282
{
83-
logger.LogInfo("AssemblyLookupLocation: Path not found: " + path);
83+
logger.LogDebug("AssemblyLookupLocation: Path not found: " + path);
8484
}
8585
return dllsToIndex;
8686
}

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.cs

Lines changed: 16 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -141,32 +141,28 @@ void exitCallback(int ret, string msg, bool silent)
141141
// Output the findings
142142
foreach (var r in usedReferences.Keys.OrderBy(r => r))
143143
{
144-
logger.LogInfo($"Resolved reference {r}");
144+
logger.LogDebug($"Resolved reference {r}");
145145
}
146146

147147
foreach (var r in unresolvedReferences.OrderBy(r => r.Key))
148148
{
149-
logger.LogInfo($"Unresolved reference {r.Key} in project {r.Value}");
149+
logger.LogDebug($"Unresolved reference {r.Key} in project {r.Value}");
150150
}
151151

152-
var webViewExtractionOption = Environment.GetEnvironmentVariable(EnvironmentVariableNames.WebViewGeneration);
153-
if (webViewExtractionOption == null ||
154-
bool.TryParse(webViewExtractionOption, out var shouldExtractWebViews) &&
155-
shouldExtractWebViews)
152+
var sourceGenerators = new ISourceGenerator[]
156153
{
157-
CompilationInfos.Add(("WebView extraction enabled", "1"));
158-
GenerateSourceFilesFromWebViews();
159-
}
160-
else
154+
new ImplicitUsingsGenerator(fileContent, logger, tempWorkingDirectory),
155+
new WebViewGenerator(fileProvider, fileContent, dotnet, this, logger, tempWorkingDirectory, usedReferences.Keys)
156+
};
157+
158+
foreach (var sourceGenerator in sourceGenerators)
161159
{
162-
CompilationInfos.Add(("WebView extraction enabled", "0"));
160+
this.generatedSources.AddRange(sourceGenerator.Generate());
163161
}
164162

165163
CompilationInfos.Add(("UseWPF set", fileContent.UseWpf ? "1" : "0"));
166164
CompilationInfos.Add(("UseWindowsForms set", fileContent.UseWindowsForms ? "1" : "0"));
167165

168-
GenerateSourceFileFromImplicitUsings();
169-
170166
const int align = 6;
171167
logger.LogInfo("");
172168
logger.LogInfo("Build analysis summary:");
@@ -253,7 +249,7 @@ private void RemoveNugetAnalyzerReferences()
253249
if (isInAnalyzersFolder)
254250
{
255251
usedReferences.Remove(filename);
256-
logger.LogInfo($"Removed analyzer reference {filename}");
252+
logger.LogDebug($"Removed analyzer reference {filename}");
257253
}
258254
}
259255
}
@@ -265,19 +261,19 @@ private void SelectNewestFrameworkPath(string frameworkPath, string frameworkTyp
265261
if (versionFolders.Length > 1)
266262
{
267263
var versions = string.Join(", ", versionFolders.Select(d => d.Name));
268-
logger.LogInfo($"Found multiple {frameworkType} DLLs in NuGet packages at {frameworkPath}. Using the latest version ({versionFolders[0].Name}) from: {versions}.");
264+
logger.LogDebug($"Found multiple {frameworkType} DLLs in NuGet packages at {frameworkPath}. Using the latest version ({versionFolders[0].Name}) from: {versions}.");
269265
}
270266

271267
var selectedFrameworkFolder = versionFolders.FirstOrDefault()?.FullName;
272268
if (selectedFrameworkFolder is null)
273269
{
274-
logger.LogInfo($"Found {frameworkType} DLLs in NuGet packages at {frameworkPath}, but no version folder was found.");
270+
logger.LogDebug($"Found {frameworkType} DLLs in NuGet packages at {frameworkPath}, but no version folder was found.");
275271
selectedFrameworkFolder = frameworkPath;
276272
}
277273

278274
dllLocations.Add(selectedFrameworkFolder);
279275
frameworkLocations.Add(selectedFrameworkFolder);
280-
logger.LogInfo($"Found {frameworkType} DLLs in NuGet packages at {selectedFrameworkFolder}.");
276+
logger.LogDebug($"Found {frameworkType} DLLs in NuGet packages at {selectedFrameworkFolder}.");
281277
}
282278

283279
private static DirectoryInfo[] GetPackageVersionSubDirectories(string packagePath)
@@ -361,18 +357,13 @@ private void RemoveNugetPackageReference(string packagePrefix, ISet<AssemblyLook
361357
foreach (var path in toRemove)
362358
{
363359
dllLocations.Remove(path);
364-
logger.LogInfo($"Removed reference {path}");
360+
logger.LogDebug($"Removed reference {path}");
365361
}
366362
}
367363

368-
private bool IsAspNetCoreDetected()
369-
{
370-
return fileContent.IsNewProjectStructureUsed && fileContent.UseAspNetCoreDlls;
371-
}
372-
373364
private void AddAspNetCoreFrameworkDlls(ISet<AssemblyLookupLocation> dllLocations, ISet<string> frameworkLocations)
374365
{
375-
if (!IsAspNetCoreDetected())
366+
if (!fileContent.IsAspNetCoreDetected)
376367
{
377368
return;
378369
}
@@ -413,103 +404,6 @@ private void AddMicrosoftWindowsDesktopDlls(ISet<AssemblyLookupLocation> dllLoca
413404
.FullName;
414405
}
415406

416-
private void GenerateSourceFileFromImplicitUsings()
417-
{
418-
var usings = new HashSet<string>();
419-
if (!fileContent.UseImplicitUsings)
420-
{
421-
return;
422-
}
423-
424-
// Hardcoded values from https://learn.microsoft.com/en-us/dotnet/core/project-sdk/overview#implicit-using-directives
425-
usings.UnionWith([ "System", "System.Collections.Generic", "System.IO", "System.Linq", "System.Net.Http", "System.Threading",
426-
"System.Threading.Tasks" ]);
427-
428-
if (fileContent.UseAspNetCoreDlls)
429-
{
430-
usings.UnionWith([ "System.Net.Http.Json", "Microsoft.AspNetCore.Builder", "Microsoft.AspNetCore.Hosting",
431-
"Microsoft.AspNetCore.Http", "Microsoft.AspNetCore.Routing", "Microsoft.Extensions.Configuration",
432-
"Microsoft.Extensions.DependencyInjection", "Microsoft.Extensions.Hosting", "Microsoft.Extensions.Logging" ]);
433-
}
434-
435-
if (fileContent.UseWindowsForms)
436-
{
437-
usings.UnionWith(["System.Drawing", "System.Windows.Forms"]);
438-
}
439-
440-
usings.UnionWith(fileContent.CustomImplicitUsings);
441-
442-
logger.LogInfo($"Generating source file for implicit usings. Namespaces: {string.Join(", ", usings.OrderBy(u => u))}");
443-
444-
if (usings.Count > 0)
445-
{
446-
var tempDir = GetTemporaryWorkingDirectory("implicitUsings");
447-
var path = Path.Combine(tempDir, "GlobalUsings.g.cs");
448-
using (var writer = new StreamWriter(path))
449-
{
450-
writer.WriteLine("// <auto-generated/>");
451-
writer.WriteLine("");
452-
453-
foreach (var u in usings.OrderBy(u => u))
454-
{
455-
writer.WriteLine($"global using global::{u};");
456-
}
457-
}
458-
459-
this.generatedSources.Add(path);
460-
}
461-
}
462-
463-
private void GenerateSourceFilesFromWebViews()
464-
{
465-
var views = fileProvider.RazorViews;
466-
if (views.Count == 0)
467-
{
468-
return;
469-
}
470-
471-
logger.LogInfo($"Found {views.Count} cshtml and razor files.");
472-
473-
if (!IsAspNetCoreDetected())
474-
{
475-
logger.LogInfo("Generating source files from cshtml files is only supported for new (SDK-style) project files");
476-
return;
477-
}
478-
479-
logger.LogInfo("Generating source files from cshtml and razor files...");
480-
481-
var sdk = new Sdk(dotnet).GetNewestSdk();
482-
if (sdk != null)
483-
{
484-
try
485-
{
486-
var razor = new Razor(sdk, dotnet, logger);
487-
var targetDir = GetTemporaryWorkingDirectory("razor");
488-
var generatedFiles = razor.GenerateFiles(views, usedReferences.Keys, targetDir);
489-
this.generatedSources.AddRange(generatedFiles);
490-
}
491-
catch (Exception ex)
492-
{
493-
// It's okay, we tried our best to generate source files from cshtml files.
494-
logger.LogInfo($"Failed to generate source files from cshtml files: {ex.Message}");
495-
}
496-
}
497-
}
498-
499-
/// <summary>
500-
/// Creates a temporary directory with the given subfolder name.
501-
/// The created directory might be inside the repo folder, and it is deleted when the object is disposed.
502-
/// </summary>
503-
/// <param name="subfolder"></param>
504-
/// <returns></returns>
505-
private string GetTemporaryWorkingDirectory(string subfolder)
506-
{
507-
var temp = Path.Combine(tempWorkingDirectory.ToString(), subfolder);
508-
Directory.CreateDirectory(temp);
509-
510-
return temp;
511-
}
512-
513407
/// <summary>
514408
/// Resolves conflicts between all of the resolved references.
515409
/// If the same assembly name is duplicated with different versions,
@@ -561,7 +455,7 @@ private void ResolveConflicts(IEnumerable<string> frameworkPaths)
561455
if (resolvedInfo.Version != r.Version || resolvedInfo.NetCoreVersion != r.NetCoreVersion)
562456
{
563457
var asm = resolvedInfo.Id + (resolvedInfo.NetCoreVersion is null ? "" : $" (.NET Core {resolvedInfo.NetCoreVersion})");
564-
logger.LogInfo($"Resolved {r.Id} as {asm}");
458+
logger.LogDebug($"Resolved {r.Id} as {asm}");
565459

566460
++conflictedReferences;
567461
}

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private DotNet(ILogger logger, string? dotNetPath, TemporaryDirectory tempWorkin
3535

3636
private void Info()
3737
{
38-
var res = dotnetCliInvoker.RunCommand("--info");
38+
var res = dotnetCliInvoker.RunCommand("--info", silent: false);
3939
if (!res)
4040
{
4141
throw new Exception($"{dotnetCliInvoker.Exec} --info failed.");
@@ -91,13 +91,13 @@ public bool AddPackage(string folder, string package)
9191
return dotnetCliInvoker.RunCommand(args);
9292
}
9393

94-
public IList<string> GetListedRuntimes() => GetResultList("--list-runtimes");
94+
public IList<string> GetListedRuntimes() => GetResultList("--list-runtimes", null, false);
9595

96-
public IList<string> GetListedSdks() => GetResultList("--list-sdks");
96+
public IList<string> GetListedSdks() => GetResultList("--list-sdks", null, false);
9797

98-
private IList<string> GetResultList(string args, string? workingDirectory = null)
98+
private IList<string> GetResultList(string args, string? workingDirectory = null, bool silent = true)
9999
{
100-
if (dotnetCliInvoker.RunCommand(args, workingDirectory, out var results))
100+
if (dotnetCliInvoker.RunCommand(args, workingDirectory, out var results, silent))
101101
{
102102
return results;
103103
}
@@ -316,4 +316,4 @@ public static BuildScript WithDotNet(IBuildActions actions, ILogger logger, IEnu
316316
});
317317
}
318318
}
319-
}
319+
}

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNetCliInvoker.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ private ProcessStartInfo MakeDotnetStartInfo(string args, string? workingDirecto
4040
return startInfo;
4141
}
4242

43-
private bool RunCommandAux(string args, string? workingDirectory, out IList<string> output)
43+
private bool RunCommandAux(string args, string? workingDirectory, out IList<string> output, bool silent)
4444
{
4545
var dirLog = string.IsNullOrWhiteSpace(workingDirectory) ? "" : $" in {workingDirectory}";
4646
logger.LogInfo($"Running {Exec} {args}{dirLog}");
4747
var pi = MakeDotnetStartInfo(args, workingDirectory);
4848
var threadId = Environment.CurrentManagedThreadId;
49-
void onOut(string s) => logger.LogInfo(s, threadId);
49+
void onOut(string s) => logger.Log(silent ? Severity.Debug : Severity.Info, s, threadId);
5050
void onError(string s) => logger.LogError(s, threadId);
5151
var exitCode = pi.ReadOutput(out output, onOut, onError);
5252
if (exitCode != 0)
@@ -57,13 +57,13 @@ private bool RunCommandAux(string args, string? workingDirectory, out IList<stri
5757
return true;
5858
}
5959

60-
public bool RunCommand(string args) =>
61-
RunCommandAux(args, null, out _);
60+
public bool RunCommand(string args, bool silent) =>
61+
RunCommandAux(args, null, out _, silent);
6262

63-
public bool RunCommand(string args, out IList<string> output) =>
64-
RunCommandAux(args, null, out output);
63+
public bool RunCommand(string args, out IList<string> output, bool silent) =>
64+
RunCommandAux(args, null, out output, silent);
6565

66-
public bool RunCommand(string args, string? workingDirectory, out IList<string> output) =>
67-
RunCommandAux(args, workingDirectory, out output);
66+
public bool RunCommand(string args, string? workingDirectory, out IList<string> output, bool silent) =>
67+
RunCommandAux(args, workingDirectory, out output, silent);
6868
}
6969
}

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/FileContent.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ public bool UseAspNetCoreDlls
5050
}
5151
}
5252

53+
public bool IsAspNetCoreDetected
54+
{
55+
get
56+
{
57+
return IsNewProjectStructureUsed && UseAspNetCoreDlls;
58+
}
59+
}
60+
5361
private bool useImplicitUsings = false;
5462

5563
public bool UseImplicitUsings

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/IDotNetCliInvoker.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,22 @@ internal interface IDotNetCliInvoker
1111

1212
/// <summary>
1313
/// Execute `dotnet <args>` and return true if the command succeeded, otherwise false.
14+
/// If `silent` is true the output of the command is logged as `debug` otherwise as `info`.
1415
/// </summary>
15-
bool RunCommand(string args);
16+
bool RunCommand(string args, bool silent = true);
1617

1718
/// <summary>
1819
/// Execute `dotnet <args>` and return true if the command succeeded, otherwise false.
1920
/// The output of the command is returned in `output`.
21+
/// If `silent` is true the output of the command is logged as `debug` otherwise as `info`.
2022
/// </summary>
21-
bool RunCommand(string args, out IList<string> output);
23+
bool RunCommand(string args, out IList<string> output, bool silent = true);
2224

2325
/// <summary>
2426
/// Execute `dotnet <args>` in `<workingDirectory>` and return true if the command succeeded, otherwise false.
2527
/// The output of the command is returned in `output`.
28+
/// If `silent` is true the output of the command is logged as `debug` otherwise as `info`.
2629
/// </summary>
27-
bool RunCommand(string args, string? workingDirectory, out IList<string> output);
30+
bool RunCommand(string args, string? workingDirectory, out IList<string> output, bool silent = true);
2831
}
2932
}

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/NugetExeWrapper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ private bool TryRestoreNugetPackage(string package)
175175
};
176176

177177
var threadId = Environment.CurrentManagedThreadId;
178-
void onOut(string s) => logger.LogInfo(s, threadId);
178+
void onOut(string s) => logger.LogDebug(s, threadId);
179179
void onError(string s) => logger.LogError(s, threadId);
180180
var exitCode = pi.ReadOutput(out var _, onOut, onError);
181181
if (exitCode != 0)
@@ -235,7 +235,7 @@ private void RunMonoNugetCommand(string command, out IList<string> stdout)
235235
};
236236

237237
var threadId = Environment.CurrentManagedThreadId;
238-
void onOut(string s) => logger.LogInfo(s, threadId);
238+
void onOut(string s) => logger.LogDebug(s, threadId);
239239
void onError(string s) => logger.LogError(s, threadId);
240240
pi.ReadOutput(out stdout, onOut, onError);
241241
}

0 commit comments

Comments
 (0)