Skip to content

Commit df962d7

Browse files
committed
PS: Also extract files from the PSModulePath environment variable.
1 parent 2f835e5 commit df962d7

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

powershell/extractor/Semmle.Extraction.PowerShell.Standalone/Options.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public override bool HandleFlag(string key, bool value)
2424
case "dry-run":
2525
SkipExtraction = value;
2626
return true;
27+
case "skip-psmodulepath-files":
28+
SkipPSModulePathFiles = value;
29+
return true;
2730
default:
2831
return base.HandleFlag(key, value);
2932
}
@@ -127,6 +130,12 @@ private static FileInfo[] GetDefaultFiles()
127130
/// </summary>
128131
public bool SkipExtraction { get; private set; } = false;
129132

133+
/// <summary>
134+
/// Whether to extract files in the paths found in the `PSModulePath`
135+
/// environment variable.
136+
/// </summary>
137+
public bool SkipPSModulePathFiles { get; private set; } = false;
138+
130139
/// <summary>
131140
/// Whether errors were encountered parsing the arguments.
132141
/// </summary>
@@ -158,13 +167,14 @@ public static void ShowHelp(System.IO.TextWriter output)
158167
"PowerShell# standalone extractor\n\nExtracts PowerShell scripts in the current directory.\n"
159168
);
160169
output.WriteLine("Additional options:\n");
161-
output.WriteLine(" <path> Use the provided path instead.");
170+
output.WriteLine(" <path> Use the provided path instead.");
162171
output.WriteLine(
163-
" --exclude:xxx Exclude a file or directory (can be specified multiple times)"
172+
" --exclude:xxx Exclude a file or directory (can be specified multiple times)"
164173
);
165-
output.WriteLine(" --dry-run Stop before extraction");
166-
output.WriteLine(" --threads:nnn Specify number of threads (default=CPU cores)");
167-
output.WriteLine(" --verbose Produce more output");
174+
output.WriteLine(" --dry-run Stop before extraction");
175+
output.WriteLine(" --threads:nnn Specify number of threads (default=CPU cores)");
176+
output.WriteLine(" --verbose Produce more output");
177+
output.WriteLine(" --skip-psmodulepath-files Avoid extracting source files in paths specified by the PSModulePath environment variable.");
168178
}
169179

170180
private Options() { }

powershell/extractor/Semmle.Extraction.PowerShell.Standalone/Program.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public static int Main(string[] args)
4545

4646
output.Log(Severity.Info, "Running PowerShell standalone extractor");
4747
var sourceFiles = options
48-
.Files.Where(d =>
48+
.Files.Concat(GetPSModuleFiles(options))
49+
.Where(d =>
4950
options.Extensions.Contains(
5051
d.Extension,
5152
StringComparer.InvariantCultureIgnoreCase
@@ -87,6 +88,30 @@ public static int Main(string[] args)
8788
return 0;
8889
}
8990

91+
private static String[] GetPSModulePaths()
92+
{
93+
return Environment.GetEnvironmentVariable("PSModulePath")?.Split(Path.PathSeparator)
94+
?? Array.Empty<string>();
95+
}
96+
97+
private static IEnumerable<FileInfo> GetPSModuleFiles(Options options)
98+
{
99+
if(options.SkipPSModulePathFiles)
100+
{
101+
return Array.Empty<FileInfo>();
102+
}
103+
104+
return GetPSModulePaths()
105+
.Where(d => Directory.Exists(d))
106+
.SelectMany(d =>
107+
{
108+
var di = new DirectoryInfo(d);
109+
return di.Exists
110+
? di.GetFiles("*.*", SearchOption.AllDirectories)
111+
: new FileInfo[] { new(d) };
112+
});
113+
}
114+
90115
private class ExtractionProgress : IProgressMonitor
91116
{
92117
public ExtractionProgress(ILogger output)

0 commit comments

Comments
 (0)