Ejemplo n.º 1
0
        /// <summary>
        /// Export resources of specified type and culture to resx
        /// </summary>
        /// <typeparam name="TResource"></typeparam>
        /// <param name="expression"></param>
        /// <param name="culture"></param>
        /// <param name="overwrite"></param>
        /// <param name="p"></param>
        /// <param name="s"></param>
        /// <returns></returns>
        public async Task <int> ExportToResx <TResource>(Expression <Func <TResource, bool> > expression, string culture, bool overwrite, int p, int s)
            where TResource : class, IXDbResource
        {
            ResxWriter manager = new ResxWriter(typeof(TResource), _options.ResourcesPath, culture, _loggerFactory);

            int totalRawData = await GetRawDataCountAsync <TResource>(expression);

            int totalExported = 0;

            for (int i = 0; i < totalRawData; i += s)
            {
                var dataLot = await GetRawDataListAsync <TResource>(p, s, expression);

                totalExported += await manager.AddRangeAsync(dataLot, overwrite);

                if (totalExported > 0)
                {
                    var saved = await manager.SaveAsync();

                    if (saved)
                    {
                        _logger.LogInformation($"Total '{totalExported}' resources exported to '{manager.ResourceFilePath}'");
                    }
                    else
                    {
                        _logger.LogError($"Exported not successful to '{manager.ResourceFilePath}'");
                    }
                }
            }

            return(totalExported);
        }
        /// <summary>
        /// Export xml data to resx
        /// </summary>
        /// <param name="type"></param>
        /// <param name="culture"></param>
        /// <param name="approvedOnly"></param>
        /// <param name="overwriteExistingKeys"></param>
        /// <returns></returns>
        public async Task <int> ExportToResxAsync(Type type, string culture, bool approvedOnly, bool overwriteExistingKeys)
        {
            string resFileName  = ResourceTypeHelper.CreateResourceName(type, _options.ResourcesPath);
            var    filePath     = Path.Combine(_options.ResourcesPath, $"{resFileName}.{culture}");
            var    xmlFilePath  = $"{filePath}.xml";
            var    resxFilePath = $"{filePath}.resx";

            var _xd = XDocument.Load(xmlFilePath);

            var elements = approvedOnly
                ? _xd.Root.Descendants("data")
                           .Where(x => x.Attribute("isActive").Value == "true")
                           .Select(x => new ResxElement
            {
                Key     = x.Element("key")?.Value,
                Value   = x.Element("value")?.Value,
                Comment = x.Element("comment")?.Value
            })
                : _xd.Root.Descendants("data")
                           .Select(x => new ResxElement
            {
                Key     = x.Element("key")?.Value,
                Value   = x.Element("value")?.Value,
                Comment = x.Element("comment")?.Value
            });

            var resxWriter    = new ResxWriter(resxFilePath, _loggerFactory);
            var totalExported = await resxWriter.AddRangeAsync(elements.Where(x => x.Key != null && x.Value != null), overwriteExistingKeys);

            if (totalExported > 0)
            {
                var saved = await resxWriter.SaveAsync();

                if (saved)
                {
                    _logger.LogInformation($"Total '{totalExported}' resources exported to '{resxFilePath}'");
                }
                else
                {
                    _logger.LogError($"Exported not successful to '{resxFilePath}'");
                }
            }

            return(totalExported);
        }
        public ResxWriter GenerateResx()
        {
            var resxWriter = new ResxWriter();

            resxWriter.AddEntry("SolidityCompilerVersion", _solidityCompilerVersion);

            // Make paths relative
            var solSourceContent = _solSourceContent.ToDictionary(d => Util.GetRelativeFilePath(_solSourceDir, d.Key), d => d.Value);

            // Scan ast json for absolute paths and make them relative
            foreach (var absPathToken in _solcOutput.JObject["sources"].SelectTokens("$..absolutePath").OfType <JValue>())
            {
                var absPath = Util.GetRelativeFilePath(_solSourceDir, absPathToken.Value <string>());
                absPathToken.Value = absPath;
            }

            var solcSourceInfos = new List <SolcSourceInfo>();

            foreach (JProperty item in _solcOutput.JObject["sources"])
            {
                var fileName      = Util.GetRelativeFilePath(_solSourceDir, item.Name);
                var id            = item.Value.Value <int>("id");
                var astObj        = (JObject)item.Value["ast"];
                var sourceContent = solSourceContent[fileName];
                var sourceInfo    = new SolcSourceInfo
                {
                    AstJson    = astObj,
                    FileName   = fileName,
                    ID         = id,
                    SourceCode = sourceContent
                };
                solcSourceInfos.Add(sourceInfo);
            }

            var solcSourceInfosJson = JsonConvert.SerializeObject(solcSourceInfos, Formatting.Indented);

            resxWriter.AddEntry("SourcesList", solcSourceInfosJson);

            var solcBytecodeInfos = new List <SolcBytecodeInfo>();

            foreach (JProperty solFile in _solcOutput.JObject["contracts"])
            {
                foreach (JProperty solContract in solFile.Value)
                {
                    var fileName     = Util.GetRelativeFilePath(_solSourceDir, solFile.Name);
                    var contractName = solContract.Name;

                    var bytecodeObj         = solContract.Value["evm"]["bytecode"];
                    var deployedBytecodeObj = solContract.Value["evm"]["deployedBytecode"];

                    var sourceMap         = bytecodeObj.Value <string>("sourceMap");
                    var sourceMapDeployed = deployedBytecodeObj.Value <string>("sourceMap");

                    var opcodes         = bytecodeObj.Value <string>("opcodes");
                    var opcodesDeployed = deployedBytecodeObj.Value <string>("opcodes");

                    var bytecode         = bytecodeObj.Value <string>("object");
                    var bytecodeDeployed = deployedBytecodeObj.Value <string>("object");

                    solcBytecodeInfos.Add(new SolcBytecodeInfo
                    {
                        FilePath          = fileName,
                        ContractName      = contractName,
                        SourceMap         = sourceMap,
                        Opcodes           = opcodes,
                        SourceMapDeployed = sourceMapDeployed,
                        OpcodesDeployed   = opcodesDeployed,
                        Bytecode          = bytecode,
                        BytecodeDeployed  = bytecodeDeployed
                    });
                }
            }

            var solcBytecodeInfosJson = JsonConvert.SerializeObject(solcBytecodeInfos, Formatting.Indented);

            resxWriter.AddEntry("ByteCodeData", solcBytecodeInfosJson);

            var contractAbis     = _solcOutput.ContractsFlattened.ToDictionary(c => c.SolFile + "/" + c.ContractName, c => c.Contract.Abi);
            var contractAbisJson = JsonConvert.SerializeObject(contractAbis, Formatting.Indented);

            resxWriter.AddEntry("ContractAbiJson", contractAbisJson);

            return(resxWriter);
        }
Ejemplo n.º 4
0
        public Converter(string sourceFolderXml, string targetFolderYaml, string targetFolderJson, string targetFolderResx, bool checkBSDD = false)
        {
            CheckBSDD = checkBSDD;
            _bsdd     = new Bsdd();

            string propertySetVersionList  = string.Empty;
            string propertySetTemplateList = string.Empty;
            string propertyTypeList        = string.Empty;
            string propertyUnitList        = string.Empty;

            foreach (string sourceFile in Directory.EnumerateFiles(sourceFolderXml, "PSet*.xml").OrderBy(x => x).ToList())//.Where(x=>x.Contains("Pset_ConstructionResource")))
            {
                numberOfPsets++;
                PropertySetDef pSet = PropertySetDef.LoadFromFile(sourceFile);
                log.Info("--------------------------------------------------");
                log.Info($"Checking PSet {pSet.Name}");
                log.Info($"Opened PSet-File {sourceFile.Replace(sourceFolderXml + @"\", string.Empty)}");

                if (!propertySetVersionList.Contains(pSet.IfcVersion.version))
                {
                    propertySetVersionList += pSet.IfcVersion.version + ",";
                }
                if (!propertySetTemplateList.Contains(pSet.templatetype.ToString()))
                {
                    propertySetTemplateList += pSet.templatetype.ToString() + ",";
                }

                PropertySet propertySet = new PropertySet()
                {
                    name = pSet.Name,
                    dictionaryReference = new DictionaryReference()
                    {
                        ifdGuid    = "",
                        legacyGuid = ""
                    },
                    ifcVersion = new IfcVersion()
                    {
                        version = ConvertToSematicVersion(pSet.IfcVersion.version).ToString(),
                        schema  = pSet.IfcVersion.schema
                    },
                    definition = pSet.Definition
                };

                propertySet.applicableIfcClasses = new List <ApplicableIfcClass>();
                foreach (var applicableClass in pSet.ApplicableClasses)
                {
                    propertySet.applicableIfcClasses.Add(new ApplicableIfcClass()
                    {
                        name = applicableClass,
                        type = pSet.ApplicableTypeValue
                    });
                }

                //Insert missing standard localizations as dummys
                propertySet.localizations = new List <Localization>();
                foreach (string standardLanguage in StandardLanguages.OrderBy(x => x))
                {
                    if (propertySet.localizations.Where(x => x.language == standardLanguage).FirstOrDefault() == null)
                    {
                        propertySet.localizations.Add(new Localization()
                        {
                            language   = standardLanguage,
                            name       = string.Empty,
                            definition = string.Empty
                        });
                    }
                }

                if (CheckBSDD)
                {
                    if (propertySet.dictionaryReference.legacyGuid.Length == 0)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        log.Info($"      ERROR: The GUID is missing in PSet!");
                        Console.ResetColor();
                    }
                }
                IfdConceptList ifdConceptList = _bsdd.SearchNests(pSet.Name);
                if (ifdConceptList == null)
                {
                    log.Info($"      Could not find the PSet in bSDD");
                }
                else
                {
                    numberOfPsetsWithbSDDGuid++;
                    IfdConcept bsddPSet = ifdConceptList.IfdConcept.FirstOrDefault();
                    log.Info($"      Loaded Property from bSDD (1 out of {ifdConceptList.IfdConcept.Count})");
                    log.Info($"      Loaded PSet from bSDD");
                    log.Info($"         Guid:        {bsddPSet.Guid}");
                    log.Info($"         Status:      {bsddPSet.Status}");
                    log.Info($"         VersionDate: {bsddPSet.VersionDate}");
                    log.Info($"         Web:         http://bsdd.buildingsmart.org/#concept/browse/{bsddPSet.Guid}");

                    if (ifdConceptList.IfdConcept.Count == 1)
                    {
                        log.Info($"      The GUID of the PSet in the file was changed {propertySet.dictionaryReference.legacyGuid} => {bsddPSet.Guid}");
                        propertySet.dictionaryReference.ifdGuid = bsddPSet.Guid;
                    }
                }
                propertySet.dictionaryReference.dictionaryWebUri = $"http://bsdd.buildingsmart.org/#concept/browse/{propertySet.dictionaryReference.ifdGuid}";
                propertySet.dictionaryReference.dictionaryApiUri = $"http://bsdd.buildingsmart.org/api/4.0/IfdConcept/{propertySet.dictionaryReference.ifdGuid}";

                log.Info($"   Now checking the properties within the PSet");
                propertySet.properties = LoadProperties(pSet, pSet.PropertyDefs);
                propertySet            = Utils.PrepareTexts(propertySet);

                string targetFileYaml = sourceFile.Replace("xml", "YAML").Replace(sourceFolderXml, targetFolderYaml);
                string targetFileJson = sourceFile.Replace("xml", "json").Replace(sourceFolderXml, targetFolderJson);
                string targetFileResx = sourceFile.Replace("xml", "resx").Replace(sourceFolderXml, targetFolderResx);

                var ScalarStyleSingleQuoted = new YamlMemberAttribute()
                {
                    ScalarStyle = ScalarStyle.SingleQuoted
                };

                var yamlSerializer = new SerializerBuilder()
                                     //.WithNamingConvention(new CamelCaseNamingConvention())
                                     .WithAttributeOverride <PropertySet>(nc => nc.name, ScalarStyleSingleQuoted)
                                     .WithAttributeOverride <PropertySet>(nc => nc.definition, ScalarStyleSingleQuoted)
                                     .WithAttributeOverride <Localization>(nc => nc.name, ScalarStyleSingleQuoted)
                                     .WithAttributeOverride <Localization>(nc => nc.definition, ScalarStyleSingleQuoted)
                                     .Build();

                string yamlContent = yamlSerializer.Serialize(propertySet);
                File.WriteAllText(targetFileYaml, yamlContent, Encoding.UTF8);
                log.Info("The PSet was saved as YAML file");

                JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
                jsonSerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
                jsonSerializerSettings.NullValueHandling = NullValueHandling.Ignore;
                string jsonContent = JsonConvert.SerializeObject(propertySet, Formatting.Indented, jsonSerializerSettings);
                File.WriteAllText(targetFileJson, jsonContent, Encoding.UTF8);
                log.Info("The PSet was saved as JSON file");

                var yamlDeserializer = new DeserializerBuilder()
                                       .Build();
                try
                {
                    propertySet = yamlDeserializer.Deserialize <PropertySet>(new StringReader(File.ReadAllText(targetFileYaml)));
                    log.Info("The YAML file is valid");
                }
                catch (Exception ex)
                {
                    Console.Write("   ERROR!");
                    log.Info(ex.Message);
                }


                ResxWriter resx = new ResxWriter(targetFileResx);
                resx.Write(propertySet, StandardLanguages);
                log.Info("The PSet was saved as RESX file");
            }
            log.Info($"Number of PSets:                 {numberOfPsets}");
            log.Info($"   with not resolved bSDD Guid:  {numberOfPsetsWithbSDDGuid}");
            log.Info($"Number of Properties:            {numberOfProperties}");
            log.Info($"   with not resolved bSDD Guid:  {numberOfPropertiesWithbSDDGuid}");
        }
Ejemplo n.º 5
0
        public YamlTranslationWriter(string translationSourceFile, string folderYaml, string folderJson, string folderResx)
        {
            log.Info($"Inject the translation into the YAML files from this source table: {translationSourceFile}");
            if (translationSourceFile != null)
            {
                log.Info($"Inject translations from {translationSourceFile}");
            }
            if (folderYaml != null)
            {
                log.Info($"Inject into YAML files in this target folder: {folderYaml}");
            }
            if (folderJson != null)
            {
                log.Info($"Inject into JSON files in this target folder: {folderJson}");
            }
            if (folderResx != null)
            {
                log.Info($"Inject into RESX files in this target folder: {folderResx}");
            }

            if (translationSourceFile == null)
            {
                log.Error($"ERROR - The parameter translationSourceFile does not exist. Exiting!");
                return;
            }
            else
            if (!File.Exists(translationSourceFile))
            {
                log.Error($"ERROR - File {translationSourceFile} does not exist. Exiting!");
                return;
            }

            if (folderYaml == null)
            {
                log.Error($"ERROR - The parameter folderXml does not exist. Exiting!");
                return;
            }
            else
            if (!Directory.Exists(folderYaml))
            {
                log.Error($"ERROR - The Directory {folderYaml} does not exist. Exiting!");
                return;
            }

            if (folderJson != null)
            {
                if (!Directory.Exists(folderJson))
                {
                    log.Error($"ERROR - The Directory {folderJson} does not exist. Exiting!");
                    return;
                }
            }

            if (folderResx != null)
            {
                if (!Directory.Exists(folderResx))
                {
                    log.Error($"ERROR - The Directory {folderResx} does not exist. Exiting!");
                    return;
                }
            }


            foreach (Translation translation in ReadTranslationDataFromExcel(translationSourceFile))
            {
                string      yamlFileName     = Path.Combine(folderYaml, $"{translation.pset}.YAML");
                var         yamlDeserializer = new DeserializerBuilder().Build();
                PropertySet propertySet;
                try
                {
                    propertySet = yamlDeserializer.Deserialize <PropertySet>(new StringReader(File.ReadAllText(yamlFileName)));
                    log.Info($"Opened the YAML file {yamlFileName}");
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);
                    return;
                }

                var ScalarStyleSingleQuoted = new YamlMemberAttribute()
                {
                    ScalarStyle = ScalarStyle.SingleQuoted
                };
                var yamlSerializer = new SerializerBuilder()
                                     //.WithNamingConvention(new CamelCaseNamingConvention())
                                     .WithAttributeOverride <PropertySet>(nc => nc.name, ScalarStyleSingleQuoted)
                                     .WithAttributeOverride <PropertySet>(nc => nc.definition, ScalarStyleSingleQuoted)
                                     .WithAttributeOverride <Localization>(nc => nc.name, ScalarStyleSingleQuoted)
                                     .WithAttributeOverride <Localization>(nc => nc.definition, ScalarStyleSingleQuoted)
                                     .Build();

                switch (translation.type)
                {
                case "PSet":
                    log.Info($"Translate PSet {translation.pset} => {translation.name_TL} [{translation.language}]");
                    var localizationPSet = propertySet.localizations.Where(x => x.language.ToLower() == translation.language.ToLower()).FirstOrDefault();
                    localizationPSet.name       = translation.name_TL;
                    localizationPSet.definition = translation.definition_tl;
                    string yamlContentPSet = yamlSerializer.Serialize(propertySet);
                    File.WriteAllText(yamlFileName, yamlContentPSet, Encoding.UTF8);
                    break;

                case "Property":
                    log.Info($"Translated Property {translation.pset}.{translation.name} => {translation.name_TL} [{translation.language}]");
                    try
                    {
                        var localizationProperty = propertySet.properties.Where(x => x.name == translation.name).FirstOrDefault().localizations.Where(x => x.language.ToLower() == translation.language.ToLower()).FirstOrDefault();
                        localizationProperty.name       = translation.name_TL;
                        localizationProperty.definition = translation.definition_tl;
                        string yamlContentProperty = yamlSerializer.Serialize(propertySet);
                        File.WriteAllText(yamlFileName, yamlContentProperty, Encoding.UTF8);
                    }
                    catch (Exception ex)
                    {
                        log.Error($"ERROR: {ex.Message}");
                    }
                    break;
                }

                if (folderJson != null)
                {
                    string targetFileJson = yamlFileName.Replace(".YAML", ".json").Replace(folderYaml, folderJson);
                    JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
                    jsonSerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
                    jsonSerializerSettings.NullValueHandling = NullValueHandling.Ignore;
                    string jsonContent = JsonConvert.SerializeObject(propertySet, Formatting.Indented, jsonSerializerSettings);
                    File.WriteAllText(targetFileJson, jsonContent, Encoding.UTF8);
                    log.Info("The PSet was saved as JSON file");
                }

                if (folderResx != null)
                {
                    string     targetFileResx = yamlFileName.Replace(".YAML", ".resx").Replace(folderYaml, folderResx);
                    ResxWriter resx           = new ResxWriter(targetFileResx);
                    resx.Write(propertySet, StandardLanguages);
                    log.Info("The PSet was saved as RESX file");
                }
            }
        }