aboutsummaryrefslogtreecommitdiffstats
path: root/generate.el
blob: 121b6d37d9642d9486a07bbdf26557e8126add45 (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
(require 'libyaml)
(require 'cl-lib)

(defun directory-name->package-name (dir)
  (format "package-%s" dir))

(defun directory->prepackage (module)
  `(,(directory-name->package-name module)
    ("stage" . "pre-package-multifile")
    ("before_script" ,(format "rm -rf %s/dist" module))
    ("script" ,(format "cask --path %s package" module))
    ("artifacts"
     ("paths" ,(format "%s/dist" module)))))

(defun module->unit-test (module)
  `(,(format "unit-test-%s" module)
    ("stage" . "unit-test")
    ("dependencies" "package")
    ("before_script" "mkdir -p ~/documents/org/projects/")
    ("script" . ,(format "make unit-test-%s TEST_ARCHIVE=$(realpath bin/)" module))))

(defun module->integration-test (module)
  `(,(format "integration-test-%s" module)
    ("stage" . "integration-test")
    ("dependencies" "package")
    ("before_script" "mkdir -p ~/.emacs.d/data/ ~/documents/org/projects/")
    ("script" . ,(format "make integration-test-%s TEST_ARCHIVE=$(realpath bin/)" module))))

(defun unit-test-exists-p (module)
  (file-exists-p (format "test/%s-test.el" module)))

(defun integration-test-exists-p (module)
  (file-exists-p (format "test/integration/%s.bats" module)))

(defun prepare-value-for-yaml (object)
  (pcase object
    ((and (pred proper-list-p)
          lst
          (guard (listp (car lst))))
     (prepare-for-yaml object))
    ((pred proper-list-p)
     (apply #'vector object))
    (_ object)))

(defun prepare-for-yaml (object)
  (let ((hash (make-hash-table)))
    (dolist (item object)
      (puthash (car item) (prepare-value-for-yaml (cdr item)) hash))
    hash))

(defun generate ()
  (let* ((modules
          (mapcar (lambda (file) (string-remove-suffix ".el" file))
                  (directory-files "." nil (rx string-start "oni-"))))
         (directories (cl-remove-if-not #'file-directory-p modules)))
    (with-temp-buffer
      (insert
       (yaml-dump
        (prepare-for-yaml
         `(("image" . "registry.gitlab.com/ryuslash/emacs-config")
           ("stages" "pre-package-multifile" "package" "unit-test" "integration-test" "deploy")
           ,@(mapcar #'directory->prepackage directories)
           ("package"
            ("stage" . "package")
            ("dependencies" ,@(mapcar #'directory-name->package-name directories))
            ("before_script"
             "rm -rf bin/"
             "mkdir bin/")
            ("script" . "make package")
            ("artifacts"
             ("paths" "bin/")))
           ;; ,@(mapcar #'module->unit-test
           ;;           (cl-remove-if-not #'unit-test-exists-p modules))
           ;; ,@(mapcar #'module->integration-test
           ;;           (cl-remove-if-not #'integration-test-exists-p modules))
           ("deploy"
            ("only" "master")
            ("stage" . "deploy")
            ("image" . "instrumentisto/rsync-ssh")
            ("dependencies" "package")
            ("before_script" "chmod 600 \"$DEPLOY_KEY\"")
            ("script"
             "rsync -e \"ssh -o \\\"UserKnownHostsFile $KNOWN_HOSTS\\\" -p 4511 -i $DEPLOY_KEY\" -v -c -r --delete bin/ \"elpa@ryuslash.org:\""))))))
      (write-file "generated-config.yml"))))