Skip to content

Commit b6c2ea5

Browse files
committed
C#: Some re-factoring of NugetPackages and logic for file downloading.
1 parent e7dbe9f commit b6c2ea5

File tree

3 files changed

+54
-55
lines changed

3 files changed

+54
-55
lines changed

csharp/autobuilder/Semmle.Autobuild.Shared/BuildActions.cs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
using Semmle.Util;
2-
using System;
1+
using System;
32
using System.Collections.Generic;
43
using System.Diagnostics;
5-
using System.IO;
6-
using System.Xml;
7-
using System.Net.Http;
84
using System.Diagnostics.CodeAnalysis;
9-
using System.Threading.Tasks;
5+
using System.IO;
106
using System.Runtime.InteropServices;
7+
using System.Xml;
8+
using Semmle.Util;
119

1210
namespace Semmle.Autobuild.Shared
1311
{
@@ -283,17 +281,8 @@ XmlDocument IBuildActions.LoadXml(string filename)
283281

284282
public string EnvironmentExpandEnvironmentVariables(string s) => Environment.ExpandEnvironmentVariables(s);
285283

286-
private static async Task DownloadFileAsync(string address, string filename)
287-
{
288-
using var httpClient = new HttpClient();
289-
using var request = new HttpRequestMessage(HttpMethod.Get, address);
290-
using var contentStream = await (await httpClient.SendAsync(request)).Content.ReadAsStreamAsync();
291-
using var stream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None, 4096, true);
292-
await contentStream.CopyToAsync(stream);
293-
}
294-
295284
public void DownloadFile(string address, string fileName) =>
296-
DownloadFileAsync(address, fileName).Wait();
285+
FileUtils.DownloadFile(address, fileName);
297286

298287
public IDiagnosticsWriter CreateDiagnosticsWriter(string filename) => new DiagnosticsStream(filename);
299288

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

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,26 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
1414
/// </summary>
1515
internal class NugetPackages
1616
{
17+
private readonly string nugetExe;
18+
private readonly ProgressMonitor progressMonitor;
19+
20+
/// <summary>
21+
/// The list of package files.
22+
/// </summary>
23+
public FileInfo[] PackageFiles { get; }
24+
25+
/// <summary>
26+
/// The source directory used.
27+
/// </summary>
28+
public string SourceDirectory { get; }
29+
30+
/// <summary>
31+
/// The computed packages directory.
32+
/// This will be in the Temp location
33+
/// so as to not trample the source tree.
34+
/// </summary>
35+
public TemporaryDirectory PackageDirectory { get; }
36+
1737
/// <summary>
1838
/// Create the package manager for a specified source tree.
1939
/// </summary>
@@ -32,47 +52,11 @@ public NugetPackages(string sourceDir, TemporaryDirectory packageDirectory, Prog
3252
if (!File.Exists(nugetExe))
3353
throw new FileNotFoundException(string.Format("NuGet could not be found at {0}", nugetExe));
3454

35-
packages = new DirectoryInfo(SourceDirectory)
55+
PackageFiles = new DirectoryInfo(SourceDirectory)
3656
.EnumerateFiles("packages.config", SearchOption.AllDirectories)
3757
.ToArray();
3858
}
3959

40-
// List of package files to download.
41-
private readonly FileInfo[] packages;
42-
43-
/// <summary>
44-
/// The list of package files.
45-
/// </summary>
46-
public IEnumerable<FileInfo> PackageFiles => packages;
47-
48-
/// <summary>
49-
/// Download the packages to the temp folder.
50-
/// </summary>
51-
/// <param name="pm">The progress monitor used for reporting errors etc.</param>
52-
public void InstallPackages()
53-
{
54-
foreach (var package in packages)
55-
{
56-
RestoreNugetPackage(package.FullName);
57-
}
58-
}
59-
60-
/// <summary>
61-
/// The source directory used.
62-
/// </summary>
63-
public string SourceDirectory
64-
{
65-
get;
66-
private set;
67-
}
68-
69-
/// <summary>
70-
/// The computed packages directory.
71-
/// This will be in the Temp location
72-
/// so as to not trample the source tree.
73-
/// </summary>
74-
public TemporaryDirectory PackageDirectory { get; }
75-
7660
/// <summary>
7761
/// Restore all files in a specified package.
7862
/// </summary>
@@ -133,7 +117,16 @@ private void RestoreNugetPackage(string package)
133117
}
134118
}
135119

136-
private readonly string nugetExe;
137-
private readonly ProgressMonitor progressMonitor;
120+
/// <summary>
121+
/// Download the packages to the temp folder.
122+
/// </summary>
123+
/// <param name="pm">The progress monitor used for reporting errors etc.</param>
124+
public void InstallPackages()
125+
{
126+
foreach (var package in PackageFiles)
127+
{
128+
RestoreNugetPackage(package.FullName);
129+
}
130+
}
138131
}
139132
}

csharp/extractor/Semmle.Util/FileUtils.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
22
using System.IO;
33
using System.Linq;
4+
using System.Net.Http;
45
using System.Security.Cryptography;
56
using System.Text;
7+
using System.Threading.Tasks;
68

79
namespace Semmle.Util
810
{
@@ -91,5 +93,20 @@ public static string ComputeFileHash(string filePath)
9193
hex.AppendFormat("{0:x2}", b);
9294
return hex.ToString();
9395
}
96+
97+
private static async Task DownloadFileAsync(string address, string filename)
98+
{
99+
using var httpClient = new HttpClient();
100+
using var request = new HttpRequestMessage(HttpMethod.Get, address);
101+
using var contentStream = await (await httpClient.SendAsync(request)).Content.ReadAsStreamAsync();
102+
using var stream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None, 4096, true);
103+
await contentStream.CopyToAsync(stream);
104+
}
105+
106+
/// <summary>
107+
/// Downloads the file at <paramref name="address"/> to <paramref name="fileName"/>.
108+
/// </summary>
109+
public static void DownloadFile(string address, string fileName) =>
110+
DownloadFileAsync(address, fileName).Wait();
94111
}
95112
}

0 commit comments

Comments
 (0)