Skip to content

Commit b71db62

Browse files
author
Kayla Davis
committed
Finds all dot sourced files in a ScriptFile
1 parent e307480 commit b71db62

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

src/PowerShellEditorServices/Language/AstOperations.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//
55

66
using System.Collections.Generic;
7+
using System.Linq;
78
using System.Management.Automation;
89
using System.Management.Automation.Language;
910
using System.Management.Automation.Runspaces;
@@ -135,5 +136,18 @@ static public SymbolReference FindDefinitionOfSymbol(Ast scriptAst, SymbolRefere
135136

136137
return declarationVisitor.FoundDeclartion;
137138
}
139+
140+
/// <summary>
141+
/// Finds all files dot sourced in a script
142+
/// </summary>
143+
/// <param name="scriptAst">The abstract syntax tree of the given script</param>
144+
/// <returns></returns>
145+
static public string[] FindDotSourcedIncludes(Ast scriptAst)
146+
{
147+
FindDotSourcedVisitor dotSourcedVisitor = new FindDotSourcedVisitor();
148+
scriptAst.Visit(dotSourcedVisitor);
149+
150+
return dotSourcedVisitor.DotSourcedFiles.ToArray();
151+
}
138152
}
139153
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using Microsoft.PowerShell.EditorServices.Session;
7+
using System.Collections.Generic;
8+
using System.IO;
9+
using System.Management.Automation.Language;
10+
11+
namespace Microsoft.PowerShell.EditorServices.Language
12+
{
13+
/// <summary>
14+
/// The vistor used to find the dont sourced files in an AST
15+
/// </summary>
16+
internal class FindDotSourcedVisitor : AstVisitor
17+
{
18+
/// <summary>
19+
/// A hash set of the dot sourced files (because we don't want duplicates)
20+
/// </summary>
21+
public HashSet<string> DotSourcedFiles { get; private set; }
22+
23+
public FindDotSourcedVisitor()
24+
{
25+
this.DotSourcedFiles = new HashSet<string>();
26+
}
27+
28+
/// <summary>
29+
/// Checks to see if the command invocation is a dot
30+
/// in order to find a dot sourced file
31+
/// </summary>
32+
/// <param name="commandAst">A CommandAst object in the script's AST</param>
33+
/// <returns>A descion to stop searching if the right commandAst was found,
34+
/// or a decision to continue if it wasn't found</returns>
35+
public override AstVisitAction VisitCommand(CommandAst commandAst)
36+
{
37+
if (commandAst.InvocationOperator.Equals(TokenKind.Dot))
38+
{
39+
string fileName = commandAst.CommandElements[0].Extent.Text;
40+
DotSourcedFiles.Add(fileName);
41+
}
42+
return base.VisitCommand(commandAst);
43+
}
44+
}
45+
}

src/PowerShellEditorServices/PowerShellEditorServices.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<Compile Include="Language\CompletionResults.cs" />
7373
<Compile Include="Language\FindCommandVisitor.cs" />
7474
<Compile Include="Language\FindDeclartionVisitor.cs" />
75+
<Compile Include="Language\FindDotSourcedVisitor.cs" />
7576
<Compile Include="Language\FindOccurrencesResult.cs" />
7677
<Compile Include="Language\FindReferencesResult.cs" />
7778
<Compile Include="Language\FindReferencesVisitor.cs" />

src/PowerShellEditorServices/Session/ScriptFile.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
44
//
55

6+
using Microsoft.PowerShell.EditorServices.Language;
67
using Microsoft.PowerShell.EditorServices.Utility;
78
using System;
89
using System.Collections.Generic;
@@ -78,6 +79,15 @@ public Token[] ScriptTokens
7879
get { return this.scriptTokens; }
7980
}
8081

82+
/// <summary>
83+
/// Gets the array of filepaths dot sourced in this ScriptFile
84+
/// </summary>
85+
public string[] ReferencedFiles
86+
{
87+
get;
88+
private set;
89+
}
90+
8191
#endregion
8292

8393
#region Constructors
@@ -224,6 +234,16 @@ private void ReadFile(TextReader textReader)
224234

225235
// Parse the contents to get syntax tree and errors
226236
this.ParseFileContents();
237+
this.FindDotSourcedFiles();
238+
}
239+
240+
/// <summary>
241+
/// Finds the dot sourced files in this ScriptFile
242+
/// </summary>
243+
private void FindDotSourcedFiles()
244+
{
245+
ReferencedFiles =
246+
AstOperations.FindDotSourcedIncludes(this.ScriptAst);
227247
}
228248

229249
/// <summary>

test/PowerShellEditorServices.Test/Session/FileChangeTests.cs renamed to test/PowerShellEditorServices.Test/Session/ScriptFileTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,25 @@ public void CanApplyMultiLineDelete()
123123
});
124124
}
125125

126+
[Fact]
127+
public void FindsDotSourcedFiles()
128+
{
129+
string exampleScriptContents =
130+
@". .\athing.ps1"+"\r\n"+
131+
@". .\somefile.ps1"+"\r\n" +
132+
@". .\somefile.ps1"+"\r\n" +
133+
@"Do-Stuff $uri"+"\r\n" +
134+
@". simpleps.ps1";
135+
136+
using (StringReader stringReader = new StringReader(exampleScriptContents))
137+
{
138+
ScriptFile scriptFile = new ScriptFile("DotSourceTestFile.ps1", stringReader);
139+
Assert.Equal(3, scriptFile.ReferencedFiles.Length);
140+
System.Console.Write("a" + scriptFile.ReferencedFiles[0]);
141+
Assert.Equal(@".\athing.ps1", scriptFile.ReferencedFiles[0]);
142+
}
143+
}
144+
126145
private void AssertFileChange(
127146
string initialString,
128147
string expectedString,

0 commit comments

Comments
 (0)