예제 #1
0
        private static void ThrowWhenDuplicateProperties(IEnumerable <KeyValuePair <string, string> > settings, CodeDomProvider provider)
        {
            var duplicateCollapsedKeys = settings
                                         .GroupBy(s => StronglyTypedConfigBuilder.VerifyConfigKey(s.Key, provider))
                                         .Where(g => g.Count() > 1)
                                         .Select(g => g.Select(x => x.Key).Distinct())
                                         .ToList();

            if (duplicateCollapsedKeys.Any())
            {
                var keys = String.Join("; ", duplicateCollapsedKeys.Select(x => String.Join(", ", x)));
                throw new Exception($"The following sets of keys are unsupported as they'll normalize to duplicate property names: {keys}");
            }
        }
예제 #2
0
        public override bool Execute()
        {
            if (String.IsNullOrEmpty(Source))
            {
                return(true);
            }

            try {
                var provider = CodeProvider.ToUpperInvariant() == "VB"
                                        ? (CodeDomProvider) new VBCodeProvider()
                                        : (CodeDomProvider) new CSharpCodeProvider();

                //Validate config keys and generate code
                using (provider) {
                    IList <KeyValuePair <string, string> > settings;
                    if (File.Exists(Source))
                    {
                        settings = XDocument
                                   .Load(Source)
                                   .XPathSelectElements("/configuration/appSettings/add")
                                   .Select(x => new KeyValuePair <string, string>(x.Attribute("key")?.Value, x.Attribute("value")?.Value))
                                   .ToList();

                        ThrowWhenDuplicateKeys(settings);
                        ThrowWhenDuplicateProperties(settings, provider);
                    }
                    else
                    {
                        Log.LogWarning($"Could not find source config file: \"{Source}\"");
                        settings = new List <KeyValuePair <string, string> >();
                    }

                    using (var writer = new StringWriter()) {
                        var ccu = StronglyTypedConfigBuilder.Create(settings, Class, Namespace, provider, InternalClass);
                        provider.GenerateCodeFromCompileUnit(ccu, writer, new CodeGeneratorOptions {
                            BlankLinesBetweenMembers = false
                        });
                        GeneratedCode = writer.ToString();
                    }
                }

                //Prepare temporary file location for generated code
                string tempFilePath = OutputPath == null
                                        ? Path.Combine(Path.GetTempPath(), $"{Namespace.Replace(".", "_")}_{Class}_{Path.GetRandomFileName().Replace(".", "")}.g.cs")
                                        : Path.Combine(OutputPath, $"{Namespace.Replace(".", "_")}_{Class}.g.cs");

                string tempFileDir = Path.GetDirectoryName(tempFilePath);
                if (tempFileDir != null)
                {
                    Directory.CreateDirectory(tempFileDir);
                }

                //Write and output generated code
                File.WriteAllText(tempFilePath, GeneratedCode, Encoding.UTF8);
                GeneratedConfigPath = tempFilePath;
                return(true);
            } catch (Exception ex) {
                Log.LogErrorFromException(ex, false, false, Source);
                return(false);
            }
        }