|
using System; |
|
using System.IO; |
|
using System.Linq; |
|
using JetBrains.Annotations; |
|
using Microsoft.CodeAnalysis; |
|
using ReFuc.Tasks; |
|
using ReFuc.Utilities; |
|
|
|
namespace ReFuc |
|
{ |
|
|
|
[UsedImplicitly] |
|
class Program |
|
{ |
|
private static readonly Log Log = new Log(); |
|
|
|
static void Main(string[] args) |
|
{ |
|
|
|
string solutionPath = null; |
|
string projectPath = null; |
|
string taskName = null; |
|
var os = new OptionSet |
|
{ |
|
{"solution=|sln=", "solution entry point", path => solutionPath = path}, |
|
{"project=|prj=", "project entry point", path => projectPath = path}, |
|
{"task=|t=", "task to run", tsk => taskName = tsk} |
|
}; |
|
os.Parse(args); |
|
|
|
if (taskName == null) |
|
Exit("No taskname provided"); |
|
|
|
// ReSharper disable once AssignNullToNotNullAttribute |
|
var taskType = FindType(taskName); |
|
|
|
if (taskType == null) |
|
Exit("No type called " + taskName + " found"); |
|
|
|
// ReSharper disable once AssignNullToNotNullAttribute |
|
var taskInstance = (IRefactoringTask) Activator.CreateInstance(taskType); |
|
|
|
var hub = new CompileHub(Log); |
|
if (solutionPath != null) |
|
hub.StartWithSolution(solutionPath).Wait(); |
|
else if (projectPath != null) |
|
hub.StartWithProject(projectPath).Wait(); |
|
|
|
Log.LogInfo("About to run {0}", taskInstance.Description); |
|
taskInstance.Run(hub.WithProjects()).Subscribe(SaveTree, Done); |
|
|
|
Console.ReadLine(); |
|
} |
|
|
|
private static void Exit(string msg) |
|
{ |
|
Log.LogWarning(msg); |
|
Console.ReadLine(); |
|
Environment.Exit(0); |
|
} |
|
|
|
private static void Done() |
|
{ |
|
Log.LogInfo("Done"); |
|
} |
|
|
|
private static void SaveTree(SyntaxTree syntaxTree) |
|
{ |
|
try |
|
{ |
|
using (var fs = new FileStream(syntaxTree.FilePath, FileMode.Truncate)) |
|
using (var tw = new StreamWriter(fs)) |
|
{ |
|
syntaxTree.GetText().Write(tw); |
|
} |
|
} |
|
catch (Exception e) |
|
{ |
|
Log.LogException(e); |
|
} |
|
} |
|
|
|
private static Type FindType(string taskName) |
|
{ |
|
return typeof (Program).Assembly.GetExportedTypes().FirstOrDefault(t => t.Name == taskName); |
|
} |
|
} |
|
} |