Пример #1
0
        public static IVariableConfig FromJObject(JObject configData)
        {
            Dictionary <string, string> sourceFormats = new Dictionary <string, string>();
            List <string> order = new List <string>();

            if (configData.TryGetValue("sources", System.StringComparison.OrdinalIgnoreCase, out JToken sourcesData))
            {
                foreach (JObject source in (JArray)sourcesData)
                {
                    string name   = source.ToString("name");
                    string format = source.ToString("format");
                    sourceFormats[name] = format;
                    order.Add(name);
                }
            }

            IVariableConfig config = new VariableConfig
            {
                Sources        = sourceFormats,
                Order          = order,
                FallbackFormat = configData.ToString(nameof(FallbackFormat)),
                Expand         = configData.ToBool(nameof(Expand))
            };

            return(config);
        }
Пример #2
0
        public static IVariableConfig DefaultVariableSetup(string fallbackFormat)
        {
            IVariableConfig config = new VariableConfig
            {
                Sources = new Dictionary <string, string>
                {
                    { "environment", "env_{0}" },
                    { "user", "usr_{0}" }
                },
                Order = new List <string>()
                {
                    "environment", "user"
                },
                FallbackFormat = fallbackFormat ?? "{0}",
                Expand         = false
            };

            return(config);
        }
Пример #3
0
        public static CustomFileGlobModel FromJObject(JObject globData, string globName)
        {
            // setup the variable config
            IVariableConfig variableConfig;
            JToken          variableData;

            if (globData.TryGetValue(nameof(VariableFormat), System.StringComparison.OrdinalIgnoreCase, out variableData))
            {
                variableConfig = VariableConfig.FromJObject((JObject)variableData);
            }
            else
            {
                variableConfig = VariableConfig.DefaultVariableSetup();
            }

            // setup the custom operations
            List <ICustomOperationModel> customOpsForGlob = new List <ICustomOperationModel>();
            JToken operationData;

            if (globData.TryGetValue("Operations", System.StringComparison.OrdinalIgnoreCase, out operationData))
            {
                foreach (JObject operationConfig in (JArray)operationData)
                {
                    customOpsForGlob.Add(CustomOperationModel.FromJObject(operationConfig));
                }
            }

            CustomFileGlobModel globModel = new CustomFileGlobModel()
            {
                Glob           = globName,
                Operations     = customOpsForGlob,
                VariableFormat = variableConfig,
                FlagPrefix     = globData.ToString(nameof(FlagPrefix)),
                Condition      = globData.ToString(nameof(Condition))
            };

            return(globModel);
        }
Пример #4
0
        private IGlobalRunConfig ProduceOperationSetup(SpecialOperationConfigParams defaultModel, bool generateMacros, ICustomFileGlobModel customGlobModel = null)
        {
            List <IOperationProvider> operations = new List <IOperationProvider>();

            // TODO: if we allow custom config to specify a built-in conditional type, decide what to do.
            if (defaultModel.ConditionalStyle != ConditionalType.None)
            {
                operations.AddRange(ConditionalConfig.ConditionalSetup(defaultModel.ConditionalStyle, "C++", true, true, null));
            }

            if (customGlobModel == null || string.IsNullOrEmpty(customGlobModel.FlagPrefix))
            {   // these conditions may need to be separated - if there is custom info, but the flag prefix was not provided, we might want to raise a warning / error
                operations.AddRange(FlagsConfig.FlagsDefaultSetup(defaultModel.FlagPrefix));
            }
            else
            {
                operations.AddRange(FlagsConfig.FlagsDefaultSetup(customGlobModel.FlagPrefix));
            }

            IVariableConfig variableConfig;

            if (customGlobModel != null)
            {
                variableConfig = customGlobModel.VariableFormat;
            }
            else
            {
                variableConfig = VariableConfig.DefaultVariableSetup();
            }

            IReadOnlyList <IMacroConfig> macros                     = null;
            List <IMacroConfig>          computedMacros             = new List <IMacroConfig>();
            List <IReplacementTokens>    macroGeneratedReplacements = new List <IReplacementTokens>();

            if (generateMacros)
            {
                macros = ProduceMacroConfig(computedMacros);
            }

            macroGeneratedReplacements.Add(new ReplacementTokens(_safeNameName, SourceName));

            if (Symbols != null)
            {
                foreach (KeyValuePair <string, ISymbolModel> symbol in Symbols)
                {
                    if (symbol.Value.Replaces != null)
                    {
                        macroGeneratedReplacements.Add(new ReplacementTokens(symbol.Value.Replaces, symbol.Key));
                    }
                }
            }

            foreach (KeyValuePair <Guid, string> map in _guidToGuidPrefixMap)
            {
                foreach (char format in GuidMacroConfig.DefaultFormats)
                {
                    string newGuid = char.IsUpper(format) ? map.Key.ToString(format.ToString()).ToUpperInvariant() : map.Key.ToString(format.ToString()).ToLowerInvariant();
                    macroGeneratedReplacements.Add(new ReplacementTokens(map.Value + "-" + format, newGuid));
                }
            }

            IReadOnlyList <ICustomOperationModel> customOperationConfig;

            if (customGlobModel != null && customGlobModel.Operations != null)
            {
                customOperationConfig = customGlobModel.Operations;
            }
            else
            {
                customOperationConfig = new List <ICustomOperationModel>();
            }

            GlobalRunConfig config = new GlobalRunConfig()
            {
                Operations       = operations,
                VariableSetup    = variableConfig,
                Macros           = macros,
                ComputedMacros   = computedMacros,
                Replacements     = macroGeneratedReplacements,
                CustomOperations = customOperationConfig,
            };

            return(config);
        }
Пример #5
0
        private IGlobalRunConfig ProduceOperationSetup(SpecialOperationConfigParams defaultModel, bool generateMacros, ICustomFileGlobModel customGlobModel = null)
        {
            List <IOperationProvider> operations = new List <IOperationProvider>();

            // TODO: if we allow custom config to specify a built-in conditional type, decide what to do.
            if (defaultModel.ConditionalStyle != ConditionalType.None)
            {
                operations.AddRange(ConditionalConfig.ConditionalSetup(defaultModel.ConditionalStyle, defaultModel.EvaluatorName, true, true, null));
            }

            if (customGlobModel == null || string.IsNullOrEmpty(customGlobModel.FlagPrefix))
            {   // these conditions may need to be separated - if there is custom info, but the flag prefix was not provided, we might want to raise a warning / error
                operations.AddRange(FlagsConfig.FlagsDefaultSetup(defaultModel.FlagPrefix));
            }
            else
            {
                operations.AddRange(FlagsConfig.FlagsDefaultSetup(customGlobModel.FlagPrefix));
            }

            IVariableConfig variableConfig;

            if (customGlobModel != null)
            {
                variableConfig = customGlobModel.VariableFormat;
            }
            else
            {
                variableConfig = VariableConfig.DefaultVariableSetup(defaultModel.VariableFormat);
            }

            IReadOnlyList <IMacroConfig> macros                     = null;
            List <IMacroConfig>          computedMacros             = new List <IMacroConfig>();
            List <IReplacementTokens>    macroGeneratedReplacements = new List <IReplacementTokens>();

            if (generateMacros)
            {
                macros = ProduceMacroConfig(computedMacros);
            }

            if (SourceName != null)
            {
                if (SourceName.ToLower() != SourceName)
                {
                    if (SourceName.IndexOf('.') > -1)
                    {
                        macroGeneratedReplacements.Add(new ReplacementTokens(_lowerSafeNamespaceName, SourceName.ToLowerInvariant()));
                        macroGeneratedReplacements.Add(new ReplacementTokens(_lowerSafeNameName, SourceName.ToLowerInvariant().Replace('.', '_')));
                    }
                    else
                    {
                        macroGeneratedReplacements.Add(new ReplacementTokens(_lowerSafeNameName, SourceName.ToLowerInvariant()));
                    }
                }

                if (SourceName.IndexOf('.') > -1)
                {
                    macroGeneratedReplacements.Add(new ReplacementTokens(_safeNamespaceName, SourceName));
                    macroGeneratedReplacements.Add(new ReplacementTokens(_safeNameName, SourceName.Replace('.', '_')));
                }
                else
                {
                    macroGeneratedReplacements.Add(new ReplacementTokens(_safeNameName, SourceName));
                }
            }

            if (Symbols != null)
            {
                foreach (KeyValuePair <string, ISymbolModel> symbol in Symbols)
                {
                    if (symbol.Value.Replaces != null)
                    {
                        if (symbol.Value.Type == "bind")
                        {
                            if (string.IsNullOrWhiteSpace(symbol.Value.Binding))
                            {
                                EnvironmentSettings.Host.LogMessage($"Binding wasn't specified for bind-type symbol {symbol.Key}");
                            }
                            else
                            {
                                //Since this is a bind symbol, don't replace the literal with this symbol's value,
                                //  replace it with the value of the bound symbol
                                macroGeneratedReplacements.Add(new ReplacementTokens(symbol.Value.Binding, symbol.Value.Replaces));
                            }
                        }
                        else
                        {
                            //Replace the literal value in the "replaces" property with the evaluated value of the symbol
                            macroGeneratedReplacements.Add(new ReplacementTokens(symbol.Key, symbol.Value.Replaces));
                        }
                    }
                }
            }

            foreach (KeyValuePair <Guid, string> map in _guidToGuidPrefixMap)
            {
                foreach (char format in GuidMacroConfig.DefaultFormats)
                {
                    string newGuid = char.IsUpper(format) ? map.Key.ToString(format.ToString()).ToUpperInvariant() : map.Key.ToString(format.ToString()).ToLowerInvariant();
                    macroGeneratedReplacements.Add(new ReplacementTokens(map.Value + "-" + format, newGuid));
                }
            }

            IReadOnlyList <ICustomOperationModel> customOperationConfig;

            if (customGlobModel != null && customGlobModel.Operations != null)
            {
                customOperationConfig = customGlobModel.Operations;
            }
            else
            {
                customOperationConfig = new List <ICustomOperationModel>();
            }

            GlobalRunConfig config = new GlobalRunConfig()
            {
                Operations       = operations,
                VariableSetup    = variableConfig,
                Macros           = macros,
                ComputedMacros   = computedMacros,
                Replacements     = macroGeneratedReplacements,
                CustomOperations = customOperationConfig,
            };

            return(config);
        }