1
- using Semmle . Util . Logging ;
2
1
using System . Collections . Generic ;
3
2
using System . IO ;
4
3
using System . Linq ;
5
4
using Semmle . Util ;
5
+ using Semmle . Util . Logging ;
6
6
7
7
namespace Semmle . Extraction . PowerShell . Standalone
8
8
{
@@ -36,22 +36,31 @@ public override bool HandleOption(string key, string value)
36
36
case "exclude" :
37
37
Excludes . Add ( value ) ;
38
38
return true ;
39
+ case "file-list" :
40
+ Files = File . ReadAllLines ( value ) . Select ( f => new FileInfo ( f ) ) . ToArray ( ) ;
41
+ return true ;
39
42
default :
40
43
return base . HandleOption ( key , value ) ;
41
44
}
42
45
}
43
46
44
47
public override bool HandleArgument ( string arg )
45
48
{
46
- SrcDir = arg ;
47
- if ( ! new FileInfo ( SrcDir ) . Exists )
49
+ if ( ! new FileInfo ( arg ) . Exists )
48
50
{
49
- var di = new DirectoryInfo ( SrcDir ) ;
51
+ var di = new DirectoryInfo ( arg ) ;
50
52
if ( ! di . Exists )
51
53
{
52
- System . Console . WriteLine ( "Error: The file or directory {0} does not exist" , di . FullName ) ;
54
+ System . Console . WriteLine (
55
+ "Error: The file or directory {0} does not exist" ,
56
+ di . FullName
57
+ ) ;
53
58
Errors = true ;
54
59
}
60
+ else
61
+ {
62
+ Files = di . GetFiles ( "*.*" , SearchOption . AllDirectories ) ;
63
+ }
55
64
}
56
65
return true ;
57
66
}
@@ -70,12 +79,48 @@ public override void InvalidArgument(string argument)
70
79
/// <summary>
71
80
/// Files/patterns to exclude.
72
81
/// </summary>
73
- public IList < string > Excludes { get ; } = new List < string > ( ) { "node_modules" , "bower_components" } ;
82
+ public IList < string > Excludes { get ; } =
83
+ new List < string > ( ) { "node_modules" , "bower_components" } ;
84
+
85
+ /// <summary>
86
+ /// Returns a FileInfo object for each file in the given path (recursively).
87
+ /// </summary>
88
+ private static FileInfo [ ] GetFiles ( string path )
89
+ {
90
+ var di = new DirectoryInfo ( path ) ;
91
+ return di . Exists
92
+ ? di . GetFiles ( "*.*" , SearchOption . AllDirectories )
93
+ : new FileInfo [ ] { new FileInfo ( path ) } ;
94
+ }
95
+
96
+ /// <summary>
97
+ /// Returns a list of files to extract. By default, this is the list of all files in
98
+ /// the current directory. However, if the LGTM_INDEX_INCLUDE environment variable is
99
+ /// set, it is used as a list of files to include instead of the files from the current
100
+ /// directory.
101
+ /// </summary>
102
+ private static FileInfo [ ] GetDefaultFiles ( )
103
+ {
104
+ // Check if the LGTM_INDEX_INCLUDE environmen variable exists
105
+ var include = System . Environment . GetEnvironmentVariable ( "LGTM_INDEX_INCLUDE" ) ;
106
+ if ( include != null )
107
+ {
108
+ System . Console . WriteLine ( "Using LGTM_INDEX_INCLUDE: {0}" , include ) ;
109
+ return include . Split ( ';' ) . Select ( GetFiles ) . SelectMany ( f => f ) . ToArray ( ) ;
110
+ }
111
+ else
112
+ {
113
+ return new DirectoryInfo ( Directory . GetCurrentDirectory ( ) ) . GetFiles (
114
+ "*.*" ,
115
+ SearchOption . AllDirectories
116
+ ) ;
117
+ }
118
+ }
74
119
75
120
/// <summary>
76
121
/// The directory or file containing the source code;
77
122
/// </summary>
78
- public string SrcDir { get ; set ; } = Directory . GetCurrentDirectory ( ) ;
123
+ public FileInfo [ ] Files { get ; set ; } = GetDefaultFiles ( ) ;
79
124
80
125
/// <summary>
81
126
/// Whether the extraction phase should be skipped (dry-run).
@@ -109,19 +154,20 @@ public bool ExcludesFile(string path)
109
154
/// </summary>
110
155
public static void ShowHelp ( System . IO . TextWriter output )
111
156
{
112
- output . WriteLine ( "PowerShell# standalone extractor\n \n Extracts PowerShell scripts in the current directory.\n " ) ;
157
+ output . WriteLine (
158
+ "PowerShell# standalone extractor\n \n Extracts PowerShell scripts in the current directory.\n "
159
+ ) ;
113
160
output . WriteLine ( "Additional options:\n " ) ;
114
161
output . WriteLine ( " <path> Use the provided path instead." ) ;
115
- output . WriteLine ( " --exclude:xxx Exclude a file or directory (can be specified multiple times)" ) ;
162
+ output . WriteLine (
163
+ " --exclude:xxx Exclude a file or directory (can be specified multiple times)"
164
+ ) ;
116
165
output . WriteLine ( " --dry-run Stop before extraction" ) ;
117
166
output . WriteLine ( " --threads:nnn Specify number of threads (default=CPU cores)" ) ;
118
167
output . WriteLine ( " --verbose Produce more output" ) ;
119
-
120
168
}
121
169
122
- private Options ( )
123
- {
124
- }
170
+ private Options ( ) { }
125
171
126
172
public static Options Create ( string [ ] args )
127
173
{
0 commit comments