private static IEnumerable <KeyValuePair <string, string> > GetParameters(ProjectionInput generatorContext) { var allParameters = new Dictionary <string, string>(); foreach (var parameter in generatorContext.Parameters) { if (allParameters.ContainsKey(parameter.Key)) { allParameters[parameter.Key] = parameter.Value; } else { allParameters.Add(parameter.Key, parameter.Value); } } foreach (var parameter in generatorContext.ModelContext.Parameters) { if (allParameters.ContainsKey(parameter.Key)) { allParameters[parameter.Key] = parameter.Value; } else { allParameters.Add(parameter.Key, parameter.Value); } } return(allParameters); }
public string TransformString(ProjectionContext targetContext, ProjectionInput input, string outputPathRule) { var hash = PrepareHash(input); var template = Template.Parse(outputPathRule); var output = template.Render(hash); return(output); }
private static Hash PrepareHash(ProjectionInput generatorContext) { Hash hash = null; if (generatorContext == null) { hash = new Hash(); return(hash); } var modelContainer = generatorContext.Model; if (modelContainer.Format == ModelFormat.Clr) { // on CLR models we need to configure the engine to allow the public properties // of the model objects PrepareDotLiquidEngine(modelContainer.GetType()); } Template.NamingConvention = new CSharpNamingConvention(); var model = modelContainer.GetModelInstance(); if (model is JObject modelJObject) { model = modelJObject.ToDictionary(); } if (model is IDictionary <string, object> modelDictionary) { hash = Hash.FromDictionary(modelDictionary); } if (hash == null) { hash = Hash.FromAnonymousObject(modelContainer); } foreach (var p in GetParameters(generatorContext)) { if (!hash.ContainsKey(p.Key)) { hash.Add(p.Key, p.Value); } } return(hash); }
public ModelToTextOutputFile Transform(ProjectionContext transformationContext, ProjectionInput input, TransformationRuleDeclaration tranformationRule) { var hash = transformationContext.GetStateEntry <Hash>(HashStateKey); if (hash == null) { hash = PrepareHash(input); transformationContext.AddStateEntry(HashStateKey, hash); } var templateFile = _fileProvider.GetFile(transformationContext, tranformationRule); ModelToTextOutputFile result; try { var template = Template.Parse(templateFile.Text); var output = template.Render(hash); var outputPath = string.Empty; if (string.IsNullOrWhiteSpace(tranformationRule.OutputPathTemplate)) { var templateFileName = tranformationRule.TemplateName; var languageExtension = _inferenceService.InferFileExtension(output); outputPath = Path.ChangeExtension(templateFileName, languageExtension); } else { var nameTemplate = Template.Parse(tranformationRule.OutputPathTemplate); outputPath = nameTemplate.Render(hash); } result = new ModelToTextOutputFile(output, outputPath); } catch (Exception e) { AppTrace.Error("Template processing failed:", e); throw; } return(result); }