2016-02-24 06:40:07 +01:00
|
|
|
package cmds
|
|
|
|
|
2016-02-29 12:45:28 +01:00
|
|
|
import (
|
|
|
|
"sort"
|
2016-02-24 06:40:07 +01:00
|
|
|
|
2016-02-29 12:45:28 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var boilCmd = &cobra.Command{
|
|
|
|
Use: "boil",
|
|
|
|
Short: "Generates ALL templates by running every command alphabetically",
|
2016-02-29 10:39:49 +01:00
|
|
|
}
|
|
|
|
|
2016-02-29 12:45:28 +01:00
|
|
|
// boilRun executes every sqlboiler command, starting with structs.
|
|
|
|
func boilRun(cmd *cobra.Command, args []string) {
|
2016-03-02 06:37:14 +01:00
|
|
|
commandNames := buildCommandList()
|
|
|
|
|
|
|
|
// Prepend "struct" command to templateNames slice so it sits at top of sort
|
|
|
|
commandNames = append([]string{"struct"}, commandNames...)
|
|
|
|
|
|
|
|
for i := 0; i < len(cmdData.Columns); i++ {
|
|
|
|
data := tplData{
|
|
|
|
Table: cmdData.Tables[i],
|
|
|
|
Columns: cmdData.Columns[i],
|
2016-03-16 15:33:58 +01:00
|
|
|
PkgName: cmdData.PkgName,
|
2016-03-02 06:37:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var out [][]byte
|
2016-03-02 16:18:26 +01:00
|
|
|
var imps imports
|
2016-03-03 20:30:48 +01:00
|
|
|
|
|
|
|
imps.standard = sqlBoilerDefaultImports.standard
|
|
|
|
imps.thirdparty = sqlBoilerDefaultImports.thirdparty
|
2016-03-02 06:37:14 +01:00
|
|
|
// Loop through and generate every command template (excluding skipTemplates)
|
2016-03-03 20:30:48 +01:00
|
|
|
for _, command := range commandNames {
|
|
|
|
imps = combineImports(imps, sqlBoilerCustomImports[command])
|
2016-03-02 06:37:14 +01:00
|
|
|
out = append(out, generateTemplate(command, &data))
|
|
|
|
}
|
|
|
|
|
2016-03-02 16:18:26 +01:00
|
|
|
err := outHandler(cmdData.OutFolder, out, &data, &imps)
|
2016-03-02 06:37:14 +01:00
|
|
|
if err != nil {
|
|
|
|
errorQuit(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildCommandList() []string {
|
2016-02-29 10:39:49 +01:00
|
|
|
// Exclude these commands from the output
|
2016-02-29 06:49:26 +01:00
|
|
|
skipTemplates := []string{
|
2016-02-29 12:45:28 +01:00
|
|
|
"boil",
|
2016-02-24 06:40:07 +01:00
|
|
|
}
|
|
|
|
|
2016-03-02 06:37:14 +01:00
|
|
|
var commandNames []string
|
2016-02-29 10:39:49 +01:00
|
|
|
|
|
|
|
// Build a list of template names
|
2016-02-29 06:49:26 +01:00
|
|
|
for _, c := range sqlBoilerCommands {
|
|
|
|
skip := false
|
|
|
|
for _, s := range skipTemplates {
|
2016-02-29 10:39:49 +01:00
|
|
|
// Skip name if it's in the exclude list.
|
|
|
|
// Also skip "struct" so that it can be added manually at the beginning
|
|
|
|
// of the slice. Structs should always go to the top of the file.
|
|
|
|
if s == c.Name() || c.Name() == "struct" {
|
2016-02-29 06:49:26 +01:00
|
|
|
skip = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-02-24 06:40:07 +01:00
|
|
|
|
2016-02-29 10:39:49 +01:00
|
|
|
if !skip {
|
2016-03-02 06:37:14 +01:00
|
|
|
commandNames = append(commandNames, c.Name())
|
2016-02-29 06:49:26 +01:00
|
|
|
}
|
2016-02-29 10:39:49 +01:00
|
|
|
}
|
|
|
|
|
2016-02-29 12:45:28 +01:00
|
|
|
// Sort all names alphabetically
|
2016-03-02 06:37:14 +01:00
|
|
|
sort.Strings(commandNames)
|
|
|
|
return commandNames
|
2016-02-24 06:40:07 +01:00
|
|
|
}
|