Rename project to kaarvok

project-template was too general and used by other projects.
This commit is contained in:
Tom Willemse 2013-05-20 02:57:04 +02:00
parent b02f331032
commit a163906b15
3 changed files with 83 additions and 81 deletions

26
kaarvok Executable file
View file

@ -0,0 +1,26 @@
#!/bin/sh
## kaarvok --- Generate directory structures from templates
# Copyright (C) 2013 Tom Willemse <tom at ryuslash dot org>
# This file is part of kaarvok.
# kaarvok is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# kaarvok is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with kaarvok. If not, see <http://www.gnu.org/licenses/>.
if [ "${#@}" -lt 2 ]; then
echo "Usage: $0 <template name> <destination>"
exit 1
fi
/usr/bin/emacs -Q -batch -l kaarvok.el -eval \
"(kaarvok-create-project-from-template \"$1\" \"$2\")"

View file

@ -1,4 +1,4 @@
;;; project-template --- Generate dircetory structures from templates ;;; kaarvok --- Generate directory structures from templates
;; Copyright (C) 2013 Tom Willemse ;; Copyright (C) 2013 Tom Willemse
@ -6,21 +6,20 @@
;; Keywords: convenience ;; Keywords: convenience
;; Package-Version: 0.1.0 ;; Package-Version: 0.1.0
;; This file is part of project-template. ;; This file is part of kaarvok.
;; project-template is free software; you can redistribute it and/or ;; kaarvok is free software; you can redistribute it and/or modify it
;; modify it under the terms of the GNU General Public License as ;; under the terms of the GNU General Public License as published by
;; published by the Free Software Foundation, either version 3 of the ;; the Free Software Foundation, either version 3 of the License, or
;; License, or (at your option) any later version. ;; (at your option) any later version.
;; project-template is distributed in the hope that it will be useful, ;; kaarvok is distributed in the hope that it will be useful, but
;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details. ;; General Public License for more details.
;; You should have received a copy of the GNU General Public License ;; You should have received a copy of the GNU General Public License
;; along with project-template. ;; along with kaarvok. If not, see <http://www.gnu.org/licenses/>.
;; If not, see <http://www.gnu.org/licenses/>.
;;; Commentary: ;;; Commentary:
@ -31,85 +30,88 @@
;;; Code: ;;; Code:
(defvar pt-templates-directory "~/.emacs.d/templates" (defvar kaarvok-templates-directory "~/.emacs.d/templates"
"Where templates are stored.") "Where templates are stored.")
(defvar pt-template-var-delimiter "$!" (defvar kaarvok-template-var-delimiter "$!"
"Strings used to delimit variable names.") "Strings used to delimit variable names.")
(defun pt-template-var-regexp () (defun kaarvok-template-var-regexp ()
"Create the regexp which identifies a variable in a template." "Create the regexp which identifies a variable in a template."
(concat pt-template-var-delimiter (concat kaarvok-template-var-delimiter
"\\([^" pt-template-var-delimiter " \t\n]+\\)" "\\([^" kaarvok-template-var-delimiter " \t\n]+\\)"
pt-template-var-delimiter)) kaarvok-template-var-delimiter))
(defvar pt-value-alist nil (defvar kaarvok-value-alist nil
"A placeholder where replacement values will be kept. This is "A placeholder where replacement values will be kept.
let-bound when `pt-create-project-from-template' is called and
should not be edited directly.")
(defun pt-replace-all (from to str) This is let-bound when `kaarvok-create-project-from-template' is
called and should not be edited directly.")
(defun kaarvok-replace-all (from to str)
"Simply replace all occurrences of FROM with TO in STR." "Simply replace all occurrences of FROM with TO in STR."
(while (string-match from str) (while (string-match from str)
(set 'str (replace-match to t t str))) (set 'str (replace-match to t t str)))
str) str)
(defun pt-get-replacement (key) (defun kaarvok-get-replacement (key)
"Find, or ask for and save, the replacement value for KEY." "Find, or ask for and save, the replacement value for KEY."
(let ((replacement (assoc key pt-value-alist))) (let ((replacement (assoc key kaarvok-value-alist)))
(when (eq replacement nil) (when (eq replacement nil)
(set 'replacement (set 'replacement
`(,key . ,(read-from-minibuffer (concat key ": ")))) `(,key . ,(read-from-minibuffer (concat key ": "))))
(add-to-list 'pt-value-alist replacement)) (add-to-list 'kaarvok-value-alist replacement))
replacement)) replacement))
(defun pt-parse-file-name (filename) (defun kaarvok-parse-file-name (filename)
"Parse FILENAME and replace all __variables__ with values "Parse FILENAME and replace all variables.
provided by the user."
(while (string-match (pt-template-var-regexp) filename) Use values provided by the user."
(while (string-match (kaarvok-template-var-regexp) filename)
(let* ((tpl-var (match-string 1 filename)) (let* ((tpl-var (match-string 1 filename))
(replacement-value (pt-get-replacement tpl-var))) (replacement-value (kaarvok-get-replacement tpl-var)))
(set 'filename (replace-match (cdr replacement-value) t t (set 'filename (replace-match (cdr replacement-value) t t
filename)))) filename))))
(let ((noext (file-name-sans-extension filename)) (let ((noext (file-name-sans-extension filename))
(ext (file-name-extension filename t))) (ext (file-name-extension filename t)))
(set 'noext (pt-replace-all "\\." "/" noext)) (set 'noext (kaarvok-replace-all "\\." "/" noext))
(concat noext ext))) (concat noext ext)))
(defun pt-parse-file (file) (defun kaarvok-parse-file (file)
"Parse FILE and replace all __variables__ with values provided by "Parse FILE and replace all variables."
the user."
(insert-file-contents file) (insert-file-contents file)
(while (re-search-forward (pt-template-var-regexp) nil t) (while (re-search-forward (kaarvok-template-var-regexp) nil t)
(let* ((tpl-var (match-string 1)) (let* ((tpl-var (match-string 1))
(replacement-value (pt-get-replacement tpl-var))) (replacement-value (kaarvok-get-replacement tpl-var)))
(replace-match (cdr replacement-value) t t)))) (replace-match (cdr replacement-value) t t))))
(defun pt-parse-and-copy-file (src dst) (defun kaarvok-parse-and-copy-file (src dst)
"Copy SRC to DST, but, if necessary, parse the file and filename "Copy SRC to DST.
first."
(let* ((parsed-dst (pt-parse-file-name dst)) If necessary, parse the file and filename first."
(let* ((parsed-dst (kaarvok-parse-file-name dst))
(parsed-dst-dir (file-name-directory parsed-dst))) (parsed-dst-dir (file-name-directory parsed-dst)))
(when (not (file-exists-p parsed-dst-dir)) (when (not (file-exists-p parsed-dst-dir))
(make-directory parsed-dst-dir t)) (make-directory parsed-dst-dir t))
(with-temp-file parsed-dst (with-temp-file parsed-dst
(pt-parse-file src)))) (kaarvok-parse-file src))))
(defun pt-copy-directory (directory to) (defun kaarvok-copy-directory (directory to)
"Copy template directory DIRECTORY to the location indicated by "Copy template DIRECTORY to TO.
TO and parse both paths and files in the process."
Parse both paths and files in the process."
(let ((files (directory-files directory t "[^.]\\{1,2\\}$" t))) (let ((files (directory-files directory t "[^.]\\{1,2\\}$" t)))
(while files (while files
(let* ((src-filename (car files)) (let* ((src-filename (car files))
(dst-filename (dst-filename
(concat to "/" (file-name-nondirectory src-filename)))) (concat to "/" (file-name-nondirectory src-filename))))
(if (file-directory-p src-filename) (if (file-directory-p src-filename)
(pt-copy-directory src-filename dst-filename) (kaarvok-copy-directory src-filename dst-filename)
(if (string-equal (file-name-extension src-filename) "etpl") (if (string-equal (file-name-extension src-filename) "etpl")
(pt-parse-and-copy-file (kaarvok-parse-and-copy-file
src-filename (file-name-sans-extension dst-filename)) src-filename (file-name-sans-extension dst-filename))
(if (not (file-exists-p to)) (if (not (file-exists-p to))
(make-directory to t) (make-directory to t)
@ -122,13 +124,14 @@ TO and parse both paths and files in the process."
(set 'files (cdr files))))) (set 'files (cdr files)))))
;;;###autoload ;;;###autoload
(defun pt-create-project-from-template (template destination) (defun kaarvok-create-project-from-template (template destination)
"Take TEMPLATE, copy it to DESTINATION and replace any "Take TEMPLATE, copy it to DESTINATION.
occurrences of __variables__ with user-povided values."
(interactive "MTemplate: \nGDestination: ")
(let ((pt-value-alist))
(pt-copy-directory
(concat pt-templates-directory "/" template) destination)))
(provide 'project-template) Replace any occurrences of variables with user-povided values."
;;; project-template.el ends here (interactive "MTemplate: \nGDestination: ")
(let ((kaarvok-value-alist))
(kaarvok-copy-directory
(concat kaarvok-templates-directory "/" template) destination)))
(provide 'kaarvok)
;;; kaarvok.el ends here

27
pt
View file

@ -1,27 +0,0 @@
#!/bin/sh
# pt -- A small shell wrapper around project-template
# Copyright (C) 2013 Tom Willemse <tom at ryuslash dot org>
# This file is part of project-template.
# project-template is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# project-template is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with project-template.
# If not, see <http://www.gnu.org/licenses/>.
if [ "${#@}" -lt 2 ]; then
echo "Usage: $0 <template name> <destination>"
exit 1
fi
/usr/bin/emacs -Q -batch -l project-template.el -eval \
"(pt-create-project-from-template \"$1\" \"$2\")"