// Generates file from template // Returns generated file path public string ProcessTemplateToString(TemplateInfoRecord template) { TTSettingsDictionary settingsDictionary = template.SettingsDictionary; string templateFilePath = template.Path; host.Session["klasaInfo"] = klasaInfo; //host.Session["ustawienia"] = settingsDictionary; string templateContent = File.ReadAllText(templateFilePath); T4Callback cb = new T4Callback(); string result = textTemplatingService.ProcessTemplate(templateFilePath, templateContent, cb); return(result); }
public List <ImmutableSetting> GetImmutableSettings(TemplateInfoRecord templateInfo) { TemplateExecutor executor = new TemplateExecutor(); string preprocessedContent = executor.ProcessTemplateToString(templateInfo); //MessageBox.Show(preprocessedContent); StringReader reader = new StringReader(preprocessedContent); List <ImmutableSetting> imSettings = new List <ImmutableSetting>(); string line; var immutableSettingsSection = false; while ((line = reader.ReadLine()) != null) { if (line.StartsWith("!!!!")) { if (immutableSettingsSection) { break; } } else if (line.StartsWith("####")) { immutableSettingsSection = true; string[] elements = line.Substring(4, line.Length - 4).Split('='); if (elements.Length < 2) { continue; } imSettings.Add(new ImmutableSetting(elements[0].Trim(), elements[1].Trim())); } else if (line.StartsWith("<#@ ")) { if (immutableSettingsSection) { break; } } else { break; } } return(imSettings); }
public string ProcessTemplateToFile(TemplateInfoRecord template) { TTSettingsDictionary settingsDictionary = template.SettingsDictionary; string templateFilePath = template.Path; ITextTemplatingSessionHost host = textTemplatingService as ITextTemplatingSessionHost; host.Session["klasaInfo"] = klasaInfo; //host.Session["ustawienia"] = settingsDictionary; string templateContent = TTUtils.GetClearTemplate(templateFilePath); T4Callback cb = new T4Callback(); string result = textTemplatingService.ProcessTemplate(templateFilePath, templateContent, cb); result = ArrangeUsingRoslyn(result); //Format code string fileName = klasaInfo.Nazwa; ImmutableSetting fileNameSetting = template.ImmutableSettings.Find(x => x.Name.Contains("NazwaPliku")); if (fileNameSetting != null) { if (fileNameSetting.Value != null) { fileName = fileNameSetting.Value; } } string resultFileName = Path.Combine(Path.GetDirectoryName(templateFilePath), fileName + Path.GetFileNameWithoutExtension(templateFilePath)) + cb.fileExtension; // Writing the processed output to file: File.WriteAllText(resultFileName, result, cb.outputEncoding); // Append any error messages: if (cb.errorMessages.Count > 0) { File.AppendAllLines(resultFileName, cb.errorMessages); } return(resultFileName); }
internal void UpdateDictionary(TemplateInfoRecord templateInfoRecord) { if (templateInfoRecord.SettingsDictionary == null) { templateInfoRecord.SettingsDictionary = new TTSettingsDictionary(); } else { templateInfoRecord.SettingsDictionary.Clear(); } lock (templateInfoRecord.SettingsDictionary) { foreach (InteractiveSetting s in templateInfoRecord.InteractiveSettings) { templateInfoRecord.SettingsDictionary.Add( s.Name, s.DefaultValue.ToString() ); } } }
public List <InteractiveSetting> GetInteractiveSettings(TemplateInfoRecord templateInfo) { StreamReader reader = new StreamReader(templateInfo.Path); List <InteractiveSetting> interSettings = new List <InteractiveSetting>(); string line; var interactiveSettingsSection = false; while ((line = reader.ReadLine()) != null) { if (line.StartsWith("####")) { if (interactiveSettingsSection) { break; } } else if (line.StartsWith("!!!!")) { interactiveSettingsSection = true; string[] elements = line.Substring(4, line.Length - 4).Split(';'); if (elements.Length < 3) { continue; } Type type = null; string name = null; object defaultValue = null; object[] values = null; foreach (string elem in elements) { if (!elem.Contains('=')) { continue; } if (elem.Contains("Typ")) { type = GetTypeFromLine(elem); } else if (elem.Contains("Nazwa")) { name = GetNameFromLine(elem); } else if (elem.Contains("WartoscDomyslna")) { defaultValue = GetDefaultValueFromLine(elem, type); } else if (elem.Contains("Wartosci")) { values = GetValuesFromLine(elem, type); } } if (type != null && name != null) { interSettings.Add(new InteractiveSetting(type, name, defaultValue, values)); } } else if (line.StartsWith("<#@ ")) { if (interactiveSettingsSection) { break; } } else { break; } } return(interSettings); }