public CodeGeneratorResult Prepare(TemplateEngineConfig inputConfig) { this.config = inputConfig; result = new CodeGeneratorResult(); if (config == null) { return(result.SetError("No Config", "No config for SmartFormatTemplateEngine provided.")); } try { if (File.Exists(config.TemplatePath)) { Logger.Debug("Reading template file " + config.TemplatePath); using (StreamReader fileReader = new StreamReader(config.TemplatePath)) { template = fileReader.ReadToEnd(); fileReader.Close(); if (string.IsNullOrEmpty(template)) { result.SetError("Template Empty", "Template file " + config.TemplatePath + " found but it seems to be empty."); } } } else { result.SetError("Template Not Found", "Template file " + config.TemplatePath + " does not exist."); } } catch (System.Exception ex) { result.SetError("Error Loading Template", "I/O error while trying to load Template file " + config.TemplatePath + "\n" + ex.Message); } return(result); }
CodeGeneratorResult SearchTemplateDirectory (CodeGeneratorResult result) { templateDir = ""; TemplateConfig.TemplatePath = ""; string searchRoot = Path.Combine (Application.dataPath, Manager.SharedInstance.InstallDir); Logger.Debug ("Searching for default template in " + searchRoot); string[] files = Directory.GetFiles (searchRoot, config.GetDefaultTemplateFileName (), SearchOption.AllDirectories); if (files.Length == 0) { // fallback, scan all directories under Assets folder files = Directory.GetFiles (Application.dataPath, config.GetDefaultTemplateFileName (), SearchOption.AllDirectories); } if (files.Length == 0) { return result.SetError ("Template Directory Not Found", "The default template " + config.GetDefaultTemplateFileName () + "could not be found anywhere under your Assets directory."); } else if (files.Length > 1) { string rootDir = Path.Combine (Manager.SharedInstance.InstallDir, config.PathToTemplateDirectory); Logger.Debug ("More than one default template found. Searching the best match i.e. path contains [" + rootDir + "] "); foreach (string item in files) { if (item.Contains (rootDir)) { TemplateConfig.TemplatePath = item; break; } } if (string.IsNullOrEmpty (TemplateConfig.TemplatePath)) { TemplateConfig.TemplatePath = files [0]; Logger.Info ("More than one default template found but non of them matching the path " + rootDir); } } else { TemplateConfig.TemplatePath = files [0]; } templateDir = Path.GetDirectoryName (TemplateConfig.TemplatePath); Logger.Info ("Template directory found, using " + templateDir); Preferences.SetString (Preferences.Key.TemplateDir, templateDir); return result; }
public CodeGeneratorResult GetPathToTemplate (string className) { CodeGeneratorResult result = new CodeGeneratorResult (); templateDir = Preferences.GetString (Preferences.Key.TemplateDir); if (string.IsNullOrEmpty (templateDir) || !Directory.Exists (templateDir)) { result = SearchTemplateDirectory (result); if (result.NoSuccess) { return result; } } else { string classSpecificTemplate = Path.Combine (templateDir, className + ".txt"); if (File.Exists (classSpecificTemplate)) { TemplateConfig.TemplatePath = classSpecificTemplate; return result; } else { string defaultTemplate = Path.Combine (templateDir, config.GetDefaultTemplateFileName ()); if (File.Exists (defaultTemplate)) { TemplateConfig.TemplatePath = defaultTemplate; return result; } else { result = SearchTemplateDirectory (result); if (result.NoSuccess) { return result; } } } } string defaultTemplate2 = Path.Combine (templateDir, config.GetDefaultTemplateFileName ()); if (!File.Exists (defaultTemplate2)) { return result.SetError ("Default Template Not Found", "The default template file " + config.GetDefaultTemplateFileName () + " could not be found. Path: " + defaultTemplate2); } TemplateConfig.TemplatePath = defaultTemplate2; return result; }
public CodeGeneratorResult GenerateCode (FileCodeElement c) { this.fileCodeElement = c; result = new CodeGeneratorResult (); if (fileCodeElement == null) { return result.SetError ("No Class Data", "The providing classCodeElement is null. This indicates a problem during preprocessing the input source."); } Smart.Default.ErrorAction = ErrorAction.OutputErrorInResult; Smart.Default.Parser.ErrorAction = ErrorAction.ThrowError; Smart.Default.Parser.UseAlternativeEscapeChar ('\\'); code = Smart.Format(template, fileCodeElement); return result; }
public CodeGeneratorResult Prepare (TemplateEngineConfig inputConfig) { this.config = inputConfig; result = new CodeGeneratorResult (); if (config == null) { return result.SetError ("No Config", "No config for SmartFormatTemplateEngine provided."); } try { if (File.Exists (config.TemplatePath)) { Logger.Debug ("Reading template file " + config.TemplatePath); using (StreamReader fileReader = new StreamReader (config.TemplatePath)) { template = fileReader.ReadToEnd (); fileReader.Close (); if (string.IsNullOrEmpty (template)) { result.SetError ("Template Empty", "Template file " + config.TemplatePath + " found but it seems to be empty."); } } } else { result.SetError ("Template Not Found", "Template file " + config.TemplatePath + " does not exist."); } } catch (System.Exception ex) { result.SetError ("Error Loading Template", "I/O error while trying to load Template file " + config.TemplatePath + "\n" + ex.Message); } return result; }
public CodeGeneratorResult GenerateCode(FileCodeElement c) { this.fileCodeElement = c; result = new CodeGeneratorResult(); if (fileCodeElement == null) { return(result.SetError("No Class Data", "The providing classCodeElement is null. This indicates a problem during preprocessing the input source.")); } Smart.Default.ErrorAction = ErrorAction.OutputErrorInResult; Smart.Default.Parser.ErrorAction = ErrorAction.ThrowError; Smart.Default.Parser.UseAlternativeEscapeChar('\\'); code = Smart.Format(template, fileCodeElement); return(result); }