private bool TryGetLangPackFromFile(IFile file, out ILocalizationModel locModel)
        {
            if (file == null)
            {
                locModel = null;
                return(false);
            }

            try
            {
                JObject srcObject = ReadJObjectFromIFile(file);
                locModel = SimpleConfigModel.LocalizationFromJObject(srcObject);
                return(true);
            }
            catch (Exception ex)
            {
                ITemplateEngineHost host = file.MountPoint.EnvironmentSettings.Host;
                host.LogMessage($"Error reading Langpack from file: {file.FullPath} | Error = {ex.ToString()}");
            }

            locModel = null;
            return(false);
        }
Beispiel #2
0
        public bool TryGetTemplateFromConfigInfo(IFileSystemInfo templateFileConfig, out ITemplate template, IFileSystemInfo localeFileConfig = null, IFile hostTemplateConfigFile = null)
        {
            IFile templateFile = templateFileConfig as IFile;

            if (templateFile == null)
            {
                template = null;
                return(false);
            }

            IFile localeFile = localeFileConfig as IFile;

            try
            {
                JObject baseSrcObject = ReadJObjectFromIFile(templateFile);
                JObject srcObject     = MergeAdditionalConfiguration(baseSrcObject, templateFileConfig);

                JObject localeSourceObject = null;
                if (localeFile != null)
                {
                    localeSourceObject = ReadJObjectFromIFile(localeFile);
                }

                SimpleConfigModel templateModel = SimpleConfigModel.FromJObject(templateFile.MountPoint.EnvironmentSettings, srcObject, localeSourceObject);
                template = new RunnableProjectTemplate(srcObject, this, templateFile, templateModel, null, hostTemplateConfigFile);
                return(true);
            }
            catch (Exception ex)
            {
                ITemplateEngineHost host = templateFileConfig.MountPoint.EnvironmentSettings.Host;
                host.LogMessage($"Error reading template from file: {templateFile.FullPath} | Error = {ex.Message}");
            }

            template = null;
            return(false);
        }
        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);
        }
 public EvaluatedSimpleConfig(SimpleConfigModel simpleConfigModel)
 {
     _simpleConfigModel = simpleConfigModel;
 }
        private bool PerformTemplateValidation(SimpleConfigModel templateModel, IFile templateFile, ITemplateEngineHost host)
        {
            //Do some basic checks...
            List <string> errorMessages   = new List <string>();
            List <string> warningMessages = new List <string>();

            if (string.IsNullOrEmpty(templateModel.Identity))
            {
                errorMessages.Add(string.Format(LocalizableStrings.Authoring_MissingValue, "identity", templateFile.FullPath));
            }

            if (string.IsNullOrEmpty(templateModel.Name))
            {
                errorMessages.Add(string.Format(LocalizableStrings.Authoring_MissingValue, "name", templateFile.FullPath));
            }

            if ((templateModel.ShortNameList?.Count ?? 0) == 0)
            {
                errorMessages.Add(string.Format(LocalizableStrings.Authoring_MissingValue, "shortName", templateFile.FullPath));
            }

            if (string.IsNullOrEmpty(templateModel.SourceName))
            {
                warningMessages.Add(string.Format(LocalizableStrings.Authoring_MissingValue, "sourceName", templateFile.FullPath));
            }

            if (string.IsNullOrEmpty(templateModel.Author))
            {
                warningMessages.Add(string.Format(LocalizableStrings.Authoring_MissingValue, "author", templateFile.FullPath));
            }

            if (string.IsNullOrEmpty(templateModel.GroupIdentity))
            {
                warningMessages.Add(string.Format(LocalizableStrings.Authoring_MissingValue, "groupIdentity", templateFile.FullPath));
            }

            if (string.IsNullOrEmpty(templateModel.GeneratorVersions))
            {
                warningMessages.Add(string.Format(LocalizableStrings.Authoring_MissingValue, "generatorVersions", templateFile.FullPath));
            }

            if (templateModel.Precedence == 0)
            {
                warningMessages.Add(string.Format(LocalizableStrings.Authoring_MissingValue, "precedence", templateFile.FullPath));
            }

            if ((templateModel.Classifications?.Count ?? 0) == 0)
            {
                warningMessages.Add(string.Format(LocalizableStrings.Authoring_MissingValue, "classifications", templateFile.FullPath));
            }

            if (templateModel.PostActionModel != null && templateModel.PostActionModel.Any(x => x.ManualInstructionInfo == null || x.ManualInstructionInfo.Count == 0))
            {
                warningMessages.Add(string.Format(LocalizableStrings.Authoring_MalformedPostActionManualInstructions, templateFile.FullPath));
            }

            if (warningMessages.Count > 0)
            {
                host.LogDiagnosticMessage(string.Format(LocalizableStrings.Authoring_TemplateMissingCommonInformation, templateFile.FullPath), "Authoring");

                foreach (string message in warningMessages)
                {
                    host.LogDiagnosticMessage("    " + message, "Authoring");
                }
            }

            if (errorMessages.Count > 0)
            {
                host.LogDiagnosticMessage(string.Format(LocalizableStrings.Authoring_TemplateNotInstalled, templateFile.FullPath), "Authoring");

                foreach (string message in errorMessages)
                {
                    host.LogDiagnosticMessage("    " + message, "Authoring");
                }

                return(false);
            }

            return(true);
        }
        public bool TryGetTemplateFromConfigInfo(IFileSystemInfo templateFileConfig, out ITemplate template, IFileSystemInfo localeFileConfig = null, IFile hostTemplateConfigFile = null, string baselineName = null)
        {
            IFile templateFile = templateFileConfig as IFile;

            if (templateFile == null)
            {
                template = null;
                return(false);
            }

            IFile localeFile         = localeFileConfig as IFile;
            ITemplateEngineHost host = templateFileConfig.MountPoint.EnvironmentSettings.Host;

            try
            {
                JObject baseSrcObject = ReadJObjectFromIFile(templateFile);
                JObject srcObject     = MergeAdditionalConfiguration(baseSrcObject, templateFileConfig);

                JObject localeSourceObject = null;
                if (localeFile != null)
                {
                    localeSourceObject = ReadJObjectFromIFile(localeFile);
                }

                ISimpleConfigModifiers configModifiers = new SimpleConfigModifiers()
                {
                    BaselineName = baselineName
                };
                SimpleConfigModel templateModel = SimpleConfigModel.FromJObject(templateFile.MountPoint.EnvironmentSettings, srcObject, configModifiers, localeSourceObject);

                if (!PerformTemplateValidation(templateModel, templateFile, host))
                {
                    template = null;
                    return(false);
                }

                if (!CheckGeneratorVersionRequiredByTemplate(templateModel.GeneratorVersions))
                {   // template isn't compatible with this generator version
                    template = null;
                    return(false);
                }

                RunnableProjectTemplate runnableProjectTemplate = new RunnableProjectTemplate(srcObject, this, templateFile, templateModel, null, hostTemplateConfigFile);
                if (!AreAllTemplatePathsValid(templateModel, runnableProjectTemplate))
                {
                    template = null;
                    return(false);
                }

                // Record the timestamp of the template file so we
                // know to reload it if it changes
                if (templateFile.MountPoint.Info.MountPointFactoryId == FileSystemMountPointFactoryId &&
                    host.FileSystem is IFileLastWriteTimeSource timeSource)
                {
                    var physicalPath = Path.Combine(templateFile.MountPoint.Info.Place, templateFile.FullPath.TrimStart('/'));
                    runnableProjectTemplate.ConfigTimestampUtc = timeSource.GetLastWriteTimeUtc(physicalPath);
                }

                template = runnableProjectTemplate;
                return(true);
            }
            catch (Exception ex)
            {
                host.LogMessage($"Error reading template from file: {templateFile.FullPath} | Error = {ex.Message}");
            }

            template = null;
            return(false);
        }
        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);
        }
        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);
        }
Beispiel #9
0
        public bool TryGetTemplateFromConfigInfo(IFileSystemInfo templateFileConfig, out ITemplate template, IFileSystemInfo localeFileConfig = null, IFile hostTemplateConfigFile = null, string baselineName = null)
        {
            IFile templateFile = templateFileConfig as IFile;

            if (templateFile == null)
            {
                template = null;
                return(false);
            }

            IFile localeFile         = localeFileConfig as IFile;
            ITemplateEngineHost host = templateFileConfig.MountPoint.EnvironmentSettings.Host;

            try
            {
                JObject baseSrcObject = ReadJObjectFromIFile(templateFile);
                JObject srcObject     = MergeAdditionalConfiguration(baseSrcObject, templateFileConfig);

                JObject localeSourceObject = null;
                if (localeFile != null)
                {
                    localeSourceObject = ReadJObjectFromIFile(localeFile);
                }

                ISimpleConfigModifiers configModifiers = new SimpleConfigModifiers()
                {
                    BaselineName = baselineName
                };
                SimpleConfigModel templateModel = SimpleConfigModel.FromJObject(templateFile.MountPoint.EnvironmentSettings, srcObject, configModifiers, localeSourceObject);

                if (!PerformTemplateValidation(templateModel, templateFile, host))
                {
                    template = null;
                    return(false);
                }

                if (!CheckGeneratorVersionRequiredByTemplate(templateModel.GeneratorVersions))
                {
                    // template isn't compatible with this generator version
                    template = null;
                    return(false);
                }

                RunnableProjectTemplate runnableProjectTemplate = new RunnableProjectTemplate(srcObject, this, templateFile, templateModel, null, hostTemplateConfigFile);
                if (!AreAllTemplatePathsValid(templateModel, runnableProjectTemplate))
                {
                    template = null;
                    return(false);
                }

                template = runnableProjectTemplate;
                return(true);
            }
            catch (Exception ex)
            {
                host.LogMessage($"Error reading template from file: {templateFile.FullPath} | Error = {ex.Message}");
            }

            template = null;
            return(false);
        }