11using System ;
22using System . Collections . Generic ;
3- using System . Diagnostics ;
43using System . IO ;
54using System . Linq ;
65using System . Text . RegularExpressions ;
7- using Semmle . Util ;
86
97namespace Semmle . Extraction . CSharp . DependencyFetching
108{
@@ -13,63 +11,27 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
1311 /// </summary>
1412 internal partial class DotNet : IDotNet
1513 {
14+ private readonly IDotnetCommand dotnet ;
1615 private readonly ProgressMonitor progressMonitor ;
17- private readonly string dotnet ;
1816
19- public DotNet ( IDependencyOptions options , ProgressMonitor progressMonitor )
17+ internal DotNet ( IDotnetCommand dotnet , ProgressMonitor progressMonitor )
2018 {
2119 this . progressMonitor = progressMonitor ;
22- this . dotnet = Path . Combine ( options . DotNetPath ?? string . Empty , " dotnet" ) ;
20+ this . dotnet = dotnet ;
2321 Info ( ) ;
2422 }
2523
24+ public DotNet ( IDependencyOptions options , ProgressMonitor progressMonitor ) : this ( new DotnetCommand ( progressMonitor , Path . Combine ( options . DotNetPath ?? string . Empty , "dotnet" ) ) , progressMonitor ) { }
25+
26+
2627 private void Info ( )
2728 {
2829 // TODO: make sure the below `dotnet` version is matching the one specified in global.json
29- var res = RunCommand ( "--info" ) ;
30+ var res = dotnet . RunCommand ( "--info" ) ;
3031 if ( ! res )
3132 {
32- throw new Exception ( $ "{ dotnet } --info failed.") ;
33- }
34- }
35-
36- private ProcessStartInfo MakeDotnetStartInfo ( string args , bool redirectStandardOutput )
37- {
38- var startInfo = new ProcessStartInfo ( dotnet , args )
39- {
40- UseShellExecute = false ,
41- RedirectStandardOutput = redirectStandardOutput
42- } ;
43- // Set the .NET CLI language to English to avoid localized output.
44- startInfo . EnvironmentVariables [ "DOTNET_CLI_UI_LANGUAGE" ] = "en" ;
45- return startInfo ;
46- }
47-
48- private bool RunCommand ( string args )
49- {
50- progressMonitor . RunningProcess ( $ "{ dotnet } { args } ") ;
51- using var proc = Process . Start ( MakeDotnetStartInfo ( args , redirectStandardOutput : false ) ) ;
52- proc ? . WaitForExit ( ) ;
53- var exitCode = proc ? . ExitCode ?? - 1 ;
54- if ( exitCode != 0 )
55- {
56- progressMonitor . CommandFailed ( dotnet , args , exitCode ) ;
57- return false ;
58- }
59- return true ;
60- }
61-
62- private bool RunCommand ( string args , out IList < string > output )
63- {
64- progressMonitor . RunningProcess ( $ "{ dotnet } { args } ") ;
65- var pi = MakeDotnetStartInfo ( args , redirectStandardOutput : true ) ;
66- var exitCode = pi . ReadOutput ( out output ) ;
67- if ( exitCode != 0 )
68- {
69- progressMonitor . CommandFailed ( dotnet , args , exitCode ) ;
70- return false ;
33+ throw new Exception ( $ "{ dotnet . Exec } --info failed.") ;
7134 }
72- return true ;
7335 }
7436
7537 private static string GetRestoreArgs ( string projectOrSolutionFile , string packageDirectory ) =>
@@ -82,7 +44,7 @@ public bool RestoreProjectToDirectory(string projectFile, string packageDirector
8244 {
8345 args += $ " --configfile \" { pathToNugetConfig } \" ";
8446 }
85- var success = RunCommand ( args , out var output ) ;
47+ var success = dotnet . RunCommand ( args , out var output ) ;
8648 stdout = string . Join ( "\n " , output ) ;
8749 return success ;
8850 }
@@ -91,7 +53,7 @@ public bool RestoreSolutionToDirectory(string solutionFile, string packageDirect
9153 {
9254 var args = GetRestoreArgs ( solutionFile , packageDirectory ) ;
9355 args += " --verbosity normal" ;
94- if ( RunCommand ( args , out var output ) )
56+ if ( dotnet . RunCommand ( args , out var output ) )
9557 {
9658 var regex = RestoreProjectRegex ( ) ;
9759 projects = output
@@ -108,13 +70,13 @@ public bool RestoreSolutionToDirectory(string solutionFile, string packageDirect
10870 public bool New ( string folder )
10971 {
11072 var args = $ "new console --no-restore --output \" { folder } \" ";
111- return RunCommand ( args ) ;
73+ return dotnet . RunCommand ( args ) ;
11274 }
11375
11476 public bool AddPackage ( string folder , string package )
11577 {
11678 var args = $ "add \" { folder } \" package \" { package } \" --no-restore";
117- return RunCommand ( args ) ;
79+ return dotnet . RunCommand ( args ) ;
11880 }
11981
12082 public IList < string > GetListedRuntimes ( ) => GetListed ( "--list-runtimes" , "runtime" ) ;
@@ -123,7 +85,7 @@ public bool AddPackage(string folder, string package)
12385
12486 private IList < string > GetListed ( string args , string artifact )
12587 {
126- if ( RunCommand ( args , out var artifacts ) )
88+ if ( dotnet . RunCommand ( args , out var artifacts ) )
12789 {
12890 progressMonitor . LogInfo ( $ "Found { artifact } s: { string . Join ( "\n " , artifacts ) } ") ;
12991 return artifacts ;
@@ -134,7 +96,7 @@ private IList<string> GetListed(string args, string artifact)
13496 public bool Exec ( string execArgs )
13597 {
13698 var args = $ "exec { execArgs } ";
137- return RunCommand ( args ) ;
99+ return dotnet . RunCommand ( args ) ;
138100 }
139101
140102 [ GeneratedRegex ( "Restored\\ s+(.+\\ .csproj)" , RegexOptions . Compiled ) ]
0 commit comments