protected Assembly GetAssembly(string path)
{
if (!File.Exists(path))
{
Log.Error("File '{0}' not found.", path);
return null;
}
var ext = Path.GetExtension(path).TrimStart('.');
// Load dlls directly
if (ext == "dll")
return Assembly.LoadFrom(path);
// Try to compile other files
var outPath = this.GetCachePath(path);
Compiler compiler;
_compilers.TryGetValue(ext, out compiler);
if (compiler == null)
{
Log.Error("No compiler found for script '{0}'.", path);
return null;
}
try
{
return compiler.Compile(path, outPath, this.Caching);
}
catch (CompilerErrorsException ex)
{
try
{
File.Delete(outPath);
}
catch (UnauthorizedAccessException)
{
Log.Warning("Unable to delete '{0}'", outPath);
}
var lines = File.ReadAllLines(path);
foreach (var err in ex.Errors)
{
// Error msg
Log.WriteLine((!err.IsWarning ? LogLevel.Error : LogLevel.Warning), "In {0} on line {1}, column {2}", err.File, err.Line, err.Column);
Log.WriteLine(LogLevel.None, " {0}", err.Message);
// Display lines around the error
int startLine = Math.Max(1, err.Line - 1);
int endLine = Math.Min(lines.Length, startLine + 2);
for (int i = startLine; i <= endLine; ++i)
{
// Make sure we don't get out of range.
// (ReadAllLines "trims" the input)
var line = (i <= lines.Length) ? lines[i - 1] : "";
Log.WriteLine(LogLevel.None, " {2} {0:0000}: {1}", i, line, (err.Line == i ? '*' : ' '));
}
}
}
catch (Exception ex)
{
Log.Exception(ex, "LoadScript: Problem while loading script '{0}'", path);
//File.Delete(outPath);
}
return null;
}