2
2
using System . Collections . Generic ;
3
3
using System . Diagnostics ;
4
4
using System . IO ;
5
+ using System . Linq ;
6
+ using System . Text . RegularExpressions ;
5
7
using Semmle . Util ;
6
8
7
9
namespace Semmle . Extraction . CSharp . DependencyFetching
8
10
{
9
11
/// <summary>
10
12
/// Utilities to run the "dotnet" command.
11
13
/// </summary>
12
- internal class DotNet : IDotNet
14
+ internal partial class DotNet : IDotNet
13
15
{
14
16
private readonly ProgressMonitor progressMonitor ;
15
17
private readonly string dotnet ;
@@ -41,7 +43,7 @@ private ProcessStartInfo MakeDotnetStartInfo(string args, bool redirectStandardO
41
43
private bool RunCommand ( string args )
42
44
{
43
45
progressMonitor . RunningProcess ( $ "{ dotnet } { args } ") ;
44
- using var proc = Process . Start ( this . MakeDotnetStartInfo ( args , redirectStandardOutput : false ) ) ;
46
+ using var proc = Process . Start ( MakeDotnetStartInfo ( args , redirectStandardOutput : false ) ) ;
45
47
proc ? . WaitForExit ( ) ;
46
48
var exitCode = proc ? . ExitCode ?? - 1 ;
47
49
if ( exitCode != 0 )
@@ -52,14 +54,51 @@ private bool RunCommand(string args)
52
54
return true ;
53
55
}
54
56
55
- public bool RestoreToDirectory ( string projectOrSolutionFile , string packageDirectory , string ? pathToNugetConfig = null )
57
+ private bool RunCommand ( string args , out IList < string > output )
56
58
{
57
- var args = $ "restore --no-dependencies \" { projectOrSolutionFile } \" --packages \" { packageDirectory } \" /p:DisableImplicitNuGetFallbackFolder=true";
59
+ progressMonitor . RunningProcess ( $ "{ dotnet } { args } ") ;
60
+ var pi = MakeDotnetStartInfo ( args , redirectStandardOutput : true ) ;
61
+ var exitCode = pi . ReadOutput ( out output ) ;
62
+ if ( exitCode != 0 )
63
+ {
64
+ progressMonitor . CommandFailed ( dotnet , args , exitCode ) ;
65
+ return false ;
66
+ }
67
+ return true ;
68
+ }
69
+
70
+ private static string GetRestoreArgs ( string projectOrSolutionFile , string packageDirectory ) =>
71
+ $ "restore --no-dependencies \" { projectOrSolutionFile } \" --packages \" { packageDirectory } \" /p:DisableImplicitNuGetFallbackFolder=true";
72
+
73
+ public bool RestoreProjectToDirectory ( string projectFile , string packageDirectory , string ? pathToNugetConfig = null )
74
+ {
75
+ var args = GetRestoreArgs ( projectFile , packageDirectory ) ;
58
76
if ( pathToNugetConfig != null )
77
+ {
59
78
args += $ " --configfile \" { pathToNugetConfig } \" ";
79
+ }
60
80
return RunCommand ( args ) ;
61
81
}
62
82
83
+ public bool RestoreSolutionToDirectory ( string solutionFile , string packageDirectory , out IList < string > projects )
84
+ {
85
+ var args = GetRestoreArgs ( solutionFile , packageDirectory ) ;
86
+ args += " --verbosity normal" ;
87
+ if ( RunCommand ( args , out var output ) )
88
+ {
89
+ var regex = RestoreProjectRegex ( ) ;
90
+ projects = output
91
+ . Select ( line => regex . Match ( line ) )
92
+ . Where ( match => match . Success )
93
+ . Select ( match => match . Groups [ 1 ] . Value )
94
+ . ToList ( ) ;
95
+ return true ;
96
+ }
97
+
98
+ projects = new List < string > ( ) ;
99
+ return false ;
100
+ }
101
+
63
102
public bool New ( string folder )
64
103
{
65
104
var args = $ "new console --no-restore --output \" { folder } \" ";
@@ -78,22 +117,21 @@ public bool AddPackage(string folder, string package)
78
117
79
118
private IList < string > GetListed ( string args , string artifact )
80
119
{
81
- progressMonitor . RunningProcess ( $ "{ dotnet } { args } ") ;
82
- var pi = this . MakeDotnetStartInfo ( args , redirectStandardOutput : true ) ;
83
- var exitCode = pi . ReadOutput ( out var artifacts ) ;
84
- if ( exitCode != 0 )
120
+ if ( RunCommand ( args , out var artifacts ) )
85
121
{
86
- progressMonitor . CommandFailed ( dotnet , args , exitCode ) ;
87
- return new List < string > ( ) ;
122
+ progressMonitor . LogInfo ( $ "Found { artifact } s: { string . Join ( " \n " , artifacts ) } " ) ;
123
+ return artifacts ;
88
124
}
89
- progressMonitor . LogInfo ( $ "Found { artifact } s: { string . Join ( "\n " , artifacts ) } ") ;
90
- return artifacts ;
125
+ return new List < string > ( ) ;
91
126
}
92
127
93
128
public bool Exec ( string execArgs )
94
129
{
95
130
var args = $ "exec { execArgs } ";
96
131
return RunCommand ( args ) ;
97
132
}
133
+
134
+ [ GeneratedRegex ( "Restored\\ s+(.+\\ .csproj)" , RegexOptions . Compiled ) ]
135
+ private static partial Regex RestoreProjectRegex ( ) ;
98
136
}
99
137
}
0 commit comments