Skip to content

Commit f9f5550

Browse files
daneadsnh2
authored andcommitted
Add logging to VS Code output channel
* Create a VS Code output channel during init * Log basic operations and error messages
1 parent 3ad21a8 commit f9f5550

File tree

7 files changed

+92
-51
lines changed

7 files changed

+92
-51
lines changed

src/main/ext/actions.cljs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
(not))) %1)
2626
files-res)))
2727

28-
(defn show-propose-env-dialog []
28+
(defn show-propose-env-dialog [log-channel]
2929
(let [select-label (-> l/lang :label :select-env)
3030
dismiss-label (-> l/lang :label :dismiss)
3131
dialog (w/show-notification (-> l/lang :notification :env-available)
3232
[select-label dismiss-label])]
3333
(p/mapcat dialog
3434
#((cond
35-
(= select-label %1) (cmd/execute :nix-env-selector/select-env)
35+
(= select-label %1) (cmd/execute :nix-env-selector/select-env log-channel)
3636
(= dismiss-label %1) (workspace/config-set! vscode-config
3737
:workspace
3838
:nix-env-selector/suggestion
@@ -54,21 +54,23 @@
5454
(= answer support-label)
5555
(open-external-url constants/donate-url)))))))
5656

57-
(defn show-reload-dialog []
57+
(defn show-reload-dialog [log-channel]
5858
(let [reload-label (-> l/lang :label :reload)
5959
reload-message (-> l/lang :notification :env-applied)
6060
dialog (w/show-notification reload-message
6161
[reload-label])]
6262
(p/chain dialog
6363
#(when (= reload-label %1)
64-
(cmd/execute :workbench/action.reload-window)))))
64+
(cmd/execute :workbench/action.reload-window log-channel)))))
6565

66-
(defn load-env-by-path [nix-path status]
66+
(defn load-env-by-path [nix-path status log-channel]
6767
(when nix-path
68+
(w/write-log log-channel (str "Loading env in path: " nix-path))
6869
(status-bar/show {:text (-> l/lang :label :env-loading)}
6970
status)
7071
(->> (env/get-nix-env-async {:nix-config nix-path
71-
:nix-shell-path (:nix-shell-path @config)})
72+
:nix-shell-path (:nix-shell-path @config)}
73+
log-channel)
7274
(p/map (fn [env-vars]
7375
(when env-vars
7476
(env/set-current-env env-vars)
@@ -79,12 +81,14 @@
7981
:command :nix-env-selector/select-env} status))))
8082
(p/mapcat show-reload-dialog))))
8183

82-
(defn hit-nix-environment [status]
84+
(defn hit-nix-environment [status log-channel]
85+
(w/write-log log-channel "Running action: Hit environment")
8386
(fn []
8487
(-> (:nix-file @config)
85-
(load-env-by-path status))))
88+
(load-env-by-path status log-channel))))
8689

87-
(defn select-nix-environment [status]
90+
(defn select-nix-environment [status log-channel]
91+
(w/write-log log-channel "Running action: Select environment")
8892
(fn []
8993
(->> (get-nix-files (:workspace-root @config))
9094
(p/mapcat #(w/show-quick-pick {:place-holder (-> l/lang :label :select-config-placeholder)}
@@ -97,6 +101,7 @@
97101
(cond
98102
(= "disable" (:id nix-file-name))
99103
(do
104+
(w/write-log log-channel "Selected to disable Nix environment")
100105
(status-bar/hide status)
101106
(workspace/config-set! vscode-config
102107
:workspace
@@ -109,10 +114,11 @@
109114

110115
(not-empty nix-file-name)
111116
(let [nix-file (str (:workspace-root @config) "/" (:id nix-file-name))]
117+
(w/write-log log-channel (str "Selected Nix file: " nix-file))
112118
(workspace/config-set! vscode-config
113119
:workspace
114120
:nix-env-selector/nix-file
115121
(unrender-workspace nix-file (:workspace-root @config)))
116122
nix-file))))
117-
(p/mapcat #(load-env-by-path %1 status))
123+
(p/mapcat #(load-env-by-path %1 status log-channel))
118124
(p/error #(js/console.error %)))))

src/main/ext/constants.cljs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
(ns ext.constants)
22

33
(def donate-url "https://aidkit.shop")
4+
(def log-channel "Nix Env Selector")

src/main/ext/nix_env.cljs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
(:require ["child_process" :refer [exec execSync]]
33
["path" :refer [dirname]]
44
[clojure.string :as s]
5-
[promesa.core :as p]))
5+
[promesa.core :as p]
6+
[vscode.window :as w]))
67

78
(defn ^:private list-to-args [pref-arg list]
89
(s/join " " (map #(str pref-arg " " %1) list)))
@@ -38,20 +39,24 @@
3839
(catch js/Error _ nil))]))))
3940
(filter not-empty)))
4041

41-
(defn get-nix-env-sync [options]
42-
(-> (get-shell-env-cmd options)
43-
(execSync (clj->js {:cwd (dirname (:nix-config options))}))
44-
(.toString)
45-
(parse-exported-vars)))
46-
47-
(defn get-nix-env-async [options]
48-
(let [env-result (p/deferred)]
49-
(exec (get-shell-env-cmd options)
50-
(clj->js {:cwd (dirname (:nix-config options))})
51-
(fn [err result]
42+
(defn get-nix-env-sync [options log-channel]
43+
(let [cmd (get-shell-env-cmd options)]
44+
(w/write-log log-channel (str "Running command synchronously: " cmd))
45+
(-> (execSync (clj->js cmd {:cwd (dirname (:nix-config options))}))
46+
(.toString)
47+
(parse-exported-vars))))
48+
49+
(defn get-nix-env-async [options log-channel]
50+
(let [env-result (p/deferred)
51+
cmd (get-shell-env-cmd options)]
52+
(w/write-log log-channel (str "Running command asynchronously: " cmd))
53+
(exec (clj->js cmd {:cwd (dirname (:nix-config options))})
54+
(fn [err result stderr]
5255
(if (nil? err)
5356
(p/resolve! env-result result)
54-
(p/reject! env-result err))))
57+
(
58+
(w/write-log log-channel (str "Error applying environment: " stderr))
59+
(p/reject! env-result err)))))
5560
(p/map parse-exported-vars env-result)))
5661

5762
(defn set-current-env [env-vars]

src/main/main.cljs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,44 @@
44
[vscode.status-bar :as status]
55
[vscode.context :refer [subsciribe global-state]]
66
[vscode.command :as cmd]
7+
[vscode.window :as w]
78
[ext.actions :as act]
89
[ext.nix-env :as env]
910
[ext.lang :refer [lang]]
1011
[utils.helpers :refer [render-env-status]]))
1112

1213
(defn activate [ctx]
13-
(update-config!)
14-
(let [status-bar (status/create :left 100)]
15-
(if (or (not-empty (:nix-file @config)) (not-empty (:nix-packages @config)))
16-
(try
17-
(-> (env/get-nix-env-sync {:nix-config (:nix-file @config)
18-
:packages (:nix-packages @config)
19-
:args (:nix-args @config)
20-
:nix-shell-path (:nix-shell-path @config)})
21-
(env/set-current-env))
22-
(->> status-bar
23-
(status/show {:text (render-env-status lang (:nix-file @config))
24-
:command :nix-env-selector/select-env}))
25-
(act/show-donate-message (global-state ctx))
14+
(w/create-output-channel ctx)
15+
(let [log-channel (w/get-output-channel ctx)]
16+
(w/write-log log-channel "Initializing config...")
17+
(update-config!)
18+
(w/write-log log-channel (str "Loaded config: " @config))
19+
(let [status-bar (status/create :left 100)]
20+
(if (or (not-empty (:nix-file @config)) (not-empty (:nix-packages @config)))
21+
(try
22+
(-> (env/get-nix-env-sync {:nix-config (:nix-file @config)
23+
:packages (:nix-packages @config)
24+
:args (:nix-args @config)
25+
:nix-shell-path (:nix-shell-path @config)}
26+
log-channel)
27+
(env/set-current-env))
28+
(->> status-bar
29+
(status/show {:text (render-env-status lang (:nix-file @config))
30+
:command :nix-env-selector/select-env}))
31+
(act/show-donate-message (global-state ctx))
2632

27-
(catch :default e
28-
(js/console.error "Applying environment error" e)))
33+
(catch :default e
34+
(w/write-log log-channel (str "Error applying environment: " e))))
2935

30-
;; show notification that nix config available
31-
;; if workspace contains .nix file(s)
32-
(p/chain (act/get-nix-files (:workspace-root @config))
33-
#(when (and (:suggest-nix? @config)
34-
(> (count %1) 0))
35-
(act/show-propose-env-dialog))))
36+
;; show notification that nix config available
37+
;; if workspace contains .nix file(s)
38+
(p/chain (act/get-nix-files (:workspace-root @config))
39+
#(when (and (:suggest-nix? @config)
40+
(> (count %1) 0))
41+
(act/show-propose-env-dialog log-channel))))
3642

37-
;; register user commands
38-
(subsciribe ctx (cmd/create :nix-env-selector/select-env (act/select-nix-environment status-bar)))
39-
(subsciribe ctx (cmd/create :nix-env-selector/hit-env (act/hit-nix-environment status-bar)))))
43+
;; register user commands
44+
(subsciribe ctx (cmd/create :nix-env-selector/select-env (act/select-nix-environment status-bar log-channel)))
45+
(subsciribe ctx (cmd/create :nix-env-selector/hit-env (act/hit-nix-environment status-bar log-channel))))))
4046

4147
(defn deactivate [])

src/main/vscode/command.cljs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(ns vscode.command
22
(:require ["vscode" :refer [commands]]
3+
[vscode.window :as w]
34
[utils.interop :refer [clj->js' keyword-to-path]]))
45

56
(set! *warn-on-infer* false)
@@ -8,6 +9,7 @@
89
(.registerCommand commands
910
(keyword-to-path cmd-id)
1011
(clj->js' handler)))
11-
(defn execute [cmd-id]
12+
(defn execute [cmd-id log-channel]
13+
(w/write-log log-channel (str "Executing command: " cmd-id))
1214
(.executeCommand commands
1315
(keyword-to-path cmd-id)))

src/main/vscode/context.cljs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@
77

88
(defn global-state [ctx]
99
(.-globalState ctx))
10+
11+
(defn add-to-global-state [ctx key value]
12+
(let [state (global-state ctx)]
13+
(.update state key value)))
14+
15+
(defn get-from-global-state [ctx key]
16+
(let [state (global-state ctx)]
17+
(.get state key)))

src/main/vscode/window.cljs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
(ns vscode.window
22
(:require ["vscode" :refer [window]]
3+
[vscode.context :as context]
34
[promesa.core :as p]
4-
[utils.interop :refer [js->clj' clj->js']]))
5-
6-
(set! *warn-on-infer* false)
5+
[utils.interop :refer [js->clj' clj->js']]
6+
[ext.constants :as constants]))
77

88
(defn show-quick-pick [options items]
99
(let [pick-result (p/deferred)]
@@ -21,3 +21,16 @@
2121
#(p/reject! pick-result %1)))
2222
(p/chain pick-result
2323
js->clj')))
24+
25+
26+
(defn create-output-channel [ctx]
27+
(let [output-channel (.createOutputChannel window constants/log-channel)]
28+
(context/add-to-global-state ctx constants/log-channel output-channel)))
29+
30+
31+
(defn get-output-channel [ctx]
32+
(context/get-from-global-state ctx constants/log-channel))
33+
34+
35+
(defn write-log [^OutputChannel channel text]
36+
(.appendLine channel text))

0 commit comments

Comments
 (0)