Пример #1
0
 private void Batch <T>(GitCmdArgs args, IEnumerable <T> source, Action <T> action)
 {
     Batch(args, source, o => {
         action(o);
         return(Task.FromResult <object>(null));
     });
 }
Пример #2
0
        public static void SimpleNameTest()
        {
            var args = GitCmdArgs.Parse(ExeName);

            Assert.AreEqual(Exe, args.Exe);
            Assert.AreEqual(Name, args.Name);
            Assert.AreEqual(0, args.Length);
            Assert.AreEqual(0, args.Switches().Count());
        }
Пример #3
0
        public static void QuotedSwitchName()
        {
            var args = GitCmdArgs.Parse($"{ExeName} --swi\"tch\"");

            Assert.AreEqual(Exe, args.Exe);
            Assert.AreEqual(Name, args.Name);
            Assert.AreEqual(0, args.Length);
            Assert.IsTrue(args.IsSet("switch"));
        }
Пример #4
0
        public static void SimpleSwitch()
        {
            var longArgs = GitCmdArgs.Parse($"{ExeName} --{LongSwitch}");

            Assert.IsTrue(longArgs.IsSet(LongSwitch.ToUpper()));
            Assert.IsTrue(longArgs.IsSet(LongSwitch.ToLower()));

            var shortArgs = GitCmdArgs.Parse($"{ExeName} -{ShortSwitch}");

            Assert.IsTrue(shortArgs.IsSet(ShortSwitch.ToUpper()));
            Assert.IsTrue(shortArgs.IsSet(ShortSwitch.ToLower()));
        }
Пример #5
0
        private GitCmdArgs Parse(
            int minArgs = 0,
            int maxArgs = int.MaxValue,
            IEnumerable <GitCmdSwitchInfo> switchInfo = null)
        {
            if (switchInfo == null)
            {
                switchInfo = GitCmdSwitchInfo.Create();
            }

            return(GitCmdArgs.Parse(m_commandLine, minArgs, maxArgs, switchInfo));
        }
Пример #6
0
        public static void KnownSwitchEnum()
        {
            var args = GitCmdArgs.Parse(
                $"{ExeName} -{ShortSwitch} --{LongSwitch}",
                switchInfo: GitCmdSwitchInfo.Create(
                    KnownSwitches.MyGitSwitch,
                    KnownSwitches.M
                    )
                );

            Assert.IsTrue(args.IsSet(KnownSwitches.MyGitSwitch));
            Assert.IsTrue(args.IsSet(KnownSwitches.M));
        }
Пример #7
0
        private IEnumerable <GitFile> GetFiles(GitCmdArgs args, bool pointer = false, bool content = false)
        {
            string dir    = null;
            string filter = null;

            if (args.Length > 0)
            {
                dir    = args[0].GetDir();
                filter = Path.GetFileName(args[0]);
            }

            var flags    = GetFileFlags(args, pointer, content);
            var lfxFiles = LfxFile.Load(filter, dir, flags);

            return(lfxFiles);
        }
Пример #8
0
        public static void UnknownSwitchName()
        {
            var switches = GitCmdSwitchInfo.Create(
                ShortSwitch,
                LongSwitch
                );

            var longSwitchCmd = $"{ExeName} --{OtherLongSwitch}";

            GitCmdArgs.Parse(longSwitchCmd);
            Throws(() => GitCmdArgs.Parse(longSwitchCmd, switchInfo: switches));

            var shortSwitchCmd = $"{ExeName} -{OtherShortSwitch}";

            GitCmdArgs.Parse(shortSwitchCmd);
            Throws(() => GitCmdArgs.Parse(shortSwitchCmd, switchInfo: switches));
        }
Пример #9
0
        private void Batch <T>(GitCmdArgs args, IEnumerable <T> source, Func <T, Task> action)
        {
            var quite = args.IsSet(LfxCmdSwitches.Q, LfxCmdSwitches.Quite);

            int    count = 0;
            double total = source.Count();
            var    top   = Console.CursorTop;
            var    sw    = new Stopwatch();

            sw.Start();

            var block = new ActionBlock <T>(async o => {
                if (!quite)
                {
                    Interlocked.Increment(ref count);
                    lock (Lock) {
                        Console.SetCursorPosition(0, top);
                        Console.Write($"Progress: {count}/{total}={count / total:P}, t={sw.Elapsed:hh':'mm':'ss'.'ff}, {o}"
                                      .ExpandOrTrim(Console.BufferWidth - 1));
                    }
                }

                try {
                    await action(o);
                } catch (Exception e) {
                    Console.WriteLine();
                    Console.WriteLine(e.Message);
                }
            });

            foreach (var o in source)
            {
                block.Post(o);
            }
            block.Complete();
            block.Completion.Wait();

            Console.SetCursorPosition(0, top);
            Console.Write($"Progress: {count}/{total}={count / total:P}, t={sw.Elapsed:hh':'mm':'ss'.'ff}"
                          .ExpandOrTrim(Console.BufferWidth - 1));
            Console.WriteLine();
        }
Пример #10
0
        private void Execute()
        {
            var bf =
                BindingFlags.Instance |
                BindingFlags.Public |
                BindingFlags.IgnoreCase;

            try {
                var args = GitCmdArgs.Parse(m_commandLine);
                var name = args.Name;

                // default
                if (args.Name == null)
                {
                    Help();
                    return;
                }

                // check name
                var method = typeof(LfxCmd).GetMethod(name, bf);
                if (method == null)
                {
                    throw new GitCmdException($"Command '{name}' unrecognized.");
                }

                // dispach
                method.Invoke(this, null);
            } catch (TargetInvocationException tie) {
                var e = tie.InnerException;

                var ae = e as AggregateException;
                if (ae != null)
                {
                    e = ae.InnerException;
                }

                Log($"{e.GetType()}: {e.Message}");
            } catch (Exception e) {
                Log($"{e.GetType()}: {e.Message}");
            }
        }
Пример #11
0
        private LfxFileFlags GetFileFlags(GitCmdArgs args, bool pointer = false, bool content = false)
        {
            var flags = default(LfxFileFlags);

            if (args.IsSet(LfxCmdSwitches.Cached, LfxCmdSwitches.C))
            {
                flags |= LfxFileFlags.Tracked;
            }

            if (args.IsSet(LfxCmdSwitches.Others, LfxCmdSwitches.O))
            {
                flags |= LfxFileFlags.Untracked;
            }

            if (args.IsSet(LfxCmdSwitches.Content))
            {
                flags |= LfxFileFlags.Content;
            }

            if (args.IsSet(LfxCmdSwitches.Pointer))
            {
                flags |= LfxFileFlags.Pointer;
            }

            if (flags == default(LfxFileFlags))
            {
                flags = LfxFileFlags.Tracked;
            }

            if (pointer)
            {
                flags |= LfxFileFlags.Pointer;
            }

            if (content)
            {
                flags |= LfxFileFlags.Content;
            }

            return(flags);
        }
Пример #12
0
        public static void AllTypesTest()
        {
            var args = GitCmdArgs.Parse($"{ExeName} -{ShortSwitch} --{LongSwitch} \"{FileNameWithSpaces}\"");

            Assert.AreEqual(Exe, args.Exe);
            Assert.AreEqual(Name, args.Name);
            Assert.IsTrue(args.IsSet(LongSwitch));
            Assert.IsTrue(args.IsSet(ShortSwitch));
            Assert.IsTrue(args.IsSet(ShortSwitch, OtherShortSwitch));
            Assert.IsFalse(args.IsSet(OtherShortSwitch));
            Assert.AreEqual(FileNameWithSpaces, args.Single());
            Assert.AreEqual(FileNameWithSpaces, args[0]);
            Assert.AreEqual(1, args.Length);

            var toString = args.ToString();

            Assert.IsTrue(toString.Contains($"{Exe}"));
            Assert.IsTrue(toString.Contains($"{Name}"));
            Assert.IsTrue(toString.Contains($"{ShortSwitch.ToLower()}"));
            Assert.IsTrue(toString.Contains($"{LongSwitch.ToLower()}"));
            Assert.IsTrue(toString.Contains($"{FileNameWithSpaces}"));
        }
Пример #13
0
 public static void ThrowsDuplicateSwitch()
 {
     Throws(() => GitCmdArgs.Parse($"{ExeName} -a -a"));
     Throws(() => GitCmdArgs.Parse($"{ExeName} --aa --aa"));
 }
Пример #14
0
 public static void ThrowsBadSwitchLength()
 {
     Throws(() => GitCmdArgs.Parse($"{ExeName} --a"));
     Throws(() => GitCmdArgs.Parse($"{ExeName} -aa"));
 }
Пример #15
0
 public static void ThrowsNoSwitchName()
 {
     Throws(() => GitCmdArgs.Parse($"{ExeName} -"));
     GitCmdArgs.Parse($"{ExeName} --");
 }
Пример #16
0
 public static void ThrowsDasheCount()
 {
     Throws(() => GitCmdArgs.Parse($"{ExeName} ---a"));
 }
Пример #17
0
 public static void ArgBoundsName()
 {
     GitCmdArgs.Parse($"{ExeName} a", minArgs: 1, maxArgs: 1);
     Throws(() => GitCmdArgs.Parse($"{ExeName} a b", minArgs: 1, maxArgs: 1));
     Throws(() => GitCmdArgs.Parse($"{ExeName}", minArgs: 1, maxArgs: 1));
 }
Пример #18
0
 public static void ThrowsNoName()
 {
     Throws(() => GitCmdArgs.Parse($"--a"));
 }