public string GenerateCode(GenerateCodeOptions opt, string templatePath)
        {
            // first load the file
            string template = File.ReadAllText(templatePath);

            return(GenerateCodeFromTemplate(opt, template));
        }
 public void GenerateTemplateGroup(GenerateCodeOptions opt, TemplateGroup group)
 {
     foreach (var template in group.Templates)
     {
         if (template.GenerateTemplate)
         {
             GenerateTemplate(opt, template.Template);
         }
     }
 }
        public static string GenerateCodeFromTemplate(GenerateCodeOptions opt, string template)
        {
            //// get all the variables
            //var variables = TemplateWorker.GetVariables(template);
            //// go through all of the mappings
            //foreach (var map in opt.Mapper.Mappings) {
            //    // see if this tag exists in the code
            //    template = ReplaceIfExists(variables, opt, map, template);
            //}

            return(template);
        }
        string ReplaceIfExistsVersioned(List <CustomVariable> variables, GenerateCodeOptions opt, CustomTagMapping mapping, string code)
        {
            // see if this variable exists in the list
            var list = variables.Where(v => v.Tag == mapping.TagName).ToList();

            foreach (var v in list)
            {
                // get the value
                string value = mapping.Result(opt, v);
                code = code.Replace(v.RawElement, value);
            }

            return(code);
        }
        static string  ReplaceIfExists(List <FileVariable> variables, GenerateCodeOptions opt, TagMapping mapping, string code)
        {
            //// see if this variable exists in the list
            //var list = variables.Where(v => v.Name == mapping.TagName).ToList();
            //if (list.Count > 0) {
            //    // get the value
            //    string value = mapping.Result(opt, code);
            //    list.ForEach(v => code = code.Replace(v.Tag, value));
            //    // replace basics
            //    code = code.Replace("[@tab]", "\t");
            //    return code;
            //}

            return(code);
        }
        public string GenerateCodeFromTemplate(GenerateCodeOptions opt, string template)
        {
            // get all the variables
            var info = GetTemplateInfo(template);

            // go through all of the versioned mappings
            foreach (var map in mapper.CustomMappings)
            {
                template = ReplaceIfExistsVersioned(info.CustomVariables, opt, map, template);
            }

            // go through all of the mappings
            foreach (var map in mapper.Mappings)
            {
                // see if this tag exists in the code
                template = ReplaceIfExists(info.Variables, opt, map, template);
            }

            return(template);
        }
        public void GenerateTemplate(GenerateCodeOptions opt, TemplateFile template)
        {
            // first create the template
            string path = Path.Combine(opt.TemplatePath, template.ProjectType, template.LanguageType, $"{template.FileName}.txt");
            string code = GenerateCode(opt, path);
            // create the path folder name
            string genPathFolder = GenerateCodeFromTemplate(opt, template.GenerationPathMask);
            // now save the file to the appropriate location
            var genPath = Path.Combine(GetProjectPath(opt.Project, template.ProjectType, template.LanguageType), genPathFolder);

            if (!Directory.Exists(genPath))
            {
                Directory.CreateDirectory(genPath);
            }

            // create the file name
            string fileName = GenerateCodeFromTemplate(opt, template.GeneratedFileMask);
            var    filePath = Path.Combine(genPath, fileName);

            File.WriteAllText(filePath, code);
        }
        public string GetEntityFields(GenerateCodeOptions opt, CustomVariable variable)
        {
            string code = "";

            foreach (var field in opt.Entity.Fields)
            {
                string template = variable.Template;
                // add tabs if necessary
                if (variable.Tabs > 0)
                {
                    template = Repeat("\t", variable.Tabs) + template;
                }

                // make sure it meets the criteria
                if (!variable.IsSearchField || field.IsSearchField)
                {
                    bool shouldInclude = true;
                    // see if this variable has variables
                    if (variable.Versions != null && variable.Versions.Count > 0)
                    {
                        // figure out this field matches a version
                        TemplateVersion fieldVersion = null;
                        shouldInclude = false;
                        foreach (var version in variable.Versions)
                        {
                            if ((string.IsNullOrEmpty(version.DataType) || field.DataType == version.DataType) &&
                                (!version.IsContainsSearch || field.IsContainsSearch))
                            {
                                fieldVersion = version;
                                break;
                            }
                        }

                        if (fieldVersion != null)
                        {
                            template      = !string.IsNullOrEmpty(template) ? template.Replace("{{version}}", fieldVersion.Template) : fieldVersion.Template;
                            shouldInclude = true;
                        }
                        else
                        {
                            // see if there is a default version
                            var defList = variable.Versions.Where(v => v.IsDefault).ToList();
                            if (defList.Count > 0)
                            {
                                // use the default version
                                template      = !string.IsNullOrEmpty(template) ? template.Replace("{{version}}", defList[0].Template) : defList[0].Template;
                                shouldInclude = true;
                            }
                        }
                    }

                    if (shouldInclude)
                    {
                        // see if we need to add a seperator
                        if (!string.IsNullOrEmpty(code) && !string.IsNullOrEmpty(variable.Seperator))
                        {
                            code += variable.Seperator;
                        }

                        var innerVars = GetVariables(template);
                        template = ReplaceIfExists(innerVars, Tags.Entity.EntityFieldName, field.FieldName, template);

                        // see if the data type should be nullable
                        string dataType = field.DataType;
                        if (variable.Nullable)
                        {
                            dataType = GetNullableDataType(field.DataType);
                        }

                        template = ReplaceIfExists(innerVars, Tags.Entity.EntityFieldDataType, dataType, template);
                        code    += template;
                    }
                }
            }

            // replace any basic values the template might have
            code = GenerateCodeFromTemplate(opt, code);
            return(code);
        }