public static Subsystem <B> Bind <A, B>(this Subsystem <A> ma, Func <A, Subsystem <B> > f) => () => { try { // Run ma var outA = ma(); if (outA.IsFailed) { // If running ma failed then early out return(Out <B> .FromError((Error)outA.Error, outA.Output)); } else { // Run the bind function to get the mb monad var mb = f(outA.Value); // Run the mb monad var outB = mb(); // Concatenate the output from running ma and mb var output = outA.Output + Seq1(outA.Value.ToString()) + outB.Output + Seq1(outB.Value.ToString()); // Return our result return(outB.IsFailed ? Out <B> .FromError((Error)outB.Error, output) : Out <B> .FromValue(outB.Value, output)); } } catch (Exception e) { // Capture exceptions return(Out <B> .FromException(e)); } };
public static Subsystem <A> Return <A>(A value, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "", [System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "", [System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0) { Console.WriteLine($"{memberName} {sourceFilePath} {sourceLineNumber}"); return(() => Out <A> .FromValue(value)); }
public Subsystem <Node> WalkDirectoryTree(DirectoryInfo root) => () => { var files = root.GetFiles("*.*"); var subDirs = root.GetDirectories("*.*"); var filesNodes = files.Select(x => new Node(x)).ToSeq(); var subDirNodes = subDirs .Select(x => WalkDirectoryTree(x)().Value) .ToSeq(); return(Out <Node> .FromValue(new Node(root, subDirNodes + filesNodes))); };
public Subsystem <Node> ShowUpgradePlan(Node sourceNode, string sourceDir, string targetDir) => () => { foreach (var node in sourceNode.Enumerate().Tail()) { MarkKUpgradePlanColor(node, sourceDir, targetDir); } ConsoleW.WriteLine(@"Please Check your upgrade plan", ConsoleColor.Black, ConsoleColor.White); ConsoleW.PrintNode(sourceNode, "", true); return(Out <Node> .FromValue(sourceNode)); };
public Subsystem <string> ExtractZipToTmpDirectory(string path) => () => { string extractPath = GetTmpPath() + Path.GetFileNameWithoutExtension(path); if (Directory.Exists(extractPath)) { Directory.Delete(extractPath, recursive: true); } ZipFile.ExtractToDirectory(path, extractPath); return(Out <string> .FromValue(extractPath)); };
public Subsystem <Unit> SelectUpgradePlanDiff(Node sourceNode, string sourceDir, string targetDir) => () => { var nodeList = (from node in sourceNode.Enumerate().Tail() let colorNode = MarkKUpgradePlanColor(node, sourceDir, targetDir) select colorNode) .ToList(); var index = 0; Console.WriteLine("Press (Top/Donw/Enter/End) to select file and diff..."); while (true) { ConsoleKeyInfo ckey = Console.ReadKey(); Console.Clear(); switch (ckey.Key) { case ConsoleKey.DownArrow: index = index + 1 < nodeList.Count() ? index + 1 : 0; break; case ConsoleKey.RightArrow: index = index + 5 < nodeList.Count() ? index + 5 : index + 5 - nodeList.Count(); break; case ConsoleKey.UpArrow: index = index - 1 >= 0 ? index - 1 : nodeList.Count() - 1; break; case ConsoleKey.LeftArrow: index = index - 5 >= 0 ? index - 5 : nodeList.Count() + index - 5; break; case ConsoleKey.Enter: Diff(nodeList[index], sourceDir, targetDir); Console.ReadKey(); Console.Clear(); break; case ConsoleKey.Escape: return(Out <Unit> .FromValue(unit)); } ConsoleW.PrintNode(sourceNode, "", true, nodeList[index]); } };
public Subsystem <Config> FetchConfig(string configPath) { Subsystem <Config> ParseConfig(FileInfo info) => () => { if (!info.Exists) { ConsoleW.Write("Dangerous ", ConsoleColor.Red); ConsoleW.WriteLine($"Config {info.FullName} is not existed."); return(Out <Config> .FromValue(new Config())); } else { var expr = from context in fs.ReadAllText(info.FullName) let cfg = JsonConvert.DeserializeObject <Config>(context) select cfg; return(expr()); } }; return(from info in fs.GetFileInfo(configPath) from cfg in ParseConfig(info) select cfg); }
public void FetchConfigTest_Success() { var fakeJson = @"C:\mock.json"; // mock filInfo var fileInfo = Telerik.JustMock.Mock.Create <FileInfo>(Constructor.Mocked); Telerik.JustMock.Mock.Arrange(() => fileInfo.Exists).Returns(true); Telerik.JustMock.Mock.Arrange(() => fileInfo.FullName).Returns(fakeJson); // mock FileSystem var mock = new Mock <FileSystem>(); mock.Setup(m => m.GetFileInfo(fakeJson)).Returns(() => Out <FileInfo> .FromValue(fileInfo)); mock.Setup(m => m.ReadAllText(fakeJson)).Returns(() => Out <string> .FromValue("{\"replaceList\":[{\"pattern\":\"EDconfig\",\"replacement\":\"EDconfig.1\",\"type\":0}],\"ignoreList\":[\"CVS\"]}")); var service = new UpgradeService(mock.Object, null, null); var expr = service.FetchConfig(fakeJson); var value = expr().Value; Assert.AreEqual(value.IgnoreList[0], "CVS"); Assert.AreEqual(value.ReplaceList[0].Replacement, "EDconfig.1"); }
public static Subsystem <Unit> Log(string message) => () => Out <Unit> .FromValue(unit, Seq1(message));
public virtual Subsystem <string> ReadAllText(string path) => () => Out <string> .FromValue(File.ReadAllText(path));
public virtual Subsystem <DirectoryInfo> GetDirectoryInfo(string path) => () => Out <DirectoryInfo> .FromValue(new DirectoryInfo(path));
public virtual Subsystem <FileInfo> GetFileInfo(string path) => () => Out <FileInfo> .FromValue(new FileInfo(path));
public static Subsystem <Unit> WriteLine(string value) => () => Out <Unit> .FromValue(fun(() => Console.WriteLine(value))());
public static Subsystem <Unit> Return(Action f) => () => { f(); return(Out <Unit> .FromValue(unit)); };
public static Subsystem <A> Return <A>(A value) { return(() => Out <A> .FromValue(value)); }