private string GetConfigArguments(Config config, CreationType type)
        {
            var sb = new StringBuilder();
            sb.AppendFormat("/url:\"{0}\" ", config.Url);

            foreach (var argument in config.CommandLineArguments.Where(a => a.SettingType == CreationType.All || a.SettingType == type))
            {
                var value = argument.Value;
                if (argument.Name == "out")
                {
                    value = GetOutputFilePath(config, type);
                }
                if (argument.Value == null)
                {
                    sb.AppendFormat("/{0} ", argument.Name);
                }
                else
                {
                    sb.AppendFormat("/{0}:\"{1}\" ", argument.Name, value);
                }
            }

            if (!String.IsNullOrWhiteSpace(config.Password))
            {
                sb.AppendFormat("/username:\"{0}\" ", config.UserName);
                sb.AppendFormat("/password:\"{0}\" ", config.Password);

                // Add Login Info
                if (!config.UseCrmOnline && !String.IsNullOrWhiteSpace(config.Domain))
                {
                    sb.AppendFormat("/domain:\"{0}\" ", config.Domain);
                }
            }

            return sb.ToString();
        }
        private void UpdateCrmSvcUtilConfig(Config config)
        {
            lock (_updateAppConfigToken)
            {
                if (_configUpdated) { return; }
                //load custom config file
                Configuration file;

                string filePath = Path.GetFullPath(config.CrmSvcUtilPath) + ".config";
                var map = new ExeConfigurationFileMap { ExeConfigFilename = filePath };
                try
                {
                    file = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
                }
                catch (ConfigurationErrorsException ex)
                {
                    if (ex.BareMessage == "Root element is missing.")
                    {
                        File.Delete(filePath);
                        UpdateCrmSvcUtilConfig(config);
                        return;
                    }
                    throw;
                }

                if (UpdateConfigAppSetting(file, "ActionCommandLineText", config.ExtensionConfig.ActionCommandLineText, true) |
                    UpdateConfigAppSetting(file, "ActionsToSkip", config.ExtensionConfig.ActionsToSkip) |
                    UpdateConfigAppSetting(file, "AddDebuggerNonUserCode", config.ExtensionConfig.AddDebuggerNonUserCode.ToString()) |
                    UpdateConfigAppSetting(file, "AddNewFilesToProject", config.ExtensionConfig.AddNewFilesToProject.ToString()) |
                    UpdateConfigAppSetting(file, "CreateOneFilePerAction", config.ExtensionConfig.CreateOneFilePerAction.ToString()) |
                    UpdateConfigAppSetting(file, "CreateOneFilePerEntity", config.ExtensionConfig.CreateOneFilePerEntity.ToString()) |
                    UpdateConfigAppSetting(file, "CreateOneFilePerOptionSet", config.ExtensionConfig.CreateOneFilePerOptionSet.ToString()) |
                    UpdateConfigAppSetting(file, "EntityAttributeSpecifiedNames", config.ExtensionConfig.EntityAttributeSpecifiedNames) |
                    UpdateConfigAppSetting(file, "EntityCommandLineText", config.ExtensionConfig.EntityCommandLineText, true) |
                    UpdateConfigAppSetting(file, "EntitiesToSkip", config.ExtensionConfig.EntitiesToSkip) |
                    UpdateConfigAppSetting(file, "GenerateAttributeNameConsts", config.ExtensionConfig.GenerateAttributeNameConsts.ToString()) |
                    UpdateConfigAppSetting(file, "GenerateAnonymousTypeConstructor", config.ExtensionConfig.GenerateAnonymousTypeConstructor.ToString()) |
                    UpdateConfigAppSetting(file, "GenerateEnumProperties", config.ExtensionConfig.GenerateEnumProperties.ToString()) |
                    UpdateConfigAppSetting(file, "OptionSetsToSkip", config.ExtensionConfig.OptionSetsToSkip) |
                    UpdateConfigAppSetting(file, "OptionSetCommandLineText", config.ExtensionConfig.OptionSetCommandLineText, true) |
                    UpdateConfigAppSetting(file, "PropertyEnumMappings", config.ExtensionConfig.PropertyEnumMappings) |
                    UpdateConfigAppSetting(file, "RemoveRuntimeVersionComment", config.ExtensionConfig.RemoveRuntimeVersionComment.ToString()) |
                    UpdateConfigAppSetting(file, "UnmappedProperties", config.ExtensionConfig.UnmappedProperties) |
                    UpdateConfigAppSetting(file, "UseTfsToCheckoutFiles", config.ExtensionConfig.UseTfsToCheckoutFiles.ToString()) |
                    UpdateConfigAppSetting(file, "UseXrmClient", config.ExtensionConfig.UseXrmClient.ToString()))

                {
                    file.Save(ConfigurationSaveMode.Minimal);
                }
                _configUpdated = true;
            }
        }
 public Logic(Config config)
 {
     Config = config;
 }
 private static string GetSafeArgs(Config config, Process p)
 {
     var args = p.StartInfo.Arguments;
     if (config.MaskPassword && !String.IsNullOrWhiteSpace(config.Password))
     {
         args = p.StartInfo.Arguments.Replace(config.Password, new String('*', config.Password.Length));
     }
     return args;
 }
        private string GetOutputFilePath(Config config, CreationType creationType)
        {
            // ReSharper disable once AssignNullToNotNullAttribute
            var filePath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), Config.GetSettingValue(creationType, Config.UserArgumentNames.Out));

            if (creationType == CreationType.Actions && config.ExtensionConfig.CreateOneFilePerAction)
            {
                return Path.Combine(filePath, "Actions.cs");
            }
            else if (creationType == CreationType.Entities && config.ExtensionConfig.CreateOneFilePerEntity)
            {
                return Path.Combine(filePath, config.ServiceContextName + ".cs");
            }
            else if (creationType == CreationType.OptionSets && config.ExtensionConfig.CreateOneFilePerOptionSet)
            {
                return Path.Combine(filePath, "OptionSets.cs");
            }
            else
            {
                return filePath;
            }
        }
        public static Config Load(string filePath)
        {
            filePath = Path.Combine(filePath, "DLaB.EarlyBoundGenerator.Settings.xml");
            if (!File.Exists(filePath))
            {
                var config = GetDefault();
                config._filePath = filePath;
                return config;
            }

            var serializer = new XmlSerializer(typeof(POCO.Config));
            POCO.Config poco;
            using (var fs = new FileStream(filePath, FileMode.Open))
            {
                poco = (POCO.Config)serializer.Deserialize(fs);
                fs.Close();
            }
            var settings = new Config(poco, filePath);
            return settings;
        }
        private string GetConfigArguments(Config config, CreationType type)
        {
            var sb = new StringBuilder();
            if (!config.UseConnectionString)
            {
                sb.AppendFormat("/url:\"{0}\" ", config.Url);
            }

            foreach (var argument in config.CommandLineArguments.Where(a => a.SettingType == CreationType.All || a.SettingType == type))
            {
                var value = argument.Value;
                if (argument.Name == "out")
                {
                    value = GetOutputFilePath(config, type);
                }
                if (argument.Value == null)
                {
                    sb.AppendFormat("/{0} ", argument.Name);
                }
                else
                {
                    sb.AppendFormat("/{0}:\"{1}\" ", argument.Name, value);
                }
            }

            if (!string.IsNullOrWhiteSpace(config.Password))
            {
                if (Config.UseConnectionString)
                {
                    // Fix for https://github.com/daryllabar/DLaB.Xrm.XrmToolBoxTools/issues/14 - Problem with CRM 2016 on premises with ADFS
                    // CrmSvcUtil.exe /out:entitie.cs / connectionstring:"Url=https://serverName.domain.com:444/orgName;Domain=myDomain;UserName=username;Password=*****"
                    // And this command doesn't work :
                    // CrmSvcUtil.exe /out:entitie.cs /url:"https://serverName.domain.com:444/orgName" / domain:"myDomain" / username:"******" / password:"******"

                    var domain = string.Empty;
                    if (!string.IsNullOrWhiteSpace(config.Domain))
                    {
                        domain = "Domain=" +config.Domain + ";";
                    }
                    var password = config.Password.Replace("\"", "^\"").Replace("&", "^&");  // Handle Double Quotes and &s???
                    var builder = new System.Data.Common.DbConnectionStringBuilder
                    {
                        {"A", $"Url={config.Url};{domain}UserName={config.UserName};Password={password}"}
                    };
                    
                    sb.AppendFormat("/connectionstring:{0} ", builder.ConnectionString.Substring(2)); // Replace "A=" with "/connectionstring:"
                }
                else
                {
                    sb.AppendFormat("/username:\"{0}\" ", config.UserName);
                    sb.AppendFormat("/password:\"{0}\" ", config.Password);

                    // Add Login Info
                    if (!config.UseCrmOnline && !string.IsNullOrWhiteSpace(config.Domain))
                    {
                        sb.AppendFormat("/domain:\"{0}\" ", config.Domain);
                    }
                }
            }

            return sb.ToString();
        }
        private void RemoveObsoleteValues(POCO.Config poco, Config @default)
        {
            if (CrmSvcUtilRelativePath == @"CrmSvcUtil Ref\crmsvcutil.exe")
            {
                // 5.14.2015 XTB changed to use the Plugins Directory, but then MEF changed Paths to be realtive to Dll. 
                CrmSvcUtilRelativePath = @default.CrmSvcUtilRelativePath;
            }
            foreach (var value in poco.ExtensionArguments.Where(a => string.Equals(a.Value, "DLaB.CrmSvcUtilExtensions.Entity.OverridePropertyNames,DLaB.CrmSvcUtilExtensions", StringComparison.InvariantCultureIgnoreCase)).ToList())
            {
                // Pre 2.13.2016, this was the default value.  Replaced with a single naming service that both Entities and OptionSets can use
                poco.ExtensionArguments.Remove(value);
            }

            // Pre 2.13.2016, this was the default value.  Not Needed Anymore
            var old = "OpportunityProduct.OpportunityStateCode,opportunity_statuscode|" +
                      "OpportunityProduct.PricingErrorCode,qooi_pricingerrorcode|" +
                      "ResourceGroup.GroupTypeCode,constraintbasedgroup_grouptypecode";
            if (string.Equals(poco.ExtensionConfig.PropertyEnumMappings, old, StringComparison.InvariantCultureIgnoreCase) || string.Equals(poco.ExtensionConfig.PropertyEnumMappings, old + "|", StringComparison.InvariantCultureIgnoreCase))
            {
                poco.ExtensionConfig.PropertyEnumMappings = string.Empty;
            }
        }