Skip to content

Commit

Permalink
fix: joker path in service
Browse files Browse the repository at this point in the history
  • Loading branch information
yqrashawn committed Jul 12, 2022
1 parent 39e0e9f commit 446d6c7
Show file tree
Hide file tree
Showing 19 changed files with 448 additions and 459 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-and-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
run: |
touch gokuw
echo '#!/bin/sh' >> gokuw
echo 'watchexec -r -w `[[ -z $GOKU_EDN_CONFIG_FILE ]] && echo ~/.config/karabiner.edn || echo $GOKU_EDN_CONFIG_FILE` goku' >> gokuw
echo 'watchexec -r -e edn -w `[[ -z $GOKU_EDN_CONFIG_FILE ]] && echo ~/.config/karabiner.edn || echo $GOKU_EDN_CONFIG_FILE` goku' >> gokuw
chmod +x gokuw
- name: Pack
run: zip -r goku.zip goku.zip goku gokuw # https://github.com/actions/virtual-environments/issues/2619#issuecomment-778827140
Expand Down
43 changes: 25 additions & 18 deletions CHANGELOG.org
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ All notable changes to this project will be documented in this file. This change

** Unreleased

[Unreleased Commits]: https://github.com/yqrashawn/GokuRakuJoudo/compare/v0.5.3...HEAD
[Unreleased Commits]: https://github.com/yqrashawn/GokuRakuJoudo/compare/v0.5.4...HEAD
** 0.5.4 - 2022-07-12
*** Changed
- use ~/bin/zsh~ instead of ~/bin/sh~ in launch agent https://github.com/yqrashawn/homebrew-goku/blob/9a4f6cd23f51535e046a33fe260876630fc20292/Formula/goku.rb#L28
- fix joker PATH
- run through clj-kondo

[commits in 0.5.4]: https://github.com/yqrashawn/GokuRakuJoudo/compare/v0.5.3...v0.5.4

** 0.5.3 - 2022-05-16

Expand Down Expand Up @@ -61,8 +68,8 @@ All notable changes to this project will be documented in this file. This change
- support simultaneous pointing_button
#+NAME: sim key in from
#+BEGIN_SRC clojure
{:des "sim pkey in from"
:rules [[[{:pkey :button5} {:pkey :button2}] {:pkey :button1}]]}
{:des "sim pkey in from"
:rules [[[{:pkey :button5} {:pkey :button2}] {:pkey :button1}]]}
#+END_SRC

[commits in 0.3.10]: https://github.com/yqrashawn/GokuRakuJoudo/compare/v0.3.9...v0.3.10
Expand Down Expand Up @@ -108,18 +115,18 @@ All notable changes to this project will be documented in this file. This change
- ~:applications~ now supports ~file_paths~
[[https://karabiner-elements.pqrs.org/docs/json/complex-modifications-manipulator-definition/conditions/frontmost-application/][karabiner-elements documentation about file_paths]]
#+BEGIN_SRC clojure
{...
:applications
{:Chromes ["^org\\.chromium\\.Chromium$" "^com\\.google\\.Chrome\\.canary$" :paths "^/Applications/Google Chrome\\.app"]
;; same as
:Chromes [:identifiers "^org\\.chromium\\.Chromium$" "^com\\.google\\.Chrome\\.canary$" :paths "^/Applications/Google Chrome\\.app"]

;; can specify multiple :paths or :identifiers (can omit keyword :identifiers)
:Chromes [:identifiers "^org\\.chromium\\.Chromium$" "^com\\.google\\.Chrome\\.canary$"
:paths "^/Applications/Google Chrome\\.app" "^/Applications/Google Chrome Canary\\.app"]

;; paths only (can't omit keyword :paths)
:Chromes [:paths "^/Applications/.*Chrom.*"]}}
{...
:applications
{:Chromes ["^org\\.chromium\\.Chromium$" "^com\\.google\\.Chrome\\.canary$" :paths "^/Applications/Google Chrome\\.app"]
;; same as
:Chromes [:identifiers "^org\\.chromium\\.Chromium$" "^com\\.google\\.Chrome\\.canary$" :paths "^/Applications/Google Chrome\\.app"]

;; can specify multiple :paths or :identifiers (can omit keyword :identifiers)
:Chromes [:identifiers "^org\\.chromium\\.Chromium$" "^com\\.google\\.Chrome\\.canary$"
:paths "^/Applications/Google Chrome\\.app" "^/Applications/Google Chrome Canary\\.app"]

;; paths only (can't omit keyword :paths)
:Chromes [:paths "^/Applications/.*Chrom.*"]}}
#+END_SRC

[commits in 0.3.4]: https://github.com/yqrashawn/GokuRakuJoudo/compare/v0.3.3...0.3.4
Expand Down Expand Up @@ -292,9 +299,9 @@ both stderr and stdout.

;; now we can do it with predefined layer
{:layers {:wlayer {:key :w}}
:main [{:des "w layer"
:rules [[:e "open -a Emacs.app" :wlayer]
[:s "open -a Safari.app" :wlayer]]}]}
:main [{:des "w layer"
:rules [[:e "open -a Emacs.app" :wlayer]
[:s "open -a Safari.app" :wlayer]]}]}
#+end_src

- group rules by conditions
Expand Down
47 changes: 23 additions & 24 deletions src/karabiner_configurator/conditions.clj
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
(ns karabiner-configurator.conditions
(:require
[karabiner-configurator.misc :refer :all]
[karabiner-configurator.data :refer :all]))
[karabiner-configurator.data :refer [conf-data simlayers? input-sources? devices?]]
[karabiner-configurator.misc :refer [massert]]))

(def used-simlayers-config nil)
(def used-simlayers-config (atom nil))

(defn update-used-simlayers-config [config]
(def used-simlayers-config config))
(reset! used-simlayers-config config))

(defn cleanup-used-simlayers-config []
(def used-simlayers-config nil))
(reset! used-simlayers-config nil))

(defn is-simple-set-variable? [vec]
(and (= 2 (count vec)) (string? (first vec)) (number? (second vec))))
Expand All @@ -19,23 +19,23 @@
[]
(vec
(for [condi condis
:let [validate-condi
:let [_validate-condi
(massert (or (and (vector? condi) (is-simple-set-variable? condi)) (keyword? condi))
(str "invalid condition " condi ", must be a keyword or simple condition definition"))
condi!? (if (keyword? condi) (= \! (first (vec (name condi)))))
condi!? (when (keyword? condi) (= \! (first (vec (name condi)))))
condi (if condi!? (keyword (subs (name condi) 1))
condi)
result nil
condi-type (if condi!?
"frontmost_application_unless"
"frontmost_application_if")
result (if (and (keyword? condi) (nn? (condi (:applications conf-data))))
(let [this-condi (condi (:applications conf-data))
this-condi (if (= (first this-condi) :identifiers) this-condi (into [:identifiers] (condi (:applications conf-data))))
result (if (and (keyword? condi) (some? (condi (:applications @conf-data))))
(let [this-condi (condi (:applications @conf-data))
this-condi (if (= (first this-condi) :identifiers) this-condi (into [:identifiers] (condi (:applications @conf-data))))
[identifiers paths] [(into [] (rest (take-while (complement #{:paths}) this-condi))) (into [] (rest (drop-while (complement #{:paths}) this-condi)))]
[identifiers paths] [(or identifiers []) (or paths [])]
identifiers? (not (empty? identifiers))
paths? (not (empty? paths))
identifiers? (seq identifiers)
paths? (seq paths)
rst {:type condi-type}
rst (if identifiers? (assoc rst :bundle_identifiers identifiers) rst)
rst (if paths? (assoc rst :file_paths paths) rst)]
Expand All @@ -46,28 +46,27 @@
"device_if")
result (if (and (keyword? condi) (devices? condi))
{:identifiers
(vec (condi (:devices conf-data)))
(vec (condi (:devices @conf-data)))
:type condi-type}
result)
condi-type (if condi!?
"input_source_unless"
"input_source_if")
result (if (and (keyword? condi) (input-sources? condi))
{:input_sources
[(condi (:input-sources conf-data))]
[(condi (:input-sources @conf-data))]
:type condi-type}
result)
condi-type (if condi!?
"variable_unless"
"variable_if")
result (if (and (keyword? condi) (simlayers? condi))
(do
(if (and from to (not condi!?))
(do
(update-used-simlayers-config (condi (:simlayers conf-data)))
(update-used-simlayers-config (assoc-in used-simlayers-config [:from :sim]
(vec (conj (:sim (:from used-simlayers-config))
(keyword (:key_code from))))))))
(when (and from to (not condi!?))
(update-used-simlayers-config (condi (:simlayers @conf-data)))
(update-used-simlayers-config (assoc-in @used-simlayers-config [:from :sim]
(vec (conj (:sim (:from @used-simlayers-config))
(keyword (:key_code from)))))))

;; so that we can filter simlayer conditions in the
;; none sim one and concat it into the sim one
Expand All @@ -76,8 +75,8 @@
:type condi-type}
{:simlayer condi}))
result)
result (if (and (keyword? condi) (nn? (or (condi (:layers conf-data))
(condi layers))))
result (if (and (keyword? condi) (some? (or (condi (:layers @conf-data))
(condi layers))))
(with-meta {:name (name condi)
:value 1
:type condi-type}
Expand All @@ -96,6 +95,6 @@
:value 1
:type condi-type}
result)
validate-result (massert (nn? result)
(str "invalid condition keyword " condi ", can't find in any predefined conditions"))]]
_validate-result (massert (some? result)
(str "invalid condition keyword " condi ", can't find in any predefined conditions"))]]
result))))
82 changes: 39 additions & 43 deletions src/karabiner_configurator/core.clj
Original file line number Diff line number Diff line change
@@ -1,51 +1,45 @@
(ns karabiner-configurator.core
(:require
[cheshire.core :as json]
[clojure.string :as string]
[clojure.java.shell :as shell]
[karabiner-configurator.modifiers :as modifiers]
[karabiner-configurator.misc :refer :all]
[karabiner-configurator.data :refer :all]
[karabiner-configurator.layers :as layers]
[clojure.string :as string]
[clojure.tools.cli :as cli]
[environ.core :refer [env]]
[karabiner-configurator.data :as d]
[karabiner-configurator.froms :as froms]
[karabiner-configurator.tos :as tos]
[karabiner-configurator.rules :as rules]
[karabiner-configurator.layers :as layers]
[karabiner-configurator.misc :refer [load-edn load-json massert]]
[karabiner-configurator.modifiers :as modifiers]
[karabiner-configurator.profiles :as profiles]
[clojure.edn :as edn]
[me.raynes.fs :as fs]
[clojure.tools.cli :as cli]
[environ.core :refer [env]])
[karabiner-configurator.rules :as rules]
[karabiner-configurator.tos :as tos]
[me.raynes.fs :as fs])
(:gen-class))

;; helper function
(defn update-static-conf
"Helper function to update conf-data from reading rules"
[key conf]
(if (nn? conf)
(assoc-conf-data key conf)))
(when (some? conf)
(d/assoc-conf-data key conf)))

(defn check-edn-syntax
"Call joker to check syntax of karabiner.edn"
[path]
(let [;; intel mac
joker-bin1 "/usr/local/opt/joker/bin/joker"
;; arm mac
joker-bin2 "/opt/homebrew/opt/joker/bin/joker"
;; nix
joker-bin3 (str (System/getenv "HOME") "/.nix-profile/bin/joker")
;; fallback to which joker
joker-bin (cond (fs/exists? joker-bin1) joker-bin1
(fs/exists? joker-bin2) joker-bin2
(fs/exists? joker-bin3) joker-bin3
:else (-> (shell/sh "which" "joker")
:out
(string/trim-newline)
(str "/bin/joker")))]
(shell/sh joker-bin "--lint" path)))
(let [sys-env (into {} (System/getenv))]
(shell/sh "joker" "--lint" path
:env (merge
sys-env
{"PATH"
(str "/etc/profiles/per-user/" (System/getenv "USER") "/bin:" ;; nix profile
"/run/current-system/sw/bin:" ;; nix darwin multiuser
"/opt/homebrew/bin:" ;; arm homebrew
"/usr/local/bin:" ;; homebrew
(get sys-env "PATH"))}))))

(defn exit [status & [msg]]
(if msg (println msg))
(if (not (env :is-dev)) (System/exit status)))
(when msg (println msg))
(when-not (env :is-dev) (System/exit status)))

;; paths
(defn json-config-file-path
Expand Down Expand Up @@ -73,17 +67,18 @@
(defn parse-edn
"Init conf data and return new rules based on karabiner.edn (main section in edn file)"
[conf]
(init-conf-data)
(let [{:keys [applications devices keyboard-type input-sources tos froms modifiers layers simlayers raws main simlayer-threshold templates profiles]} conf]
(d/init-conf-data)
(let [{:keys [applications devices keyboard-type input-sources tos froms modifiers layers simlayers ;; raws
main simlayer-threshold templates profiles]} conf]
(if (nil? profiles)
(profiles/parse-profiles (:profiles conf-data))
(profiles/parse-profiles (:profiles d/conf-data))
(profiles/parse-profiles profiles))
(update-static-conf :applications applications)
(update-static-conf :devices devices)
(update-static-conf :keyboard-type keyboard-type)
(update-static-conf :input-sources input-sources)
(update-static-conf :templates templates)
(if (number? simlayer-threshold)
(when (number? simlayer-threshold)
(update-static-conf :simlayer-threshold simlayer-threshold))
(modifiers/parse-modifiers modifiers)
(layers/parse-layers layers)
Expand All @@ -103,10 +98,10 @@
(:profiles karabiner-config)]
{(keyword (:name json-profile))
json-profile}))]
(doseq [[profile-k profile-v] customized-profiles]
(doseq [[profile-k _] customized-profiles]
(let [profile-name-str (name profile-k)]
(massert
(nn? (profile-k user-profiles))
(some? (profile-k user-profiles))
(format
"Can't find profile named \"%s\" in karabiner.json, please create a profile named \"%s\" using the Karabiner-Elements.app."
profile-name-str
Expand Down Expand Up @@ -143,17 +138,17 @@
"Root function to parse karabiner.edn and update karabiner.json."
[path & [dry-run dry-run-all]]
(let [edn-syntax-err (:err (check-edn-syntax path))]
(if (> (count edn-syntax-err) 0)
(do (println "Syntax error in config:")
(println edn-syntax-err)
(exit 1))))
(when (> (count edn-syntax-err) 0)
(println "Syntax error in config:")
(println edn-syntax-err)
(exit 1)))
(update-to-karabiner-json (parse-edn (load-edn path)) dry-run dry-run-all))

(defn open-log-file []
(shell/sh "open" (log-file)))
;; cli stuff

(defn help-message [options-summary]
(defn help-message [_]
(->> ["GokuRakuJoudo -- karabiner configurator"
""
"goku will read config file and update `Goku` profile in karabiner.json"
Expand Down Expand Up @@ -233,8 +228,9 @@
;; main
(defn -main
[& args]
(let [{:keys [action options exit-message ok? config dry-run dry-run-all]} (validate-args args)]
(if exit-message
(let [{:keys [action ;; options
exit-message ok? config dry-run dry-run-all]} (validate-args args)]
(when exit-message
(case action
"run" (do (parse (or config (edn-config-file-path)) dry-run dry-run-all)
(exit (if ok? 0 1) exit-message))
Expand Down
Loading

0 comments on commit 446d6c7

Please sign in to comment.