2016-03-28 10:17:41 +02:00
|
|
|
package cmds
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-04-03 08:01:26 +02:00
|
|
|
"errors"
|
2016-03-28 10:17:41 +02:00
|
|
|
"fmt"
|
|
|
|
"go/format"
|
|
|
|
"io"
|
|
|
|
"os"
|
2016-04-06 22:10:12 +02:00
|
|
|
"path/filepath"
|
2016-03-28 10:17:41 +02:00
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
var testHarnessStdout io.Writer = os.Stdout
|
|
|
|
var testHarnessFileOpen = func(filename string) (io.WriteCloser, error) {
|
|
|
|
file, err := os.Create(filename)
|
|
|
|
return file, err
|
|
|
|
}
|
|
|
|
|
2016-04-06 22:10:12 +02:00
|
|
|
// generateOutput builds the file output and sends it to outHandler for saving
|
|
|
|
func generateOutput(cmdData *CmdData, data *tplData) error {
|
|
|
|
if len(cmdData.Templates) == 0 {
|
2016-04-03 08:01:26 +02:00
|
|
|
return errors.New("No template files located for generation")
|
2016-03-28 10:17:41 +02:00
|
|
|
}
|
|
|
|
var out [][]byte
|
|
|
|
var imps imports
|
|
|
|
|
2016-04-06 22:10:12 +02:00
|
|
|
imps.standard = sqlBoilerImports.standard
|
|
|
|
imps.thirdparty = sqlBoilerImports.thirdparty
|
|
|
|
|
|
|
|
for _, template := range cmdData.Templates {
|
|
|
|
imps = combineTypeImports(imps, sqlBoilerTypeImports, data.Table.Columns)
|
2016-03-28 10:17:41 +02:00
|
|
|
resp, err := generateTemplate(template, data)
|
|
|
|
if err != nil {
|
2016-04-13 15:51:58 +02:00
|
|
|
return fmt.Errorf("Error generating template %s: %s", template.Name(), err)
|
2016-03-28 10:17:41 +02:00
|
|
|
}
|
|
|
|
out = append(out, resp)
|
|
|
|
}
|
|
|
|
|
2016-04-06 22:10:12 +02:00
|
|
|
fName := data.Table.Name + ".go"
|
|
|
|
err := outHandler(cmdData.OutFolder, fName, cmdData.PkgName, imps, out)
|
|
|
|
if err != nil {
|
2016-03-28 10:17:41 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-06 22:10:12 +02:00
|
|
|
// generateTestOutput builds the test file output and sends it to outHandler for saving
|
|
|
|
func generateTestOutput(cmdData *CmdData, data *tplData) error {
|
|
|
|
if len(cmdData.TestTemplates) == 0 {
|
|
|
|
return errors.New("No template files located for generation")
|
|
|
|
}
|
2016-03-28 10:17:41 +02:00
|
|
|
|
2016-04-06 22:10:12 +02:00
|
|
|
var out [][]byte
|
|
|
|
var imps imports
|
2016-03-28 10:17:41 +02:00
|
|
|
|
2016-04-06 22:10:12 +02:00
|
|
|
imps.standard = sqlBoilerTestImports.standard
|
|
|
|
imps.thirdparty = sqlBoilerTestImports.thirdparty
|
2016-03-28 10:17:41 +02:00
|
|
|
|
2016-04-06 22:10:12 +02:00
|
|
|
for _, template := range cmdData.TestTemplates {
|
|
|
|
resp, err := generateTemplate(template, data)
|
2016-03-28 10:17:41 +02:00
|
|
|
if err != nil {
|
2016-04-13 15:51:58 +02:00
|
|
|
return fmt.Errorf("Error generating test template %s: %s", template.Name(), err)
|
2016-03-28 10:17:41 +02:00
|
|
|
}
|
2016-04-06 22:10:12 +02:00
|
|
|
out = append(out, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
fName := data.Table.Name + "_test.go"
|
|
|
|
err := outHandler(cmdData.OutFolder, fName, cmdData.PkgName, imps, out)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-19 04:02:32 +02:00
|
|
|
func generateSinglesOutput(cmdData *CmdData) error {
|
|
|
|
if cmdData.SingleTemplates == nil {
|
|
|
|
return errors.New("No single templates located for generation")
|
|
|
|
}
|
|
|
|
|
2016-04-26 11:27:00 +02:00
|
|
|
tplData := &tplData{
|
|
|
|
PkgName: cmdData.PkgName,
|
|
|
|
DriverName: cmdData.DriverName,
|
|
|
|
}
|
|
|
|
|
2016-04-19 04:02:32 +02:00
|
|
|
for _, template := range cmdData.SingleTemplates {
|
|
|
|
var imps imports
|
|
|
|
|
2016-04-26 11:27:00 +02:00
|
|
|
resp, err := generateTemplate(template, tplData)
|
2016-04-19 04:02:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error generating template %s: %s", template.Name(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fName := template.Name()
|
|
|
|
ext := filepath.Ext(fName)
|
|
|
|
fName = fName[0 : len(fName)-len(ext)]
|
|
|
|
|
|
|
|
imps.standard = sqlBoilerSinglesImports[fName].standard
|
|
|
|
imps.thirdparty = sqlBoilerSinglesImports[fName].thirdparty
|
|
|
|
|
|
|
|
fName = fName + ".go"
|
|
|
|
|
|
|
|
err = outHandler(cmdData.OutFolder, fName, cmdData.PkgName, imps, [][]byte{resp})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-02 23:07:51 +02:00
|
|
|
func generateSinglesTestOutput(cmdData *CmdData) error {
|
|
|
|
if cmdData.SingleTestTemplates == nil {
|
|
|
|
return errors.New("No single test templates located for generation")
|
|
|
|
}
|
|
|
|
|
|
|
|
tplData := &tplData{
|
|
|
|
PkgName: cmdData.PkgName,
|
|
|
|
DriverName: cmdData.DriverName,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, template := range cmdData.SingleTestTemplates {
|
|
|
|
var imps imports
|
|
|
|
|
|
|
|
resp, err := generateTemplate(template, tplData)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error generating test template %s: %s", template.Name(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fName := template.Name()
|
|
|
|
ext := filepath.Ext(fName)
|
|
|
|
fName = fName[0 : len(fName)-len(ext)]
|
|
|
|
|
|
|
|
imps.standard = sqlBoilerSinglesTestImports[fName].standard
|
|
|
|
imps.thirdparty = sqlBoilerSinglesTestImports[fName].thirdparty
|
|
|
|
|
|
|
|
fName = fName + "_test.go"
|
|
|
|
|
|
|
|
err = outHandler(cmdData.OutFolder, fName, cmdData.PkgName, imps, [][]byte{resp})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-06 22:10:12 +02:00
|
|
|
func generateTestMainOutput(cmdData *CmdData) error {
|
|
|
|
if cmdData.TestMainTemplate == nil {
|
|
|
|
return errors.New("No TestMain template located for generation")
|
|
|
|
}
|
|
|
|
|
|
|
|
var out [][]byte
|
|
|
|
var imps imports
|
|
|
|
|
|
|
|
imps.standard = sqlBoilerTestMainImports[cmdData.DriverName].standard
|
|
|
|
imps.thirdparty = sqlBoilerTestMainImports[cmdData.DriverName].thirdparty
|
|
|
|
|
2016-06-02 23:07:51 +02:00
|
|
|
var tables []string
|
|
|
|
for _, v := range cmdData.Tables {
|
|
|
|
tables = append(tables, v.Name)
|
|
|
|
}
|
|
|
|
|
2016-04-26 11:27:00 +02:00
|
|
|
tplData := &tplData{
|
|
|
|
PkgName: cmdData.PkgName,
|
|
|
|
DriverName: cmdData.DriverName,
|
2016-06-02 23:07:51 +02:00
|
|
|
Tables: tables,
|
2016-04-26 11:27:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := generateTemplate(cmdData.TestMainTemplate, tplData)
|
2016-04-06 22:10:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
out = append(out, resp)
|
|
|
|
|
|
|
|
err = outHandler(cmdData.OutFolder, "main_test.go", cmdData.PkgName, imps, out)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func outHandler(outFolder string, fileName string, pkgName string, imps imports, contents [][]byte) error {
|
|
|
|
out := testHarnessStdout
|
|
|
|
|
|
|
|
path := filepath.Join(outFolder, fileName)
|
|
|
|
|
|
|
|
outFile, err := testHarnessFileOpen(path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to create output file %s: %s", path, err)
|
2016-03-28 10:17:41 +02:00
|
|
|
}
|
2016-04-06 22:10:12 +02:00
|
|
|
defer outFile.Close()
|
|
|
|
out = outFile
|
2016-03-28 10:17:41 +02:00
|
|
|
|
2016-04-06 22:10:12 +02:00
|
|
|
if _, err := fmt.Fprintf(out, "package %s\n\n", pkgName); err != nil {
|
|
|
|
return fmt.Errorf("Unable to write package name %s to file: %s", pkgName, path)
|
2016-03-28 10:17:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impStr := buildImportString(imps)
|
|
|
|
if len(impStr) > 0 {
|
|
|
|
if _, err := fmt.Fprintf(out, "%s\n", impStr); err != nil {
|
|
|
|
return fmt.Errorf("Unable to write imports to file handle: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 22:10:12 +02:00
|
|
|
for _, templateOutput := range contents {
|
2016-03-28 10:17:41 +02:00
|
|
|
if _, err := fmt.Fprintf(out, "%s\n", templateOutput); err != nil {
|
|
|
|
return fmt.Errorf("Unable to write template output to file handle: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateTemplate takes a template and returns the output of the template execution.
|
|
|
|
func generateTemplate(t *template.Template, data *tplData) ([]byte, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := t.Execute(&buf, data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := format.Source(buf.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return output, nil
|
|
|
|
}
|