public void When_passed_a_link_it_reads_link() { var lnk = OiLnkReader.Read(getJSON()); Assert.That(lnk.Preparer, Is.EqualTo(toOSPath("application-files/compile.py"))); Assert.That(lnk.LinkCommand, Is.EqualTo(toOSPath("path/to/my/application"))); Assert.That(lnk.LinkArguments, Is.EqualTo("{args}")); }
public void When_reading_file_with_handlers_it_reads_handlers() { var lnk = OiLnkReader.Read(getJSON()); Assert.That(lnk.Handlers.Length, Is.EqualTo(1)); Assert.That(lnk.Handlers[0].Arguments[0], Is.EqualTo("arg1")); Assert.That(lnk.Handlers[0].Arguments[1], Is.EqualTo("arg2")); Assert.That(lnk.Handlers[0].Responses[0], Is.EqualTo("My script")); }
private static bool handleOiLnk(ref string command, ref string arguments, string workingDir, Action <bool, string> onRecievedLine, KeyValuePair <string, string>[] replacements) { if (Path.GetExtension(command) != ".oilnk") { return(false); } var fileDir = Path.GetDirectoryName(command); var args = new CommandStringParser(' ').Parse(arguments); var lnk = OiLnkReader.Read(File.ReadAllText(command)); if ((args.Count() == 2 && args.ElementAt(1) == "get-command-definitions") || (args.Count() == 1 && args.ElementAt(0) == "reactive-script-reacts-to")) { // Run preparer if exists if (lnk.Preparer != null) { var preparerArgs = new CommandStringParser(' ').Parse(lnk.Preparer); if (preparerArgs.Count() > 0) { var preparerFile = Path.Combine(fileDir, Path.GetFileNameWithoutExtension(command) + "-files", preparerArgs.ElementAt(0)); var preparerArguments = ""; if (preparerArgs.Count() > 1) { preparerArguments = new CommandStringParser(' ').GetArgumentString(preparerArgs.Skip(1).ToArray()); } Logger.Write("Running preparer: " + preparerFile); if (File.Exists(preparerFile)) { string[] errors; var output = new Process() .QueryAll(preparerFile, preparerArguments, false, workingDir, out errors); var hasErrors = false; foreach (var line in output) { if (line.StartsWith("error|")) { onRecievedLine(true, line.Substring(6, line.Length - 6)); hasErrors = true; } } foreach (var line in errors) { if (line.Trim().Length > 0) { onRecievedLine(true, line); hasErrors = true; } } if (hasErrors) { return(true); } } } } else { Logger.Write("No preparer registered"); } } foreach (var handler in lnk.Handlers) { if (handler.Matches(args.ToArray())) { handler.WriteResponses((line) => onRecievedLine(false, line)); return(true); } } if (lnk.LinkCommand == null) { return(true); } if (fileDir != null && File.Exists(Path.Combine(fileDir, lnk.LinkCommand))) { command = Path.Combine(fileDir, lnk.LinkCommand); } else if (File.Exists(Path.Combine(workingDir, lnk.LinkCommand))) { command = Path.Combine(workingDir, lnk.LinkCommand); } else { command = lnk.LinkCommand; } // This is a terrible hack, shame on me!!!!!!! // If get command definitions don't mess with arguments if ( (args.Count() == 2 && args.ElementAt(1) == "get-command-definitions") || (args.Count() == 1 && args.ElementAt(0) == "reactive-script-reacts-to") ) { return(false); } var originalArguments = arguments; foreach (var replacement in replacements) { originalArguments = originalArguments.Replace(replacement.Key, ""); } arguments = lnk.LinkArguments .Replace("{args}", originalArguments).Trim(); return(false); }
public void When_reading_invalid_link_file_it_returns_null() { Assert.That(OiLnkReader.Read("invalid json!"), Is.Null); }