2017-07-29 07:41:05 +00:00
|
|
|
#!/usr/bin/emacs --script
|
|
|
|
;;
|
|
|
|
;;; run.el -- Spacemacs documentation export runner -*- lexical-binding: t -*-
|
|
|
|
;;
|
|
|
|
;; Copyright (C) 2012-2017 Sylvain Benner & Contributors
|
|
|
|
;;
|
|
|
|
;; Author: Eugene "JAremko" Yaremenko <w3techplayground@gmail.com>
|
|
|
|
;; URL: https://github.com/syl20bnr/spacemacs
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
;;
|
|
|
|
;; Note: see `spacemacs-export-docs-help-text' for usage.
|
|
|
|
;;
|
|
|
|
;;; License: GPLv3
|
2017-08-16 15:50:02 +00:00
|
|
|
|
2017-07-29 07:41:05 +00:00
|
|
|
(require 'json)
|
2017-08-16 15:50:02 +00:00
|
|
|
(require 'cl-lib)
|
|
|
|
(require 'subr-x)
|
2017-07-29 07:41:05 +00:00
|
|
|
|
2017-08-16 15:50:02 +00:00
|
|
|
(defconst spacemacs-export-docs-this-file-name
|
2017-07-29 07:41:05 +00:00
|
|
|
(or load-file-name
|
|
|
|
buffer-file-name)
|
|
|
|
"`load-file-name' or `buffer-file-name'/")
|
|
|
|
|
2017-08-16 15:50:02 +00:00
|
|
|
(defconst spacemacs-export-docs-this-file-dir
|
|
|
|
(file-name-directory spacemacs-export-docs-this-file-name)
|
|
|
|
"`file-name-directory' of `spacemacs-export-docs-this-file-name'")
|
|
|
|
|
|
|
|
(load
|
|
|
|
(expand-file-name
|
|
|
|
"../lib/shared.el"
|
|
|
|
spacemacs-export-docs-this-file-dir)
|
|
|
|
nil t)
|
2017-07-29 07:41:05 +00:00
|
|
|
|
2017-08-16 15:50:02 +00:00
|
|
|
(defvar spacemacs--export-docs-default-exclude-re
|
2017-07-29 07:41:05 +00:00
|
|
|
(regexp-opt
|
|
|
|
(append
|
|
|
|
(mapcar (lambda (el)
|
|
|
|
(file-truename
|
|
|
|
(concat
|
|
|
|
"../../../"
|
|
|
|
el)))
|
|
|
|
`("export/"
|
|
|
|
"private/"
|
|
|
|
"tests/"
|
|
|
|
"elpa/"))
|
|
|
|
(file-expand-wildcards
|
2017-08-16 15:50:02 +00:00
|
|
|
(file-truename (concat spacemacs--spacetools-root-dir
|
2017-07-29 07:41:05 +00:00
|
|
|
".*")))
|
|
|
|
'("LAYERS.org")))
|
|
|
|
"Default regexp for ignoring ORG and static files.
|
|
|
|
It will be matched against full path of each exported file.")
|
|
|
|
|
|
|
|
(defconst spacemacs-export-docs-help-text
|
|
|
|
`((:general . ("Spacemacs documentation exporter"
|
|
|
|
"================================"
|
|
|
|
"First argument should be either \"test\" or \"export\""
|
|
|
|
"Use \"test -h\" or \"export -h\" for more info"))
|
|
|
|
(:test . ("Spacemacs documentation exporter (test mode)"
|
|
|
|
"==============================================="
|
|
|
|
"Arguments:"
|
|
|
|
" - <test> - You already know this :)"
|
|
|
|
" - list of files for testing"
|
|
|
|
" Example: ./run.el test \"doc/FAQ.org\" \"CONTRIBUTING.org\""
|
2017-12-03 16:11:16 +00:00
|
|
|
"NOTE: In this mode warnings are errors"))
|
2017-07-29 07:41:05 +00:00
|
|
|
(:export . ("Spacemacs documentation exporter (export mode)"
|
|
|
|
"================================================="
|
|
|
|
"Arguments:"
|
|
|
|
" - <export> - You already know this :)"
|
|
|
|
" - Target directory for export"
|
2017-11-08 14:46:55 +00:00
|
|
|
" (default: \"<TMP>/spacemacs-export/\")"
|
2017-12-03 16:11:16 +00:00
|
|
|
" - Number of Emacs instances that will be used for exporting"
|
|
|
|
" (default \"6\", minimal value is 1 and maximal"
|
|
|
|
" is total number of ORG files)"
|
2017-07-29 07:41:05 +00:00
|
|
|
" - Regexp for ignoring ORG and static files,"
|
|
|
|
" exclude nothing if empty string"
|
|
|
|
" (It will be matched against full path of each exported file"
|
|
|
|
,(format " Current default: %S)"
|
2017-08-16 15:50:02 +00:00
|
|
|
spacemacs--export-docs-default-exclude-re))))
|
2017-07-29 07:41:05 +00:00
|
|
|
"Help text for `spacemacs//export-docs-maybe-show-help' .")
|
|
|
|
|
2017-08-16 15:50:02 +00:00
|
|
|
(defvar spacemacs--export-docs-configs-plist nil
|
|
|
|
"Script configurations created with `spacemacs//export-docs-parse-args'.
|
|
|
|
The shape:(
|
|
|
|
:mode <export or test>
|
|
|
|
:target-directory <directory to output stuff>
|
|
|
|
:workers-count <how many workers will be used>
|
|
|
|
:exclude-re <file exclusion regular expression>
|
|
|
|
:files <filtered files list>
|
|
|
|
)
|
|
|
|
See `spacemacs-export-docs-help-text' for details.")
|
|
|
|
|
|
|
|
(defvar spacemacs--export-docs-workers-fin 0
|
2017-12-03 16:11:16 +00:00
|
|
|
"Number of Emacs instances that finished exporting.")
|
2017-08-16 15:50:02 +00:00
|
|
|
(defvar spacemacs--export-docs-mode nil
|
|
|
|
"Current mode")
|
|
|
|
(defvar spacemacs--export-docs-test-failed? nil
|
|
|
|
"Will be set to t if exporting fails in the test mode.")
|
|
|
|
(defvar spacemacs--export-docs-stop-waiting nil
|
|
|
|
"Used for blocking until all exporters have exited.")
|
|
|
|
(defvar spacemacs--export-docs-copy-queue '()
|
|
|
|
"Queue of static dependencies to be copied to
|
|
|
|
the export dir.")
|
2017-07-29 07:41:05 +00:00
|
|
|
|
|
|
|
(defun spacemacs//export-docs-maybe-show-help (arg-list)
|
|
|
|
"Show help message if arguments are invalid.
|
|
|
|
Returns t if help was shown."
|
2017-08-16 15:50:02 +00:00
|
|
|
(message
|
|
|
|
(cond
|
|
|
|
((or (not arg-list)
|
|
|
|
(not (member (car arg-list)
|
|
|
|
'("test" "export"))))
|
2017-07-29 07:41:05 +00:00
|
|
|
(mapconcat
|
|
|
|
'identity
|
|
|
|
(alist-get :general spacemacs-export-docs-help-text)
|
2017-08-16 15:50:02 +00:00
|
|
|
"\n"))
|
2017-07-29 07:41:05 +00:00
|
|
|
|
2017-08-16 15:50:02 +00:00
|
|
|
;; test
|
|
|
|
((and (string= (car arg-list) "test")
|
|
|
|
(or (< (length arg-list) 2)
|
|
|
|
(string-match "-h" (or (cadr arg-list) ""))))
|
2017-07-29 07:41:05 +00:00
|
|
|
(mapconcat
|
|
|
|
'identity
|
|
|
|
(alist-get :test spacemacs-export-docs-help-text)
|
2017-08-16 15:50:02 +00:00
|
|
|
"\n"))
|
2017-07-29 07:41:05 +00:00
|
|
|
|
2017-08-16 15:50:02 +00:00
|
|
|
;; export
|
|
|
|
((and (string= (car arg-list) "export")
|
|
|
|
(or (> (length arg-list) 3)
|
|
|
|
(string-match "-h" (or (cadr arg-list) ""))))
|
2017-07-29 07:41:05 +00:00
|
|
|
(mapconcat
|
|
|
|
'identity
|
|
|
|
(alist-get :export spacemacs-export-docs-help-text)
|
|
|
|
"\n")))))
|
|
|
|
|
2017-08-16 15:50:02 +00:00
|
|
|
(defun spacemacs//export-docs-parse-args (arg-list)
|
2017-07-29 07:41:05 +00:00
|
|
|
"Initialization. ARG-LIST is argument list.
|
2017-08-16 15:50:02 +00:00
|
|
|
The function will return a plist of configs or print
|
|
|
|
a help message if the list of arguments is invalid.
|
|
|
|
See `spacemacs-export-docs-help-text' for description."
|
2017-07-29 07:41:05 +00:00
|
|
|
(when (and (spacemacs//export-docs-maybe-show-help arg-list)
|
|
|
|
noninteractive)
|
|
|
|
(kill-emacs 1))
|
2017-08-16 15:50:02 +00:00
|
|
|
(let ((script-mode (pop arg-list)))
|
|
|
|
(append
|
|
|
|
`(:mode ,script-mode)
|
|
|
|
(cond
|
|
|
|
;; Export mode.
|
|
|
|
((string= script-mode "export")
|
|
|
|
(append
|
|
|
|
`(:target-directory
|
2017-10-18 01:30:30 +00:00
|
|
|
,(file-name-as-directory
|
|
|
|
(or (pop arg-list)
|
|
|
|
(concat temporary-file-directory
|
|
|
|
"spacemacs-export/")))
|
2017-08-16 15:50:02 +00:00
|
|
|
:workers-count
|
2017-10-18 01:30:30 +00:00
|
|
|
,(max (or (when-let (cnt (pop arg-list))
|
|
|
|
(string-to-number cnt))
|
|
|
|
6)
|
|
|
|
1))
|
2017-08-16 15:50:02 +00:00
|
|
|
(let ((exclude-re
|
|
|
|
(or (pop arg-list)
|
|
|
|
spacemacs--export-docs-default-exclude-re))
|
|
|
|
(all-org-files (directory-files-recursively
|
|
|
|
spacemacs--spacetools-root-dir
|
|
|
|
"\\.org$")))
|
|
|
|
`(:exclude-re
|
|
|
|
,exclude-re
|
|
|
|
:files
|
|
|
|
,(if (or (not exclude-re) (string-empty-p exclude-re))
|
|
|
|
all-org-files
|
|
|
|
(cl-remove-if
|
|
|
|
(lambda(e)(string-match-p exclude-re e))
|
|
|
|
all-org-files))))))
|
|
|
|
;; Test mode.
|
|
|
|
((string= script-mode "test")
|
|
|
|
`(:target-directory
|
|
|
|
,(file-name-as-directory (make-temp-file "spacemacs-test-export" t))
|
|
|
|
:workers-count
|
|
|
|
1
|
|
|
|
:exclude-re
|
|
|
|
""
|
|
|
|
:files
|
|
|
|
,(mapcar
|
|
|
|
(lambda (path)
|
|
|
|
(let ((abs-path
|
|
|
|
(file-truename
|
|
|
|
(if (file-name-absolute-p path)
|
|
|
|
path
|
|
|
|
(expand-file-name
|
|
|
|
path
|
|
|
|
spacemacs--spacetools-root-dir)))))
|
|
|
|
(if (and (file-readable-p abs-path)
|
|
|
|
(not (file-directory-p abs-path))
|
|
|
|
(string-prefix-p
|
|
|
|
spacemacs--spacetools-root-dir
|
|
|
|
abs-path))
|
|
|
|
abs-path
|
|
|
|
(error "%S is invalid file or outside %S"
|
|
|
|
path
|
|
|
|
spacemacs--spacetools-root-dir))))
|
|
|
|
arg-list)))
|
|
|
|
(t (error "Unknown script mode: %s"
|
|
|
|
spacemacs--export-docs-mode))))))
|
2017-07-29 07:41:05 +00:00
|
|
|
|
|
|
|
(defun spacemacs//export-docs-copy-file-to-export-dir (path export-dir)
|
|
|
|
"Copy file at PATH into corresponding PATH in
|
|
|
|
TO EXPORT-DIR unless PATH
|
2017-08-16 15:50:02 +00:00
|
|
|
matches :exclude-re from `spacemacs--export-docs-configs-plist'.
|
2017-07-29 07:41:05 +00:00
|
|
|
NOTE: PATH mast be absolute path."
|
2017-08-16 15:50:02 +00:00
|
|
|
(let ((exclude-re (plist-get
|
|
|
|
spacemacs--export-docs-configs-plist
|
|
|
|
:exclude-re)))
|
|
|
|
(if (and
|
|
|
|
(not (string= exclude-re ""))
|
|
|
|
(string-match-p exclude-re
|
|
|
|
path))
|
|
|
|
(message "File %S was ignored (matched by the exclusion regexp)"
|
|
|
|
path)
|
2017-08-18 19:46:52 +00:00
|
|
|
(let* ((new-path
|
|
|
|
(concat
|
|
|
|
export-dir
|
|
|
|
(substring
|
|
|
|
path
|
|
|
|
(length spacemacs--spacetools-root-dir))))
|
|
|
|
(new-path-dir (file-name-as-directory
|
|
|
|
(file-name-directory new-path))))
|
|
|
|
(make-directory new-path-dir t)
|
2017-08-16 15:50:02 +00:00
|
|
|
(message "Copying file %S into %S"
|
|
|
|
path
|
|
|
|
new-path)
|
|
|
|
(copy-file path new-path t)))))
|
2017-07-29 07:41:05 +00:00
|
|
|
|
|
|
|
(defun spacemacs//export-docs-concurrently-sentinel (p e)
|
2017-08-16 15:50:02 +00:00
|
|
|
(condition-case err
|
|
|
|
(let ((buff (process-buffer p)))
|
|
|
|
(if (not (eq (process-status p) 'exit))
|
|
|
|
(error "Process %s doesn't have status: exit" p)
|
|
|
|
(spacemacs//export-docs-interpret-proc-output p buff)
|
|
|
|
(kill-buffer buff))
|
|
|
|
(if (string-match-p "finished" e)
|
|
|
|
(progn
|
|
|
|
(message "Process %s has finished\n" p)
|
|
|
|
(when
|
|
|
|
(= (setq spacemacs--export-docs-workers-fin
|
|
|
|
(1+ spacemacs--export-docs-workers-fin))
|
|
|
|
(plist-get
|
|
|
|
spacemacs--export-docs-configs-plist
|
|
|
|
:workers-count))
|
|
|
|
(setq spacemacs--export-docs-stop-waiting t)))
|
|
|
|
(error "Process %s was %s"
|
|
|
|
p e)
|
|
|
|
(setq spacemacs--export-docs-stop-waiting t)))
|
|
|
|
(error (setq spacemacs--export-docs-stop-waiting t)
|
|
|
|
(error "%s" err))))
|
2017-07-29 07:41:05 +00:00
|
|
|
|
|
|
|
(defun spacemacs//export-docs-interpret-proc-output (proc buff)
|
|
|
|
"Parses process PROC BUFFER. Process P should be finished."
|
|
|
|
(message "PROCESS: %S\n" proc)
|
|
|
|
(dolist (line (split-string (with-current-buffer buff (buffer-string)) "\n"))
|
|
|
|
(unless (or (string= line "")
|
|
|
|
(string-match-p "^Loading.*\\.\\.\\.$" line))
|
2017-08-02 08:44:08 +00:00
|
|
|
(let ((resp (ignore-errors (json-read-from-string line))))
|
|
|
|
(unless resp
|
|
|
|
(error "Malformed response:%s" line))
|
|
|
|
(let ((type (alist-get 'type resp))
|
|
|
|
(text (replace-regexp-in-string
|
|
|
|
"\r"
|
|
|
|
"\n"
|
2017-08-16 15:50:02 +00:00
|
|
|
(alist-get 'text resp)))
|
|
|
|
(mode (plist-get
|
|
|
|
spacemacs--export-docs-configs-plist
|
|
|
|
:mode)))
|
2017-08-02 08:44:08 +00:00
|
|
|
(cond
|
|
|
|
;; Export mode.
|
2017-08-16 15:50:02 +00:00
|
|
|
((string= mode "export")
|
2017-08-02 08:44:08 +00:00
|
|
|
(message
|
|
|
|
"%s"
|
|
|
|
(cond
|
|
|
|
((string= type "message")
|
|
|
|
text)
|
|
|
|
((string= type "warning")
|
|
|
|
(concat "\n=============== WARNING ===============\n"
|
|
|
|
text
|
|
|
|
"\n=======================================\n"))
|
|
|
|
((string= type "error")
|
|
|
|
(concat "\n!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!\n"
|
|
|
|
text
|
|
|
|
"\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"))
|
|
|
|
((string= type "export")
|
|
|
|
(format
|
|
|
|
(concat "File %S has static dependency %S\n"
|
|
|
|
"=> it will be copied into the export directory")
|
|
|
|
(alist-get 'source resp)
|
|
|
|
(progn
|
|
|
|
(push text spacemacs--export-docs-copy-queue)
|
|
|
|
text)))
|
|
|
|
(t
|
|
|
|
(error
|
|
|
|
"%s"
|
2017-07-29 07:41:05 +00:00
|
|
|
(concat "\n?????????? UNKNOWN EVENT TYPE ????????????\n"
|
|
|
|
(format "TYPE:\"%s\" TEXT: \"%s\"" type text)
|
2017-08-02 08:44:08 +00:00
|
|
|
"\n?????????????????????????????????????????\n"))))))
|
|
|
|
;; Test mode.
|
2017-08-16 15:50:02 +00:00
|
|
|
((string= mode "test")
|
|
|
|
(when-let
|
|
|
|
((err
|
|
|
|
(cond
|
|
|
|
((or (string= type "message")
|
|
|
|
(string= type "export"))
|
|
|
|
nil)
|
|
|
|
((or (string= type "warning")
|
|
|
|
(string= type "error"))
|
|
|
|
(concat "\n!!!!!!!!!!!!!! TEST FAILED !!!!!!!!!!!!!!\n"
|
|
|
|
text
|
|
|
|
"\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"))
|
|
|
|
(t
|
|
|
|
(concat "\n!!!!!!! ERROR: UNKNOWN EVENT TYPE !!!!!!!\n"
|
|
|
|
(format "TYPE:\"%s\" TEXT: \"%s\"" type text)
|
|
|
|
"\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")))))
|
|
|
|
(message "%s" (setq spacemacs--export-docs-test-failed? err))))
|
|
|
|
(t (error "Unknown script mode: %s" mode)))))))
|
2017-07-29 07:41:05 +00:00
|
|
|
(while spacemacs--export-docs-copy-queue
|
|
|
|
(spacemacs//export-docs-copy-file-to-export-dir
|
|
|
|
(pop spacemacs--export-docs-copy-queue)
|
2017-08-16 15:50:02 +00:00
|
|
|
(plist-get spacemacs--export-docs-configs-plist :target-directory))))
|
2017-07-29 07:41:05 +00:00
|
|
|
|
|
|
|
(defun spacemacs//export-docs-run (arg-list)
|
|
|
|
"Main function for running as a script. ARG-LIST is an argument list.
|
|
|
|
See `spacemacs-export-docs-help-text' for description."
|
2017-08-16 15:50:02 +00:00
|
|
|
(setq spacemacs--export-docs-test-failed? nil
|
|
|
|
spacemacs--export-docs-workers-fin 0
|
|
|
|
spacemacs--export-docs-stop-waiting nil)
|
|
|
|
(let* ((mode (car arg-list))
|
|
|
|
(default-directory spacemacs-export-docs-this-file-dir)
|
|
|
|
(w-path
|
|
|
|
(let ((worker-path (file-truename "_worker.elc")))
|
|
|
|
(byte-compile-file "_worker.el")
|
|
|
|
worker-path)))
|
|
|
|
(let ((conf (setq spacemacs--export-docs-configs-plist
|
|
|
|
(spacemacs//export-docs-parse-args arg-list))))
|
|
|
|
(unwind-protect
|
|
|
|
(spacemacs//spacetools-do-concurrently
|
|
|
|
(plist-get conf :files)
|
|
|
|
(plist-get conf :workers-count)
|
|
|
|
w-path
|
|
|
|
(lambda (files)
|
|
|
|
(format
|
|
|
|
"%S"
|
|
|
|
`(spacemacs/export-docs-to-edn
|
2017-08-18 19:46:52 +00:00
|
|
|
,(let ((dir (file-name-as-directory
|
|
|
|
(plist-get conf :target-directory))))
|
2017-08-16 15:50:02 +00:00
|
|
|
(make-directory dir t)
|
|
|
|
dir)
|
|
|
|
',files)))
|
|
|
|
'spacemacs//export-docs-concurrently-sentinel)
|
|
|
|
(when (string= mode "test")
|
|
|
|
(delete-directory (plist-get conf :target-directory) t)))))
|
|
|
|
(while (not spacemacs--export-docs-stop-waiting)
|
|
|
|
(accept-process-output)))
|
2017-07-29 07:41:05 +00:00
|
|
|
|
|
|
|
(defun spacemacs/export-docs-do-test (&rest files)
|
|
|
|
"Try exporting Spacemacs documentation .org FILES.
|
2017-08-16 15:50:02 +00:00
|
|
|
if error occurs return nil - otherwise t.
|
|
|
|
NOTE: See `spacemacs-export-docs-help-text' for more details."
|
|
|
|
(spacemacs//export-docs-run (append '("test") files))
|
|
|
|
(not spacemacs--export-docs-test-failed?))
|
2017-07-29 07:41:05 +00:00
|
|
|
|
|
|
|
(defun spacemacs/export-docs-do-export
|
2017-08-16 15:50:02 +00:00
|
|
|
(&optional target-dir workers-count exclude-re files)
|
2017-07-29 07:41:05 +00:00
|
|
|
"Export Spacemacs documentation files
|
2017-08-16 15:50:02 +00:00
|
|
|
into TARGET-DIR using WORKERS-COUNT worker threads and
|
|
|
|
filtering FILE list with EXCLUDE-RE matching
|
2017-07-29 07:41:05 +00:00
|
|
|
against full paths of the files.
|
|
|
|
NOTE: See `spacemacs-export-docs-help-text' for more details."
|
2017-08-16 15:50:02 +00:00
|
|
|
(spacemacs//export-docs-run
|
|
|
|
(list "export" target-dir workers-count exclude-re files)))
|
2017-07-29 07:41:05 +00:00
|
|
|
|
|
|
|
;; Script entry point.
|
|
|
|
(when (and load-file-name
|
|
|
|
noninteractive)
|
2017-08-16 15:50:02 +00:00
|
|
|
(spacemacs//export-docs-run argv)
|
|
|
|
(when spacemacs--export-docs-test-failed?
|
|
|
|
(error "Test failed.")))
|