summaryrefslogtreecommitdiff
path: root/config/builder.go
blob: 0f92d24d6ecfe993dff3bf716738b0f987a75066 (plain)
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
package config

import (
	"gerrit.wikimedia.org/r/blubber/build"
)

// BuilderConfig contains configuration for the definition of an arbitrary
// build command and the files required to successfully execute the command.
//
type BuilderConfig struct {
	Command      []string `json:"command"`
	Requirements []string `json:"requirements"`
}

// Merge takes another BuilderConfig and merges its fields into this one's,
// overwriting the builder command.
//
func (bc *BuilderConfig) Merge(bc2 BuilderConfig) {
	if bc2.Command != nil {
		bc.Command = bc2.Command
	}

	if bc2.Requirements != nil {
		bc.Requirements = bc2.Requirements
	}
}

// InstructionsForPhase injects instructions into the build related to
// builder commands and required files.
//
// PhasePreInstall
//
// Creates directories for requirements files, copies in requirements files,
// and runs the builder command.
//
func (bc BuilderConfig) InstructionsForPhase(phase build.Phase) []build.Instruction {
	if len(bc.Command) == 0 {
		return []build.Instruction{}
	}

	switch phase {
	case build.PhasePreInstall:
		syncs := build.SyncFiles(bc.Requirements, ".")
		run := build.Run{Command: bc.Command[0]}

		if len(bc.Command) > 1 {
			run.Arguments = bc.Command[1:]
		}

		return append(syncs, run)
	}

	return []build.Instruction{}
}