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,30 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
1311 /// </summary>
1412 internal partial class DotNet : IDotNet
1513 {
14+ private readonly IDotNetCliInvoker dotnetCliInvoker ;
1615 private readonly ProgressMonitor progressMonitor ;
17- private readonly string dotnet ;
1816
19- public DotNet ( IDependencyOptions options , ProgressMonitor progressMonitor )
17+ private DotNet ( IDotNetCliInvoker dotnetCliInvoker , ProgressMonitor progressMonitor )
2018 {
2119 this . progressMonitor = progressMonitor ;
22- this . dotnet = Path . Combine ( options . DotNetPath ?? string . Empty , "dotnet" ) ;
20+ this . dotnetCliInvoker = dotnetCliInvoker ;
2321 Info ( ) ;
2422 }
2523
26- private void Info ( )
27- {
28- // TODO: make sure the below `dotnet` version is matching the one specified in global.json
29- var res = RunCommand ( "--info" ) ;
30- if ( ! res )
31- {
32- throw new Exception ( $ "{ dotnet } --info failed.") ;
33- }
34- }
24+ private DotNet ( IDependencyOptions options , ProgressMonitor progressMonitor ) : this ( new DotNetCliInvoker ( progressMonitor , Path . Combine ( options . DotNetPath ?? string . Empty , "dotnet" ) ) , progressMonitor ) { }
3525
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- }
26+ internal static IDotNet Make ( IDotNetCliInvoker dotnetCliInvoker , ProgressMonitor progressMonitor ) => new DotNet ( dotnetCliInvoker , progressMonitor ) ;
4727
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- }
28+ public static IDotNet Make ( IDependencyOptions options , ProgressMonitor progressMonitor ) => new DotNet ( options , progressMonitor ) ;
6129
62- private bool RunCommand ( string args , out IList < string > output )
30+ private void Info ( )
6331 {
64- progressMonitor . RunningProcess ( $ "{ dotnet } { args } ") ;
65- var pi = MakeDotnetStartInfo ( args , redirectStandardOutput : true ) ;
66- var exitCode = pi . ReadOutput ( out output ) ;
67- if ( exitCode != 0 )
32+ // TODO: make sure the below `dotnet` version is matching the one specified in global.json
33+ var res = dotnetCliInvoker . RunCommand ( "--info" ) ;
34+ if ( ! res )
6835 {
69- progressMonitor . CommandFailed ( dotnet , args , exitCode ) ;
70- return false ;
36+ throw new Exception ( $ "{ dotnetCliInvoker . Exec } --info failed.") ;
7137 }
72- return true ;
7338 }
7439
7540 private static string GetRestoreArgs ( string projectOrSolutionFile , string packageDirectory ) =>
@@ -82,7 +47,7 @@ public bool RestoreProjectToDirectory(string projectFile, string packageDirector
8247 {
8348 args += $ " --configfile \" { pathToNugetConfig } \" ";
8449 }
85- var success = RunCommand ( args , out var output ) ;
50+ var success = dotnetCliInvoker . RunCommand ( args , out var output ) ;
8651 stdout = string . Join ( "\n " , output ) ;
8752 return success ;
8853 }
@@ -91,7 +56,7 @@ public bool RestoreSolutionToDirectory(string solutionFile, string packageDirect
9156 {
9257 var args = GetRestoreArgs ( solutionFile , packageDirectory ) ;
9358 args += " --verbosity normal" ;
94- if ( RunCommand ( args , out var output ) )
59+ if ( dotnetCliInvoker . RunCommand ( args , out var output ) )
9560 {
9661 var regex = RestoreProjectRegex ( ) ;
9762 projects = output
@@ -108,13 +73,13 @@ public bool RestoreSolutionToDirectory(string solutionFile, string packageDirect
10873 public bool New ( string folder )
10974 {
11075 var args = $ "new console --no-restore --output \" { folder } \" ";
111- return RunCommand ( args ) ;
76+ return dotnetCliInvoker . RunCommand ( args ) ;
11277 }
11378
11479 public bool AddPackage ( string folder , string package )
11580 {
11681 var args = $ "add \" { folder } \" package \" { package } \" --no-restore";
117- return RunCommand ( args ) ;
82+ return dotnetCliInvoker . RunCommand ( args ) ;
11883 }
11984
12085 public IList < string > GetListedRuntimes ( ) => GetListed ( "--list-runtimes" , "runtime" ) ;
@@ -123,7 +88,7 @@ public bool AddPackage(string folder, string package)
12388
12489 private IList < string > GetListed ( string args , string artifact )
12590 {
126- if ( RunCommand ( args , out var artifacts ) )
91+ if ( dotnetCliInvoker . RunCommand ( args , out var artifacts ) )
12792 {
12893 progressMonitor . LogInfo ( $ "Found { artifact } s: { string . Join ( "\n " , artifacts ) } ") ;
12994 return artifacts ;
@@ -134,7 +99,7 @@ private IList<string> GetListed(string args, string artifact)
13499 public bool Exec ( string execArgs )
135100 {
136101 var args = $ "exec { execArgs } ";
137- return RunCommand ( args ) ;
102+ return dotnetCliInvoker . RunCommand ( args ) ;
138103 }
139104
140105 [ GeneratedRegex ( "Restored\\ s+(.+\\ .csproj)" , RegexOptions . Compiled ) ]
0 commit comments