Skip to content

Commit 387a275

Browse files
committed
Added command line options to allow daemon connection
1 parent 3ba5af0 commit 387a275

File tree

3 files changed

+71
-56
lines changed

3 files changed

+71
-56
lines changed

Diff for: ls/builder.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,8 @@ func (ls *INOLanguageServer) generateBuildEnvironment(ctx context.Context, logge
159159
// Extract all build information from language server status
160160
ls.readLock(logger, false)
161161
sketchRoot := ls.sketchRoot
162-
fqbn := ls.config.Fqbn
163162
buildPath := ls.buildPath
164-
cliPath := ls.config.CliPath
165-
cliConfigPath := ls.config.CliConfigPath
163+
config := ls.config
166164
type overridesFile struct {
167165
Overrides map[string]string `json:"overrides"`
168166
}
@@ -178,19 +176,19 @@ func (ls *INOLanguageServer) generateBuildEnvironment(ctx context.Context, logge
178176
ls.readUnlock(logger)
179177

180178
var success bool
181-
if cliPath == nil {
179+
if config.CliPath == nil {
182180
// Establish a connection with the gRPC server, started with the command:
183181
// arduino-cli daemon
184-
conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure(), grpc.WithBlock())
182+
conn, err := grpc.Dial(config.CliDaemonAddress, grpc.WithInsecure(), grpc.WithBlock())
185183
if err != nil {
186184
return false, fmt.Errorf("error connecting to arduino-cli rpc server: %w", err)
187185
}
188186
defer conn.Close()
189187

190188
client := rpc.NewArduinoCoreServiceClient(conn)
191189
compileReq := &rpc.CompileRequest{
192-
Instance: &rpc.Instance{Id: 1}, // XXX
193-
Fqbn: fqbn,
190+
Instance: &rpc.Instance{Id: int32(config.CliInstanceNumber)},
191+
Fqbn: config.Fqbn,
194192
SketchPath: sketchRoot.String(),
195193
SourceOverride: data.Overrides,
196194
BuildPath: buildPath.String(),
@@ -248,15 +246,15 @@ func (ls *INOLanguageServer) generateBuildEnvironment(ctx context.Context, logge
248246
}
249247

250248
// Run arduino-cli to perform the build
251-
args := []string{cliPath.String(),
252-
"--config-file", cliConfigPath.String(),
249+
args := []string{config.CliPath.String(),
250+
"--config-file", config.CliConfigPath.String(),
253251
"compile",
254-
"--fqbn", fqbn,
252+
"--fqbn", config.Fqbn,
255253
"--only-compilation-database",
256-
//"--clean",
257254
"--source-override", overridesJSON.String(),
258255
"--build-path", buildPath.String(),
259256
"--format", "json",
257+
//"--clean",
260258
sketchRoot.String(),
261259
}
262260
cmd, err := executils.NewProcess(args...)

Diff for: ls/ls.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ type INOLanguageServer struct {
4949

5050
// Config describes the language server configuration.
5151
type Config struct {
52-
Fqbn string
53-
CliPath *paths.Path
54-
CliConfigPath *paths.Path
55-
ClangdPath *paths.Path
56-
FormatterConf *paths.Path
57-
EnableLogging bool
52+
Fqbn string
53+
CliPath *paths.Path
54+
CliConfigPath *paths.Path
55+
ClangdPath *paths.Path
56+
CliDaemonAddress string
57+
CliInstanceNumber int
58+
FormatterConf *paths.Path
59+
EnableLogging bool
5860
}
5961

6062
var yellow = color.New(color.FgHiYellow)

Diff for: main.go

+54-39
Original file line numberDiff line numberDiff line change
@@ -14,69 +14,84 @@ import (
1414
"github.com/arduino/go-paths-helper"
1515
)
1616

17-
var clangdPath string
18-
var compileCommandsDir string
19-
var cliPath string
20-
var cliConfigPath string
21-
var initialFqbn string
22-
var initialBoardName string
23-
var enableLogging bool
24-
var loggingBasePath string
25-
var formatFilePath string
26-
2717
func main() {
28-
flag.StringVar(&clangdPath, "clangd", "clangd", "Path to clangd executable")
29-
flag.StringVar(&compileCommandsDir, "compile-commands-dir", "", "Specify a path to look for compile_commands.json. If path is invalid, clangd will look in the current directory and parent paths of each source file. If not specified, the clangd process is started without the compilation database.")
30-
flag.StringVar(&cliPath, "cli", "arduino-cli", "Path to arduino-cli executable")
31-
flag.StringVar(&cliConfigPath, "cli-config", "", "Path to arduino-cli config file")
32-
flag.StringVar(&initialFqbn, "fqbn", "arduino:avr:uno", "Fully qualified board name to use initially (can be changed via JSON-RPC)")
33-
flag.StringVar(&initialBoardName, "board-name", "", "User-friendly board name to use initially (can be changed via JSON-RPC)")
34-
flag.BoolVar(&enableLogging, "log", false, "Enable logging to files")
35-
flag.StringVar(&loggingBasePath, "logpath", ".", "Location where to write logging files to when logging is enabled")
36-
flag.StringVar(&formatFilePath, "format-conf-path", "", "Path to global clang-format configuration file")
18+
clangdPath := flag.String(
19+
"clangd", "",
20+
"Path to clangd executable")
21+
cliPath := flag.String(
22+
"cli", "",
23+
"Path to arduino-cli executable")
24+
cliConfigPath := flag.String(
25+
"cli-config", "",
26+
"Path to arduino-cli config file")
27+
fqbn := flag.String(
28+
"fqbn", "",
29+
"Fully qualified board name to use initially (can be changed via JSON-RPC)")
30+
/* unused */ _ = flag.String(
31+
"board-name", "",
32+
"User-friendly board name to use initially (can be changed via JSON-RPC)")
33+
enableLogging := flag.Bool(
34+
"log", false,
35+
"Enable logging to files")
36+
loggingBasePath := flag.String(
37+
"logpath", ".",
38+
"Location where to write logging files to when logging is enabled")
39+
formatFilePath := flag.String(
40+
"format-conf-path", "",
41+
"Path to global clang-format configuration file")
42+
cliDaemonAddress := flag.String(
43+
"cli-daemon-addr", "",
44+
"TCP address and port of the Arduino CLI daemon (for example: localhost:50051)")
45+
cliDaemonInstanceNumber := flag.Int(
46+
"cli-daemon-instance", -1,
47+
"Instance number of the Arduino CLI daemon")
3748
flag.Parse()
3849

39-
if loggingBasePath != "" {
40-
streams.GlobalLogDirectory = paths.New(loggingBasePath)
41-
} else if enableLogging {
50+
if *loggingBasePath != "" {
51+
streams.GlobalLogDirectory = paths.New(*loggingBasePath)
52+
} else if *enableLogging {
4253
log.Fatalf("Please specify logpath")
4354
}
4455

45-
if enableLogging {
56+
if *enableLogging {
4657
logfile := streams.OpenLogFileAs("inols-err.log")
4758
log.SetOutput(io.MultiWriter(logfile, os.Stderr))
4859
defer streams.CatchAndLogPanic()
4960
go func() {
5061
log.Println(http.ListenAndServe("localhost:6060", nil))
5162
}()
63+
log.Println("Language server launched with arguments:")
64+
for i, arg := range os.Args {
65+
log.Printf(" arg[%d] = %s", i, arg)
66+
}
5267
} else {
5368
log.SetOutput(os.Stderr)
5469
}
5570

56-
if cliConfigPath == "" {
57-
log.Fatal("Path to ArduinoCLI config file must be set.")
71+
if *cliPath != "" {
72+
if *cliConfigPath == "" {
73+
log.Fatal("Path to ArduinoCLI config file must be set.")
74+
}
75+
} else if *cliDaemonAddress == "" || *cliDaemonInstanceNumber == -1 {
76+
log.Fatal("ArduinoCLI daemon address and instance number must be set.")
5877
}
59-
if clangdPath == "" {
78+
if *clangdPath == "" {
6079
log.Fatal("Path to Clangd must be set.")
6180
}
6281

6382
config := &ls.Config{
64-
Fqbn: initialFqbn,
65-
ClangdPath: paths.New(clangdPath),
66-
EnableLogging: enableLogging,
67-
}
68-
if cliPath != "" {
69-
config.CliPath = paths.New(cliPath)
70-
}
71-
if cliConfigPath != "" {
72-
config.CliConfigPath = paths.New(cliConfigPath)
73-
}
74-
if formatFilePath != "" {
75-
config.FormatterConf = paths.New(formatFilePath)
83+
Fqbn: *fqbn,
84+
ClangdPath: paths.New(*clangdPath),
85+
EnableLogging: *enableLogging,
86+
CliPath: paths.New(*cliPath),
87+
CliConfigPath: paths.New(*cliConfigPath),
88+
FormatterConf: paths.New(*formatFilePath),
89+
CliDaemonAddress: *cliDaemonAddress,
90+
CliInstanceNumber: *cliDaemonInstanceNumber,
7691
}
7792

7893
stdio := streams.NewReadWriteCloser(os.Stdin, os.Stdout)
79-
if enableLogging {
94+
if *enableLogging {
8095
stdio = streams.LogReadWriteCloserAs(stdio, "inols.log")
8196
}
8297

0 commit comments

Comments
 (0)