org-taskjuggler.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. ;;; org-taskjuggler.el --- TaskJuggler exporter for org-mode
  2. ;;
  3. ;; Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
  4. ;;
  5. ;; Emacs Lisp Archive Entry
  6. ;; Filename: org-taskjuggler.el
  7. ;; Version: 6.34trans
  8. ;; Author: Christian Egli
  9. ;; Maintainer: Christian Egli
  10. ;; Keywords: org, taskjuggler, project planning
  11. ;; Description: Converts an org-mode buffer into a taskjuggler project plan
  12. ;; URL:
  13. ;; This file is part of GNU Emacs.
  14. ;; GNU Emacs is free software: you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation, either version 3 of the License, or
  17. ;; (at your option) any later version.
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;; GNU General Public License for more details.
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  24. ;; Commentary:
  25. ;;
  26. ;; This library implements a TaskJuggler exporter for org-mode.
  27. ;;
  28. ;; The interactive functions are similar to those of the HTML and LaTeX
  29. ;; exporters:
  30. ;;
  31. ;; M-x `org-export-as-taskjuggler'
  32. ;; M-x `org-export-as-taskjuggler-and-open'
  33. ;;
  34. ;;; TODO:
  35. ;; * Handle explicit dependencies such as BLOCKER and depends attribute
  36. ;; * Code cleanup
  37. ;; * Add documentation
  38. ;; * Try using plists instead of alists
  39. ;;
  40. ;;; Code:
  41. (eval-when-compile
  42. (require 'cl))
  43. (require 'org)
  44. (require 'org-exp)
  45. ;;; User variables:
  46. (defgroup org-export-taskjuggler nil
  47. "Options for exporting Org-mode files to TaskJuggler."
  48. :tag "Org Export TaskJuggler"
  49. :group 'org-export)
  50. (defcustom org-export-taskjuggler-extension ".tjp"
  51. "Extension of TaskJuggler files."
  52. :group 'org-export-taskjuggler
  53. :type 'string)
  54. (defcustom org-export-taskjuggler-project-tag "project"
  55. "Tag, property or todo used to find the tree containing all
  56. the tasks for the project."
  57. :group 'org-export-taskjuggler
  58. :type 'string)
  59. (defcustom org-export-taskjuggler-resource-tag "resource"
  60. "Tag, property or todo used to find the tree containing all the
  61. resources for the project."
  62. :group 'org-export-taskjuggler
  63. :type 'string)
  64. (defcustom org-export-taskjuggler-default-project-version "1.0"
  65. "Default version string for the project."
  66. :group 'org-export-taskjuggler
  67. :type 'string)
  68. (defcustom org-export-taskjuggler-default-project-duration 180
  69. "Default project duration if no start and end date have been defined
  70. in the root node of the task tree, i.e. the tree that has been marked
  71. with `org-export-taskjuggler-project-tag'"
  72. :group 'org-export-taskjuggler
  73. :type 'integer)
  74. (defcustom org-export-taskjuggler-default-reports
  75. '("taskreport \"Gantt Chart\" {
  76. headline \"Project Gantt Chart\"
  77. columns hierarchindex, name, start, end, effort, duration, completed, chart
  78. timeformat \"%a %Y-%m-%d\"
  79. loadunit days
  80. }"
  81. "resourcereport \"Resource Graph\" {
  82. headline \"Resource Allocation Graph\"
  83. columns no, name, rate, utilization, freeload, chart
  84. loadunit days
  85. hidetask 1
  86. }")
  87. "Default reports for the project."
  88. :group 'org-export-taskjuggler
  89. :type '(repeat (string :tag "Report")))
  90. ;;; Hooks
  91. (defvar org-export-taskjuggler-final-hook nil
  92. "Hook run at the end of TaskJuggler export, in the new buffer.")
  93. ;;; Autoload functions:
  94. ;;;###autoload
  95. (defun org-export-as-taskjuggler ()
  96. "Export parts of the current buffer as a TaskJuggler file.
  97. The exporter looks for a tree with tag, property or todo that
  98. matches `org-export-taskjuggler-project-tag' and takes this as
  99. the tasks for this project. The first node of this tree defines
  100. the project properties such as project name and project period.
  101. If there is a tree with tag, property or todo that matches
  102. `org-export-taskjuggler-resource-tag' this three is taken as
  103. resources for the project. If no resources are specified, a
  104. default resource is created and allocated to the project. Also
  105. the taskjuggler project will be created with default reports as
  106. defined in `org-export-taskjuggler-default-reports'."
  107. (interactive)
  108. (message "Exporting...")
  109. (setq-default org-done-keywords org-done-keywords)
  110. (let* ((tasks
  111. (org-taskjuggler-resolve-dependencies
  112. (org-taskjuggler-assign-task-ids
  113. (org-map-entries '(org-taskjuggler-components)
  114. org-export-taskjuggler-project-tag nil 'archive 'comment))))
  115. (resources
  116. (org-taskjuggler-assign-resource-ids
  117. (org-map-entries '(org-taskjuggler-components)
  118. org-export-taskjuggler-resource-tag nil 'archive 'comment)))
  119. (filename (expand-file-name
  120. (concat
  121. (file-name-sans-extension
  122. (file-name-nondirectory buffer-file-name))
  123. org-export-taskjuggler-extension)))
  124. (buffer (find-file-noselect filename))
  125. (old-level 0)
  126. task resource)
  127. ;; add a default resource
  128. (unless resources
  129. (setq resources
  130. `((("ID" . ,(user-login-name))
  131. ("headline" . ,user-full-name)
  132. ("level" . 1)))))
  133. ;; add a default allocation to the first task if none was given
  134. (unless (assoc "allocate" (car tasks))
  135. (let ((task (car tasks))
  136. (resource-id (cdr (assoc "ID" (car resources)))))
  137. (setcar tasks (push (cons "allocate" resource-id) task))))
  138. ;; add a default start date to the first task if none was given
  139. (unless (assoc "start" (car tasks))
  140. (let ((task (car tasks))
  141. (time-string (format-time-string "%Y-%m-%d")))
  142. (setcar tasks (push (cons "start" time-string) task))))
  143. ;; add a default end date to the first task if none was given
  144. (unless (assoc "end" (car tasks))
  145. (let* ((task (car tasks))
  146. (now (current-time))
  147. (duration
  148. (days-to-time org-export-taskjuggler-default-project-duration))
  149. (time-string
  150. (format-time-string "%Y-%m-%d" (time-add now duration))))
  151. (setcar tasks (push (cons "end" time-string) task))))
  152. ;; add a default version if none was given
  153. (unless (assoc "version" (car tasks))
  154. (let ((task (car tasks))
  155. (version org-export-taskjuggler-default-project-version))
  156. (setcar tasks (push (cons "version" version) task))))
  157. (with-current-buffer buffer
  158. (erase-buffer)
  159. (org-taskjuggler-open-project (car tasks))
  160. (dolist (resource resources)
  161. (let ((level (cdr (assoc "level" resource))))
  162. (org-taskjuggler-close-maybe level)
  163. (org-taskjuggler-open-resource resource)
  164. (setq old-level level)))
  165. (org-taskjuggler-close-maybe 1)
  166. (setq old-level 0)
  167. (dolist (task tasks)
  168. (let ((level (cdr (assoc "level" task))))
  169. (org-taskjuggler-close-maybe level)
  170. (org-taskjuggler-open-task task)
  171. (setq old-level level)))
  172. (org-taskjuggler-close-maybe 1)
  173. (org-taskjuggler-insert-reports)
  174. (save-buffer)
  175. (or (org-export-push-to-kill-ring "TaskJuggler")
  176. (message "Exporting... done"))
  177. (current-buffer))))
  178. ;;;###autoload
  179. (defun org-export-as-taskjuggler-and-open ()
  180. "Export the current buffer as a TaskJuggler file and open it with the TaskJuggler GUI."
  181. (interactive)
  182. (let ((file-name (buffer-file-name (org-export-as-taskjuggler)))
  183. (command "TaskJugglerUI"))
  184. (start-process-shell-command command nil command file-name)))
  185. (defun org-taskjuggler-parent-is-ordered-p ()
  186. "Return true if the parent of the current node has a property
  187. \"ORDERED\". Return nil otherwise."
  188. (save-excursion
  189. (and (org-up-heading-safe) (org-entry-get (point) "ORDERED"))))
  190. (defun org-taskjuggler-components ()
  191. "Return an alist containing all the pertinent information for
  192. the current node such as the headline, the level, todo state
  193. information, all the properties, etc."
  194. (let* ((props (org-entry-properties))
  195. (components (org-heading-components))
  196. (level (car components))
  197. (headline (nth 4 components))
  198. (parent-ordered (org-taskjuggler-parent-is-ordered-p)))
  199. (push (cons "level" level) props)
  200. (push (cons "headline" headline) props)
  201. (push (cons "parent-ordered" parent-ordered) props)))
  202. (defun org-taskjuggler-assign-task-ids (tasks)
  203. "Given a list of tasks return the same list assigning a unique id
  204. and the full path to each task. Taskjuggler takes hierarchical ids.
  205. For that reason we have to make ids locally unique and we have to keep
  206. a path to the current task."
  207. (let ((previous-level 0)
  208. unique-ids
  209. path
  210. task resolved-tasks tmp)
  211. (dolist (task tasks resolved-tasks)
  212. (let ((level (cdr (assoc "level" task))))
  213. (cond
  214. ((< previous-level level)
  215. (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
  216. (dotimes (tmp (- level previous-level))
  217. (push (list unique-id) unique-ids)
  218. (push unique-id path)))
  219. ((= previous-level level)
  220. (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
  221. (push unique-id (car unique-ids))
  222. (setcar path unique-id))
  223. ((> previous-level level)
  224. (dotimes (tmp (- previous-level level))
  225. (pop unique-ids)
  226. (pop path))
  227. (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
  228. (push unique-id (car unique-ids))))
  229. (push (cons "unique-id" unique-id) task)
  230. (push (cons "path" (mapconcat 'identity (reverse path) ".")) task)
  231. (setq previous-level level)
  232. (setq resolved-tasks (append resolved-tasks (list task)))))))
  233. (defun org-taskjuggler-assign-resource-ids (resources)
  234. "Given a list of resources return the same list, assigning a
  235. unique id to each resource."
  236. (let (unique-ids
  237. unique-id
  238. resource resolved-resources)
  239. (dolist (resource resources resolved-resources)
  240. (setq unique-id (org-taskjuggler-get-unique-id resource unique-ids))
  241. (push unique-id unique-ids)
  242. (push (cons "unique-id" unique-id) resource)
  243. (setq resolved-resources (append resolved-resources (list resource))))))
  244. (defun org-taskjuggler-resolve-dependencies (tasks)
  245. (let ((previous-level 0)
  246. siblings
  247. task resolved-tasks)
  248. (dolist (task tasks resolved-tasks)
  249. (let* ((level (cdr (assoc "level" task)))
  250. (depends (cdr (assoc "depends" task)))
  251. (parent-ordered (cdr (assoc "parent-ordered" task)))
  252. (blocker (cdr (assoc "BLOCKER" task)))
  253. (blocked-on-previous (and blocker (string-match "previous-sibling" blocker)))
  254. (dependencies
  255. (org-taskjuggler-resolve-explicit-dependencies
  256. (append
  257. (and depends (split-string depends "[, \f\t\n\r\v]+" t))
  258. (and blocker (split-string blocker "[, \f\t\n\r\v]+" t))) tasks))
  259. previous-sibling)
  260. ; update previous sibling info
  261. (cond
  262. ((< previous-level level)
  263. (dotimes (tmp (- level previous-level))
  264. (push task siblings)))
  265. ((= previous-level level)
  266. (setq previous-sibling (car siblings))
  267. (setcar siblings task))
  268. ((> previous-level level)
  269. (dotimes (tmp (- previous-level level))
  270. (pop siblings))
  271. (setq previous-sibling (car siblings))
  272. (setcar siblings task)))
  273. ; insert a dependency on previous sibling if the parent is
  274. ; ordered or if the tasks has a BLOCKER attribute with value "previous-sibling"
  275. (when (or (and previous-sibling parent-ordered) blocked-on-previous)
  276. (push (format "!%s" (cdr (assoc "unique-id" previous-sibling))) dependencies))
  277. ; store dependency information
  278. (when dependencies
  279. (push (cons "depends" (mapconcat 'identity dependencies ", ")) task))
  280. (setq previous-level level)
  281. (setq resolved-tasks (append resolved-tasks (list task)))))))
  282. (defun org-taskjuggler-resolve-explicit-dependencies (dependencies tasks)
  283. (let (path)
  284. (cond
  285. ((null dependencies) nil)
  286. ; ignore previous sibling dependencies
  287. ((equal (car dependencies) "previous-sibling")
  288. (org-taskjuggler-resolve-explicit-dependencies (cdr dependencies) tasks))
  289. ; if the id is found in another task use its path
  290. ((setq path (org-taskjuggler-find-task-with-id (car dependencies) tasks))
  291. (cons path (org-taskjuggler-resolve-explicit-dependencies (cdr dependencies) tasks)))
  292. ; silently ignore all other dependencies
  293. (t (org-taskjuggler-resolve-explicit-dependencies (cdr dependencies) tasks)))))
  294. (defun org-taskjuggler-find-task-with-id (id tasks)
  295. "Find ID in tasks. If found return the path of task. Otherwise return nil."
  296. (cond
  297. ((null tasks) nil)
  298. ((equal (cdr (assoc "ID" (car tasks))) id)
  299. (cdr (assoc "path" (car tasks))))
  300. (t (org-taskjuggler-find-task-with-id id (cdr tasks)))))
  301. (defun org-taskjuggler-get-unique-id (item unique-ids)
  302. "Return a unique id for an ITEM which can be a task or a resource.
  303. The id is derived from the headline and made unique against
  304. UNIQUE-IDS. If the first part of the headline is not unique try to add
  305. more parts of the headline or finally add more underscore characters (\"_\")."
  306. (let* ((headline (cdr (assoc "headline" item)))
  307. (parts (split-string headline))
  308. (id (downcase (pop parts))))
  309. ; try to add more parts of the headline to make it unique
  310. (while (member id unique-ids)
  311. (setq id (concat id "_" (downcase (pop parts)))))
  312. ; if its still not unique add "_"
  313. (while (member id unique-ids)
  314. (setq id (concat id "_")))
  315. (org-taskjuggler-clean-id id)))
  316. (defun org-taskjuggler-clean-id (id)
  317. "Clean and return ID to make it acceptable for taskjuggler."
  318. (and id (replace-regexp-in-string "[^a-zA-Z0-9_]" "_" id)))
  319. (defun org-taskjuggler-open-project (project)
  320. (let ((unique-id (cdr (assoc "unique-id" project)))
  321. (headline (cdr (assoc "headline" project)))
  322. (version (cdr (assoc "version" project)))
  323. (start (cdr (assoc "start" project)))
  324. (end (cdr (assoc "end" project))))
  325. (insert
  326. (concat
  327. "project " unique-id
  328. " \"" headline "\" \"" version "\" " start " - " end " {\n }\n"))))
  329. (defun org-taskjuggler-open-resource (resource)
  330. (let ((id (org-taskjuggler-clean-id (cdr (assoc "ID" resource))))
  331. (unique-id (org-taskjuggler-clean-id (cdr (assoc "unique-id" resource))))
  332. (headline (cdr (assoc "headline" resource)))
  333. (limits (cdr (assoc "limits" resource)))
  334. (vacation (cdr (assoc "vacation" resource))))
  335. (insert
  336. (concat "resource " (or id unique-id) " \"" headline "\" {\n "
  337. (and limits (concat "\n limits { " limits " }\n"))
  338. (and vacation (concat "\n vacation " vacation "\n"))))))
  339. (defun org-taskjuggler-clean-effort (effort)
  340. "Translate effort strings into a format acceptable to taskjuggler,
  341. i.e. REAL UNIT. If the effort string is something like 5:00 it
  342. will be assumed to be hours and will be translated into 5.0h.
  343. Otherwise if it contains something like 3.0 it is assumed to be
  344. days and will be translated into 3.0d. Other formats that taskjuggler
  345. supports (like weeks, months and years) are currently not supported."
  346. (cond
  347. ((null effort) effort)
  348. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" effort)
  349. ; FIXME: translate the minutes to a decimal
  350. (concat (match-string 1 effort) "." (match-string 2 effort) "h"))
  351. ((string-match "\\([0-9]+\\).\\([0-9]+\\)" effort) (concat effort "d"))
  352. (t (error "Not a valid effort (%s)" effort))))
  353. (defun org-taskjuggler-open-task (task)
  354. (let* ((unique-id (cdr (assoc "unique-id" task)))
  355. (headline (cdr (assoc "headline" task)))
  356. (effort (org-taskjuggler-clean-effort (cdr (assoc org-effort-property task))))
  357. (depends (cdr (assoc "depends" task)))
  358. (allocate (cdr (assoc "allocate" task)))
  359. (account (cdr (assoc "account" task)))
  360. (start (cdr (assoc "start" task)))
  361. (state (cdr (assoc "TODO" task)))
  362. (complete (or (and (member state org-done-keywords) "100")
  363. (cdr (assoc "complete" task))))
  364. (note (cdr (assoc "note" task)))
  365. (priority (cdr (assoc "priority" task)))
  366. (parent-ordered (cdr (assoc "parent-ordered" task)))
  367. (previous-sibling (cdr (assoc "previous-sibling" task))))
  368. (insert
  369. (concat
  370. "task " unique-id " \"" headline "\" {"
  371. (and effort (concat "\n effort " effort))
  372. (if (and parent-ordered previous-sibling)
  373. (concat "\n depends " previous-sibling)
  374. (and depends (concat "\n depends " depends)))
  375. (and allocate (concat "\n purge allocations\n allocate " allocate))
  376. (and account (concat "\n account " account))
  377. (and start (concat "\n start " start))
  378. (and complete (concat "\n complete " complete))
  379. (and note (concat "\n note " note))
  380. (and priority (concat "\n priority " priority))
  381. "\n"))))
  382. (defun org-taskjuggler-close-maybe (level)
  383. (while (> old-level level)
  384. (insert "}\n")
  385. (setq old-level (1- old-level)))
  386. (when (= old-level level)
  387. (insert "}\n")))
  388. (defun org-taskjuggler-insert-reports ()
  389. (let (report)
  390. (dolist (report org-export-taskjuggler-default-reports)
  391. (insert report "\n"))))
  392. (provide 'org-taskjuggler)
  393. ;;; org-taskjuggler.el ends here