Esempio n. 1
0
        /// <summary>
        /// Initialises templateEngine and builds newClass and existingClass.
        /// </summary>
        /// <returns>The classes.</returns>
        CodeGeneratorResult BuildClasses()
        {
            TemplateLookup      templateLookup = new TemplateLookup(config);
            CodeGeneratorResult result         = templateLookup.GetPathToTemplate(className);

            if (result.Success)
            {
                result = templateEngine.Prepare(templateLookup.TemplateConfig);
                if (result.NoSuccess)
                {
                    return(result);
                }
                newClass = builder.Build();
                if (newClass.IsEmpty())
                {
                    return(result.SetError("No Input", "The input seems to be invalid. Check that there are any states or parameter to process."));
                }
                Logger.Debug("New: " + newClass);
                if (!existingClassBuilder.HasType())
                {
                    Logger.Info("Generating source for " + className + " the very first time");
                }
                try {
                    existingClassBuilder.MethodBinding = BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public |
                                                         BindingFlags.InvokeMethod | BindingFlags.NonPublic;
                    existingClassBuilder.MethodInfoFilter = (MethodInfo mi) => mi.Name.StartsWith("Is") ||
                                                            mi.Name.StartsWith("Set") || mi.Name.StartsWith("Get") || mi.Name == "IdToName" ||
                                                            mi.Name == "Update" || mi.Name == "FixedUpdate";
                    existingClass = existingClassBuilder.Build();
                    // little bit dirty hack: we don't want special methods like Update to participate in the regular
                    // workflow i.e. mark as obsolete to be regenerated once with code throwing NotImplementedException.
                    // So if settings have changed, mark them as obsolete to force their removal
                    List <GenericMethodCodeElement> updateMethods = existingClass.Methods.FindAll(
                        (GenericMethodCodeElement m) => m.Name == "Update" || m.Name == "FixedUpdate");
                    updateMethods.ForEach((GenericMethodCodeElement m) => m.Obsolete = true);
                } catch (System.Exception ex) {
                    Logger.Warning(ex.Message + "\n" + ex.StackTrace);
                    result.SetError("Error", "Oops. An unexpected error occurred. Details" + ex.Message + "\n" + ex.StackTrace);
                }
            }
            return(result);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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);
        }