Refactoring; fixed race conditions

dependabot/npm_and_yarn/BuildServer/eslint-7.2.0
Inga 🏳‍🌈 9 years ago
parent 859c465ff5
commit ba73d4770f
  1. 19
      DotNetBuilder/Compiler.cs
  2. 37
      DotNetBuilder/Message.cs
  3. 44
      DotNetBuilder/Messages.cs
  4. 2
      DotNetBuilder/MicroBuildServer.DotNetBuilder.csproj
  5. 33
      DotNetBuilder/NUnitTester.cs
  6. 18
      DotNetBuilder/NuGetter.cs
  7. 4
      DotNetBuilder/Program.cs
  8. 39
      DotNetBuilder/Response.cs

@ -15,7 +15,7 @@ namespace MicroBuildServer.DotNetBuilder
{ {
private class CompilerLogger : Logger private class CompilerLogger : Logger
{ {
public readonly IList<Response.Message> Messages = new List<Response.Message>(); public readonly Messages Messages = new Messages();
private int indent = 0; private int indent = 0;
@ -34,30 +34,30 @@ namespace MicroBuildServer.DotNetBuilder
private void OnProjectStarted(object sender, ProjectStartedEventArgs e) private void OnProjectStarted(object sender, ProjectStartedEventArgs e)
{ {
Messages.Add(Response.Message.CreateInfo(GetLine("Started {0}", e.ProjectFile))); Messages.Add(Message.CreateInfo(GetLine("Started {0}", e.ProjectFile)));
indent++; indent++;
} }
private void OnProjectFinished(object sender, ProjectFinishedEventArgs e) private void OnProjectFinished(object sender, ProjectFinishedEventArgs e)
{ {
indent--; indent--;
Messages.Add(Response.Message.CreateInfo(GetLine("Finished {0}", e.ProjectFile))); Messages.Add(Message.CreateInfo(GetLine("Finished {0}", e.ProjectFile)));
} }
private void OnError(object sender, BuildErrorEventArgs e) private void OnError(object sender, BuildErrorEventArgs e)
{ {
Messages.Add(Response.Message.CreateError(GetLine("{0} (#{1}, {2}:{3},{4})", e.Message, e.Code, e.File, e.LineNumber, e.ColumnNumber))); Messages.Add(Message.CreateError(GetLine("{0} (#{1}, {2}:{3},{4})", e.Message, e.Code, e.File, e.LineNumber, e.ColumnNumber)));
} }
private void OnWarning(object sender, BuildWarningEventArgs e) private void OnWarning(object sender, BuildWarningEventArgs e)
{ {
Messages.Add(Response.Message.CreateWarn(GetLine("{0} (#{1}, {2}:{3},{4})", e.Message, e.Code, e.File, e.LineNumber, e.ColumnNumber))); Messages.Add(Message.CreateWarn(GetLine("{0} (#{1}, {2}:{3},{4})", e.Message, e.Code, e.File, e.LineNumber, e.ColumnNumber)));
} }
private void OnMessage(object sender, BuildMessageEventArgs e) private void OnMessage(object sender, BuildMessageEventArgs e)
{ {
//if (e.Importance != MessageImportance.High) return; //if (e.Importance != MessageImportance.High) return;
Messages.Add(Response.Message.CreateInfo(GetLine("{0}: {1}", e.Importance, e.Message))); Messages.Add(Message.CreateInfo(GetLine("{0}: {1}", e.Importance, e.Message)));
} }
private string GetLine(string format, params object[] args) private string GetLine(string format, params object[] args)
@ -105,13 +105,10 @@ namespace MicroBuildServer.DotNetBuilder
var buildResult = BuildManager.DefaultBuildManager.Build(parameters, buildRequest); var buildResult = BuildManager.DefaultBuildManager.Build(parameters, buildRequest);
if (buildResult.OverallResult == BuildResultCode.Failure) if (buildResult.OverallResult == BuildResultCode.Failure)
{ {
logger.Messages.Add(Response.Message.CreateError("BuildResult is false")); logger.Messages.Add(Message.CreateError("BuildResult is false"));
} }
return new Response return new Response(logger.Messages);
{
Messages = logger.Messages.ToArray(),
};
} }
} }
} }

@ -0,0 +1,37 @@
using Newtonsoft.Json;
namespace MicroBuildServer.DotNetBuilder
{
public class Message
{
public readonly string Type;
public readonly string Body;
private Message(string type, string body)
{
Type = type;
Body = body;
}
public static Message CreateInfo(string body)
{
return new Message("info", body);
}
public static Message CreateWarn(string body)
{
return new Message("warn", body);
}
public static Message CreateError(string body)
{
return new Message("error", body);
}
public override string ToString()
{
return string.Format("{0}: {1}", Type, Body);
}
}
}

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MicroBuildServer.DotNetBuilder
{
internal class Messages
{
private readonly object syncRoot = new object();
private readonly List<Message> storage = new List<Message>();
public void Add(Message message)
{
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}
lock(syncRoot)
{
storage.Add(message);
}
}
public TResult[] ToArray<TResult>(Func<Message, TResult> selector)
{
lock(syncRoot)
{
return storage.Select(selector).ToArray();
}
}
public bool Any()
{
lock(syncRoot)
{
return storage.Any();
}
}
}
}

@ -97,6 +97,8 @@
<ItemGroup> <ItemGroup>
<Compile Include="Compiler.cs" /> <Compile Include="Compiler.cs" />
<Compile Include="CompileRequest.cs" /> <Compile Include="CompileRequest.cs" />
<Compile Include="Message.cs" />
<Compile Include="Messages.cs" />
<Compile Include="NuGetPackRequest.cs" /> <Compile Include="NuGetPackRequest.cs" />
<Compile Include="NuGetPushRequest.cs" /> <Compile Include="NuGetPushRequest.cs" />
<Compile Include="NuGetRestoreRequest.cs" /> <Compile Include="NuGetRestoreRequest.cs" />

@ -13,7 +13,7 @@ namespace MicroBuildServer.DotNetBuilder
private class Listener : EventListener private class Listener : EventListener
{ {
public readonly List<Response.Message> Messages = new List<Response.Message>(); public readonly Messages Messages = new Messages();
private bool runFail; private bool runFail;
private bool suiteFail; private bool suiteFail;
@ -56,7 +56,7 @@ namespace MicroBuildServer.DotNetBuilder
public void RunFinished(Exception exception) public void RunFinished(Exception exception)
{ {
Messages.Add(Response.Message.CreateError("Run finished: " + exception)); Messages.Add(Message.CreateError("Run finished: " + exception));
runFail = true; runFail = true;
} }
@ -66,17 +66,17 @@ namespace MicroBuildServer.DotNetBuilder
if (!IsSuccess(result) && !runFail) if (!IsSuccess(result) && !runFail)
{ {
Messages.Add(Response.Message.CreateError(message)); Messages.Add(Message.CreateError(message));
} }
else else
{ {
Messages.Add(Response.Message.CreateInfo(message)); Messages.Add(Message.CreateInfo(message));
} }
} }
public void RunStarted(string name, int testCount) public void RunStarted(string name, int testCount)
{ {
Messages.Add(Response.Message.CreateInfo("Run started: " + name)); Messages.Add(Message.CreateInfo("Run started: " + name));
runFail = false; runFail = false;
} }
@ -86,11 +86,11 @@ namespace MicroBuildServer.DotNetBuilder
if (!IsSuccess(result) && !suiteFail) if (!IsSuccess(result) && !suiteFail)
{ {
Messages.Add(Response.Message.CreateError(message)); Messages.Add(Message.CreateError(message));
} }
else else
{ {
Messages.Add(Response.Message.CreateInfo(message)); Messages.Add(Message.CreateInfo(message));
} }
if (!result.IsSuccess) if (!result.IsSuccess)
@ -101,7 +101,7 @@ namespace MicroBuildServer.DotNetBuilder
public void SuiteStarted(TestName testName) public void SuiteStarted(TestName testName)
{ {
Messages.Add(Response.Message.CreateInfo("Suite started: " + testName.Name)); Messages.Add(Message.CreateInfo("Suite started: " + testName.Name));
suiteFail = false; suiteFail = false;
} }
@ -111,28 +111,28 @@ namespace MicroBuildServer.DotNetBuilder
if (!IsSuccess(result)) if (!IsSuccess(result))
{ {
Messages.Add(Response.Message.CreateError(message)); Messages.Add(Message.CreateError(message));
} }
else else
{ {
Messages.Add(Response.Message.CreateInfo(message)); Messages.Add(Message.CreateInfo(message));
} }
suiteFail = true; suiteFail = true;
} }
public void TestOutput(TestOutput testOutput) public void TestOutput(TestOutput testOutput)
{ {
Messages.Add(Response.Message.CreateInfo("Test output: " + testOutput.Text)); Messages.Add(Message.CreateInfo("Test output: " + testOutput.Text));
} }
public void TestStarted(TestName testName) public void TestStarted(TestName testName)
{ {
Messages.Add(Response.Message.CreateInfo("Test started: " + testName.Name)); Messages.Add(Message.CreateInfo("Test started: " + testName.Name));
} }
public void UnhandledException(Exception exception) public void UnhandledException(Exception exception)
{ {
Messages.Add(Response.Message.CreateError("Unhandled exception: " + exception)); Messages.Add(Message.CreateError("Unhandled exception: " + exception));
suiteFail = true; suiteFail = true;
runFail = true; runFail = true;
} }
@ -162,9 +162,12 @@ namespace MicroBuildServer.DotNetBuilder
} }
//DebugTestResult(Console.Out, result); //DebugTestResult(Console.Out, result);
var messages = listener.Messages.Any() ? listener.Messages.ToArray() : new[] {Response.Message.CreateError("No messages from listener")}; if (!listener.Messages.Any())
{
listener.Messages.Add(Message.CreateError("No messages from listener"));
}
AppDomain.CurrentDomain.SetData(DATA_TEST_RESULTS_KEY, new Response { Messages = messages }); AppDomain.CurrentDomain.SetData(DATA_TEST_RESULTS_KEY, new Response(listener.Messages));
} }
} }

@ -11,7 +11,7 @@ namespace MicroBuildServer.DotNetBuilder
{ {
private class Console : IConsole private class Console : IConsole
{ {
public readonly IList<Response.Message> Messages = new List<Response.Message>(); public readonly Messages Messages = new Messages();
public bool Confirm(string description) public bool Confirm(string description)
{ {
@ -74,7 +74,7 @@ namespace MicroBuildServer.DotNetBuilder
public void Write(string value) public void Write(string value)
{ {
Messages.Add(Response.Message.CreateInfo(value)); Messages.Add(Message.CreateInfo(value));
} }
public void Write(object value) public void Write(object value)
@ -89,7 +89,7 @@ namespace MicroBuildServer.DotNetBuilder
public void WriteError(string value) public void WriteError(string value)
{ {
Messages.Add(Response.Message.CreateError(value)); Messages.Add(Message.CreateError(value));
} }
public void WriteError(object value) public void WriteError(object value)
@ -123,7 +123,7 @@ namespace MicroBuildServer.DotNetBuilder
public void WriteWarning(bool prependWarningText, string value, params object[] args) public void WriteWarning(bool prependWarningText, string value, params object[] args)
{ {
throw new NotImplementedException(); WriteWarning(value, args);
} }
public void WriteWarning(string value, params object[] args) public void WriteWarning(string value, params object[] args)
@ -133,12 +133,12 @@ namespace MicroBuildServer.DotNetBuilder
public void WriteWarning(bool prependWarningText, string value) public void WriteWarning(bool prependWarningText, string value)
{ {
throw new NotImplementedException(); WriteWarning(value);
} }
public void WriteWarning(string value) public void WriteWarning(string value)
{ {
Messages.Add(Response.Message.CreateWarn(value)); Messages.Add(Message.CreateWarn(value));
} }
public void Log(MessageLevel level, string message, params object[] args) public void Log(MessageLevel level, string message, params object[] args)
@ -187,7 +187,7 @@ namespace MicroBuildServer.DotNetBuilder
console.WriteError(e); console.WriteError(e);
} }
return new Response { Messages = console.Messages.ToArray() }; return new Response(console.Messages);
} }
public static Response Push(NuGetPushRequest request) public static Response Push(NuGetPushRequest request)
@ -211,7 +211,7 @@ namespace MicroBuildServer.DotNetBuilder
console.WriteError(e); console.WriteError(e);
} }
return new Response {Messages = console.Messages.ToArray()}; return new Response(console.Messages);
} }
public static Response Restore(NuGetRestoreRequest request) public static Response Restore(NuGetRestoreRequest request)
@ -235,7 +235,7 @@ namespace MicroBuildServer.DotNetBuilder
console.WriteError(e); console.WriteError(e);
} }
return new Response { Messages = console.Messages.ToArray() }; return new Response(console.Messages);
} }
} }
} }

@ -30,7 +30,9 @@ namespace MicroBuildServer.DotNetBuilder
} }
catch (Exception e) catch (Exception e)
{ {
return new Response { Messages = new[] { Response.Message.CreateError(e.ToString()) } }; var messages = new Messages();
messages.Add(Message.CreateError(e.ToString()));
return new Response(messages);
} }
} }

@ -1,40 +1,33 @@
using System; using Newtonsoft.Json;
using System.Collections.Generic; using System;
using System.Linq;
using System.Text;
namespace MicroBuildServer.DotNetBuilder namespace MicroBuildServer.DotNetBuilder
{ {
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
[Serializable] [Serializable]
class Response class Response
{ {
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
[Serializable] [Serializable]
public class Message private class ResponseMessage
{ {
public string Type { get; private set; } [JsonProperty(Required = Required.Always)]
public string Body { get; private set; } public string Type { get; set; }
public static Message CreateInfo(string body) [JsonProperty(Required = Required.Always)]
{ public string Body { get; set; }
return new Message { Type = "info", Body = body };
} }
public static Message CreateWarn(string body) [JsonProperty(Required = Required.Always, PropertyName = "Messages")]
{ private ResponseMessage[] Messages { get; set; }
return new Message { Type = "warn", Body = body };
}
public static Message CreateError(string body) public Response(Messages messages)
{ {
return new Message { Type = "error", Body = body }; Messages = messages.ToArray(message => new ResponseMessage
}
public override string ToString()
{ {
return string.Format("{0}: {1}", Type, Body); Type = message.Type,
} Body = message.Body,
});
} }
public Message[] Messages { get; set; }
} }
} }

Loading…
Cancel
Save