Skip to content

Commit 5d07a1e

Browse files
committed
Added command line argument for specifying cli path
1 parent e69a455 commit 5d07a1e

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

Diff for: handler/builder.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
"strings"
1212
)
1313

14-
const arduinoCLI = "arduino-cli"
15-
1614
func generateCpp(inoCode []byte, name, fqbn string) (cppPath string, cppCode []byte, err error) {
1715
tempDir, err := ioutil.TempDir("", "ino2cpp-")
1816
if err != nil {
@@ -42,9 +40,10 @@ func generateCpp(inoCode []byte, name, fqbn string) (cppPath string, cppCode []b
4240
}
4341

4442
// Generate target file
45-
preprocessCmd := exec.Command(arduinoCLI, "compile", "--fqbn", fqbn, "--preprocess", inoPath)
43+
preprocessCmd := exec.Command(globalCliPath, "compile", "--fqbn", fqbn, "--preprocess", inoPath)
4644
cppCode, err = preprocessCmd.Output()
4745
if err != nil {
46+
logCommandErr(globalCliPath, cppCode, err)
4847
return
4948
}
5049

@@ -66,9 +65,10 @@ func updateCpp(inoCode []byte, fqbn, cppPath string) (cppCode []byte, err error)
6665
}
6766

6867
// Generate target file
69-
preprocessCmd := exec.Command(arduinoCLI, "compile", "--fqbn", fqbn, "--preprocess", inoPath)
68+
preprocessCmd := exec.Command(globalCliPath, "compile", "--fqbn", fqbn, "--preprocess", inoPath)
7069
cppCode, err = preprocessCmd.Output()
7170
if err != nil {
71+
logCommandErr(globalCliPath, cppCode, err)
7272
return
7373
}
7474

@@ -78,9 +78,10 @@ func updateCpp(inoCode []byte, fqbn, cppPath string) (cppCode []byte, err error)
7878
}
7979

8080
func generateCompileFlags(tempDir, inoPath, fqbn string) (string, error) {
81-
propertiesCmd := exec.Command(arduinoCLI, "compile", "--fqbn", fqbn, "--show-properties", inoPath)
81+
propertiesCmd := exec.Command(globalCliPath, "compile", "--fqbn", fqbn, "--show-properties", inoPath)
8282
output, err := propertiesCmd.Output()
8383
if err != nil {
84+
logCommandErr(globalCliPath, output, err)
8485
return "", err
8586
}
8687
properties, err := readProperties(bytes.NewReader(output))
@@ -119,3 +120,16 @@ func generateCompileFlags(tempDir, inoPath, fqbn string) (string, error) {
119120
writer.Flush()
120121
return flagsPath, nil
121122
}
123+
124+
func logCommandErr(command string, stdout []byte, err error) {
125+
log.Println("Command error:", command, err)
126+
if len(stdout) > 0 {
127+
log.Println("------------------------------BEGIN STDOUT\n", string(stdout), "\n------------------------------END STDOUT")
128+
}
129+
if exitErr, ok := err.(*exec.ExitError); ok {
130+
stderr := exitErr.Stderr
131+
if len(stderr) > 0 {
132+
log.Println("------------------------------BEGIN STDERR\n", string(stderr), "\n------------------------------END STDERR")
133+
}
134+
}
135+
}

Diff for: handler/handler.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@ import (
1414
"github.com/sourcegraph/jsonrpc2"
1515
)
1616

17+
var globalCliPath string
1718
var enableLogging bool
1819

20+
// Setup initializes global variables.
21+
func Setup(cliPath string, _enableLogging bool) {
22+
globalCliPath = cliPath
23+
enableLogging = _enableLogging
24+
}
25+
1926
// NewInoHandler creates and configures an InoHandler.
2027
func NewInoHandler(stdin io.ReadCloser, stdout io.WriteCloser, stdinLog, stdoutLog io.Writer,
21-
clangdIn io.ReadCloser, clangdOut io.WriteCloser, clangdinLog, clangdoutLog io.Writer,
22-
logging bool) *InoHandler {
23-
enableLogging = logging
28+
clangdIn io.ReadCloser, clangdOut io.WriteCloser, clangdinLog, clangdoutLog io.Writer) *InoHandler {
2429
handler := &InoHandler{
2530
data: make(map[lsp.DocumentURI]*FileData),
2631
}
@@ -128,7 +133,8 @@ func (handler *InoHandler) transformClangdParams(method string, raw *json.RawMes
128133
func (handler *InoHandler) createFileData(sourceURI lsp.DocumentURI, sourceText string) (*FileData, []byte, error) {
129134
sourcePath := uriToPath(sourceURI)
130135
// TODO get board from sketch config
131-
targetPath, targetBytes, err := generateCpp([]byte(sourceText), filepath.Base(sourcePath), "arduino:avr:uno")
136+
fqbn := "arduino:avr:uno"
137+
targetPath, targetBytes, err := generateCpp([]byte(sourceText), filepath.Base(sourcePath), fqbn)
132138
if err != nil {
133139
return nil, nil, err
134140
}
@@ -158,7 +164,8 @@ func (handler *InoHandler) updateFileData(data *FileData, change *lsp.TextDocume
158164
newSourceText = applyTextChange(data.sourceText, *rang, change.Text)
159165
}
160166
// TODO get board from sketch config
161-
targetBytes, err := updateCpp([]byte(newSourceText), "arduino:avr:uno", uriToPath(data.targetURI))
167+
fqbn := "arduino:avr:uno"
168+
targetBytes, err := updateCpp([]byte(newSourceText), fqbn, uriToPath(data.targetURI))
162169
if err != nil {
163170
return err
164171
}

Diff for: main.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ import (
1111
)
1212

1313
var clangdPath string
14+
var cliPath string
1415
var enableLogging bool
1516

1617
func main() {
17-
flag.StringVar(&clangdPath, "clangd", "clangd", "path to clangd executable")
18-
flag.BoolVar(&enableLogging, "log", false, "enable logging to files")
18+
flag.StringVar(&clangdPath, "clangd", "clangd", "Path to clangd executable")
19+
flag.StringVar(&cliPath, "cli", "arduino-cli", "Path to arduino-cli executable")
20+
flag.BoolVar(&enableLogging, "log", false, "Enable logging to files")
1921
flag.Parse()
2022

2123
var stdinLog, stdoutLog, clangdinLog, clangdoutLog, clangderrLog io.Writer
@@ -39,7 +41,8 @@ func main() {
3941
go io.Copy(clangderrLog, clangdErr)
4042
}
4143

42-
inoHandler := handler.NewInoHandler(os.Stdin, os.Stdout, stdinLog, stdoutLog, clangdIn, clangdOut, clangdinLog, clangdoutLog, enableLogging)
44+
handler.Setup(cliPath, enableLogging)
45+
inoHandler := handler.NewInoHandler(os.Stdin, os.Stdout, stdinLog, stdoutLog, clangdIn, clangdOut, clangdinLog, clangdoutLog)
4346
<-inoHandler.StdioConn.DisconnectNotify()
4447
}
4548

0 commit comments

Comments
 (0)