示例#1
0
 public void InvalidCommandExceptionIsThrownIfUnrecognizedCommandIsFound()
 {
     var configFile = GetDummyConfigFile("dummycmd arg1 arg2 arg3");
     var sourceCmd = new SourceCommand { Args = configFile };
     var result = new Action(() => this.sourceCommandHandler.Execute(sourceCmd).Result.Should().BeFalse());
     result.ShouldThrow<AggregateException>().And.InnerException.Should().BeOfType<InvalidCommandException>();
     CleanupDummyConfigFile(configFile);
 }
        private static void RegisterGetSubcommand(CommandLineApplication sourcesCmd, ReportsFactory reportsFactory)
        {
            sourcesCmd.Command("get", c =>
            {
                c.Description = "Retrieves the source code for packages used by projects";

                var packagesArgument = c.Argument(
                    "[package]",
                    "The name of the package for which to retrieve the sources. Can only specify packages used by the project.",
                    multipleValues: true);

                var projectFileOption = c.Option(
                    "-p|--project",
                    "Optional. The path to a project.json file. If not specified, the project in the current folder is used.",
                    CommandOptionType.SingleValue);
                var packagesFolderOption = c.Option(
                    "--packages",
                    "Optional. The local packages folder",
                    CommandOptionType.SingleValue);
                var sourceFolderOption = c.Option(
                    "-o|--output",
                    "Optional. The path to the folder that will hold the source files.",
                    CommandOptionType.SingleValue);

                c.OnExecute(() =>
                {
                    var command            = new SourceCommand(packagesArgument.Value, reportsFactory.CreateReports(quiet: false));
                    command.ProjectFile    = projectFileOption.Value();
                    command.PackagesFolder = packagesFolderOption.Value();
                    command.SourcesFolder  = sourceFolderOption.Value();

                    if (!command.Execute())
                    {
                        return(-1);
                    }

                    return(0);
                });
            });
        }
示例#3
0
        internal static unsafe Int64 stream_callback(IntPtr state, IntPtr data, UInt64 len, SourceCommand cmd)
        {
            byte [] buffer = null;
            var     handle = GCHandle.FromIntPtr(state);

            if (!handle.IsAllocated)
            {
                return(-1);
            }
            var stream = handle.Target as Stream;

            if (stream == null)
            {
                return(-1);
            }
            switch (cmd)
            {
            case SourceCommand.Stat:
                if (len < (UInt64)sizeof(Native.zip_stat_t))
                {
                    return(-1);
                }
                var stat = Native.ZipSourceGetArgs <Native.zip_stat_t> (data, len);
                stat.size   = (UInt64)stream.Length;
                stat.valid |= (ulong)StatFlags.Size;
                Marshal.StructureToPtr(stat, data, false);
                return((Int64)sizeof(Native.zip_stat_t));

            case SourceCommand.Tell:
            case SourceCommand.TellWrite:
                return((Int64)stream.Position);

            case SourceCommand.Write:
                buffer = new byte [len];
                Marshal.Copy(data, buffer, 0, (int)len);
                stream.Write(buffer, 0, buffer.Length);
                return(buffer.Length);

            case SourceCommand.SeekWrite:
            case SourceCommand.Seek:
                Native.zip_error_t error;
                UInt64             offset = Native.zip_source_seek_compute_offset((UInt64)stream.Position, (UInt64)stream.Length, data, len, out error);
                stream.Seek((long)offset, SeekOrigin.Begin);
                break;

            case SourceCommand.CommitWrite:
                stream.Flush();
                break;

            case SourceCommand.Read:
                var length = (int)len;
                if (length > stream.Length - stream.Position)
                {
                    length = (int)(stream.Length - stream.Position);
                }
                buffer = new byte [length];
                int bytesRead = stream.Read(buffer, 0, length);
                Marshal.Copy(buffer, 0, data, bytesRead);
                return(bytesRead);

            case SourceCommand.BeginWrite:
            case SourceCommand.Open:
                stream.Position = 0;
                return(0);

            case SourceCommand.Close:
                stream.Flush();
                break;

            case SourceCommand.Free:
                if (handle.IsAllocated)
                {
                    handle.Free();
                }
                break;

            case SourceCommand.Supports:
                var supports = (Int64)Native.zip_source_make_command_bitmap(
                    SourceCommand.Open,
                    SourceCommand.Read,
                    SourceCommand.Close,
                    SourceCommand.Stat,
                    SourceCommand.Error,
                    SourceCommand.Free
                    );
                if (stream.CanSeek)
                {
                    supports |= (Int64)Native.zip_source_make_command_bitmap(
                        SourceCommand.Seek,
                        SourceCommand.Tell,
                        SourceCommand.Supports
                        );
                }
                if (stream.CanWrite)
                {
                    supports |= (Int64)Native.zip_source_make_command_bitmap(
                        SourceCommand.BeginWrite,
                        SourceCommand.CommitWrite,
                        SourceCommand.RollbackWrite,
                        SourceCommand.Write,
                        SourceCommand.SeekWrite,
                        SourceCommand.TellWrite,
                        SourceCommand.Remove
                        );
                }
                return(supports);

            default:
                break;
            }
            return(0);
        }
示例#4
0
 public static int ZipSourceMakeCommandBitmask(SourceCommand cmd)
 {
     return(1 << (int)cmd);
 }
示例#5
0
        static void Main(string[] args)
        {
            string srcImagePath = @"http://dev.mygemplace.com/Content/_Traders/2/jwProducts/8_Ring2_1qK1b.jpg";
            string photoName    = Path.GetFileNameWithoutExtension(srcImagePath);

            MemoryStream   memory    = new MemoryStream();
            HttpWebRequest lxRequest = (HttpWebRequest)WebRequest.Create(srcImagePath);

            using (HttpWebResponse lxResponse = (HttpWebResponse)lxRequest.GetResponse())
            {
                using (BinaryReader reader = new BinaryReader(lxResponse.GetResponseStream()))
                {
                    reader.BaseStream.CopyTo(memory);
                    //Byte[] lnByte = reader.ReadBytes(1 * 1024 * 1024 * 10);
                    //using (FileStream lxFS = new FileStream("34891.jpg", FileMode.Create))
                    //{
                    //    lxFS.Write(lnByte, 0, lnByte.Length);
                    //}
                }
            }

            Bitmap photo;

            try
            {
                photo = new Bitmap(memory);
            }
            catch (ArgumentException e)
            {
                throw new FileNotFoundException(string.Format(" GDIThumbnail generator file[{0}] not found.", srcImagePath), e);
            }

            // Factory Method
            Console.WriteLine();
            Console.WriteLine("[Abstract Factory] Pattern");
            IWidgetFactory abstractFactory = new PMWidgetFactory();
            IWidgetButton  abstractButton  = abstractFactory.CreateWidgetButton();
            IWidgetDialog  abstractDialog  = abstractFactory.CreateWidgetDialog();

            abstractButton.DrawButton();
            abstractDialog.DrawWidget();

            abstractButton.SetLocation();
            abstractDialog.SetTopMost();
            //-------------------

            // FactoryMethod/Virtual Constructor
            Console.WriteLine();
            Console.WriteLine("[FactoryMethod/Virtual Constructor] Pattern");
            Creator          creator    = new ConcreteCreator();
            IAMethodDocument amDocument = creator.CreateDocument();

            amDocument.Open();
            //----------------------------------

            // Builder
            Console.WriteLine("[Builder] Pattern");
            Console.WriteLine();
            Shop            shop    = new Shop();
            IVehicleBuilder builder = new CarBuilder();

            shop.Construct(builder);
            shop.ShowVehicle();
            builder = new VeloByke();
            shop.Construct(builder);
            shop.ShowVehicle();
            //----------------------

            // Facade
            // Provides more simple unified interface instead of few interfaces some subsystem.
            // Subsystem interfaces don't keep references to facade interface.
            Console.WriteLine();
            Console.WriteLine("[Facade] Pattern");
            Facade facade = new Facade();

            facade.MethodA();
            facade.MethodB();
            //-------

            // Flyweight
            // Build a document with text
            Console.WriteLine();
            Console.WriteLine("[Flyweight] Pattern");
            string document = "AAZZBBZB";

            char[]           chars   = document.ToCharArray();
            CharacterFactory factory = new CharacterFactory();

            // extrinsic state
            int pointSize = 10;

            //For each character use a flyweight object
            foreach (char c in chars)
            {
                pointSize++;
                Character character = factory.GetCharacter(c);
                character.Display(pointSize);
            }
            //-----------

            // Proxy
            Console.WriteLine();
            Console.WriteLine("[Proxy] pattern");
            IImage proxy = new ProxyImage();

            proxy.GetSize();
            proxy.Draw();
            //--------


            //Chain Responsibilities
            Console.WriteLine();
            Console.WriteLine("[Chain of Responsibilities] pattern");
            DialogChain dc1          = new DialogChain(null);
            ButtonChain bc1          = new ButtonChain(dc1);
            DialogChain dc2          = new DialogChain(bc1);
            ButtonChain buttonChain2 = new ButtonChain(dc2);
            IRequest    request1     = new Request1();

            ((Request1)request1).Value = "QWE_RTYU";
            buttonChain2.HandleRequest(request1);

            Request2 rq2 = new Request2();

            rq2.Value = "123456";
            buttonChain2.HandleRequest(rq2);

            //----------------------

            // Command
            Console.WriteLine();
            Console.WriteLine("[Command] Pattern");
            List <SourceCommand> srcCmd = new List <SourceCommand>();

            SourceCommand scr1 = new SourceCommand();

            scr1.Command = new OpenCommand(new Receiver1("Star1"));

            SourceCommand scr2 = new SourceCommand();

            scr2.Command = new PasteCommand(new Receiver2("Paste Star 2"));

            srcCmd.Add(scr1);
            srcCmd.Add(scr2);

            TemplatedCommand <string>       templatedCommand = new TemplatedCommand <string>(delegate(string s) { Console.WriteLine("---Delegate command is executed @@@@ {0}", s); });
            TemplatedSourceCommand <string> scr3             = new TemplatedSourceCommand <string>(templatedCommand);

            scr3.ActionInvoke("1111");

            foreach (var sourceCommand in srcCmd)
            {
                sourceCommand.InvokeCommand();
            }
            //---------

            // Interpreter
            string  roman   = "MCMXXVIII";
            Context context = new Context(roman);

            // Build the 'parse tree'
            List <Expression> tree = new List <Expression>();

            tree.Add(new ThousandExpression());
            tree.Add(new HundredExpression());
            tree.Add(new TenExpression());
            tree.Add(new OneExpression());

            // Interpret
            foreach (Expression exp in tree)
            {
                exp.Interpret(context);
            }

            Console.WriteLine("{0} = {1}", roman, context.Output);

            // define booleand expression
            // (true and x) or (y and x)
            Console.WriteLine("----------------------------");
            BooleanExp     expressing;
            BooleanContext boolContext = new BooleanContext();

            expressing = new OrExp(new AndExp(new BooleanConstant("true"), new VariableExp("x")),
                                   new AndExp(new VariableExp("y"), new NotExp("x")));

            boolContext.Assign("x", false);
            boolContext.Assign("y", false);

            Console.WriteLine("Result of boolean interpreter is [{0}]", expressing.Evaluate(boolContext));
            //-------------

            // Iterator
            Console.WriteLine();
            Console.WriteLine("Pattern Iterator");
            ConcreteAggregate aggregate = new ConcreteAggregate();

            aggregate[0] = "Object 1";
            aggregate[1] = "Object 2";
            aggregate[2] = "Object 3";
            aggregate[3] = "Object 4";
            Iterator iter = aggregate.CreateIterator();

            for (object i = iter.First(); !iter.IsDone(); i = iter.Next())
            {
                Console.WriteLine("Current object [{0}]", i);
            }

            //--------------

            // Mediator
            Console.WriteLine();
            Console.WriteLine("Pattern Mediator");

            // parts could be informed about its mediator.
            ConcreteMediator cm = new ConcreteMediator(new Collegue1(), new Collegue2(), new Collegue3());

            cm.Process1AndInform23();
            cm.Process3AndInform1();
            //------------

            // Memento
            Console.WriteLine();
            Console.WriteLine("Pattern Memento");

            SalesProspect salesProspect = new SalesProspect();

            salesProspect.Budget = 45.56;
            salesProspect.Name   = "Super Man";
            salesProspect.Phone  = "45-78-96";

            ProspectMemory prospectMemory = new ProspectMemory();

            prospectMemory.Memento = salesProspect.SaveMemento();

            salesProspect.Budget = 11.11;
            salesProspect.Name   = "Spider Man";
            salesProspect.Phone  = "33-44-55";

            salesProspect.RestoreMemento(prospectMemory.Memento);
            //--------------

            // Observer (Dependents, Publish-Subscriber)
            Console.WriteLine();
            Console.WriteLine("Pattern Observer");

            Subject           subject           = new Subject();
            ConcreteObserver1 concreteObserver1 = new ConcreteObserver1();
            ConcreteObserver2 concreteObserver2 = new ConcreteObserver2();

            subject.Register(concreteObserver1);
            subject.Register(concreteObserver2);
            subject.Register(concreteObserver1);

            subject.NotifyObservers();

            subject.UnRegister(concreteObserver2);
            subject.UnRegister(concreteObserver2);

            subject.NotifyObservers();
            //------------------------------------------

            // State
            Console.WriteLine();
            Console.WriteLine("Pattern State");
            Account account = new Account("Jim Johnson");

            // Apply financial transactions
            account.Deposit(500.0);
            account.Deposit(300.0);
            account.Deposit(550.0);
            account.PayInterest();
            account.Withdraw(2000.00);
            account.Withdraw(1100.00);
            account.Deposit(50000);
            account.PayInterest();


            //------------------------------------------

            // Strategy
            // Client should knew all available strategies.
            Console.WriteLine();
            Console.WriteLine("Pattern Strategy");

            StrategyContext strategyContext = new StrategyContext(null);

            strategyContext.ContextOperationOne();
            strategyContext.ContextOperationTwo();

            strategyContext.Strategy = new ConcreteStrategy();
            strategyContext.ContextOperationOne();
            strategyContext.ContextOperationTwo();

            //------------------------------------------

            // Template Method
            Console.WriteLine();
            Console.WriteLine("Template Method");
            TemplateMethodClass tmc = new ConcreteTemplateMethodClass1();

            tmc.TemplateMethod();
            //------------------------------------------

            // Visitor
            Console.WriteLine();
            Console.WriteLine("Visitor");
            List <INode> elements = new List <INode>();

            elements.Add(new NodeType1()
            {
                Balance = 400, Name = "Qwerty"
            });
            elements.Add(new NodeType1()
            {
                Balance = 333, Name = "QasxzWe"
            });
            elements.Add(new NodeType2()
            {
                CarName = "Mersedes"
            });
            NodeVisitor visitor = new ConcreteNodeVisitor();

            foreach (var element in elements)
            {
                element.Accept(visitor);
            }

            //------------------------------------------

            ThreadTest threadTest = new ThreadTest();

            //threadTest.RunTask();
            threadTest.TestFactory();

            // Unit of Work patternt with Repository pattern
            Console.WriteLine();
            Console.WriteLine("UOW pattern");
            StudentController sc = new StudentController();

            sc.DoAction();

            MoneyPattern.Start();
            Console.Read();
        }
示例#6
0
 public void LineStartingWithHashIsTreatedAsComment()
 {
     var configFile = GetDummyConfigFile("#stubcmd arg1 arg2 invalid arg");
     var sourceCmd = new SourceCommand { Args = configFile };
     this.sourceCommandHandler.Execute(sourceCmd).Result.Should().BeTrue();
     CleanupDummyConfigFile(configFile);
 }
示例#7
0
 public void FileNotFoundExceptionIsThrownIfSourceFileIsNotFound()
 {
     var sourceCmd = new SourceCommand { Args = @"z:\dummydummyfile" };
     var result = new Action(() => this.sourceCommandHandler.Execute(sourceCmd).Result.Should().BeFalse());
     result.ShouldThrow<AggregateException>().And.InnerException.Should().BeOfType<DirectoryNotFoundException>();
 }
示例#8
0
 public void SourceCommandHasNameAsSource()
 {
     var sourceCommand = new SourceCommand();
     sourceCommand.Name.Should().Be("source");
 }
示例#9
0
 public void LineWithNullOrEmptySpaceIsIgnored()
 {
     var configFile = GetDummyConfigFile("   \r\n\r\nstubcmd arg1 arg2 arg3");
     var sourceCmd = new SourceCommand { Args = configFile };
     this.sourceCommandHandler.Execute(sourceCmd).Result.Should().BeTrue();
     CleanupDummyConfigFile(configFile);
 }
示例#10
0
 public void LineWithCommandAndArgumentIsParsedFromTheSourceFile()
 {
     var configFile = GetDummyConfigFile("stubcmd arg1 arg2 arg3");
     var sourceCmd = new SourceCommand { Args = configFile };
     this.sourceCommandHandler.Execute(sourceCmd).Result.Should().BeTrue();
     CleanupDummyConfigFile(configFile);
 }
示例#11
0
 public void LineWithACommandWhichFailsReturnsCorrectLineNumberAndCommandName()
 {
     var configFile = GetDummyConfigFile("stubcmd arg1 arg2 arg3\rstubcmd2 arg1 arg2 arg3");
     var sourceCmd = new SourceCommand { Args = configFile };
     this.sourceCommandHandler.Execute(sourceCmd).Result.Should().BeFalse();
     this.sourceCommandHandler.ErrorMessage.Should().Be("Line: 2: Failed to run command: stubcmd2 arg1 arg2 arg3");
     CleanupDummyConfigFile(configFile);
 }