private static SalesforceRepository GetRepository(ProjectHandler ph, string environment, string pullfoldername)
        {
            SalesforceRepository resp = null;

            if (String.IsNullOrWhiteSpace(pullfoldername))
            {
                resp = new SalesforceRepository(ph.GetContext(environment), new DateTimeDirectoryStrategy());
            }
            else
            {
                var strategy = new FlatDirectoryStrategy(pullfoldername);
                resp = new SalesforceRepository(ph.GetContext(environment), strategy);
            }

            return resp;
        }
        public static void PullAllEnvironments([Required][DefaultValue("1")] int daysbehind, [Optional]string pullfoldername)
        {
            var ph = new ProjectHandler().Initialize();

            int daysBack = System.Math.Abs(daysbehind) * (-1);

            foreach (string salesForcePullEnvionment in ph.GetPullEnviroments())
            {
                var ctx = ph.GetContext(salesForcePullEnvionment);
                if (ctx != null)
                {
                    var resp = GetRepository(ph, salesForcePullEnvionment, pullfoldername);
                    resp.SaveLocal(((fp, i) => fp.fullName.Contains("CP_") && fp.lastModifiedDate >= DateTime.Now.AddDays(daysBack)), CancellationToken.None);
                }
            }
        }
        public static void PushStaticResources([Required] string environment, string packagedirectory)
        {
            var ph = new ProjectHandler().Initialize();

            if(String.IsNullOrWhiteSpace(packagedirectory))
                packagedirectory = System.IO.Directory.GetCurrentDirectory();
            
            Console.WriteLine("Pushing from: {0}", packagedirectory);

            try
            {
                var resp = new SalesforceRepository(ph.GetContext(environment), null, new StaticResourcesOnlyDeploymentStrategy());

                var options = new DeployOptions()
                {
                    CheckOnly = false,
                    IgnoreWarnings = false,
                    PerformeRetrive = false,
                    RollbackOnError = true
                };

                var id = resp.Deploy(packagedirectory, options);

                Salesforce.Migrations.Assistant.Library.MetaDataService.DeployResult result = SalesforceRepositoryHelpers.WaitDeployResult(id, resp.GetContext, new CancellationToken());

                if (result.details.componentFailures != null)
                {
                    foreach (DeployMessage item in result.details.componentFailures)
                    {
                        if (!string.IsNullOrWhiteSpace(item.problem))
                        {
                            Console.WriteLine(item.problem);
                        }
                    }
                }

                if (result.done == true && result.success == true)
                {
                    Console.WriteLine("Deployment completed successfully with no errors!");
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                throw;
            }
        }
        public void PushDeployAllFiles()
        {
            var ph = new ProjectHandler().Initialize();

            var resp = new SalesforceRepository(ph.GetContext("Dev58 SandBox"),null, new VisualForceDeploymentStrategy());

            var options = new DeployOptions()
            {
                CheckOnly = false,
                IgnoreWarnings = false,
                PerformeRetrive = false,
                RollbackOnError = true
            };


            resp.Deploy(@"D:\\salesforce.migrations\\solution\\Dev54 SandBox\\_current", options);
        }
        public void DeploymentTest()
        {
            var ph = new ProjectHandler().Initialize();

            var resp = new SalesforceRepository(ph.GetContext("Dev58 SandBox"), new NoPullStrategy());

            var options = new DeployOptions()
            {
                CheckOnly = false,
                IgnoreWarnings = false,
                PerformeRetrive = false,
                RollbackOnError = true
            };

            var id = resp.Deploy(@"D:\salesforce.migrations\solution\Dev49 SandBox\10-13-2015-16-08-55", options);

            MetaDataService.DeployResult result = SalesforceRepositoryHelpers.WaitDeployResult(id, resp.GetContext, new CancellationToken());

            if (result.details.componentFailures != null)
            {
                foreach (DeployMessage item in result.details.componentFailures)
                {
                    if (!string.IsNullOrWhiteSpace(item.problem))
                    {
                        Console.WriteLine(item.problem);
                    }
                }
            }

            Assert.IsTrue(result.done);
        }
        public void RunAllEnvironments()
        {
            var ph = new ProjectHandler().Initialize();

            foreach (string salesForcePullEnvionment in ph.GetPullEnviroments())
            {
                var ctx = ph.GetContext(salesForcePullEnvionment);
                if (ctx != null)
                {
                    var resp = new SalesforceRepository(ctx, new DateTimeDirectoryStrategy());
                    resp.SaveLocal(
                        ((fp, i) => fp.fullName.Contains("CP_") && fp.lastModifiedDate >= DateTime.Now.AddDays(-15)),
                        CancellationToken.None);
                }
            }
        }
        public void RunSingle()
        {
            var ph = new ProjectHandler().Initialize();

            var resp = new SalesforceRepository(ph.GetContext("Dev43 SandBox"), new DateTimeDirectoryStrategy());
            var files = resp.DownloadFiles(resp.GetLatestFiles((properties, i) =>
                properties.fullName.Contains("CP_") && properties.lastModifiedDate >= DateTime.Now.AddDays(-15)),
                CancellationToken.None);

            resp.SaveLocal(files);
        }