This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
/
code-gen.go
119 lines (103 loc) · 3.93 KB
/
code-gen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* Copyright (C) 2014 Pivotal Software, Inc.
All rights reserved. This program and the accompanying materials
are made available under the terms of the under the Apache License,
Version 2.0 (the "License”); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
package levo
import (
"bytes"
"errors"
"fmt"
"os"
"strings"
)
const LibraryVersion string = "1.0.0"
type TemplateData struct {
PackageName string
ProjectName string
PackagePath string
Models []Model
Features map[string]bool
}
type OutputAdapter interface {
ParseTemplate(template TemplateInfo) error
GenerateFiles(templateInfo TemplateInfo, templateData TemplateData) ([]GeneratedFile, error)
GetFilesFromOutput(buffer *bytes.Buffer, directory string) ([]GeneratedFile, error)
}
type ConfigurationAdapter interface {
ProcessConfigurationFile(configFile *os.File) (Context, error)
ProcessConfigurationString(configString string) (Context, error)
}
type SchemaAdapter interface {
ProcessSchemaFile(schemaFile string) (Schema, error)
ProcessSchemaString(schemaString string) (Schema, error)
}
func ProcessMappings(context Context) ([]GeneratedFile, error) {
if len(context.Mappings) == 0 {
return []GeneratedFile{}, errors.New("No mappings to process")
}
//Pre-parse all the templates.
//For Go templates this will compile them all into one associated
//set. This way templates can reference eachother.
for _, templateInfo := range context.Templates {
err := templateInfo.Adapter.ParseTemplate(templateInfo)
if err != nil {
return []GeneratedFile{}, err
}
}
generatedFiles := make([]GeneratedFile, 0, 0)
for _, mapping := range context.Mappings {
templateModels := make([]Model, 0, 0)
for _, model := range mapping.Models {
templateModels = append(templateModels, *model)
}
for _, templateInfo := range mapping.Templates {
newestFiles, err := processTemplate(templateInfo, context, templateModels)
if err != nil {
return []GeneratedFile{}, err
}
for _, newestFile := range newestFiles {
generatedFiles = append(generatedFiles, newestFile)
}
}
}
return generatedFiles, nil
}
func processTemplate(templateInfo *TemplateInfo, context Context, templateModels []Model) ([]GeneratedFile, error) {
if strings.HasSuffix(strings.ToLower(templateInfo.FileName), ".lt") == false {
//The template is not a Levo template (.lt)
//Treat it like a static binary file
return getBinaryFiles(templateInfo)
} else {
templateData := TemplateData{PackageName: context.PackageName, ProjectName: context.ProjectName}
templateData.PackagePath = strings.Replace(context.PackageName, ".", "/", -1)
templateData.Models = templateModels
templateData.Features = context.TemplateFeatures
return templateInfo.Adapter.GenerateFiles(*templateInfo, templateData)
}
}
func getBinaryFiles(templateInfo *TemplateInfo) ([]GeneratedFile, error) {
bodyBuffer := bytes.NewBuffer(templateInfo.Body)
filesForTemplate, err := templateInfo.Adapter.GetFilesFromOutput(bodyBuffer, templateInfo.Directory)
if err != nil {
return []GeneratedFile{}, err
}
if len(filesForTemplate) > 0 {
return filesForTemplate, nil
}
newFile := GeneratedFile{FileName: templateInfo.FileName, Directory: templateInfo.Directory, Body: templateInfo.Body}
return []GeneratedFile{newFile}, nil
}
func BeginContext() Context {
fmt.Printf("")
return Context{PackageName: "com.example", ProjectName: "ExampleProject", TemplaterVersion: LibraryVersion, GoAdapter: GoTemplateAdapter{}, TemplateFeatures: make(map[string]bool, 0)}
}
func GetJSONSchemaAdapter() JSONSchemaAdapter {
return JSONSchemaAdapter{}
}