Add templateDir variable
This commit is contained in:
		
							
								
								
									
										68
									
								
								encoder.go
									
									
									
									
									
								
							
							
						
						
									
										68
									
								
								encoder.go
									
									
									
									
									
								
							@@ -14,33 +14,36 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type GenericTemplateBasedEncoder struct {
 | 
					type GenericTemplateBasedEncoder struct {
 | 
				
			||||||
	templateDir string
 | 
						templateDir    string
 | 
				
			||||||
	service     *descriptor.ServiceDescriptorProto
 | 
						service        *descriptor.ServiceDescriptorProto
 | 
				
			||||||
	file        *descriptor.FileDescriptorProto
 | 
						file           *descriptor.FileDescriptorProto
 | 
				
			||||||
	debug       bool
 | 
						debug          bool
 | 
				
			||||||
 | 
						destinationDir string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Ast struct {
 | 
					type Ast struct {
 | 
				
			||||||
	BuildDate     time.Time                          `json:"build-date"`
 | 
						BuildDate      time.Time                          `json:"build-date"`
 | 
				
			||||||
	BuildHostname string                             `json:"build-hostname"`
 | 
						BuildHostname  string                             `json:"build-hostname"`
 | 
				
			||||||
	BuildUser     string                             `json:"build-user"`
 | 
						BuildUser      string                             `json:"build-user"`
 | 
				
			||||||
	GoPWD         string                             `json:"go-pwd,omitempty"`
 | 
						GoPWD          string                             `json:"go-pwd,omitempty"`
 | 
				
			||||||
	PWD           string                             `json:"pwd"`
 | 
						PWD            string                             `json:"pwd"`
 | 
				
			||||||
	Debug         bool                               `json:"debug"`
 | 
						Debug          bool                               `json:"debug"`
 | 
				
			||||||
	File          *descriptor.FileDescriptorProto    `json:"file"`
 | 
						DestinationDir string                             `json:"destination-dir"`
 | 
				
			||||||
	RawFilename   string                             `json:"raw-filename"`
 | 
						File           *descriptor.FileDescriptorProto    `json:"file"`
 | 
				
			||||||
	Filename      string                             `json:"filename"`
 | 
						RawFilename    string                             `json:"raw-filename"`
 | 
				
			||||||
	TemplateDir   string                             `json:"template-dir"`
 | 
						Filename       string                             `json:"filename"`
 | 
				
			||||||
	Service       *descriptor.ServiceDescriptorProto `json:"service"`
 | 
						TemplateDir    string                             `json:"template-dir"`
 | 
				
			||||||
	Environment   []string                           `json:"environment"`
 | 
						Service        *descriptor.ServiceDescriptorProto `json:"service"`
 | 
				
			||||||
 | 
						Environment    []string                           `json:"environment"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewGenericTemplateBasedEncoder(templateDir string, service *descriptor.ServiceDescriptorProto, file *descriptor.FileDescriptorProto, debug bool) (e *GenericTemplateBasedEncoder) {
 | 
					func NewGenericTemplateBasedEncoder(templateDir string, service *descriptor.ServiceDescriptorProto, file *descriptor.FileDescriptorProto, debug bool, destinationDir string) (e *GenericTemplateBasedEncoder) {
 | 
				
			||||||
	e = &GenericTemplateBasedEncoder{
 | 
						e = &GenericTemplateBasedEncoder{
 | 
				
			||||||
		service:     service,
 | 
							service:        service,
 | 
				
			||||||
		file:        file,
 | 
							file:           file,
 | 
				
			||||||
		templateDir: templateDir,
 | 
							templateDir:    templateDir,
 | 
				
			||||||
		debug:       debug,
 | 
							debug:          debug,
 | 
				
			||||||
 | 
							destinationDir: destinationDir,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if debug {
 | 
						if debug {
 | 
				
			||||||
@@ -88,17 +91,18 @@ func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, err
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	ast := Ast{
 | 
						ast := Ast{
 | 
				
			||||||
		BuildDate:     time.Now(),
 | 
							BuildDate:      time.Now(),
 | 
				
			||||||
		BuildHostname: hostname,
 | 
							BuildHostname:  hostname,
 | 
				
			||||||
		BuildUser:     os.Getenv("USER"),
 | 
							BuildUser:      os.Getenv("USER"),
 | 
				
			||||||
		PWD:           pwd,
 | 
							PWD:            pwd,
 | 
				
			||||||
		GoPWD:         goPwd,
 | 
							GoPWD:          goPwd,
 | 
				
			||||||
		File:          e.file,
 | 
							File:           e.file,
 | 
				
			||||||
		TemplateDir:   e.templateDir,
 | 
							TemplateDir:    e.templateDir,
 | 
				
			||||||
		RawFilename:   templateFilename,
 | 
							DestinationDir: e.destinationDir,
 | 
				
			||||||
		Filename:      "",
 | 
							RawFilename:    templateFilename,
 | 
				
			||||||
		Environment:   os.Environ(),
 | 
							Filename:       "",
 | 
				
			||||||
		Service:       e.service,
 | 
							Environment:    os.Environ(),
 | 
				
			||||||
 | 
							Service:        e.service,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	buffer := new(bytes.Buffer)
 | 
						buffer := new(bytes.Buffer)
 | 
				
			||||||
	tmpl, err := template.New("").Funcs(ProtoHelpersFuncMap).Parse(templateFilename)
 | 
						tmpl, err := template.New("").Funcs(ProtoHelpersFuncMap).Parse(templateFilename)
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										6
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								main.go
									
									
									
									
									
								
							@@ -30,6 +30,7 @@ func main() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	// Parse parameters
 | 
						// Parse parameters
 | 
				
			||||||
	templateDir := "./templates"
 | 
						templateDir := "./templates"
 | 
				
			||||||
 | 
						destinationDir := "."
 | 
				
			||||||
	debug := false
 | 
						debug := false
 | 
				
			||||||
	if parameter := g.Request.GetParameter(); parameter != "" {
 | 
						if parameter := g.Request.GetParameter(); parameter != "" {
 | 
				
			||||||
		for _, param := range strings.Split(parameter, ",") {
 | 
							for _, param := range strings.Split(parameter, ",") {
 | 
				
			||||||
@@ -42,6 +43,9 @@ func main() {
 | 
				
			|||||||
			case "template_dir":
 | 
								case "template_dir":
 | 
				
			||||||
				templateDir = parts[1]
 | 
									templateDir = parts[1]
 | 
				
			||||||
				break
 | 
									break
 | 
				
			||||||
 | 
								case "destination_dir":
 | 
				
			||||||
 | 
									destinationDir = parts[1]
 | 
				
			||||||
 | 
									break
 | 
				
			||||||
			case "debug":
 | 
								case "debug":
 | 
				
			||||||
				switch strings.ToLower(parts[1]) {
 | 
									switch strings.ToLower(parts[1]) {
 | 
				
			||||||
				case "true", "t":
 | 
									case "true", "t":
 | 
				
			||||||
@@ -60,7 +64,7 @@ func main() {
 | 
				
			|||||||
	// Generate the encoders
 | 
						// Generate the encoders
 | 
				
			||||||
	for _, file := range g.Request.GetProtoFile() {
 | 
						for _, file := range g.Request.GetProtoFile() {
 | 
				
			||||||
		for _, service := range file.GetService() {
 | 
							for _, service := range file.GetService() {
 | 
				
			||||||
			encoder := NewGenericTemplateBasedEncoder(templateDir, service, file, debug)
 | 
								encoder := NewGenericTemplateBasedEncoder(templateDir, service, file, debug, destinationDir)
 | 
				
			||||||
			g.Response.File = append(g.Response.File, encoder.Files()...)
 | 
								g.Response.File = append(g.Response.File, encoder.Files()...)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user