1
1
using System ;
2
2
using System . Collections . Generic ;
3
- using System . Diagnostics ;
4
3
using System . IO ;
5
4
using System . Linq ;
6
5
using System . Text . RegularExpressions ;
7
- using Semmle . Util ;
8
6
9
7
namespace Semmle . Extraction . CSharp . DependencyFetching
10
8
{
@@ -13,63 +11,27 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
13
11
/// </summary>
14
12
internal partial class DotNet : IDotNet
15
13
{
14
+ private readonly IDotnetCommand dotnet ;
16
15
private readonly ProgressMonitor progressMonitor ;
17
- private readonly string dotnet ;
18
16
19
- public DotNet ( IDependencyOptions options , ProgressMonitor progressMonitor )
17
+ internal DotNet ( IDotnetCommand dotnet , ProgressMonitor progressMonitor )
20
18
{
21
19
this . progressMonitor = progressMonitor ;
22
- this . dotnet = Path . Combine ( options . DotNetPath ?? string . Empty , " dotnet" ) ;
20
+ this . dotnet = dotnet ;
23
21
Info ( ) ;
24
22
}
25
23
24
+ public DotNet ( IDependencyOptions options , ProgressMonitor progressMonitor ) : this ( new DotnetCommand ( progressMonitor , Path . Combine ( options . DotNetPath ?? string . Empty , "dotnet" ) ) , progressMonitor ) { }
25
+
26
+
26
27
private void Info ( )
27
28
{
28
29
// 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" ) ;
30
31
if ( ! res )
31
32
{
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.") ;
71
34
}
72
- return true ;
73
35
}
74
36
75
37
private static string GetRestoreArgs ( string projectOrSolutionFile , string packageDirectory ) =>
@@ -82,7 +44,7 @@ public bool RestoreProjectToDirectory(string projectFile, string packageDirector
82
44
{
83
45
args += $ " --configfile \" { pathToNugetConfig } \" ";
84
46
}
85
- var success = RunCommand ( args , out var output ) ;
47
+ var success = dotnet . RunCommand ( args , out var output ) ;
86
48
stdout = string . Join ( "\n " , output ) ;
87
49
return success ;
88
50
}
@@ -91,7 +53,7 @@ public bool RestoreSolutionToDirectory(string solutionFile, string packageDirect
91
53
{
92
54
var args = GetRestoreArgs ( solutionFile , packageDirectory ) ;
93
55
args += " --verbosity normal" ;
94
- if ( RunCommand ( args , out var output ) )
56
+ if ( dotnet . RunCommand ( args , out var output ) )
95
57
{
96
58
var regex = RestoreProjectRegex ( ) ;
97
59
projects = output
@@ -108,13 +70,13 @@ public bool RestoreSolutionToDirectory(string solutionFile, string packageDirect
108
70
public bool New ( string folder )
109
71
{
110
72
var args = $ "new console --no-restore --output \" { folder } \" ";
111
- return RunCommand ( args ) ;
73
+ return dotnet . RunCommand ( args ) ;
112
74
}
113
75
114
76
public bool AddPackage ( string folder , string package )
115
77
{
116
78
var args = $ "add \" { folder } \" package \" { package } \" --no-restore";
117
- return RunCommand ( args ) ;
79
+ return dotnet . RunCommand ( args ) ;
118
80
}
119
81
120
82
public IList < string > GetListedRuntimes ( ) => GetListed ( "--list-runtimes" , "runtime" ) ;
@@ -123,7 +85,7 @@ public bool AddPackage(string folder, string package)
123
85
124
86
private IList < string > GetListed ( string args , string artifact )
125
87
{
126
- if ( RunCommand ( args , out var artifacts ) )
88
+ if ( dotnet . RunCommand ( args , out var artifacts ) )
127
89
{
128
90
progressMonitor . LogInfo ( $ "Found { artifact } s: { string . Join ( "\n " , artifacts ) } ") ;
129
91
return artifacts ;
@@ -134,7 +96,7 @@ private IList<string> GetListed(string args, string artifact)
134
96
public bool Exec ( string execArgs )
135
97
{
136
98
var args = $ "exec { execArgs } ";
137
- return RunCommand ( args ) ;
99
+ return dotnet . RunCommand ( args ) ;
138
100
}
139
101
140
102
[ GeneratedRegex ( "Restored\\ s+(.+\\ .csproj)" , RegexOptions . Compiled ) ]
0 commit comments