private static IReadOnlyList <IOperationProvider> SetupCustomOperations(IReadOnlyList <ICustomOperationModel> customModel, IDirectory templateRoot, IVariableCollection variables) { ITemplateEngineHost host = templateRoot.MountPoint.EnvironmentSettings.Host; List <IOperationProvider> customOperations = new List <IOperationProvider>(); foreach (ICustomOperationModel opModelUntyped in customModel) { CustomOperationModel opModel = opModelUntyped as CustomOperationModel; if (opModel == null) { host.LogMessage($"Operation type = [{opModelUntyped.Type}] could not be cast as a CustomOperationModel"); continue; } string opType = opModel.Type; string condition = opModel.Condition; if (string.IsNullOrEmpty(condition) || CppStyleEvaluatorDefinition.EvaluateFromString(templateRoot.MountPoint.EnvironmentSettings, condition, variables)) { IOperationConfig realConfigObject; if (_operationConfigLookup.TryGetValue(opType, out realConfigObject)) { customOperations.AddRange( realConfigObject.ConfigureFromJObject(opModel.Configuration, templateRoot)); } else { host.LogMessage($"Operation type = [{opType}] from configuration is unknown."); } } } return(customOperations); }
public static IReadOnlyList <ICreationPath> ListFromModel(IReadOnlyList <ICreationPathModel> modelList, IVariableCollection rootVariableCollection) { List <ICreationPath> pathList = new List <ICreationPath>(); if (rootVariableCollection == null) { rootVariableCollection = new VariableCollection(); } foreach (ICreationPathModel model in modelList) { // Note: this check is probably superfluous. The Model has evaluation info. // OTOH: this is probaby a cleaner way to do it. if (string.IsNullOrEmpty(model.Condition) || CppStyleEvaluatorDefinition.EvaluateFromString(model.Condition, rootVariableCollection)) { ICreationPath path = new CreationPath() { Path = model.PathResolved }; pathList.Add(path); } } return(pathList); }
public void EvaluateCondition(IEngineEnvironmentSettings environmentSettings, IVariableCollection variables) { if (string.IsNullOrEmpty(Condition) || CppStyleEvaluatorDefinition.EvaluateFromString(environmentSettings, Condition, variables)) { ConditionResult = true; } else { ConditionResult = false; } }
public void EvaluateCondition(IVariableCollection variables) { if (string.IsNullOrEmpty(Condition) || CppStyleEvaluatorDefinition.EvaluateFromString(Condition, variables)) { ConditionResult = true; } else { ConditionResult = false; } }
public static List <IPostAction> ListFromModel(IEngineEnvironmentSettings environmentSettings, IReadOnlyList <IPostActionModel> modelList, IVariableCollection rootVariableCollection) { List <IPostAction> actionList = new List <IPostAction>(); if (rootVariableCollection == null) { rootVariableCollection = new VariableCollection(); } foreach (IPostActionModel model in modelList) { model.EvaluateCondition(environmentSettings, rootVariableCollection); if (!model.ConditionResult) { // Condition on the post action is blank, or not true. Don't include this post action. continue; } string chosenInstruction = string.Empty; foreach (KeyValuePair <string, string> modelInstruction in model.ManualInstructionInfo) { if (string.IsNullOrEmpty(modelInstruction.Value)) { // no condition if (string.IsNullOrEmpty(chosenInstruction)) { // No condition, and no instruction previously chosen. Take this one. // We don't want a default instruction to override a conditional one. chosenInstruction = modelInstruction.Key; } } else if (CppStyleEvaluatorDefinition.EvaluateFromString(environmentSettings, modelInstruction.Value, rootVariableCollection)) { // condition is not blank and true, take this one. This results in a last-in-wins behaviour for conditions that are true. chosenInstruction = modelInstruction.Key; } } IPostAction postAction = new PostAction() { Description = model.Description, ActionId = model.ActionId, ContinueOnError = model.ContinueOnError, Args = model.Args, ManualInstructions = chosenInstruction, ConfigFile = model.ConfigFile, }; actionList.Add(postAction); } return(actionList); }
internal void Evaluate(IParameterSet parameters, IVariableCollection rootVariableCollection, IFileSystemInfo configFile) { List <FileSource> sources = new List <FileSource>(); bool stable = _simpleConfigModel.Symbols == null; Dictionary <string, bool> computed = new Dictionary <string, bool>(); while (!stable) { stable = true; foreach (KeyValuePair <string, ISymbolModel> symbol in _simpleConfigModel.Symbols) { if (symbol.Value.Type == "computed") { ComputedSymbol sym = (ComputedSymbol)symbol.Value; bool value = CppStyleEvaluatorDefinition.EvaluateFromString(sym.Value, rootVariableCollection); bool currentValue; stable &= computed.TryGetValue(symbol.Key, out currentValue) && currentValue == value; rootVariableCollection[symbol.Key] = value; computed[symbol.Key] = value; } } } foreach (ExtendedFileSource source in _simpleConfigModel.Sources) { List <string> includePattern = JTokenToCollection(source.Include, SourceFile, new[] { "**/*" }).ToList(); List <string> excludePattern = JTokenToCollection(source.Exclude, SourceFile, new[] { "/[Bb]in/", "/[Oo]bj/", ".netnew.json", "**/*.filelist" }).ToList(); List <string> copyOnlyPattern = JTokenToCollection(source.CopyOnly, SourceFile, new[] { "**/node_modules/**/*" }).ToList(); if (source.Modifiers != null) { foreach (SourceModifier modifier in source.Modifiers) { if (CppStyleEvaluatorDefinition.EvaluateFromString(modifier.Condition, rootVariableCollection)) { includePattern.AddRange(JTokenToCollection(modifier.Include, SourceFile, new string[0])); excludePattern.AddRange(JTokenToCollection(modifier.Exclude, SourceFile, new string[0])); copyOnlyPattern.AddRange(JTokenToCollection(modifier.CopyOnly, SourceFile, new string[0])); } } } Dictionary <string, string> renames = new Dictionary <string, string>(); // Console.WriteLine(_simpleConfigModel.NameParameter); object resolvedValue; if (parameters.ResolvedValues.TryGetValue(_simpleConfigModel.NameParameter, out resolvedValue)) { foreach (IFileSystemInfo entry in configFile.Parent.EnumerateFileSystemInfos("*", SearchOption.AllDirectories)) { string tmpltRel = entry.PathRelativeTo(configFile.Parent); string outRel = tmpltRel.Replace(_simpleConfigModel.SourceName, (string)resolvedValue); renames[tmpltRel] = outRel; //Console.WriteLine($"Mapping {tmpltRel} -> {outRel}"); } } sources.Add(new FileSource { CopyOnly = copyOnlyPattern.ToArray(), Exclude = excludePattern.ToArray(), Include = includePattern.ToArray(), Source = source.Source ?? "./", Target = source.Target ?? "./", Rename = renames }); } if (_simpleConfigModel.Sources.Count == 0) { IReadOnlyList <string> includePattern = new[] { "**/*" }; IReadOnlyList <string> excludePattern = new[] { "/[Bb]in/", "/[Oo]bj/", ".netnew.json", "**/*.filelist" }; IReadOnlyList <string> copyOnlyPattern = new[] { "**/node_modules/**/*" }; Dictionary <string, string> renames = new Dictionary <string, string>(); // Console.WriteLine(_simpleConfigModel.NameParameter); object resolvedValue; if (parameters.ResolvedValues.TryGetValue(_simpleConfigModel.NameParameter, out resolvedValue)) { foreach (IFileSystemInfo entry in configFile.Parent.EnumerateFileSystemInfos("*", SearchOption.AllDirectories)) { string tmpltRel = entry.PathRelativeTo(configFile.Parent); string outRel = tmpltRel.Replace(_simpleConfigModel.SourceName, (string)resolvedValue); renames[tmpltRel] = outRel; //Console.WriteLine($"Mapping {tmpltRel} -> {outRel}"); } } sources.Add(new FileSource { CopyOnly = copyOnlyPattern, Exclude = excludePattern, Include = includePattern, Source = "./", Target = "./", Rename = renames }); } _sources = sources; }
public void Evaluate(IParameterSet parameters, IVariableCollection rootVariableCollection, IFileSystemInfo configFile) { List <FileSource> sources = new List <FileSource>(); bool stable = Symbols == null; Dictionary <string, bool> computed = new Dictionary <string, bool>(); while (!stable) { stable = true; foreach (KeyValuePair <string, ISymbolModel> symbol in Symbols) { if (symbol.Value.Type == "computed") { ComputedSymbol sym = (ComputedSymbol)symbol.Value; bool value = CppStyleEvaluatorDefinition.EvaluateFromString(sym.Value, rootVariableCollection); bool currentValue; stable &= computed.TryGetValue(symbol.Key, out currentValue) && currentValue == value; rootVariableCollection[symbol.Key] = value; computed[symbol.Key] = value; } } } // evaluate the file glob (specials) conditions // the result is needed for SpecialOperationConfig foreach (ICustomFileGlobModel fileGlobModel in SpecialCustomSetup) { fileGlobModel.EvaluateCondition(rootVariableCollection); } object resolvedNameParamValue; parameters.ResolvedValues.TryGetValue(NameParameter, out resolvedNameParamValue); // evaluate the conditions and resolve the paths for the PrimaryOutputs foreach (ICreationPathModel pathModel in PrimaryOutputs) { pathModel.EvaluateCondition(rootVariableCollection); if (pathModel.ConditionResult && (resolvedNameParamValue != null)) { // this path will be included in the outputs, replace the name (same thing we do to other file paths) pathModel.PathResolved = pathModel.PathOriginal.Replace(SourceName, (string)resolvedNameParamValue); } } foreach (ExtendedFileSource source in Sources) { if (!string.IsNullOrEmpty(source.Condition) && !CppStyleEvaluatorDefinition.EvaluateFromString(source.Condition, rootVariableCollection)) { continue; } List <string> includePattern = JTokenToCollection(source.Include, SourceFile, IncludePatternDefaults).ToList(); List <string> excludePattern = JTokenToCollection(source.Exclude, SourceFile, ExcludePatternDefaults).ToList(); List <string> copyOnlyPattern = JTokenToCollection(source.CopyOnly, SourceFile, CopyOnlyPatternDefaults).ToList(); if (source.Modifiers != null) { foreach (SourceModifier modifier in source.Modifiers) { if (string.IsNullOrEmpty(modifier.Condition) || CppStyleEvaluatorDefinition.EvaluateFromString(modifier.Condition, rootVariableCollection)) { includePattern.AddRange(JTokenToCollection(modifier.Include, SourceFile, new string[0])); excludePattern.AddRange(JTokenToCollection(modifier.Exclude, SourceFile, new string[0])); copyOnlyPattern.AddRange(JTokenToCollection(modifier.CopyOnly, SourceFile, new string[0])); } } } Dictionary <string, string> renames = new Dictionary <string, string>(); if (resolvedNameParamValue != null) { foreach (IFileSystemInfo entry in configFile.Parent.EnumerateFileSystemInfos("*", SearchOption.AllDirectories)) { string tmpltRel = entry.PathRelativeTo(configFile.Parent); string outRel = tmpltRel.Replace(SourceName, (string)resolvedNameParamValue); renames[tmpltRel] = outRel; } } sources.Add(new FileSource { CopyOnly = copyOnlyPattern.ToArray(), Exclude = excludePattern.ToArray(), Include = includePattern.ToArray(), Source = source.Source ?? "./", Target = source.Target ?? "./", Rename = renames }); } if (Sources.Count == 0) { IReadOnlyList <string> includePattern = IncludePatternDefaults; IReadOnlyList <string> excludePattern = ExcludePatternDefaults; IReadOnlyList <string> copyOnlyPattern = CopyOnlyPatternDefaults; Dictionary <string, string> renames = new Dictionary <string, string>(); object resolvedValue; if (parameters.ResolvedValues.TryGetValue(NameParameter, out resolvedValue)) { foreach (IFileSystemInfo entry in configFile.Parent.EnumerateFileSystemInfos("*", SearchOption.AllDirectories)) { string tmpltRel = entry.PathRelativeTo(configFile.Parent); string outRel = tmpltRel.Replace(SourceName, (string)resolvedValue); renames[tmpltRel] = outRel; } } sources.Add(new FileSource { CopyOnly = copyOnlyPattern, Exclude = excludePattern, Include = includePattern, Source = "./", Target = "./", Rename = renames }); } _sources = sources; }