summaryrefslogtreecommitdiff
path: root/build/instructions.go
diff options
context:
space:
mode:
Diffstat (limited to 'build/instructions.go')
-rw-r--r--build/instructions.go98
1 files changed, 90 insertions, 8 deletions
diff --git a/build/instructions.go b/build/instructions.go
index e807b5b..5c12796 100644
--- a/build/instructions.go
+++ b/build/instructions.go
@@ -1,14 +1,96 @@
package build
-type InstructionType int
-
-const (
- Run InstructionType = iota
- Copy
- Env
+import (
+ "fmt"
+ "sort"
+ "strconv"
+ "strings"
)
-type Instruction struct {
- Type InstructionType
+type Instruction interface {
+ Compile() []string
+}
+
+type Run struct {
+ Command string
Arguments []string
}
+
+func (run Run) Compile() []string {
+ numInnerArgs := strings.Count(run.Command, `%`) - strings.Count(run.Command, `%%`)
+ command := sprintf(run.Command, run.Arguments[0:numInnerArgs])
+
+ if len(run.Arguments) > numInnerArgs {
+ command += " " + strings.Join(quoteAll(run.Arguments[numInnerArgs:]), " ")
+ }
+
+ return []string{command}
+}
+
+type RunAll struct {
+ Runs []Run
+}
+
+func (runAll RunAll) Compile() []string {
+ commands := make([]string, len(runAll.Runs))
+
+ for i, run := range runAll.Runs {
+ commands[i] = run.Compile()[0]
+ }
+
+ return []string{strings.Join(commands, " && ")}
+}
+
+type Copy struct {
+ Sources []string
+ Destination string
+}
+
+func (copy Copy) Compile() []string {
+ return append(quoteAll(copy.Sources), quote(copy.Destination))
+}
+
+type Env struct {
+ Definitions map[string]string
+}
+
+func (env Env) Compile() []string {
+ defs := make([]string, 0, len(env.Definitions))
+ names := make([]string, 0, len(env.Definitions))
+
+ for name := range env.Definitions {
+ names = append(names, name)
+ }
+
+ sort.Strings(names)
+
+ for _, name := range names {
+ defs = append(defs, name+"="+quote(env.Definitions[name]))
+ }
+
+ return defs
+}
+
+func quote(arg string) string {
+ return strconv.Quote(arg)
+}
+
+func quoteAll(arguments []string) []string {
+ quoted := make([]string, len(arguments))
+
+ for i, arg := range arguments {
+ quoted[i] = quote(arg)
+ }
+
+ return quoted
+}
+
+func sprintf(format string, arguments []string) string {
+ args := make([]interface{}, len(arguments))
+
+ for i, v := range arguments {
+ args[i] = quote(v)
+ }
+
+ return fmt.Sprintf(format, args...)
+}