forked from sirnewton01/godev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.go
177 lines (145 loc) · 4.14 KB
/
build.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2013 Chris McGee <sirnewton_01@yahoo.ca>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"bytes"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
)
type CompileError struct {
Location string
Line int64
Column int64
Msg string
}
func parseBuildOutput(cmd *exec.Cmd) (compileErrors []CompileError, err error) {
buffer, _ := cmd.CombinedOutput()
reader := bytes.NewReader(buffer)
bufReader := bufio.NewReader(reader)
workingDir, err := os.Getwd()
if err != nil {
return []CompileError{}, err
}
for {
l, _, err := bufReader.ReadLine()
if err != nil {
break
}
line := string(l)
if strings.HasPrefix(line, "#") {
// Skip comment lines
} else if strings.HasPrefix(line, "\t") && len(compileErrors) > 0 {
// Continuation of previous error message comment
prevCompileError := compileErrors[len(compileErrors)-1]
prevCompileError.Msg = prevCompileError.Msg + " " + line
compileErrors[len(compileErrors)-1] = prevCompileError
} else if strings.Contains(line, ":") {
// Compile Error
pieces := strings.Split(line, ":")
file := pieces[0]
// Windows absolute path with a drive letter
if len(file) < 2 {
file = pieces[0] + ":" + pieces[1]
pieces = pieces[1:]
}
if !filepath.IsAbs(file) {
file = filepath.Join(workingDir, file)
}
file = filepath.Clean(file)
location := ""
for _, srcDir := range srcDirs {
pkgLoc := strings.Index(file, srcDir)
if pkgLoc == 0 {
location = filepath.Join("/file", file[len(srcDir):])
}
}
// Check the GOROOT for this error
if location == "" {
pkgLoc := strings.Index(file, goroot)
if pkgLoc == 0 {
location = filepath.Join("/file/GOROOT", file[len(goroot):])
}
}
l := pieces[1]
lineNum, err := strconv.ParseInt(l, 10, 64)
if err != nil {
continue
}
pieces = pieces[2:]
columnNum, err := strconv.ParseInt(pieces[0], 10, 64)
if err != nil {
columnNum = 0
} else {
pieces = pieces[1:]
}
msg := strings.Join(pieces, ":")
location = filepath.ToSlash(location)
error := CompileError{Location: location, Line: lineNum,
Column: columnNum, Msg: msg}
compileErrors = append(compileErrors, error)
}
}
return compileErrors, nil
}
func buildHandler(writer http.ResponseWriter, req *http.Request, path string, pathSegs []string) bool {
switch {
case req.Method == "GET":
qValues := req.URL.Query()
pkg := qValues.Get("pkg")
install := qValues.Get("install")
race := qValues.Get("race")
tmpFile, err := ioutil.TempFile("", "godev-build-temp")
if err != nil {
ShowError(writer, 500, "Unable to create temporary file for build", err)
return true
}
// Compile the regular parts of the package
tmpFileName := tmpFile.Name()
cmd := exec.Command("go", "build", "-o", tmpFileName, pkg)
compileErrors, err := parseBuildOutput(cmd)
os.Remove(tmpFileName)
if err != nil {
ShowError(writer, 500, "Error parsing build output", err)
return true
}
// Compile the tests too
// Do this in a temporary directory to avoid collisions.
// Too bad "go build" doesn't have a "-t" parameters to include the tests.
// Too bad that "go test -c" doesn't handle collisions, while "go test" does.
os.Mkdir(tmpFileName, os.ModeDir|0700)
cmd = exec.Command("go", "test", "-c", pkg)
cmd.Dir = tmpFileName
testCompileErrors, err := parseBuildOutput(cmd)
for _, newError := range testCompileErrors {
if strings.HasSuffix(newError.Location, "_test.go") {
compileErrors = append(compileErrors, newError)
}
}
os.RemoveAll(tmpFileName)
if err != nil {
ShowError(writer, 500, "Error parsing build output", err)
return true
}
if install == "true" && len(compileErrors) == 0 {
cmd := exec.Command("go", "install", pkg)
if race == "true" {
cmd = exec.Command("go", "install", "-race", pkg)
}
err = cmd.Run()
if err != nil {
ShowError(writer, 500, "Error installing package", err)
return true
}
}
ShowJson(writer, 200, compileErrors)
return true
}
return false
}