Skip to content

Commit f7bff30

Browse files
committed
Use CallerFilePath to get the script directory
1 parent d0ac94f commit f7bff30

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

eng/generate-compiler-code.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,9 @@
1212
#:project ../src/Tools/Source/CompilerGeneratorTools/Source/VisualBasicSyntaxGenerator/
1313
#:project ../src/Tools/Source/CompilerGeneratorTools/Source/VisualBasicErrorFactsGenerator/
1414

15-
var repoRoot = Environment.CurrentDirectory;
16-
if (File.Exists(Path.Join(repoRoot, "generate-compiler-code.cs")))
17-
{
18-
// We are in the eng folder, move up to the repo root
19-
repoRoot = Path.GetDirectoryName(repoRoot);
20-
}
21-
else if (!File.Exists(Path.Join(repoRoot, "eng", "generate-compiler-code.cs")))
22-
{
23-
Console.WriteLine("Could not find the repo root. This script must be run from the eng folder or the repo root.");
24-
return 1;
25-
}
15+
using System.Runtime.CompilerServices;
16+
17+
var repoRoot = GetRoslynDirectory();
2618

2719
var test = false;
2820
var configuration = "Debug";
@@ -132,6 +124,16 @@
132124

133125
return retVal;
134126

127+
static string GetRoslynDirectory([CallerFilePath] string sourceFilePath = "")
128+
{
129+
if (Path.GetDirectoryName(sourceFilePath) is not string engDir || Path.GetDirectoryName(engDir) is not string roslynRoot || !File.Exists(Path.Join(roslynRoot, "eng", Path.GetFileName(sourceFilePath))))
130+
{
131+
throw new InvalidOperationException("Could not determine source file path. This script must be located in the 'eng' directory of the Roslyn repo.");
132+
}
133+
134+
return roslynRoot;
135+
}
136+
135137
static int GenerateLanguage(BoundTreeGenerator.TargetLanguage language, string languageDir, string generatedDir, string generatedTestDir, SyntaxGeneratorAction syntaxGenerator, ErrorFactsGeneratorAction errorFactsGenerator)
136138
{
137139
Directory.CreateDirectory(generatedDir);

0 commit comments

Comments
 (0)