Commit bcde528229aa

Vincent Demeester <vincent@sbr.pm>
2020-09-11 17:02:11
tools/emacs: additional projectile project types
… starting by ko Signed-off-by: Vincent Demeester <vincent@sbr.pm>
1 parent 0245beb
Changed files (1)
tools
tools/emacs/config/config-projects.el
@@ -52,7 +52,93 @@
   (def-projectile-commander-method ?c
     "Run `compile' in the project"
     (projectile-compile-project nil))
+  (defun projectile-ko-project-p ()
+    "Check if a project contains a .ko.yaml file."
+    (projectile-verify-file ".ko.yaml"))
+  (defun projectile-ko-with-config-project-p ()
+    "Check if a project is a ko project and has a config/ folder full of yaml"
+    (and (projectile-ko-project-p)
+         (projectile-verify-file-wildcard "config/*.yaml")))
+  (defun projectile-ko-configure-command ()
+    "define a configure command for a ko project, depending on the opened file"
+    (cond
+     ((projectile-file-exists-p "hack/update-codegen.sh") "./hack/update-codegen.sh")))
+  (defun projectile-ko-compile-command ()
+    "define a compile command for a ko project, depending on the openend file "
+    (cond
+     ((eq major-mode 'go-mode) (projectile-ko-compile-command-go))
+     ((eq major-mode 'yaml-mode) "yamllint .")
+     (t "go build -v ./...")
+     ))
   
+  (defun projectile-ko-compile-command-go ()
+    "compile command for a ko project if in a go file"
+    (let* ((current-file (buffer-file-name (current-buffer)))
+           (relative-current-file (file-relative-name current-file (projectile-project-root)))
+           (relative-current-folder (file-name-directory relative-current-file)))
+      (message relative-current-file)
+      (cond
+       ((string-suffix-p "_test.go" relative-current-file) (format "go test -c -v ./%s" relative-current-folder))
+       (t (format "go build -v ./%s" relative-current-folder)))))
+  (defun projectile-ko-test-command ()
+    "define a test command for a ko project, depending on the openend file"
+    (cond
+     ((eq major-mode 'go-mode) (projectile-ko-test-command-go))
+     (t "go test -v ./...")))
+  
+  (defun projectile-ko-test-command-go ()
+    "test command for a ko project if in a go file"
+    (let* ((current-file (buffer-file-name (current-buffer)))
+           (relative-current-file (file-relative-name current-file (projectile-project-root)))
+           (relative-current-folder (file-name-directory relative-current-file)))
+      (cond
+       ((string-suffix-p "_test.go" relative-current-file) (projectile-ko-command-go-test relative-current-file))
+       (t (format "go test -v ./%s" relative-current-folder)))))
+  
+  (defun projectile-ko-command-go-test (current-file)
+    "get the command for a go test"
+    (cond
+     ((gotest-module-available-p) (projectile-ko-command-go-test-gotest current-file))
+     (t (format "go test -v ./%s" current-file))))
+  
+  (defun projectile-ko-command-go-test-gotest (current-file)
+    "get the command for a go test with gotest module enabled"
+    (message default-directory)
+    (let ((data (go-test--get-current-file-testing-data)))
+      (format "go test -run='%s' -v ./%s" data (file-name-directory current-file))))
+  
+  (defun gotest-module-available-p ()
+    "is go-test module available"
+    (fboundp 'go-test--get-current-file-data))
+  (defun projectile-ko-run-command ()
+    "define a run command for a ko project, depending on the openend file "
+    (cond
+     ((eq major-mode 'go-mode) (projectile-ko-run-command-go))
+     ;; nothing by default ?
+     ))
+  
+  (defun projectile-ko-run-command-go ()
+    "test command for a ko project if in a go file"
+    (let* ((current-file (buffer-file-name (current-buffer)))
+           (relative-current-file (file-relative-name current-file (projectile-project-root)))
+           (relative-current-folder (file-name-directory relative-current-file)))
+      (cond
+       ((string-prefix-p "cmd/" relative-current-file) (format "go run ./%s" relative-current-folder)))))
+  
+  (defun projectile-ko-package-command ()
+    "define a package command for a ko project, depending on the openend file "
+    "ko resolve --push=false --oci-layout-path=/tmp/oci -f config")
+  (defun projectile-ko-install-command ()
+    "define a install command for a ko project, depending on the openend file "
+    "ko apply -f config/")
+  (projectile-register-project-type 'ko #'projectile-ko-project-p
+                                    :project-file ".ko.yaml" ; might not be required
+  				                  :configure 'projectile-ko-configure-command
+  				                  :compile 'projectile-ko-compile-command
+  				                  :test 'projectile-ko-test-command
+  				                  :run 'projectile-ko-run-command
+  				                  :package 'projectile-ko-package-command
+  				                  :install 'projectile-ko-install-command)
   (projectile-mode))
 
 (provide 'config-projects)