Esempio n. 1
0
        public void GenerateEnumsModule(
            IReadOnlyCollection <Type> enums,
            string enumsModuleName,
            Dictionary <Type, List <Func <string> > > staticMemberGeneratorsByEnumType,
            Dictionary <Type, TSEnumLocalizationAttributeBase> localizationByEnumType)
        {
            var sw = new Stopwatch();

            sw.Start();

            var duplicateEnumNames = enums
                                     .GroupBy(e => e.Name)
                                     .Where(g => g.Count() > 1)
                                     .Select(g => g.Key)
                                     .ToList();

            if (duplicateEnumNames.Any())
            {
                throw new InvalidOperationException(
                          $"Duplicate enum names found - {string.Join(", ", duplicateEnumNames)}");
            }

            if (enumsModuleName.EndsWith(".ts"))
            {
                enumsModuleName = enumsModuleName.Remove(enumsModuleName.Length - 3);
            }

            var enumModuleGenerator = new EnumModuleGenerator();

            enumModuleGenerator.Write(
                enums,
                _arguments.UseStringEnums,
                _arguments.GetResourceModuleName,
                staticMemberGeneratorsByEnumType,
                localizationByEnumType);
            _resultFileWriter.WriteFile(enumsModuleName + ".ts", enumModuleGenerator.GetResult());

            if (localizationByEnumType.Any())
            {
                if (!_resultFileWriter.CanWriteResourceFiles)
                {
                    throw new InvalidOperationException(
                              "TSEnumLocalization attributes found in processed assemblies, " +
                              "but no IResourceModuleWriterFactory instance has been provided via a plugin"
                              );
                }

                foreach (var culture in _arguments.LocalizationLanguages)
                {
                    using (var resourceModuleWriter = _resultFileWriter.WriteResourceFile(enumsModuleName, culture))
                    {
                        var enumResourceModuleGenerator = new EnumResourceModuleGenerator(resourceModuleWriter);
                        enumResourceModuleGenerator.WriteEnumLocalizations(localizationByEnumType);
                    }
                }
            }

            Console.WriteLine($"Enums generated in {sw.ElapsedMilliseconds} ms");
        }
Esempio n. 2
0
        private void generateEnumsModule(
            IEnumerable <Type> enums,
            IReadOnlyCollection <TSExtendEnumAttribute> staticMemberProviders,
            string enumsModuleName)
        {
            var sw = new Stopwatch();

            sw.Start();

            var duplicateEnumNames = enums
                                     .GroupBy(e => e.Name)
                                     .Where(g => g.Count() > 1)
                                     .Select(g => g.Key)
                                     .ToList();

            if (duplicateEnumNames.Any())
            {
                throw new InvalidOperationException($"Duplicate enum names found - {string.Join(", ", duplicateEnumNames)}");
            }

            var staticMemberProvidersLookup = staticMemberProviders.ToLookup(e => e.EnumType);

            if (enumsModuleName.EndsWith(".ts"))
            {
                enumsModuleName = enumsModuleName.Remove(enumsModuleName.Length - 3);
            }

            var enumModuleGenerator = new EnumModuleGenerator();

            enumModuleGenerator.Write(enums, _arguments.UseStringEnums, _arguments.GetResourceModuleName, staticMemberProvidersLookup);
            writeFile(enumsModuleName + ".ts", enumModuleGenerator.GetResult());

            var enumLocalizationAttributes = staticMemberProviders.OfType <TSEnumLocalizationAttribute>().ToList();

            if (_resourceModuleWriterFactory != null && enumLocalizationAttributes.Any())
            {
                foreach (var culture in _arguments.LocalizationLanguages)
                {
                    using (var resourceModuleWriter = writeResourceFile(enumsModuleName, culture))
                    {
                        var enumResourceModuleGenerator = new EnumResourceModuleGenerator(resourceModuleWriter);
                        enumResourceModuleGenerator.WriteEnumLocalizations(enumLocalizationAttributes);
                    }
                }
            }

            Console.WriteLine($"Enums generated in {sw.ElapsedMilliseconds} ms");
        }