forked from arangodb/kube-arangodb
/
makefile.go
135 lines (115 loc) · 3.22 KB
/
makefile.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright (c) 2016 Pulcy.
//
// Licensed 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 project
import (
"bytes"
"fmt"
"html/template"
"io/ioutil"
"os"
"path/filepath"
log "github.com/op/go-logging"
)
const (
makefilePath = "Makefile"
initialMakefile = `PROJECT := $(shell pulsar project name)
SCRIPTDIR := $(shell pwd)
ROOTDIR := $(shell cd $(SCRIPTDIR) && pwd)
VERSION:= $(shell pulsar project version)
COMMIT := $(shell pulsar project commit)
GOBUILDDIR := $(SCRIPTDIR)/.gobuild
SRCDIR := $(SCRIPTDIR)
BINDIR := $(ROOTDIR)/bin
VENDORDIR := $(SCRIPTDIR)/vendor
ORGPATH := $(shell pulsar project organization path)
ORGDIR := $(GOBUILDDIR)/src/$(ORGPATH)
REPONAME := $(PROJECT)
REPODIR := $(ORGDIR)/$(REPONAME)
REPOPATH := $(ORGPATH)/$(REPONAME)
GOPATH := $(GOBUILDDIR)
GOVERSION := {{.GoVersion}}
ifndef GOOS
GOOS := $(shell go env GOHOSTOS)
endif
ifndef GOARCH
GOARCH := $(shell go env GOHOSTARCH)
endif
BINNAME := $(PROJECT)
BIN := $(BINNAME)
SOURCES := $(shell find $(SRCDIR) -name '*.go')
.PHONY: all build clean deps update-vendor
all: build
build: $(BIN)
clean:
rm -Rf $(BIN) $(GOBUILDDIR)
deps:
@${MAKE} -B -s $(GOBUILDDIR)
$(GOBUILDDIR):
@mkdir -p $(ORGDIR)
@mkdir -p $(VENDORDIR)
@rm -f $(REPODIR) && ln -s ../../../.. $(REPODIR)
@GOPATH=$(GOPATH) pulsar go flatten -V $(VENDORDIR)
update-vendor:
@rm -Rf $(VENDORDIR)
@pulsar go vendor -V $(VENDORDIR) \
github.com/juju/errgo \
github.com/op/go-logging
@${MAKE} -B clean
$(BIN): $(GOBUILDDIR) $(SOURCES)
@mkdir -p $(BINDIR)
docker run \
--rm \
-v $(ROOTDIR):/usr/code \
-e GOPATH=/usr/code/.gobuild \
-e GOOS=$(GOOS) \
-e GOARCH=$(GOARCH) \
-e CGO_ENABLED=0 \
-w /usr/code/ \
golang:$(GOVERSION) \
go build -a -installsuffix netgo -tags netgo -ldflags "-X main.projectVersion=$(VERSION) -X main.projectBuild=$(COMMIT)" -o /usr/code/$(BINNAME) $(REPOPATH)
`
)
func initMakefile(log *log.Logger, projectDir, projectType string) error {
if projectType != ProjectTypeGo {
return nil
}
path := filepath.Join(projectDir, makefilePath)
if info, err := os.Stat(path); os.IsNotExist(err) {
log.Infof("Creating %s", makefilePath)
options := struct {
GoVersion string
}{
GoVersion: "1.7.3-alpine",
}
t, err := template.New("makefile").Parse(initialMakefile)
if err != nil {
return maskAny(err)
}
buffer := &bytes.Buffer{}
if err := t.Execute(buffer, options); err != nil {
return maskAny(err)
}
if err := ioutil.WriteFile(path, buffer.Bytes(), 0644); err != nil {
return maskAny(err)
}
return nil
} else if err != nil {
return maskAny(err)
} else if info.IsDir() {
return maskAny(fmt.Errorf("%s must be a file", path))
} else {
log.Debugf("%s already initialized in %s", gitIgnorePath, projectDir)
return nil
}
}