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 PullEnvironment([Required]string environment, [Optional][DefaultValue("1")] int daysbehind, [Optional]string pullfoldername)
        {
            var ph = new ProjectHandler().Initialize();

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

            var resp = GetRepository(ph, environment, pullfoldername);

            var files = resp.DownloadFiles(resp.GetLatestFiles((properties, i) =>
                properties.fullName.Contains("CP_") && properties.lastModifiedDate >= DateTime.Now.AddDays(daysBack)),
                CancellationToken.None);

            resp.SaveLocal(files);
        }
 public static void SolutionFolder()
 {
     var ph = new ProjectHandler().Initialize();
     Console.WriteLine(ph.GetProjectLocation);
 }
        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 static void ShowConfiguration()
 {
     string project = new ProjectHandler().Initialize().Get;
     project = project.Replace("\r\n", "\n");
     Console.WriteLine(project);
 }
        public static void MoveSolution(string location, string projectname)
        {
            if (String.IsNullOrWhiteSpace(location))
                location = System.IO.Directory.GetCurrentDirectory();

            var ph = new ProjectHandler().Initialize();
            ph.SaveProject(Path.Combine(location, projectname));
        }
        public void Setup()
        {
            SalesforceMigrationsProject project = new SalesforceMigrationsProject
            {
                ContextTypes = new[]
                {
                    "ApexClass", "ApexComponent", "ApexPage", "ApexTrigger", "Workflow", "RemoteSiteSetting",
                    "PermissionSet", "CustomObject", "StaticResource", "Profile"
                }.ToList(),
                Environments = new List<SalesForceEnvionment>
                {
                    new SalesForceEnvionment
                    {
                        Name = "Dev54 SandBox",
                        AuthorizationCredential = new Credential
                        {
                            UserName = "******",
                            Password = "******",
                            Token = "UBB2jcTNepy81bfDhy1duZt4I",
                            EnvironmentType = SalesforceEnvironmentType.Sandbox
                        }
                    },
                    new SalesForceEnvionment
                    {
                        Name = "Dev49 SandBox",
                        AuthorizationCredential = new Credential
                        {
                            UserName = "******",
                            Password = "******",
                            Token = "Up5Iauh1187WdUDLB519PVoN9",
                            EnvironmentType = SalesforceEnvironmentType.Sandbox
                        }
                    },
                    new SalesForceEnvionment
                    {
                        Name = "Dev58 SandBox",
                        AuthorizationCredential = new Credential
                        {
                            UserName = "******",
                            Password = "******",
                            Token = "68CsU07xwwEcgUbC6guo1Oix",
                            EnvironmentType = SalesforceEnvironmentType.Sandbox
                        }
                    },
                    new SalesForceEnvionment
                    {
                        Name = "Dev56 SandBox",
                        AuthorizationCredential = new Credential
                        {
                            UserName = "******",
                            Password = "******",
                            Token = "YBm64kT2z18rGOnTUuTJmAf1",
                            EnvironmentType = SalesforceEnvironmentType.Sandbox
                        }
                    },
                    new SalesForceEnvionment
                    {
                        Name = "Dev43 SandBox",
                        AuthorizationCredential = new Credential
                        {
                            UserName = "******",
                            Password = "******",
                            Token = "Dcv65G0PfeMbDnVCYKsz50OZf",
                            EnvironmentType = SalesforceEnvironmentType.Sandbox
                        }
                    },
                },
                PullEnvironments = new List<string>
                {
                    "Dev54 SandBox",
                    "Dev49 SandBox",
                    "Dev58 SandBox",
                    "Dev56 SandBox",
                    "Dev43 SandBox"
                },
                PushEnvironments = new List<string>
                {
                    "Dev58 SandBox"
                }
            };

            ProjectHandler projectHandler = new ProjectHandler()
                .Initialize(project);

            projectHandler.SaveProject();
        }
        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);
        }