Skip to content

Small refactorings #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/bcmi-labs/arduino-language-server
go 1.12

require (
github.com/gorilla/websocket v1.4.0 // indirect
github.com/pkg/errors v0.8.1
github.com/sourcegraph/go-lsp v0.0.0-20181119182933-0c7d621186c1
github.com/sourcegraph/jsonrpc2 v0.0.0-20190106185902-35a74f039c6a
github.com/arduino/go-properties-orderedmap v1.4.0
github.com/pkg/errors v0.9.1
github.com/sourcegraph/go-lsp v0.0.0-20200429204803-219e11d77f5d
github.com/sourcegraph/jsonrpc2 v0.0.0-20200429184054-15c2290dcb37
)
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
github.com/arduino/go-paths-helper v1.0.1 h1:utYXLM2RfFlc9qp/MJTIYp3t6ux/xM6mWjeEb/WLK4Q=
github.com/arduino/go-paths-helper v1.0.1/go.mod h1:HpxtKph+g238EJHq4geEPv9p+gl3v5YYu35Yb+w31Ck=
github.com/arduino/go-properties-orderedmap v1.4.0 h1:YEbbzPqm1gXWDM/Jaq8tlvmh09z2qeHPJTUw9/VA4Dk=
github.com/arduino/go-properties-orderedmap v1.4.0/go.mod h1:DKjD2VXY/NZmlingh4lSFMEYCVubfeArCsGPGDwb2yk=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sourcegraph/go-lsp v0.0.0-20181119182933-0c7d621186c1 h1:O1d7nVzpGmP5pGAZBSlp9TSpjNwwI0xThxhPd9oVJuU=
github.com/sourcegraph/go-lsp v0.0.0-20181119182933-0c7d621186c1/go.mod h1:tpps84QRlOVVLYk5QpKYX8Tr289D1v/UTWDLqeguiqM=
github.com/sourcegraph/go-lsp v0.0.0-20200429204803-219e11d77f5d h1:afLbh+ltiygTOB37ymZVwKlJwWZn+86syPTbrrOAydY=
github.com/sourcegraph/go-lsp v0.0.0-20200429204803-219e11d77f5d/go.mod h1:SULmZY7YNBsvNiQbrb/BEDdEJ84TGnfyUQxaHt8t8rY=
github.com/sourcegraph/jsonrpc2 v0.0.0-20190106185902-35a74f039c6a h1:jTZwOlrDhmk4Ez2vhWh7kA0eKUahp1lCO2uyM4fi/Qk=
github.com/sourcegraph/jsonrpc2 v0.0.0-20190106185902-35a74f039c6a/go.mod h1:eESpbCslcLDs8j2D7IEdGVgul7xuk9odqDTaor30IUU=
github.com/sourcegraph/jsonrpc2 v0.0.0-20200429184054-15c2290dcb37 h1:marA1XQDC7N870zmSFIoHZpIUduK80USeY0Rkuflgp4=
github.com/sourcegraph/jsonrpc2 v0.0.0-20200429184054-15c2290dcb37/go.mod h1:ZafdZgk/axhT1cvZAPOhw+95nz2I/Ra5qMlU4gTRwIo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
37 changes: 19 additions & 18 deletions handler/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"strings"

"github.com/arduino/go-properties-orderedmap"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -139,10 +140,10 @@ func generateCompileFlags(tempDir, inoPath, sourcePath, fqbn string) (string, er
propertiesCmd := exec.Command(globalCliPath, cliArgs...)
output, err := propertiesCmd.Output()
if err != nil {
err = logCommandErr(globalCliPath, output, err, errMsgFilter(tempDir))
err = logCommandErr(propertiesCmd, output, err, errMsgFilter(tempDir))
return "", err
}
properties, err := readProperties(bytes.NewReader(output))
buildProps, err := properties.LoadFromBytes(output)
if err != nil {
return "", errors.Wrap(err, "Error while reading build properties.")
}
Expand All @@ -154,7 +155,7 @@ func generateCompileFlags(tempDir, inoPath, sourcePath, fqbn string) (string, er
defer outFile.Close()

printer := Printer{Writer: bufio.NewWriter(outFile)}
printCompileFlags(properties, &printer, fqbn)
printCompileFlags(buildProps, &printer, fqbn)
printLibraryPaths(sourcePath, &printer)
printer.Flush()
return flagsPath, printer.Err
Expand All @@ -170,7 +171,7 @@ func generateTargetFile(tempDir, inoPath, cppPath, fqbn string) (cppCode []byte,
preprocessCmd := exec.Command(globalCliPath, cliArgs...)
cppCode, err = preprocessCmd.Output()
if err != nil {
err = logCommandErr(globalCliPath, cppCode, err, errMsgFilter(tempDir))
err = logCommandErr(preprocessCmd, cppCode, err, errMsgFilter(tempDir))
return
}

Expand Down Expand Up @@ -214,40 +215,40 @@ func copyIno2Cpp(inoCode string, cppPath string) (cppCode []byte, err error) {
return
}

func printCompileFlags(properties map[string]string, printer *Printer, fqbn string) {
func printCompileFlags(buildProps *properties.Map, printer *Printer, fqbn string) {
if strings.Contains(fqbn, ":avr:") {
printer.Println("--target=avr")
} else if strings.Contains(fqbn, ":sam:") {
printer.Println("--target=arm-none-eabi")
}
cppFlags := expandProperty(properties, "compiler.cpp.flags")
cppFlags := buildProps.ExpandPropsInString(buildProps.Get("compiler.cpp.flags"))
printer.Println(splitFlags(cppFlags))
mcu := expandProperty(properties, "build.mcu")
mcu := buildProps.ExpandPropsInString(buildProps.Get("build.mcu"))
if strings.Contains(fqbn, ":avr:") {
printer.Println("-mmcu=", mcu)
} else if strings.Contains(fqbn, ":sam:") {
printer.Println("-mcpu=", mcu)
}
fcpu := expandProperty(properties, "build.f_cpu")
fcpu := buildProps.ExpandPropsInString(buildProps.Get("build.f_cpu"))
printer.Println("-DF_CPU=", fcpu)
ideVersion := expandProperty(properties, "runtime.ide.version")
ideVersion := buildProps.ExpandPropsInString(buildProps.Get("runtime.ide.version"))
printer.Println("-DARDUINO=", ideVersion)
board := expandProperty(properties, "build.board")
board := buildProps.ExpandPropsInString(buildProps.Get("build.board"))
printer.Println("-DARDUINO_", board)
arch := expandProperty(properties, "build.arch")
arch := buildProps.ExpandPropsInString(buildProps.Get("build.arch"))
printer.Println("-DARDUINO_ARCH_", arch)
if strings.Contains(fqbn, ":sam:") {
libSamFlags := expandProperty(properties, "compiler.libsam.c.flags")
libSamFlags := buildProps.ExpandPropsInString(buildProps.Get("compiler.libsam.c.flags"))
printer.Println(splitFlags(libSamFlags))
}
extraFlags := expandProperty(properties, "build.extra_flags")
extraFlags := buildProps.ExpandPropsInString(buildProps.Get("build.extra_flags"))
printer.Println(splitFlags(extraFlags))
corePath := expandProperty(properties, "build.core.path")
corePath := buildProps.ExpandPropsInString(buildProps.Get("build.core.path"))
printer.Println("-I", corePath)
variantPath := expandProperty(properties, "build.variant.path")
variantPath := buildProps.ExpandPropsInString(buildProps.Get("build.variant.path"))
printer.Println("-I", variantPath)
if strings.Contains(fqbn, ":avr:") {
avrgccPath := expandProperty(properties, "runtime.tools.avr-gcc.path")
avrgccPath := buildProps.ExpandPropsInString(buildProps.Get("runtime.tools.avr-gcc.path"))
printer.Println("-I", filepath.Join(avrgccPath, "avr", "include"))
}

Expand Down Expand Up @@ -330,9 +331,9 @@ func splitFlags(flags string) string {
return string(result)
}

func logCommandErr(command string, stdout []byte, err error, filter func(string) string) error {
func logCommandErr(command *exec.Cmd, stdout []byte, err error, filter func(string) string) error {
message := ""
log.Println("Command error:", command, err)
log.Println("Command error:", command.Args, err)
if len(stdout) > 0 {
stdoutStr := string(stdout)
log.Println("------------------------------BEGIN STDOUT\n", stdoutStr, "------------------------------END STDOUT")
Expand Down
Loading