Ejemplo n.º 1
0
        private async Task Handshake(string sourceDirectory, string buildDirectory,
                                     string makefileGenerator = "Unix Makefiles")
        {
            string message = $"{{\"cookie\":\"HypnoticCookieCutter\",\"type\":\"handshake\",\"protocolVersion\":{{\"major\":1}}, " +
                             $"\"sourceDirectory\":\"{sourceDirectory}\",\"buildDirectory\":\"{buildDirectory}\", " +
                             $"\"generator\":\"{makefileGenerator}\"}}";

            serverStream.WriteMessage(message);
            CMakeReplyMessage reply = await WaitForReply("handshake").ConfigureAwait(false);

            cookie = reply.Cookie;
        }
Ejemplo n.º 2
0
        public static T Parse <T>(string message, IUserInterface userInterface = null, bool throwJsonException = true) where T : CMakeMessage
        {
            try
            {
                CMakeMessage cMakeMessage;
                JObject      content = JObject.Parse(message, new JsonLoadSettings {
                    LineInfoHandling = LineInfoHandling.Ignore
                });
                userInterface?.WriteVerbose(content.ToString(Formatting.Indented));
                switch (content["type"].Value <string>())
                {
                case "hello":
                    cMakeMessage = CMakeHelloMessage.Create(content);
                    break;

                case "reply":
                    cMakeMessage = CMakeReplyMessage.Create(content);
                    break;

                case "progress":
                    cMakeMessage = CMakeProgressMessage.Create();
                    break;

                case "message":
                    cMakeMessage = CMakeMessageMessage.Create(content);
                    break;

                case "signal":
                    userInterface?.WriteVerbose("Received signal message from cmake server. Signal messages are ignored.");
                    return(null);

                default:
                    throw new FormattableException($"Unknown message type {content["type"].Value<string>()}.{Environment.NewLine}" +
                                                   $"Complete message: {message}");
                }

                if (cMakeMessage is T converted)
                {
                    return(converted);
                }
                throw new FormattableException($"Expected message of type {typeof(T)}, but message was of type  {cMakeMessage.GetType()}");
            }
            catch (JsonReaderException e)
            {
                if (throwJsonException)
                {
                    throw new FormattableException($"Error while parsing the response json{Environment.NewLine}{message}.", e);
                }

                return(null);
            }
        }