Exemplo n.º 1
0
		public CodeElement(CodeModelContext context, ISymbolModel symbolModel)
		{
			this.context = context;
			this.symbolModel = symbolModel;
			if (symbolModel.ParentProject != null)
				this.Language = symbolModel.ParentProject.GetCodeModelLanguage();
		}
        public void ExplicitNameSymbolWithCustomBindingRetainsCustomBinding()
        {
            SimpleConfigModel configModel = SimpleConfigModel.FromJObject(_engineEnvironmentSettings, ConfigWithNameSymbolWithCustomBinding);

            Assert.True(configModel.Symbols.ContainsKey("name"));

            ISymbolModel symbolInfo = configModel.Symbols["name"];

            Assert.True(symbolInfo is ParameterSymbol);

            ParameterSymbol nameSymbol = symbolInfo as ParameterSymbol;

            Assert.Equal("customBinding", symbolInfo.Binding);
        }
Exemplo n.º 3
0
        // Used when the template explicitly defines the symbol "name".
        // The template definition is used exclusively, except for the case where it doesn't define any value forms.
        // When that is the case, the default value forms are used.
        public static ParameterSymbol ExplicitNameSymbolMergeWithDefaults(ISymbolModel templateDefinedName, ISymbolModel defaultDefinedName)
        {
            if (!(templateDefinedName is ParameterSymbol templateSymbol))
            {
                throw new InvalidCastException("templateDefinedName is not a ParameterSymbol");
            }

            if (!(defaultDefinedName is ParameterSymbol defaultSymbol))
            {
                throw new InvalidCastException("defaultDefinedName is not a ParameterSymbol");
            }

            // the merged symbol is mostly the user defined symbol, except the conditional cases below.
            ParameterSymbol mergedSymbol = new ParameterSymbol()
            {
                DefaultValue        = templateSymbol.DefaultValue,
                Description         = templateSymbol.Description,
                IsRequired          = templateSymbol.IsRequired,
                Type                = templateSymbol.Type,
                Replaces            = templateSymbol.Replaces,
                DataType            = templateSymbol.DataType,
                FileRename          = templateSymbol.FileRename,
                IsTag               = templateSymbol.IsTag,
                TagName             = templateSymbol.TagName,
                Choices             = templateSymbol.Choices,
                ReplacementContexts = templateSymbol.ReplacementContexts,
            };

            // If the template hasn't explicitly defined a binding to the name symbol, use the default.
            if (string.IsNullOrEmpty(templateSymbol.Binding))
            {
                mergedSymbol.Binding = defaultDefinedName.Binding;
            }
            else
            {
                mergedSymbol.Binding = templateSymbol.Binding;
            }

            // if the template defined name symbol doesn't have any value forms defined, use the defaults.
            if (templateSymbol.Forms.GlobalForms.Count == 0)
            {
                mergedSymbol.Forms = defaultSymbol.Forms;
            }
            else
            {
                mergedSymbol.Forms = templateSymbol.Forms;
            }

            return(mergedSymbol);
        }
Exemplo n.º 4
0
        public void ExplicitNameSymbolWithoutBindingGetsDefaultNameBinding()
        {
            SimpleConfigModel configModel = SimpleConfigModel.FromJObject(ConfigWithNameSymbolWithoutBinding);

            Assert.True(configModel.Symbols.ContainsKey("name"));

            ISymbolModel symbolInfo = configModel.Symbols["name"];

            Assert.True(symbolInfo is ParameterSymbol);

            ParameterSymbol nameSymbol = symbolInfo as ParameterSymbol;

            Assert.Equal("name", symbolInfo.Binding);
        }
        public void ParameterSymbolWithNoValueFormsGetsIdentityFormAdded()
        {
            SimpleConfigModel configModel = SimpleConfigModel.FromJObject(_engineEnvironmentSettings, ConfigForSymbolWithoutValueForms);

            Assert.True(configModel.Symbols.ContainsKey("testSymbol"));

            ISymbolModel symbolInfo = configModel.Symbols["testSymbol"];

            Assert.True(symbolInfo is ParameterSymbol);

            ParameterSymbol paramSymbol = symbolInfo as ParameterSymbol;
            IList <string>  configuredValueFormNames = paramSymbol.Forms.GlobalForms.ToList();

            Assert.Equal(1, configuredValueFormNames.Count);
            Assert.Equal(IdentityValueForm.FormName, configuredValueFormNames[0]);
        }
        public void ParameterSymbolWithoutIdentityValueFormGetsIdentityAddedAsFirst()
        {
            SimpleConfigModel configModel = SimpleConfigModel.FromJObject(_engineEnvironmentSettings, ArrayConfigForSymbolWithFormsButNotIdentity);

            Assert.True(configModel.Symbols.ContainsKey("testSymbol"));

            ISymbolModel symbolInfo = configModel.Symbols["testSymbol"];

            Assert.True(symbolInfo is ParameterSymbol);

            ParameterSymbol paramSymbol = symbolInfo as ParameterSymbol;

            Assert.Single(paramSymbol.Forms.GlobalForms.ToList()
                          .Where(x => string.Equals(x, IdentityValueForm.FormName, StringComparison.OrdinalIgnoreCase))
                          );
            Assert.Equal(0, paramSymbol.Forms.GlobalForms.ToList().IndexOf(IdentityValueForm.FormName));
        }
        public void ObjectValueFormDefinitionRespectsAddIdentityFalse()
        {
            SimpleConfigModel configModel = SimpleConfigModel.FromJObject(_engineEnvironmentSettings, ConfigWithObjectValueFormDefinitionAddIdentityFalse);

            Assert.True(configModel.Symbols.ContainsKey("testSymbol"));

            ISymbolModel symbolInfo = configModel.Symbols["testSymbol"];

            Assert.True(symbolInfo is ParameterSymbol);

            ParameterSymbol paramSymbol = symbolInfo as ParameterSymbol;
            IList <string>  configuredValueFormNames = paramSymbol.Forms.GlobalForms.ToList();

            Assert.Equal(3, configuredValueFormNames.Count);
            Assert.Equal("foo", configuredValueFormNames[0]);
            Assert.Equal("bar", configuredValueFormNames[1]);
            Assert.Equal("baz", configuredValueFormNames[2]);
        }
Exemplo n.º 8
0
        public void NameSymbolObjectValueFormWithIdentityWithoutAddIdentityRetainsConfiguredForms()
        {
            SimpleConfigModel configModel = SimpleConfigModel.FromJObject(NameConfigObjectValueFormWithIdentityAndAddIdentityUnspecified);

            Assert.True(configModel.Symbols.ContainsKey("name"));

            ISymbolModel symbolInfo = configModel.Symbols["name"];

            Assert.True(symbolInfo is ParameterSymbol);

            ParameterSymbol paramSymbol = symbolInfo as ParameterSymbol;
            IList <string>  configuredValueFormNames = paramSymbol.Forms.GlobalForms.ToList();

            Assert.Equal(4, configuredValueFormNames.Count);
            Assert.Equal("foo", configuredValueFormNames[0]);
            Assert.Equal("bar", configuredValueFormNames[1]);
            Assert.Equal("baz", configuredValueFormNames[2]);
            Assert.Equal(IdentityValueForm.FormName, configuredValueFormNames[3]);
        }
        public void ParameterSymbolObjectValueFormDefinitionInfersAddIdentityTrue()
        {
            SimpleConfigModel configModel = SimpleConfigModel.FromJObject(_engineEnvironmentSettings, ParameterConfigObjectValueFormWithoutIdentityAndAddIdentityUnspecified);

            Assert.True(configModel.Symbols.ContainsKey("testSymbol"));

            ISymbolModel symbolInfo = configModel.Symbols["testSymbol"];

            Assert.True(symbolInfo is ParameterSymbol);

            ParameterSymbol paramSymbol = symbolInfo as ParameterSymbol;
            IList <string>  configuredValueFormNames = paramSymbol.Forms.GlobalForms.ToList();

            Assert.Equal(4, configuredValueFormNames.Count);
            Assert.Equal(IdentityValueForm.FormName, configuredValueFormNames[0]);
            Assert.Equal("foo", configuredValueFormNames[1]);
            Assert.Equal("bar", configuredValueFormNames[2]);
            Assert.Equal("baz", configuredValueFormNames[3]);
        }
        public void ObjectConfigParameterSymbolWithIdentityFormAndAddIdentityTrueRetainsConfiguredFormsExactly()
        {
            SimpleConfigModel configModel = SimpleConfigModel.FromJObject(_engineEnvironmentSettings, ObjectConfigParameterSymbolWithIdentityFormAndAddIdentityTrue);

            Assert.True(configModel.Symbols.ContainsKey("testSymbol"));

            ISymbolModel symbolInfo = configModel.Symbols["testSymbol"];

            Assert.True(symbolInfo is ParameterSymbol);

            ParameterSymbol nameSymbol = symbolInfo as ParameterSymbol;
            IList <string>  configuredValueFormNames = nameSymbol.Forms.GlobalForms.ToList();

            Assert.Equal(4, configuredValueFormNames.Count);
            Assert.Equal("foo", configuredValueFormNames[0]);
            Assert.Equal("bar", configuredValueFormNames[1]);
            Assert.Equal(IdentityValueForm.FormName, configuredValueFormNames[2]);
            Assert.Equal("baz", configuredValueFormNames[3]);
        }
        public void ParameterSymbolWithArrayIdentityValueFormRetainsFormsUnmodified()
        {
            SimpleConfigModel configModel = SimpleConfigModel.FromJObject(_engineEnvironmentSettings, ArrayConfigForSymbolWithValueFormsIncludingIdentity);

            Assert.True(configModel.Symbols.ContainsKey("testSymbol"));

            ISymbolModel symbolInfo = configModel.Symbols["testSymbol"];

            Assert.True(symbolInfo is ParameterSymbol);

            ParameterSymbol paramSymbol = symbolInfo as ParameterSymbol;
            IList <string>  configuredValueFormNames = paramSymbol.Forms.GlobalForms.ToList();

            Assert.Equal(4, configuredValueFormNames.Count);
            Assert.Equal("foo", configuredValueFormNames[0]);
            Assert.Equal("bar", configuredValueFormNames[1]);
            Assert.Equal("baz", configuredValueFormNames[2]);
            Assert.Equal(IdentityValueForm.FormName, configuredValueFormNames[3]);
        }
        public void ArrayConfigNameSymbolWithoutIdentityFormGetsIdentityFormAddedAsFirst()
        {
            SimpleConfigModel configModel = SimpleConfigModel.FromJObject(_engineEnvironmentSettings, ArrayConfigWithNameSymbolAndValueFormsButNotIdentity);

            Assert.True(configModel.Symbols.ContainsKey("name"));

            ISymbolModel symbolInfo = configModel.Symbols["name"];

            Assert.True(symbolInfo is ParameterSymbol);

            ParameterSymbol nameSymbol = symbolInfo as ParameterSymbol;
            IList <string>  configuredValueFormNames = nameSymbol.Forms.GlobalForms.ToList();

            Assert.Equal(4, configuredValueFormNames.Count);
            Assert.Equal(IdentityValueForm.FormName, configuredValueFormNames[0]);
            Assert.Equal("foo", configuredValueFormNames[1]);
            Assert.Equal("bar", configuredValueFormNames[2]);
            Assert.Equal("baz", configuredValueFormNames[3]);
        }
Exemplo n.º 13
0
        public void ArrayConfigNameSymbolWithIdentityFormRetainsConfiguredFormsExactly()
        {
            SimpleConfigModel configModel = SimpleConfigModel.FromJObject(ArrayConfigWithNameSymbolAndValueFormsWithIdentity);

            Assert.True(configModel.Symbols.ContainsKey("name"));

            ISymbolModel symbolInfo = configModel.Symbols["name"];

            Assert.True(symbolInfo is ParameterSymbol);

            ParameterSymbol nameSymbol = symbolInfo as ParameterSymbol;
            IList <string>  configuredValueFormNames = nameSymbol.Forms.GlobalForms.ToList();

            Assert.Equal(4, configuredValueFormNames.Count);
            Assert.Equal("foo", configuredValueFormNames[0]);
            Assert.Equal("bar", configuredValueFormNames[1]);
            Assert.Equal("baz", configuredValueFormNames[2]);
            Assert.Equal(IdentityValueForm.FormName, configuredValueFormNames[3]);
        }
        public void NameSymbolGetsAddedWithDefaultValueForms()
        {
            SimpleConfigModel configModel = SimpleConfigModel.FromJObject(_engineEnvironmentSettings, ArrayConfigForSymbolWithFormsButNotIdentity);

            Assert.True(configModel.Symbols.ContainsKey("name"));

            ISymbolModel symbolInfo = configModel.Symbols["name"];

            Assert.True(symbolInfo is ParameterSymbol);

            ParameterSymbol nameSymbol = symbolInfo as ParameterSymbol;
            IList <string>  configuredValueFormNames = nameSymbol.Forms.GlobalForms.ToList();

            Assert.Equal(5, configuredValueFormNames.Count);
            Assert.Equal(IdentityValueForm.FormName, configuredValueFormNames[0]);
            Assert.Equal(DefaultSafeNameValueFormModel.FormName, configuredValueFormNames[1]);
            Assert.Equal(DefaultLowerSafeNameValueFormModel.FormName, configuredValueFormNames[2]);
            Assert.Equal(DefaultSafeNamespaceValueFormModel.FormName, configuredValueFormNames[3]);
            Assert.Equal(DefaultLowerSafeNamespaceValueFormModel.FormName, configuredValueFormNames[4]);
        }
Exemplo n.º 15
0
        // Used when the template explicitly defines the symbol "name".
        // The template definition is used exclusively, except for the case where it doesn't define any value forms.
        // When that is the case, the default value forms are used.
        public static ParameterSymbol ExplicitNameSymbolMergeWithDefaults(ISymbolModel templateDefinedName, ISymbolModel defaultDefinedName)
        {
            if (!(templateDefinedName is ParameterSymbol templateSymbol))
            {
                throw new InvalidCastException("templateDefinedName is not a ParameterSymbol");
            }

            if (!(defaultDefinedName is ParameterSymbol defaultSymbol))
            {
                throw new InvalidCastException("defaultDefinedName is not a ParameterSymbol");
            }

            if (templateSymbol.Forms.GlobalForms.Count > 0)
            {   // template symbol has forms, use them
                return(templateSymbol);
            }

            ParameterSymbol mergedSymbol = new ParameterSymbol()
            {
                Binding             = templateSymbol.Binding,
                DefaultValue        = templateSymbol.DefaultValue,
                Description         = templateSymbol.Description,
                Forms               = defaultSymbol.Forms, // this is the only thing that gets replaced from the default
                IsRequired          = templateSymbol.IsRequired,
                Type                = templateSymbol.Type,
                Replaces            = templateSymbol.Replaces,
                DataType            = templateSymbol.DataType,
                FileRename          = templateSymbol.FileRename,
                IsTag               = templateSymbol.IsTag,
                TagName             = templateSymbol.TagName,
                Choices             = templateSymbol.Choices,
                ReplacementContexts = templateSymbol.ReplacementContexts,
            };

            return(mergedSymbol);
        }
Exemplo n.º 16
0
        public static SimpleConfigModel FromJObject(JObject source)
        {
            SimpleConfigModel tmp = new SimpleConfigModel();

            tmp.Author          = source.ToString(nameof(tmp.Author));
            tmp.Classifications = source.ArrayAsStrings(nameof(tmp.Classifications));
            tmp.DefaultName     = source.ToString(nameof(DefaultName));
            tmp.GroupIdentity   = source.ToString(nameof(GroupIdentity));
            tmp.Guids           = source.ArrayAsGuids(nameof(tmp.Guids));
            tmp.Identity        = source.ToString(nameof(tmp.Identity));
            tmp.Name            = source.ToString(nameof(tmp.Name));
            tmp.ShortName       = source.ToString(nameof(tmp.ShortName));
            tmp.SourceName      = source.ToString(nameof(tmp.SourceName));

            List <ExtendedFileSource> sources = new List <ExtendedFileSource>();

            tmp.Sources = sources;

            foreach (JObject item in source.Items <JObject>(nameof(tmp.Sources)))
            {
                ExtendedFileSource src = new ExtendedFileSource();
                sources.Add(src);
                src.CopyOnly = item.Get <JToken>(nameof(src.CopyOnly));
                src.Exclude  = item.Get <JToken>(nameof(src.Exclude));
                src.Include  = item.Get <JToken>(nameof(src.Include));

                List <SourceModifier> modifiers = new List <SourceModifier>();
                src.Modifiers = modifiers;
                foreach (JObject entry in item.Items <JObject>(nameof(src.Modifiers)))
                {
                    SourceModifier modifier = new SourceModifier();
                    modifier.Condition = entry.ToString(nameof(modifier.Condition));
                    modifier.CopyOnly  = entry.Get <JToken>(nameof(modifier.CopyOnly));
                    modifier.Exclude   = entry.Get <JToken>(nameof(modifier.Exclude));
                    modifier.Include   = entry.Get <JToken>(nameof(modifier.Include));
                    modifiers.Add(modifier);
                }

                src.Source = item.ToString(nameof(src.Source));
                src.Target = item.ToString(nameof(src.Target));
            }

            Dictionary <string, ISymbolModel> symbols = new Dictionary <string, ISymbolModel>(StringComparer.Ordinal);

            tmp.Symbols = symbols;
            foreach (JProperty prop in source.PropertiesOf(nameof(tmp.Symbols)))
            {
                JObject obj = prop.Value as JObject;

                if (obj == null)
                {
                    continue;
                }

                ISymbolModel model = SymbolModelConverter.GetModelForObject(obj);

                if (model != null)
                {
                    symbols[prop.Name] = model;
                }
            }

            tmp.Tags = source.ToStringDictionary(StringComparer.OrdinalIgnoreCase, nameof(tmp.Tags));

            return(tmp);
        }
Exemplo n.º 17
0
        private SimpleConfigModel(JObject source, ILogger?logger, ISimpleConfigModifiers?configModifiers = null, string?filename = null)
        {
            _logger = logger;

            //TODO: improve validation not to allow null values here
            Identity = source.ToString(nameof(Identity));
            Name     = source.ToString(nameof(Name));

            Author          = source.ToString(nameof(Author));
            Classifications = source.ArrayAsStrings(nameof(Classifications));
            DefaultName     = source.ToString(nameof(DefaultName));
            Description     = source.ToString(nameof(Description)) ?? string.Empty;
            GroupIdentity   = source.ToString(nameof(GroupIdentity));
            Precedence      = source.ToInt32(nameof(Precedence));
            Guids           = source.ArrayAsGuids(nameof(Guids));

            SourceName          = source.ToString(nameof(SourceName));
            PlaceholderFilename = source.ToString(nameof(PlaceholderFilename)) !;
            GeneratorVersions   = source.ToString(nameof(GeneratorVersions));
            ThirdPartyNotices   = source.ToString(nameof(ThirdPartyNotices));
            PreferNameDirectory = source.ToBool(nameof(PreferNameDirectory));

            ShortNameList = JTokenStringOrArrayToCollection(source.Get <JToken>("ShortName"), Array.Empty <string>());
            Forms         = SetupValueFormMapForTemplate(source);

            var sources = new List <ExtendedFileSource>();

            Sources = sources;
            foreach (JObject item in source.Items <JObject>(nameof(Sources)))
            {
                ExtendedFileSource src = new ExtendedFileSource();
                sources.Add(src);
                src.CopyOnly  = item.Get <JToken>(nameof(src.CopyOnly));
                src.Exclude   = item.Get <JToken>(nameof(src.Exclude));
                src.Include   = item.Get <JToken>(nameof(src.Include));
                src.Condition = item.ToString(nameof(src.Condition));
                src.Rename    = item.Get <JObject>(nameof(src.Rename))?.ToStringDictionary().ToDictionary(x => x.Key, x => x.Value);

                List <SourceModifier> modifiers = new List <SourceModifier>();
                src.Modifiers = modifiers;
                foreach (JObject entry in item.Items <JObject>(nameof(src.Modifiers)))
                {
                    SourceModifier modifier = new SourceModifier
                    {
                        Condition = entry.ToString(nameof(modifier.Condition)),
                        CopyOnly  = entry.Get <JToken>(nameof(modifier.CopyOnly)),
                        Exclude   = entry.Get <JToken>(nameof(modifier.Exclude)),
                        Include   = entry.Get <JToken>(nameof(modifier.Include)),
                        Rename    = entry.Get <JObject>(nameof(modifier.Rename))
                    };
                    modifiers.Add(modifier);
                }

                src.Source = item.ToString(nameof(src.Source));
                src.Target = item.ToString(nameof(src.Target));
            }

            IBaselineInfo?baseline = null;

            BaselineInfo = BaselineInfoFromJObject(source.PropertiesOf("baselines"));

            if (!string.IsNullOrEmpty(configModifiers?.BaselineName))
            {
                BaselineInfo.TryGetValue(configModifiers !.BaselineName, out baseline);
            }

            Dictionary <string, ISymbolModel> symbols = new Dictionary <string, ISymbolModel>(StringComparer.Ordinal);

            // create a name symbol. If one is explicitly defined in the template, it'll override this.
            NameSymbol = SetupDefaultNameSymbol(SourceName);
            symbols.Add(NameSymbolName, NameSymbol);

            // tags are being deprecated from template configuration, but we still read them for backwards compatibility.
            // They're turned into symbols here, which eventually become tags.
            _tags = source.ToStringDictionary(StringComparer.OrdinalIgnoreCase, "tags");
            IReadOnlyDictionary <string, ISymbolModel> symbolsFromTags = ConvertDeprecatedTagsToParameterSymbols(_tags);

            foreach (KeyValuePair <string, ISymbolModel> tagSymbol in symbolsFromTags)
            {
                if (!symbols.ContainsKey(tagSymbol.Key))
                {
                    symbols.Add(tagSymbol.Key, tagSymbol.Value);
                }
            }

            _symbols = symbols;
            foreach (JProperty prop in source.PropertiesOf(nameof(Symbols)))
            {
                if (prop.Value is not JObject obj)
                {
                    continue;
                }

                string?defaultOverride = null;
                if (baseline?.DefaultOverrides != null)
                {
                    baseline.DefaultOverrides.TryGetValue(prop.Name, out defaultOverride);
                }

                ISymbolModel modelForSymbol = SymbolModelConverter.GetModelForObject(obj, defaultOverride);

                if (modelForSymbol != null)
                {
                    // The symbols dictionary comparer is Ordinal, making symbol names case-sensitive.
                    if (string.Equals(prop.Name, NameSymbolName, StringComparison.Ordinal) &&
                        symbols.TryGetValue(prop.Name, out ISymbolModel existingSymbol) &&
                        existingSymbol is ParameterSymbol existingParameterSymbol &&
                        modelForSymbol is ParameterSymbol modelForParameterSymbol)
                    {
                        // "name" symbol is explicitly defined above. If it's also defined in the template.json, it gets special handling here.
                        symbols[prop.Name] = new ParameterSymbol(modelForParameterSymbol, existingParameterSymbol.Binding, existingParameterSymbol.Forms);
                    }
                    else
                    {
                        // last in wins (in the odd case where a template.json defined a symbol multiple times)
                        symbols[prop.Name] = modelForSymbol;
                    }
                }
            }
Exemplo n.º 18
0
        public static SimpleConfigModel FromJObject(IEngineEnvironmentSettings environmentSettings, JObject source, JObject localeSource = null)
        {
            ILocalizationModel localizationModel = LocalizationFromJObject(localeSource);

            SimpleConfigModel config = new SimpleConfigModel()
            {
                Author              = localizationModel?.Author ?? source.ToString(nameof(config.Author)),
                Classifications     = source.ArrayAsStrings(nameof(config.Classifications)),
                DefaultName         = source.ToString(nameof(DefaultName)),
                Description         = localizationModel?.Description ?? source.ToString(nameof(Description)) ?? string.Empty,
                GroupIdentity       = source.ToString(nameof(GroupIdentity)),
                Guids               = source.ArrayAsGuids(nameof(config.Guids)),
                Identity            = source.ToString(nameof(config.Identity)),
                Name                = localizationModel?.Name ?? source.ToString(nameof(config.Name)),
                ShortName           = source.ToString(nameof(config.ShortName)),
                SourceName          = source.ToString(nameof(config.SourceName)),
                PlaceholderFilename = source.ToString(nameof(config.PlaceholderFilename)),
                EnvironmentSettings = environmentSettings
            };

            List <ExtendedFileSource> sources = new List <ExtendedFileSource>();

            config.Sources = sources;

            foreach (JObject item in source.Items <JObject>(nameof(config.Sources)))
            {
                ExtendedFileSource src = new ExtendedFileSource();
                sources.Add(src);
                src.CopyOnly  = item.Get <JToken>(nameof(src.CopyOnly));
                src.Exclude   = item.Get <JToken>(nameof(src.Exclude));
                src.Include   = item.Get <JToken>(nameof(src.Include));
                src.Condition = item.ToString(nameof(src.Condition));

                List <SourceModifier> modifiers = new List <SourceModifier>();
                src.Modifiers = modifiers;
                foreach (JObject entry in item.Items <JObject>(nameof(src.Modifiers)))
                {
                    SourceModifier modifier = new SourceModifier
                    {
                        Condition = entry.ToString(nameof(modifier.Condition)),
                        CopyOnly  = entry.Get <JToken>(nameof(modifier.CopyOnly)),
                        Exclude   = entry.Get <JToken>(nameof(modifier.Exclude)),
                        Include   = entry.Get <JToken>(nameof(modifier.Include)),
                        Rename    = entry.Get <JObject>(nameof(modifier.Rename))
                    };
                    modifiers.Add(modifier);
                }

                src.Source = item.ToString(nameof(src.Source));
                src.Target = item.ToString(nameof(src.Target));
            }

            Dictionary <string, ISymbolModel> symbols = new Dictionary <string, ISymbolModel>(StringComparer.Ordinal);

            // tags are being deprecated from template configuration, but we still read them for backwards compatibility.
            // They're turned into symbols here, which eventually become tags.
            config._tagsDeprecated = source.ToStringDictionary(StringComparer.OrdinalIgnoreCase, nameof(config.Tags));
            IReadOnlyDictionary <string, ISymbolModel> symbolsFromTags = ConvertDeprecatedTagsToParameterSymbols(config._tagsDeprecated);

            foreach (KeyValuePair <string, ISymbolModel> tagSymbol in symbolsFromTags)
            {
                if (!symbols.ContainsKey(tagSymbol.Key))
                {
                    symbols.Add(tagSymbol.Key, tagSymbol.Value);
                }
            }

            config.Symbols = symbols;
            foreach (JProperty prop in source.PropertiesOf(nameof(config.Symbols)))
            {
                JObject obj = prop.Value as JObject;

                if (obj == null)
                {
                    continue;
                }

                IParameterSymbolLocalizationModel symbolLocalization = null;
                if (localizationModel != null)
                {
                    localizationModel.ParameterSymbols.TryGetValue(prop.Name, out symbolLocalization);
                }

                ISymbolModel model = SymbolModelConverter.GetModelForObject(obj, symbolLocalization);

                if (model != null)
                {
                    symbols[prop.Name] = model;
                }
            }

            config.PostActionModel = RunnableProjects.PostActionModel.ListFromJArray(source.Get <JArray>("PostActions"), localizationModel?.PostActions);
            config.PrimaryOutputs  = CreationPathModel.ListFromJArray(source.Get <JArray>(nameof(PrimaryOutputs)));

            // Custom operations at the global level
            JToken globalCustomConfigData = source[nameof(config.CustomOperations)];

            if (globalCustomConfigData != null)
            {
                config.CustomOperations = CustomFileGlobModel.FromJObject((JObject)globalCustomConfigData, string.Empty);
            }
            else
            {
                config.CustomOperations = null;
            }

            // Custom operations for specials
            IReadOnlyDictionary <string, JToken> allSpecialOpsConfig = source.ToJTokenDictionary(StringComparer.OrdinalIgnoreCase, "SpecialCustomOperations");
            List <ICustomFileGlobModel>          specialCustomSetup  = new List <ICustomFileGlobModel>();

            foreach (KeyValuePair <string, JToken> globConfigKeyValue in allSpecialOpsConfig)
            {
                string globName = globConfigKeyValue.Key;
                JToken globData = globConfigKeyValue.Value;

                CustomFileGlobModel globModel = CustomFileGlobModel.FromJObject((JObject)globData, globName);
                specialCustomSetup.Add(globModel);
            }

            config.SpecialCustomSetup = specialCustomSetup;

            // localization operations for individual files
            Dictionary <string, IReadOnlyList <IOperationProvider> > localizations = new Dictionary <string, IReadOnlyList <IOperationProvider> >();

            if (localizationModel != null && localizationModel.FileLocalizations != null)
            {
                foreach (FileLocalizationModel fileLocalization in localizationModel.FileLocalizations)
                {
                    List <IOperationProvider> localizationsForFile = new List <IOperationProvider>();
                    foreach (KeyValuePair <string, string> localizationInfo in fileLocalization.Localizations)
                    {
                        localizationsForFile.Add(new Replacement(localizationInfo.Key, localizationInfo.Value, null));
                    }

                    localizations.Add(fileLocalization.File, localizationsForFile);
                }
            }
            config.LocalizationOperations = localizations;

            return(config);
        }
Exemplo n.º 19
0
        public static SimpleConfigModel FromJObject(JObject source, JObject localeSource = null)
        {
            ILocalizationModel localizationModel = LocalizationFromJObject(localeSource);

            SimpleConfigModel config = new SimpleConfigModel();

            config.Author              = localizationModel?.Author ?? source.ToString(nameof(config.Author));
            config.Classifications     = source.ArrayAsStrings(nameof(config.Classifications));
            config.DefaultName         = source.ToString(nameof(DefaultName));
            config.Description         = localizationModel?.Description ?? source.ToString(nameof(Description));
            config.GroupIdentity       = source.ToString(nameof(GroupIdentity));
            config.Guids               = source.ArrayAsGuids(nameof(config.Guids));
            config.Identity            = source.ToString(nameof(config.Identity));
            config.Name                = localizationModel?.Name ?? source.ToString(nameof(config.Name));
            config.ShortName           = source.ToString(nameof(config.ShortName));
            config.SourceName          = source.ToString(nameof(config.SourceName));
            config.PlaceholderFilename = source.ToString(nameof(config.PlaceholderFilename));

            List <ExtendedFileSource> sources = new List <ExtendedFileSource>();

            config.Sources = sources;

            foreach (JObject item in source.Items <JObject>(nameof(config.Sources)))
            {
                ExtendedFileSource src = new ExtendedFileSource();
                sources.Add(src);
                src.CopyOnly  = item.Get <JToken>(nameof(src.CopyOnly));
                src.Exclude   = item.Get <JToken>(nameof(src.Exclude));
                src.Include   = item.Get <JToken>(nameof(src.Include));
                src.Condition = item.ToString(nameof(src.Condition));

                List <SourceModifier> modifiers = new List <SourceModifier>();
                src.Modifiers = modifiers;
                foreach (JObject entry in item.Items <JObject>(nameof(src.Modifiers)))
                {
                    SourceModifier modifier = new SourceModifier();
                    modifier.Condition = entry.ToString(nameof(modifier.Condition));
                    modifier.CopyOnly  = entry.Get <JToken>(nameof(modifier.CopyOnly));
                    modifier.Exclude   = entry.Get <JToken>(nameof(modifier.Exclude));
                    modifier.Include   = entry.Get <JToken>(nameof(modifier.Include));
                    modifiers.Add(modifier);
                }

                src.Source = item.ToString(nameof(src.Source));
                src.Target = item.ToString(nameof(src.Target));
            }

            Dictionary <string, ISymbolModel> symbols = new Dictionary <string, ISymbolModel>(StringComparer.Ordinal);

            config.Symbols = symbols;
            foreach (JProperty prop in source.PropertiesOf(nameof(config.Symbols)))
            {
                JObject obj = prop.Value as JObject;

                if (obj == null)
                {
                    continue;
                }

                string localizedDescription = null;
                if (localizationModel != null)
                {
                    localizationModel.SymbolDescriptions.TryGetValue(prop.Name, out localizedDescription);
                }

                ISymbolModel model = SymbolModelConverter.GetModelForObject(obj, localizedDescription);

                if (model != null)
                {
                    symbols[prop.Name] = model;
                }
            }

            config.Tags            = source.ToStringDictionary(StringComparer.OrdinalIgnoreCase, nameof(config.Tags));
            config.PostActionModel = RunnableProjects.PostActionModel.ListFromJArray((JArray)source["PostActions"], localizationModel?.PostActions);

            config.PrimaryOutputs = CreationPathModel.ListFromJArray((JArray)source["PrimaryOutputs"]);

            // Custom operations at the global level
            JToken globalCustomConfigData = source[nameof(config.CustomOperations)];

            if (globalCustomConfigData != null)
            {
                config.CustomOperations = CustomFileGlobModel.FromJObject((JObject)globalCustomConfigData, string.Empty);
            }
            else
            {
                config.CustomOperations = null;
            }

            // Custom operations for specials
            IReadOnlyDictionary <string, JToken> allSpecialOpsConfig = source.ToJTokenDictionary(StringComparer.OrdinalIgnoreCase, "SpecialCustomOperations");
            List <ICustomFileGlobModel>          specialCustomSetup  = new List <ICustomFileGlobModel>();

            foreach (KeyValuePair <string, JToken> globConfigKeyValue in allSpecialOpsConfig)
            {
                string globName = globConfigKeyValue.Key;
                JToken globData = globConfigKeyValue.Value;

                CustomFileGlobModel globModel = CustomFileGlobModel.FromJObject((JObject)globData, globName);
                specialCustomSetup.Add(globModel);
            }

            config.SpecialCustomSetup = specialCustomSetup;

            // localization operations for individual files
            Dictionary <string, IReadOnlyList <IOperationProvider> > localizations = new Dictionary <string, IReadOnlyList <IOperationProvider> >();

            if (localizationModel != null && localizationModel.FileLocalizations != null)
            {
                foreach (FileLocalizationModel fileLocalization in localizationModel.FileLocalizations)
                {
                    List <IOperationProvider> localizationsForFile = new List <IOperationProvider>();
                    foreach (KeyValuePair <string, string> localizationInfo in fileLocalization.Localizations)
                    {
                        localizationsForFile.Add(new Replacement(localizationInfo.Key, localizationInfo.Value, null));
                    }

                    localizations.Add(fileLocalization.File, localizationsForFile);
                }
            }
            config.LocalizationOperations = localizations;

            return(config);
        }