forked from daveaglick/Scripty
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
167 lines (152 loc) · 5.91 KB
/
Program.cs
File metadata and controls
167 lines (152 loc) · 5.91 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Scripty.Core;
using Scripty.Core.Output;
using Scripty.Core.ProjectTree;
namespace Scripty
{
public class Program
{
private static int Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionEvent;
Program program = new Program();
return program.Run(args);
}
private static void UnhandledExceptionEvent(object sender, UnhandledExceptionEventArgs e)
{
// Exit with a error exit code
Exception exception = e.ExceptionObject as Exception;
if (exception != null)
{
Console.Error.WriteLine(exception.ToString());
}
Environment.Exit((int) ExitCode.UnhandledError);
}
private readonly Settings _settings = new Settings();
private int Run(string[] args)
{
// Parse the command line if there are args
if (args.Length > 0)
{
try
{
bool hasParseArgsErrors;
if (!_settings.ParseArgs(args, out hasParseArgsErrors))
{
return hasParseArgsErrors ? (int) ExitCode.CommandLineError : (int) ExitCode.Normal;
}
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.ToString());
return (int) ExitCode.CommandLineError;
}
}
else
{
// Otherwise the settings should come in over stdin
_settings.ReadStdin();
}
// Attach
if (_settings.Attach)
{
Console.WriteLine("Waiting for a debugger to attach (or press a key to continue)...");
while (!Debugger.IsAttached && !Console.KeyAvailable)
{
Thread.Sleep(100);
}
if (Console.KeyAvailable)
{
Console.ReadKey(true);
Console.WriteLine("Key pressed, continuing execution");
}
else
{
Console.WriteLine("Debugger attached, continuing execution");
}
}
// Get the script engine
string solutionFilePath = null;
string projectFilePath = Path.Combine(Environment.CurrentDirectory, _settings.ProjectFilePath);
if (_settings.SolutionFilePath != null)
{
solutionFilePath = Path.Combine(Environment.CurrentDirectory, _settings.SolutionFilePath);
}
ScriptEngine engine = new ScriptEngine(projectFilePath, solutionFilePath, _settings.Properties);
// Get script files if none were specified
IReadOnlyList<string> finalScriptFilePaths;
if (_settings.ScriptFilePaths != null && _settings.ScriptFilePaths.Count > 0)
{
finalScriptFilePaths = _settings.ScriptFilePaths;
}
else
{
// Look for any .csx files in the project
Console.WriteLine("No script files were specified, scanning project for .csx files");
List<string> scriptFilePaths = new List<string>();
PopulateScriptFilePaths(engine.ProjectRoot, scriptFilePaths);
finalScriptFilePaths = scriptFilePaths;
}
// Set up tasks for the specified script files
ConcurrentBag<Task<ScriptResult>> tasks = new ConcurrentBag<Task<ScriptResult>>();
Parallel.ForEach(finalScriptFilePaths
.Select(x => Path.Combine(Path.GetDirectoryName(projectFilePath), x))
.Where(x => !string.IsNullOrEmpty(x))
.Distinct(),
x =>
{
if (File.Exists(x))
{
Console.WriteLine($"Adding task to evaluate {x}");
tasks.Add(engine.Evaluate(new ScriptSource(x, File.ReadAllText(x))));
}
});
// Evaluate all the scripts
try
{
Task.WaitAll(tasks.ToArray());
}
catch (AggregateException aggregateException)
{
foreach (Exception ex in aggregateException.InnerExceptions)
{
Console.Error.WriteLine(ex.ToString());
}
}
// Iterate over the completed tasks
foreach (Task<ScriptResult> task in tasks.Where(x => x.Status == TaskStatus.RanToCompletion))
{
// Check for any errors
foreach (ScriptError error in task.Result.Errors)
{
Console.Error.WriteLine($"{error.Message} [{error.Line},{error.Column}]");
}
// Output the set of generated files w/ build actions
foreach (IOutputFileInfo outputFile in task.Result.OutputFiles)
{
Console.WriteLine($"{outputFile.BuildAction}|{outputFile.FilePath}");
}
}
return (int) ExitCode.Normal;
}
private void PopulateScriptFilePaths(ProjectNode node, List<string> scriptFilePaths)
{
foreach (KeyValuePair<string, ProjectNode> child in node.Children)
{
if (Path.GetExtension(child.Key) == ".csx")
{
scriptFilePaths.Add(child.Key);
}
PopulateScriptFilePaths(child.Value, scriptFilePaths);
}
}
}
}