|
| 1 | +package jsonrpc |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "arduino.cc/builder" |
| 11 | + "arduino.cc/builder/types" |
| 12 | + "github.com/fsnotify/fsnotify" |
| 13 | + "github.com/osamingo/jsonrpc" |
| 14 | + "golang.org/x/net/context" |
| 15 | +) |
| 16 | + |
| 17 | +type ( |
| 18 | + BuildHandler struct { |
| 19 | + watcher *fsnotify.Watcher |
| 20 | + ctx *types.Context |
| 21 | + } |
| 22 | + BuildParams struct { |
| 23 | + HardwareFolders string |
| 24 | + ToolsFolders string |
| 25 | + BuiltInLibrariesFolders string |
| 26 | + OtherLibrariesFolders string |
| 27 | + SketchLocation string |
| 28 | + FQBN string |
| 29 | + ArduinoAPIVersion string |
| 30 | + CustomBuildProperties string |
| 31 | + Verbose bool |
| 32 | + BuildCachePath string |
| 33 | + BuildPath string |
| 34 | + WarningsLevel string |
| 35 | + } |
| 36 | + BuildResult struct { |
| 37 | + Message string `json:"message"` |
| 38 | + Error error |
| 39 | + } |
| 40 | +) |
| 41 | + |
| 42 | +type ( |
| 43 | + WatchHandler struct { |
| 44 | + watcher *fsnotify.Watcher |
| 45 | + } |
| 46 | + WatchParams struct { |
| 47 | + Path string `json:"path"` |
| 48 | + } |
| 49 | + WatchResult struct { |
| 50 | + Message string `json:"message"` |
| 51 | + } |
| 52 | +) |
| 53 | + |
| 54 | +func (h *BuildHandler) ServeJSONRPC(c context.Context, params *json.RawMessage) (interface{}, *jsonrpc.Error) { |
| 55 | + |
| 56 | + var p BuildParams |
| 57 | + if err := jsonrpc.Unmarshal(params, &p); err != nil { |
| 58 | + fmt.Println(err) |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + |
| 62 | + h.ctx.HardwareFolders = strings.Split(p.HardwareFolders, ",") |
| 63 | + h.ctx.ToolsFolders = strings.Split(p.ToolsFolders, ",") |
| 64 | + h.ctx.BuiltInLibrariesFolders = strings.Split(p.BuiltInLibrariesFolders, ",") |
| 65 | + h.ctx.OtherLibrariesFolders = strings.Split(p.OtherLibrariesFolders, ",") |
| 66 | + h.ctx.SketchLocation = p.SketchLocation |
| 67 | + h.ctx.CustomBuildProperties = strings.Split(p.CustomBuildProperties, ",") |
| 68 | + h.ctx.ArduinoAPIVersion = p.ArduinoAPIVersion |
| 69 | + h.ctx.FQBN = p.FQBN |
| 70 | + h.ctx.Verbose = p.Verbose |
| 71 | + h.ctx.BuildCachePath = p.BuildCachePath |
| 72 | + h.ctx.BuildPath = p.BuildPath |
| 73 | + h.ctx.WarningsLevel = p.WarningsLevel |
| 74 | + |
| 75 | + err := builder.RunBuilder(h.ctx) |
| 76 | + if err != nil { |
| 77 | + return BuildResult{ |
| 78 | + Message: h.ctx.GetLogger().Flush(), |
| 79 | + Error: err, |
| 80 | + }, nil |
| 81 | + } |
| 82 | + |
| 83 | + return BuildResult{ |
| 84 | + Message: h.ctx.GetLogger().Flush(), |
| 85 | + }, nil |
| 86 | +} |
| 87 | + |
| 88 | +func (h *WatchHandler) ServeJSONRPC(c context.Context, params *json.RawMessage) (interface{}, *jsonrpc.Error) { |
| 89 | + |
| 90 | + var p WatchParams |
| 91 | + if err := jsonrpc.Unmarshal(params, &p); err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + |
| 95 | + err := h.watcher.Add(p.Path) |
| 96 | + if err != nil { |
| 97 | + return nil, jsonrpc.ErrInvalidParams() |
| 98 | + } |
| 99 | + return BuildResult{ |
| 100 | + Message: "OK " + p.Path, |
| 101 | + }, nil |
| 102 | +} |
| 103 | + |
| 104 | +func startWatching(ctx *types.Context) *fsnotify.Watcher { |
| 105 | + watcher, err := fsnotify.NewWatcher() |
| 106 | + if err != nil { |
| 107 | + log.Fatal(err) |
| 108 | + } |
| 109 | + |
| 110 | + go func() { |
| 111 | + for { |
| 112 | + select { |
| 113 | + case event := <-watcher.Events: |
| 114 | + ctx.CanUseCachedTools = false |
| 115 | + log.Println("event:", event) |
| 116 | + case err := <-watcher.Errors: |
| 117 | + log.Println("error:", err) |
| 118 | + } |
| 119 | + } |
| 120 | + }() |
| 121 | + return watcher |
| 122 | +} |
| 123 | + |
| 124 | +func RegisterAndServeJsonRPC(ctx *types.Context) { |
| 125 | + |
| 126 | + watcher := startWatching(ctx) |
| 127 | + |
| 128 | + jsonrpc.RegisterMethod("Main.Build", &BuildHandler{watcher, ctx}, BuildParams{}, BuildResult{}) |
| 129 | + jsonrpc.RegisterMethod("Main.AddWatchPath", &WatchHandler{watcher}, WatchParams{}, WatchResult{}) |
| 130 | + |
| 131 | + http.HandleFunc("/jrpc", func(w http.ResponseWriter, r *http.Request) { |
| 132 | + jsonrpc.HandlerFunc(r.Context(), w, r) |
| 133 | + }) |
| 134 | + http.HandleFunc("/jrpc/debug", jsonrpc.DebugHandlerFunc) |
| 135 | + if err := http.ListenAndServe(":8888", nil); err != nil { |
| 136 | + log.Fatalln(err) |
| 137 | + } |
| 138 | +} |
0 commit comments