Skip to content

Commit 77a34fa

Browse files
committed
Add ability to print out dependencies of a file as part of the evaluation
1 parent 6b79e82 commit 77a34fa

File tree

6 files changed

+54
-5
lines changed

6 files changed

+54
-5
lines changed

src/IdfPlusExpVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public IdfPlusExpVisitor(string baseDirectory, FileType fileType)
3939
_variables[0]["fold"] = new FoldFunctionExpression();
4040
_variables[0]["keys"] = new KeysFunctionExpression();
4141
_variables[0]["has"] = new HasFunctionExpression();
42-
_variables[0]["load"] = new LoadFunctionExpression(baseDirectory);
42+
_variables[0]["load"] = new LoadFunctionExpression();
4343
_variables[0]["head"] = new ListHeadFunctionExpression();
4444
_variables[0]["tail"] = new ListTailFunctionExpression();
4545
_variables[0]["init"] = new ListInitFunctionExpression();

src/IdfPlusListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,4 @@ public override void EnterLambda_def(NeobemParser.Lambda_defContext context)
7777
// FunctionExpression funcExpression = new FunctionExpression(context);
7878
}
7979
}
80-
}
80+
}

src/IdfPlusVisitor.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public IdfPlusVisitor(string baseDirectory, FileType fileType, List<string> flag
3434
_environments[0]["fold"] = new FoldFunctionExpression();
3535
_environments[0]["keys"] = new KeysFunctionExpression();
3636
_environments[0]["has"] = new HasFunctionExpression();
37-
_environments[0]["load"] = new LoadFunctionExpression(baseDirectory);
37+
_environments[0]["load"] = new LoadFunctionExpression();
3838
_environments[0]["head"] = new ListHeadFunctionExpression();
3939
_environments[0]["tail"] = new ListTailFunctionExpression();
4040
_environments[0]["init"] = new ListInitFunctionExpression();
@@ -221,6 +221,8 @@ public override string VisitImport_statement(NeobemParser.Import_statementContex
221221
throw new Exception( $"Could not read contents of {filePath} in import statement, line {context.Start.Line}.");
222222
}
223223

224+
Dependencies.Set.Add(fileInfo.FullName);
225+
224226
// Read the imported file, with the current directory set to directory of the input file.
225227
visitor = new IdfPlusVisitor(fileInfo.DirectoryName, _fileType, _flags);
226228
}
@@ -301,4 +303,9 @@ public override string VisitLog_statement(NeobemParser.Log_statementContext cont
301303
return "";
302304
}
303305
}
306+
307+
public static class Dependencies
308+
{
309+
public static HashSet<string> Set = new();
310+
}
304311
}

src/LoadFunctionExpression.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace src
99
{
1010
public class LoadFunctionExpression : FunctionExpression
1111
{
12-
public LoadFunctionExpression(string baseDirectory) : base(new List<Dictionary<string, Expression>>(), new List<string>{ "options"}, FileType.Any)
12+
public LoadFunctionExpression() : base(new List<Dictionary<string, Expression>>(), new List<string>{ "options"}, FileType.Any)
1313
{
1414
}
1515

@@ -21,6 +21,8 @@ public override (string, Expression) Evaluate(List<Expression> inputs, string ba
2121
{
2222
DelimitedFileReader reader = new();
2323
var fullPath = Path.GetFullPath(stringExpression.Text, baseDirectory);
24+
25+
Dependencies.Set.Add(fullPath);
2426
return EvaluateDelimitedFile(fullPath, reader, true, 0);
2527
}
2628
else if (inputs[0] is IdfPlusObjectExpression objectExpression)
@@ -69,6 +71,8 @@ public override (string, Expression) Evaluate(List<Expression> inputs, string ba
6971

7072
DelimitedFileReader reader = new(delimiter);
7173
var fullPath = Path.GetFullPath(pathStringExpression.Text, baseDirectory);
74+
75+
Dependencies.Set.Add(fullPath);
7276
return EvaluateDelimitedFile(fullPath, reader, hasHeaderLine, skipLines);
7377
}
7478
else if (typeStringExpression.Text == "Excel")
@@ -107,12 +111,16 @@ public override (string, Expression) Evaluate(List<Expression> inputs, string ba
107111

108112
string filePath = ((StringExpression) objectExpression.Members["path"]).Text;
109113
var fullFilePath = Path.GetFullPath(filePath, baseDirectory);
114+
115+
Dependencies.Set.Add(fullFilePath);
116+
110117
return ("", ExcelDataLoader.Load( fullFilePath, worksheetName, range));
111118
}
112119
else if (objectExpression.Members["type"] is StringExpression {Text: "JSON"})
113120
{
114121
string filePath = ((StringExpression) objectExpression.Members["path"]).Text;
115122
var fullFilePath = Path.GetFullPath(filePath, baseDirectory);
123+
Dependencies.Set.Add(fullFilePath);
116124
string jsonData = File.ReadAllText(fullFilePath);
117125
JsonDataLoader jsonLoader = new();
118126

@@ -122,6 +130,7 @@ public override (string, Expression) Evaluate(List<Expression> inputs, string ba
122130
{
123131
string filePath = ((StringExpression) objectExpression.Members["path"]).Text;
124132
var fullFilePath = Path.GetFullPath(filePath, baseDirectory);
133+
Dependencies.Set.Add(fullFilePath);
125134
string jsonData = File.ReadAllText(fullFilePath);
126135
XmlDataLoader xmlDataLoader = new();
127136

src/Program.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Text;
66
using Antlr4.Runtime;
77
using Antlr4.Runtime.Tree;
8-
using OfficeOpenXml.FormulaParsing.LexicalAnalysis;
98

109
namespace src
1110
{
@@ -58,6 +57,18 @@ static int Main(string[] args)
5857
case "--doe2":
5958
options.FileType = FileType.Doe2;
6059
break;
60+
case "--deps":
61+
if (i + 1 < args.Length)
62+
{
63+
options.DependenciesFile = args[i + 1];
64+
}
65+
else
66+
{
67+
WriteLine("No file path given for --deps option.");
68+
return 1;
69+
}
70+
i++;
71+
break;
6172
case "--flags":
6273
if (i + 1 < args.Length)
6374
{
@@ -203,6 +214,26 @@ static int Main(string[] args)
203214
}
204215
}
205216

217+
if (!string.IsNullOrEmpty(options.DependenciesFile))
218+
{
219+
StringBuilder b = new();
220+
foreach (var dep in Dependencies.Set.Order())
221+
{
222+
b.Append(dep);
223+
b.Append('\n');
224+
}
225+
226+
try
227+
{
228+
File.WriteAllText(options.DependenciesFile, b.ToString());
229+
}
230+
catch (Exception)
231+
{
232+
Console.Error.WriteLine($"Could not write dependencies output to {options.DependenciesFile}.");
233+
return 1;
234+
}
235+
}
236+
206237
return 0;
207238
}
208239

@@ -219,6 +250,7 @@ public class BempOptions
219250
public bool Tree = false;
220251
public List<string> Flags = new();
221252
public bool PrintObjects = false;
253+
public string DependenciesFile = "";
222254
}
223255
}
224256
}

src/help.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ If the input file is '-', input is read from standard input rather than from a f
77
OPTIONS:
88

99
--doe2 Parse input file in DOE-2 Building Description Language format
10+
--deps <filename> Print dependencies encountered in the input file to the specified file
1011
-h, --help Show this help and exit
1112
-f, --fmt Format file instead of compiling
1213
--flags <flags> Set flags for simulation. Multiple flags can be set, comma separated.

0 commit comments

Comments
 (0)