aboutsummaryrefslogtreecommitdiffstats
path: root/oni/home/config/pop-os/emacs.el
blob: 1528c51aceea55bf620b0f36a482427c72b281d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
;; -*- lexical-binding: t; -*-
(defun snowball-reformat-chanced-file ()
  "Run the current buffer through Pint."
  (interactive)
  (let ((format-file-name (concat "fmt-" (file-name-nondirectory (buffer-file-name)))))
    (unwind-protect
        (progn
          (write-region (point-min) (point-max) format-file-name)
          (shell-command
           (format "%s %s"
                   (expand-file-name "../chanced-scripts/format" (project-root (project-current)))
                   (file-relative-name format-file-name (project-root (project-current))))
           nil nil)
          (let ((temp-buffer (find-file-noselect format-file-name)))
            (unwind-protect
                (replace-buffer-contents temp-buffer)
              (kill-buffer temp-buffer))))
      (delete-file format-file-name))))

(defun tomwpunt:enable-directory-local-modes ()
  "Enable hooks and modes that are bound to specific directory."
  (cond
   ((string-suffix-p "src/social-api/" (or (and-let* ((project (project-current)))
                                             (project-root project))
                                           ""))
    (local-set-key (kbd "C-c c") #'artisan-transient)
    (when (equal major-mode 'php-mode)
      (add-hook 'before-save-hook 'snowball-reformat-chanced-file nil t)))))

(add-hook 'find-file-hook 'tomwpunt:enable-directory-local-modes)

(transient-define-prefix artisan-transient ()
  "A transient to run artisan commands."
  ["Punt"
   ["Composer"
    ("pci" "Install" artisan-punt-composer-install)]
   ["Artisan"
    ("pac" "Run Command" artisan-punt-run-command)
    ("pam" "Migrate" artisan-punt-migrate)]]
  ["Chanced"
   ["Composer"
    ("cci" "Install" artisan-chanced-composer-install)]
   ["Artisan"
    ("cac" "Run Command" artisan-chanced-run-command)
    ("cam" "Migrate" artisan-chanced-migrate)]]

  ["Commands"
   ("t" "Test" artisan-test-transient)
   ("mm" "Make Migration" artisan-make-migration-transient)])

(defun artisan-punt-composer-install ()
  (interactive)
  (let ((default-directory (expand-file-name "punt" (project-root (project-current)))))
    (async-shell-command "docker exec -it punt-backend composer install")))

(defun artisan-chanced-composer-install ()
  (interactive)
  (let ((default-directory (expand-file-name "chanced" (project-root (project-current)))))
    (async-shell-command "docker exec -it chanced-backend composer install")))

(defun artisan-punt-run-command (command)
  (interactive
   (list
    (read-string "Command: ")))
  (let ((default-directory (expand-file-name "punt" (project-root (project-current)))))
    (async-shell-command (format "docker exec -it punt-backend php artisan %s" command))))

(defun artisan-chanced-run-command (command)
  (interactive
   (list
    (read-string "Command: ")))
  (let ((default-directory (expand-file-name "chanced" (project-root (project-current)))))
    (async-shell-command (format "docker exec -it chanced-backend php artisan %s" command))))

(defun artisan-punt-migrate ()
  (interactive)
  (let ((default-directory (expand-file-name "punt" (project-root (project-current)))))
    (async-shell-command "docker exec -it punt-backend php artisan migrate")))

(defun artisan-chanced-migrate ()
  (interactive)
  (let ((default-directory (expand-file-name "chanced" (project-root (project-current)))))
    (async-shell-command "docker exec -it chanced-backend php artisan migrate")))

(transient-define-prefix artisan-test-transient ()
  "Artisan Test."

  ["Arguments & Switches"
   ("-b" "Brand" "--brand=" :choices ("chanced" "punt"))
   ("-f" "Stop on failure" "--stop-on-failure")
   ("-p" "Pick test" "--pick")
   ("-P" "Test at point" "--at-point")]

  ["Commands"
   ("t" "Test" artisan-chanced-test)])

(defun artisan-chanced-test (&optional args)
  (interactive (list (transient-args transient-current-command)))
  (let ((pick (transient-arg-value "--pick" args))
        (fail (transient-arg-value "--stop-on-failure" args))
        (brand (transient-arg-value "--brand=" args))
        (at-point (transient-arg-value "--at-point" args)))
    (artisan--test-internal pick (or brand "chanced")
                            (if (string= brand "punt")
                                "chanced"
                              "punt")
                            fail
                            at-point)))

(defvar artisan--test-last-state nil
  "The last testing commnand that was run in the test buffer.
This variable should always be buffer local and only set in test
buffers.")
(make-variable-buffer-local 'artisan--test-last-state)

(defun artisan--test-internal (pick name ignore fail test-at-point-p)
  (let* ((default-directory (expand-file-name name (project-root (project-current))))
         (buffer-name (format "*%s Tests*" (capitalize name)))
         (buffer (get-buffer-create buffer-name))
         (state (with-current-buffer buffer
                  artisan--test-last-state))
         (filter (or (map-elt state 'filter)
                     (when test-at-point-p (which-function))))
         (files (unless (map-elt state 'file)
                  (cons '("all" . "")
                        (mapcar (lambda (dir) (cons (file-name-nondirectory dir) dir))
                                (directory-files-recursively "../" (rx "Test.php") nil (lambda (subdir) (not (or (string-suffix-p "vendor" subdir)
                                                                                                                 (string-suffix-p "data" subdir)
                                                                                                                 (string-suffix-p "cdk.out" subdir)
                                                                                                                 (string-suffix-p ignore subdir)))))))))
         (file (or (and (not pick)
                        (map-elt state 'file))
                   (and test-at-point-p
                        (concat "../" (file-relative-name (buffer-file-name) (project-root (project-current)))))
                   (map-elt files (completing-read "File: " files nil t))))
         (command (with-current-buffer buffer
                    (format "../../chanced-scripts/test %s %s %s %s"
                            name
                            (if fail "--stop-on-failure" "")
                            (if filter (format "--filter=%s" filter) "")
                            file)))
         (compilation-scroll-output t))
    (cl-letf (((symbol-function 'compilation-buffer-name)
               (lambda (&rest _) buffer-name)))
      (compile command))
    (with-current-buffer buffer
      (setq artisan--test-last-state
            `((file . ,file)
              (filter . ,filter)))
      (local-set-key (kbd "g")
                     (lambda ()
                       (interactive)
                       (artisan--test-internal nil name ignore fail)))
      (local-set-key (kbd "q") #'bury-buffer)
      (local-set-key (kbd "s") (lambda () (interactive) (interrupt-process)))
      (local-set-key (kbd "M-p") (lambda ()
                                   (interactive)
                                   (forward-line -1)
                                   (search-backward "  •")))
      (local-set-key (kbd "M-n") (lambda ()
                                   (interactive)
                                   (forward-line)
                                   (search-forward "  •"))))))

(transient-define-prefix artisan-make-migration-transient ()
  "Make migration."

  ["Arguments & Switches"]

  ["Commands"
   ("m" "Make" artisan-make-migration)])

(defun artisan-make-migration (&optional name)
  (interactive "MName: ")
  (let ((default-directory (expand-file-name "chanced" (project-root (project-current)))))
    (shell-command
     (format "docker exec chanced-backend php artisan make:migration --path=../common/database/migrations %S" name))
    (find-file (car (directory-files (expand-file-name "common/database/migrations" (project-root (project-current)))
                                     t (rx "_" (literal name) ".php"))))
    (vc-register)))

(require 'dashboard)
(dashboard-setup-startup-hook)
(run-with-idle-timer 300 t (lambda () (switch-to-buffer dashboard-buffer-name)))
(run-at-time "02:00" 86400 #'dashboard-open)
;;;
(setq dashboard-navigator-buttons `(((,(propertize " " 'display `(image :type svg :file ,(expand-file-name "search.svg" oni-gui-icons-dir) :ascent center :margin (5 . 0))) "Conversations" "Open pull requests"
                                      (lambda (&rest _)
                                        (browse-url "https://github.com/pulls?q=commenter%3Atomw-punt+review%3Achanges_requested+is%3Aopen"))
                                      default "[" "]")
                                     (,(propertize " " 'display `(image :type svg :file ,(expand-file-name "search.svg" oni-gui-icons-dir) :ascent center :margin (5 . 0))) "PRs" "Open pull requests"
                                      (lambda (&rest _)
                                        (browse-url "https://github.com/pulls?q=is%3Aopen+is%3Apr+org%3Ajuked-social+draft%3Afalse+review%3Anone+-author%3Aapp%2Fdependabot+-base%3Adev+-base%3Amain+-base%3Achanced_dev"))
                                      default "[" "]"))
                                    ((,(propertize " " 'display `(image :type svg :file ,(expand-file-name "search.svg" oni-gui-icons-dir) :ascent center :margin (5 . 0))) "back-end" "Unmerged into back-end"
                                      (lambda (&rest _)
                                        (browse-url "https://github.com/juked-social/social-api/compare/main...staging"))
                                      default "[" "]")
                                     (,(propertize " " 'display `(image :type svg :file ,(expand-file-name "search.svg" oni-gui-icons-dir) :ascent center :margin (5 . 0))) "chanced" "Unmerged into chanced front-end"
                                      (lambda (&rest _)
                                        (browse-url "https://github.com/juked-social/chanced-frontend/compare/main...staging"))
                                      default "[" "]")
                                     (,(propertize " " 'display `(image :type svg :file ,(expand-file-name "search.svg" oni-gui-icons-dir) :ascent center :margin (5 . 0))) "punt" "Unmerged into punt front-end"
                                      (lambda (&rest _)
                                        (browse-url "https://github.com/juked-social/punt-frontend/compare/main...staging"))
                                      default "[" "]"))))
(setq dashboard-set-navigator t)
(add-hook 'server-after-make-frame-hook #'dashboard-open)

(setq browse-url-browser-function #'browse-url-generic)
(setq browse-url-generic-args '("run" "--branch=stable" "--arch=x86_64" "--command=launch-script.sh" "--file-forwarding" "app.zen_browser.zen"))
(setq browse-url-generic-program "/usr/bin/flatpak")

(defun oni-fixup-phpstan-filenames (errors)
  "Change the file name from each error in ERRORS to one on local disk."
  (mapcar
   (lambda (e)
     (setf (flycheck-error-filename e)
           (expand-file-name
            (replace-regexp-in-string (rx string-start "/app/") "" (flycheck-error-filename e))
            (project-root (project-current))))
     e)
   errors))

(with-eval-after-load 'flycheck-phpstan
  (advice-add 'flycheck-phpstan-parse-output :filter-return #'oni-fixup-phpstan-filenames))

(with-eval-after-load 'grep
  (add-to-list 'grep-files-aliases '("js" . "*.js *.jsx *.json *.vue")))

(with-eval-after-load 'php-mode
  (require 'flycheck-phpstan nil t))

(with-eval-after-load 'flycheck
  (flycheck-add-mode 'javascript-eslint 'web-mode))

(with-eval-after-load 'sql
  (setf (map-elt sql-connection-alist "localhost")
        '((sql-product 'postgres)
          (sql-user "chanced")
          (sql-password "secret")
          (sql-database "chanced")
          (sql-server "localhost")
          (sql-port 5433))
        (map-elt sql-connection-alist "chanced-remote")
        '((sql-product 'postgres)
          (sql-user "tom")
          (sql-database "chanced")
          (sql-server "localhost")
          (sql-port 5432))))

(require 'oni-js)