-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathProjectLoader.cs
More file actions
69 lines (58 loc) · 1.78 KB
/
ProjectLoader.cs
File metadata and controls
69 lines (58 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
namespace RunScript;
using System.Text.Json;
internal class ProjectLoader
{
public async Task<(Project, string)> LoadAsync(string executingDirectory)
{
var jsonPath = CheckFolderForFile(executingDirectory, "global.json");
if (jsonPath is null)
{
throw new RunScriptException("No global.json found in folder path");
}
var rootPath = Path.GetDirectoryName(jsonPath)!;
var workingDirectory = rootPath != executingDirectory
? rootPath
: executingDirectory;
var project = await LoadGlobalJsonAsync(jsonPath);
if (project is null)
{
throw new RunScriptException("Error parsing global.json");
}
if (project.Scripts is null || project.Scripts.Count == 0)
{
throw new RunScriptException("No scripts found in the global.json");
}
return (project, workingDirectory);
}
private string? CheckFolderForFile(string path, string file)
{
var filePath = Path.Combine(path, file);
if (File.Exists(filePath))
{
return filePath;
}
var parentPath = Directory.GetParent(path)?.FullName;
if (parentPath is null)
{
return null;
}
return CheckFolderForFile(parentPath, file);
}
private static async Task<Project?> LoadGlobalJsonAsync(string jsonPath)
{
var json = await File.ReadAllTextAsync(jsonPath);
try
{
return JsonSerializer.Deserialize<Project>(
json,
new JsonSerializerOptions
{
ReadCommentHandling = JsonCommentHandling.Skip,
});
}
catch
{
return null;
}
}
}