示例#1
0
        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));
            }
        };
示例#2
0
 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));
 }
示例#3
0
        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)));
        };
示例#4
0
        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));
        };
示例#5
0
        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));
        };
示例#6
0
        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]);
            }
        };
示例#7
0
        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");
        }
示例#9
0
 public static Subsystem <Unit> Log(string message) => () =>
 Out <Unit> .FromValue(unit, Seq1(message));
示例#10
0
 public virtual Subsystem <string> ReadAllText(string path) => () =>
 Out <string> .FromValue(File.ReadAllText(path));
示例#11
0
 public virtual Subsystem <DirectoryInfo> GetDirectoryInfo(string path) => () =>
 Out <DirectoryInfo> .FromValue(new DirectoryInfo(path));
示例#12
0
 public virtual Subsystem <FileInfo> GetFileInfo(string path) => () =>
 Out <FileInfo> .FromValue(new FileInfo(path));
示例#13
0
 public static Subsystem <Unit> WriteLine(string value) => () =>
 Out <Unit> .FromValue(fun(() => Console.WriteLine(value))());
示例#14
0
 public static Subsystem <Unit> Return(Action f) => () =>
 {
     f();
     return(Out <Unit> .FromValue(unit));
 };
示例#15
0
 public static Subsystem <A> Return <A>(A value)
 {
     return(() => Out <A> .FromValue(value));
 }