org-taskjuggler.el 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. ;;; org-taskjuggler.el --- TaskJuggler exporter for org-mode
  2. ;;
  3. ;; Copyright (C) 2007-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Emacs Lisp Archive Entry
  6. ;; Filename: org-taskjuggler.el
  7. ;; Author: Christian Egli
  8. ;; Maintainer: Christian Egli
  9. ;; Keywords: org, taskjuggler, project planning
  10. ;; Description: Converts an org-mode buffer into a taskjuggler project plan
  11. ;; URL:
  12. ;; This file is part of GNU Emacs.
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;; Commentary:
  24. ;;
  25. ;; This library implements a TaskJuggler exporter for org-mode.
  26. ;; TaskJuggler uses a text format to define projects, tasks and
  27. ;; resources, so it is a natural fit for org-mode. It can produce all
  28. ;; sorts of reports for tasks or resources in either HTML, CSV or PDF.
  29. ;; The current version of TaskJuggler requires KDE but the next
  30. ;; version is implemented in Ruby and should therefore run on any
  31. ;; platform.
  32. ;;
  33. ;; The exporter is a bit different from other exporters, such as the
  34. ;; HTML and LaTeX exporters for example, in that it does not export
  35. ;; all the nodes of a document or strictly follow the order of the
  36. ;; nodes in the document.
  37. ;;
  38. ;; Instead the TaskJuggler exporter looks for a tree that defines the
  39. ;; tasks and a optionally tree that defines the resources for this
  40. ;; project. It then creates a TaskJuggler file based on these trees
  41. ;; and the attributes defined in all the nodes.
  42. ;;
  43. ;; * Installation
  44. ;;
  45. ;; Put this file into your load-path and the following line into your
  46. ;; ~/.emacs:
  47. ;;
  48. ;; (require 'org-taskjuggler)
  49. ;;
  50. ;; The interactive functions are similar to those of the HTML and LaTeX
  51. ;; exporters:
  52. ;;
  53. ;; M-x `org-export-as-taskjuggler'
  54. ;; M-x `org-export-as-taskjuggler-and-open'
  55. ;;
  56. ;; * Tasks
  57. ;;
  58. ;; Let's illustrate the usage with a small example. Create your tasks
  59. ;; as you usually do with org-mode. Assign efforts to each task using
  60. ;; properties (it's easiest to do this in the column view). You should
  61. ;; end up with something similar to the example by Peter Jones in
  62. ;; http://www.contextualdevelopment.com/static/artifacts/articles/2008/project-planning/project-planning.org.
  63. ;; Now mark the top node of your tasks with a tag named
  64. ;; "taskjuggler_project" (or whatever you customized
  65. ;; `org-export-taskjuggler-project-tag' to). You are now ready to
  66. ;; export the project plan with `org-export-as-taskjuggler-and-open'
  67. ;; which will export the project plan and open a Gantt chart in
  68. ;; TaskJugglerUI.
  69. ;;
  70. ;; * Resources
  71. ;;
  72. ;; Next you can define resources and assign those to work on specific
  73. ;; tasks. You can group your resources hierarchically. Tag the top
  74. ;; node of the resources with "taskjuggler_resource" (or whatever you
  75. ;; customized `org-export-taskjuggler-resource-tag' to). You can
  76. ;; optionally assign an identifier (named "resource_id") to the
  77. ;; resources (using the standard org properties commands) or you can
  78. ;; let the exporter generate identifiers automatically (the exporter
  79. ;; picks the first word of the headline as the identifier as long as
  80. ;; it is unique, see the documentation of
  81. ;; `org-taskjuggler-get-unique-id'). Using that identifier you can
  82. ;; then allocate resources to tasks. This is again done with the
  83. ;; "allocate" property on the tasks. Do this in column view or when on
  84. ;; the task type
  85. ;;
  86. ;; C-c C-x p allocate RET <resource_id> RET
  87. ;;
  88. ;; Once the allocations are done you can again export to TaskJuggler
  89. ;; and check in the Resource Allocation Graph which person is working
  90. ;; on what task at what time.
  91. ;;
  92. ;; * Export of properties
  93. ;;
  94. ;; The exporter also takes TODO state information into consideration,
  95. ;; i.e. if a task is marked as done it will have the corresponding
  96. ;; attribute in TaskJuggler ("complete 100"). Also it will export any
  97. ;; property on a task resource or resource node which is known to
  98. ;; TaskJuggler, such as limits, vacation, shift, booking, efficiency,
  99. ;; journalentry, rate for resources or account, start, note, duration,
  100. ;; end, journalentry, milestone, reference, responsible, scheduling,
  101. ;; etc for tasks.
  102. ;;
  103. ;; * Dependencies
  104. ;;
  105. ;; The exporter will handle dependencies that are defined in the tasks
  106. ;; either with the ORDERED attribute (see TODO dependencies in the Org
  107. ;; mode manual) or with the BLOCKER attribute (see org-depend.el) or
  108. ;; alternatively with a depends attribute. Both the BLOCKER and the
  109. ;; depends attribute can be either "previous-sibling" or a reference
  110. ;; to an identifier (named "task_id") which is defined for another
  111. ;; task in the project. BLOCKER and the depends attribute can define
  112. ;; multiple dependencies separated by either space or comma. You can
  113. ;; also specify optional attributes on the dependency by simply
  114. ;; appending it. The following examples should illustrate this:
  115. ;;
  116. ;; * Training material
  117. ;; :PROPERTIES:
  118. ;; :task_id: training_material
  119. ;; :ORDERED: t
  120. ;; :END:
  121. ;; ** Markup Guidelines
  122. ;; :PROPERTIES:
  123. ;; :Effort: 2d
  124. ;; :END:
  125. ;; ** Workflow Guidelines
  126. ;; :PROPERTIES:
  127. ;; :Effort: 2d
  128. ;; :END:
  129. ;; * Presentation
  130. ;; :PROPERTIES:
  131. ;; :Effort: 2d
  132. ;; :BLOCKER: training_material { gapduration 1d } some_other_task
  133. ;; :END:
  134. ;;
  135. ;;;; * TODO
  136. ;; - Use SCHEDULED and DEADLINE information (not just start and end
  137. ;; properties).
  138. ;; - Look at org-file-properties, org-global-properties and
  139. ;; org-global-properties-fixed
  140. ;; - What about property inheritance and org-property-inherit-p?
  141. ;; - Use TYPE_TODO as an way to assign resources
  142. ;; - Make sure multiple dependency definitions (i.e. BLOCKER on
  143. ;; previous-sibling and on a specific task_id) in multiple
  144. ;; attributes are properly exported.
  145. ;;
  146. ;;; Code:
  147. (eval-when-compile
  148. (require 'cl))
  149. (require 'org)
  150. (require 'org-exp)
  151. ;;; User variables:
  152. (defgroup org-export-taskjuggler nil
  153. "Options for exporting Org-mode files to TaskJuggler."
  154. :tag "Org Export TaskJuggler"
  155. :group 'org-export)
  156. (defcustom org-export-taskjuggler-extension ".tjp"
  157. "Extension of TaskJuggler files."
  158. :group 'org-export-taskjuggler
  159. :version "24.1"
  160. :type 'string)
  161. (defcustom org-export-taskjuggler-project-tag "taskjuggler_project"
  162. "Tag, property or todo used to find the tree containing all
  163. the tasks for the project."
  164. :group 'org-export-taskjuggler
  165. :version "24.1"
  166. :type 'string)
  167. (defcustom org-export-taskjuggler-resource-tag "taskjuggler_resource"
  168. "Tag, property or todo used to find the tree containing all the
  169. resources for the project."
  170. :group 'org-export-taskjuggler
  171. :version "24.1"
  172. :type 'string)
  173. (defcustom org-export-taskjuggler-target-version 2.4
  174. "Which version of TaskJuggler the exporter is targeting."
  175. :group 'org-export-taskjuggler
  176. :version "24.1"
  177. :type 'number)
  178. (defcustom org-export-taskjuggler-default-project-version "1.0"
  179. "Default version string for the project."
  180. :group 'org-export-taskjuggler
  181. :version "24.1"
  182. :type 'string)
  183. (defcustom org-export-taskjuggler-default-project-duration 280
  184. "Default project duration if no start and end date have been defined
  185. in the root node of the task tree, i.e. the tree that has been marked
  186. with `org-export-taskjuggler-project-tag'"
  187. :group 'org-export-taskjuggler
  188. :version "24.1"
  189. :type 'integer)
  190. (defcustom org-export-taskjuggler-default-reports
  191. '("taskreport \"Gantt Chart\" {
  192. headline \"Project Gantt Chart\"
  193. columns hierarchindex, name, start, end, effort, duration, completed, chart
  194. timeformat \"%Y-%m-%d\"
  195. hideresource 1
  196. loadunit shortauto
  197. }"
  198. "resourcereport \"Resource Graph\" {
  199. headline \"Resource Allocation Graph\"
  200. columns no, name, utilization, freeload, chart
  201. loadunit shortauto
  202. sorttasks startup
  203. hidetask ~isleaf()
  204. }")
  205. "Default reports for the project."
  206. :group 'org-export-taskjuggler
  207. :version "24.1"
  208. :type '(repeat (string :tag "Report")))
  209. (defcustom org-export-taskjuggler-default-global-properties
  210. "shift s40 \"Part time shift\" {
  211. workinghours wed, thu, fri off
  212. }
  213. "
  214. "Default global properties for the project. Here you typically
  215. define global properties such as shifts, accounts, rates,
  216. vacation, macros and flags. Any property that is allowed within
  217. the TaskJuggler file can be inserted. You could for example
  218. include another TaskJuggler file.
  219. The global properties are inserted after the project declaration
  220. but before any resource and task declarations."
  221. :group 'org-export-taskjuggler
  222. :version "24.1"
  223. :type '(string :tag "Preamble"))
  224. (defcustom org-export-taskjuggler-valid-task-attributes
  225. '(account start note duration endbuffer endcredit end
  226. flags journalentry length limits maxend maxstart minend
  227. minstart period reference responsible scheduling
  228. startbuffer startcredit statusnote)
  229. "Valid attributes for Taskjuggler tasks. If one of these
  230. appears as a property for a headline, it will be exported with
  231. the corresponding task."
  232. :group 'org-export-taskjuggler)
  233. (defcustom org-export-taskjuggler-valid-resource-attributes
  234. '(limits vacation shift booking efficiency journalentry rate
  235. workinghours flags)
  236. "Valid attributes for Taskjuggler resources. If one of these
  237. appears as a property for a headline, it will be exported with
  238. the corresponding resource."
  239. :group 'org-export-taskjuggler)
  240. ;;; Hooks
  241. (defvar org-export-taskjuggler-final-hook nil
  242. "Hook run at the end of TaskJuggler export, in the new buffer.")
  243. ;;; Autoload functions:
  244. ;; avoid compiler warning about free variable
  245. (defvar org-export-taskjuggler-old-level)
  246. ;;;###autoload
  247. (defun org-export-as-taskjuggler ()
  248. "Export parts of the current buffer as a TaskJuggler file.
  249. The exporter looks for a tree with tag, property or todo that
  250. matches `org-export-taskjuggler-project-tag' and takes this as
  251. the tasks for this project. The first node of this tree defines
  252. the project properties such as project name and project period.
  253. If there is a tree with tag, property or todo that matches
  254. `org-export-taskjuggler-resource-tag' this three is taken as
  255. resources for the project. If no resources are specified, a
  256. default resource is created and allocated to the project. Also
  257. the taskjuggler project will be created with default reports as
  258. defined in `org-export-taskjuggler-default-reports'."
  259. (interactive)
  260. (message "Exporting...")
  261. (setq-default org-done-keywords org-done-keywords)
  262. (let* ((tasks
  263. (org-taskjuggler-resolve-dependencies
  264. (org-taskjuggler-assign-task-ids
  265. (org-taskjuggler-compute-task-leafiness
  266. (org-map-entries
  267. 'org-taskjuggler-components
  268. org-export-taskjuggler-project-tag nil 'archive 'comment)))))
  269. (resources
  270. (org-taskjuggler-assign-resource-ids
  271. (org-map-entries
  272. 'org-taskjuggler-components
  273. org-export-taskjuggler-resource-tag nil 'archive 'comment)))
  274. (filename (expand-file-name
  275. (concat
  276. (file-name-sans-extension
  277. (file-name-nondirectory buffer-file-name))
  278. org-export-taskjuggler-extension)))
  279. (buffer (find-file-noselect filename))
  280. (old-buffer (current-buffer))
  281. (org-export-taskjuggler-old-level 0)
  282. task resource)
  283. (unless tasks
  284. (error "No tasks specified"))
  285. ;; add a default resource
  286. (unless resources
  287. (setq resources
  288. `((("resource_id" . ,(user-login-name))
  289. ("headline" . ,user-full-name)
  290. ("level" . 1)))))
  291. ;; add a default allocation to the first task if none was given
  292. (unless (assoc "allocate" (car tasks))
  293. (let ((task (car tasks))
  294. (resource-id (cdr (assoc "resource_id" (car resources)))))
  295. (setcar tasks (push (cons "allocate" resource-id) task))))
  296. ;; add a default start date to the first task if none was given
  297. (unless (assoc "start" (car tasks))
  298. (let ((task (car tasks))
  299. (time-string (format-time-string "%Y-%m-%d")))
  300. (setcar tasks (push (cons "start" time-string) task))))
  301. ;; add a default version if none was given
  302. (unless (assoc "version" (car tasks))
  303. (let ((task (car tasks))
  304. (version org-export-taskjuggler-default-project-version))
  305. (setcar tasks (push (cons "version" version) task))))
  306. (with-current-buffer buffer
  307. (erase-buffer)
  308. (org-clone-local-variables old-buffer "^org-")
  309. (org-taskjuggler-open-project (car tasks))
  310. (insert org-export-taskjuggler-default-global-properties)
  311. (insert "\n")
  312. (dolist (resource resources)
  313. (let ((level (cdr (assoc "level" resource))))
  314. (org-taskjuggler-close-maybe level)
  315. (org-taskjuggler-open-resource resource)
  316. (setq org-export-taskjuggler-old-level level)))
  317. (org-taskjuggler-close-maybe 1)
  318. (setq org-export-taskjuggler-old-level 0)
  319. (dolist (task tasks)
  320. (let ((level (cdr (assoc "level" task))))
  321. (org-taskjuggler-close-maybe level)
  322. (org-taskjuggler-open-task task)
  323. (setq org-export-taskjuggler-old-level level)))
  324. (org-taskjuggler-close-maybe 1)
  325. (org-taskjuggler-insert-reports)
  326. (save-buffer)
  327. (or (org-export-push-to-kill-ring "TaskJuggler")
  328. (message "Exporting... done"))
  329. (current-buffer))))
  330. ;;;###autoload
  331. (defun org-export-as-taskjuggler-and-open ()
  332. "Export the current buffer as a TaskJuggler file and open it
  333. with the TaskJuggler GUI."
  334. (interactive)
  335. (let* ((file-name (buffer-file-name (org-export-as-taskjuggler)))
  336. (process-name "TaskJugglerUI")
  337. (command (concat process-name " " file-name)))
  338. (start-process-shell-command process-name nil command)))
  339. (defun org-taskjuggler-targeting-tj3-p ()
  340. "Return true if we are targeting TaskJuggler III."
  341. (>= org-export-taskjuggler-target-version 3.0))
  342. (defun org-taskjuggler-parent-is-ordered-p ()
  343. "Return true if the parent of the current node has a property
  344. \"ORDERED\". Return nil otherwise."
  345. (save-excursion
  346. (and (org-up-heading-safe) (org-entry-get (point) "ORDERED"))))
  347. (defun org-taskjuggler-components ()
  348. "Return an alist containing all the pertinent information for
  349. the current node such as the headline, the level, todo state
  350. information, all the properties, etc."
  351. (let* ((props (org-entry-properties))
  352. (components (org-heading-components))
  353. (level (nth 1 components))
  354. (headline
  355. (replace-regexp-in-string
  356. "\"" "\\\"" (nth 4 components) t t)) ; quote double quotes in headlines
  357. (parent-ordered (org-taskjuggler-parent-is-ordered-p)))
  358. (push (cons "level" level) props)
  359. (push (cons "headline" headline) props)
  360. (push (cons "parent-ordered" parent-ordered) props)))
  361. (defun org-taskjuggler-assign-task-ids (tasks)
  362. "Given a list of tasks return the same list assigning a unique id
  363. and the full path to each task. Taskjuggler takes hierarchical ids.
  364. For that reason we have to make ids locally unique and we have to keep
  365. a path to the current task."
  366. (let ((previous-level 0)
  367. unique-ids unique-id
  368. path
  369. task resolved-tasks tmp)
  370. (dolist (task tasks resolved-tasks)
  371. (let ((level (cdr (assoc "level" task))))
  372. (cond
  373. ((< previous-level level)
  374. (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
  375. (dotimes (tmp (- level previous-level))
  376. (push (list unique-id) unique-ids)
  377. (push unique-id path)))
  378. ((= previous-level level)
  379. (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
  380. (push unique-id (car unique-ids))
  381. (setcar path unique-id))
  382. ((> previous-level level)
  383. (dotimes (tmp (- previous-level level))
  384. (pop unique-ids)
  385. (pop path))
  386. (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
  387. (push unique-id (car unique-ids))
  388. (setcar path unique-id)))
  389. (push (cons "unique-id" unique-id) task)
  390. (push (cons "path" (mapconcat 'identity (reverse path) ".")) task)
  391. (setq previous-level level)
  392. (setq resolved-tasks (append resolved-tasks (list task)))))))
  393. (defun org-taskjuggler-compute-task-leafiness (tasks)
  394. "Figure out if each task is a leaf by looking at it's level,
  395. and the level of its successor. If the successor is higher (ie
  396. deeper), then it's not a leaf."
  397. (let (new-list)
  398. (while (car tasks)
  399. (let ((task (car tasks))
  400. (successor (car (cdr tasks))))
  401. (cond
  402. ;; if a task has no successors it is a leaf
  403. ((null successor)
  404. (push (cons (cons "leaf-node" t) task) new-list))
  405. ;; if the successor has a lower level than task it is a leaf
  406. ((<= (cdr (assoc "level" successor)) (cdr (assoc "level" task)))
  407. (push (cons (cons "leaf-node" t) task) new-list))
  408. ;; otherwise examine the rest of the tasks
  409. (t (push task new-list))))
  410. (setq tasks (cdr tasks)))
  411. (nreverse new-list)))
  412. (defun org-taskjuggler-assign-resource-ids (resources)
  413. "Given a list of resources return the same list, assigning a
  414. unique id to each resource."
  415. (let (unique-ids new-list)
  416. (dolist (resource resources new-list)
  417. (let ((unique-id (org-taskjuggler-get-unique-id resource unique-ids)))
  418. (push (cons "unique-id" unique-id) resource)
  419. (push unique-id unique-ids)
  420. (push resource new-list)))
  421. (nreverse new-list)))
  422. (defun org-taskjuggler-resolve-dependencies (tasks)
  423. (let ((previous-level 0)
  424. siblings
  425. task resolved-tasks)
  426. (dolist (task tasks resolved-tasks)
  427. (let* ((level (cdr (assoc "level" task)))
  428. (depends (cdr (assoc "depends" task)))
  429. (parent-ordered (cdr (assoc "parent-ordered" task)))
  430. (blocker (cdr (assoc "BLOCKER" task)))
  431. (blocked-on-previous
  432. (and blocker (string-match "previous-sibling" blocker)))
  433. (dependencies
  434. (org-taskjuggler-resolve-explicit-dependencies
  435. (append
  436. (and depends (org-taskjuggler-tokenize-dependencies depends))
  437. (and blocker (org-taskjuggler-tokenize-dependencies blocker)))
  438. tasks))
  439. previous-sibling)
  440. ; update previous sibling info
  441. (cond
  442. ((< previous-level level)
  443. (dotimes (tmp (- level previous-level))
  444. (push task siblings)))
  445. ((= previous-level level)
  446. (setq previous-sibling (car siblings))
  447. (setcar siblings task))
  448. ((> previous-level level)
  449. (dotimes (tmp (- previous-level level))
  450. (pop siblings))
  451. (setq previous-sibling (car siblings))
  452. (setcar siblings task)))
  453. ; insert a dependency on previous sibling if the parent is
  454. ; ordered or if the tasks has a BLOCKER attribute with value "previous-sibling"
  455. (when (or (and previous-sibling parent-ordered) blocked-on-previous)
  456. (push (format "!%s" (cdr (assoc "unique-id" previous-sibling))) dependencies))
  457. ; store dependency information
  458. (when dependencies
  459. (push (cons "depends" (mapconcat 'identity dependencies ", ")) task))
  460. (setq previous-level level)
  461. (setq resolved-tasks (append resolved-tasks (list task)))))))
  462. (defun org-taskjuggler-tokenize-dependencies (dependencies)
  463. "Split a dependency property value DEPENDENCIES into the
  464. individual dependencies and return them as a list while keeping
  465. the optional arguments (such as gapduration) for the
  466. dependencies. A dependency will have to match `[-a-zA-Z0-9_]+'."
  467. (cond
  468. ((string-match "^ *$" dependencies) nil)
  469. ((string-match "^[ \t]*\\([-a-zA-Z0-9_]+\\([ \t]*{[^}]+}\\)?\\)[ \t,]*" dependencies)
  470. (cons
  471. (substring dependencies (match-beginning 1) (match-end 1))
  472. (org-taskjuggler-tokenize-dependencies (substring dependencies (match-end 0)))))
  473. (t (error (format "invalid dependency id %s" dependencies)))))
  474. (defun org-taskjuggler-resolve-explicit-dependencies (dependencies tasks)
  475. "For each dependency in DEPENDENCIES try to find a
  476. corresponding task with a matching property \"task_id\" in TASKS.
  477. Return a list containing the resolved links for all DEPENDENCIES
  478. where a matching tasks was found. If the dependency is
  479. \"previous-sibling\" it is ignored (as this is dealt with in
  480. `org-taskjuggler-resolve-dependencies'). If there is no matching
  481. task the dependency is ignored and a warning is displayed ."
  482. (unless (null dependencies)
  483. (let*
  484. ;; the dependency might have optional attributes such as "{
  485. ;; gapduration 5d }", so only use the first string as id for the
  486. ;; dependency
  487. ((dependency (car dependencies))
  488. (id (car (split-string dependency)))
  489. (optional-attributes
  490. (mapconcat 'identity (cdr (split-string dependency)) " "))
  491. (path (org-taskjuggler-find-task-with-id id tasks)))
  492. (cond
  493. ;; ignore previous sibling dependencies
  494. ((equal (car dependencies) "previous-sibling")
  495. (org-taskjuggler-resolve-explicit-dependencies (cdr dependencies) tasks))
  496. ;; if the id is found in another task use its path
  497. ((not (null path))
  498. (cons (mapconcat 'identity (list path optional-attributes) " ")
  499. (org-taskjuggler-resolve-explicit-dependencies
  500. (cdr dependencies) tasks)))
  501. ;; warn about dangling dependency but otherwise ignore it
  502. (t (display-warning
  503. 'org-export-taskjuggler
  504. (format "No task with matching property \"task_id\" found for id %s" id))
  505. (org-taskjuggler-resolve-explicit-dependencies (cdr dependencies) tasks))))))
  506. (defun org-taskjuggler-find-task-with-id (id tasks)
  507. "Find ID in tasks. If found return the path of task. Otherwise
  508. return nil."
  509. (let ((task-id (cdr (assoc "task_id" (car tasks))))
  510. (path (cdr (assoc "path" (car tasks)))))
  511. (cond
  512. ((null tasks) nil)
  513. ((equal task-id id) path)
  514. (t (org-taskjuggler-find-task-with-id id (cdr tasks))))))
  515. (defun org-taskjuggler-get-unique-id (item unique-ids)
  516. "Return a unique id for an ITEM which can be a task or a resource.
  517. The id is derived from the headline and made unique against
  518. UNIQUE-IDS. If the (downcased) first token of the headline is not
  519. unique try to add more (downcased) tokens of the headline or
  520. finally add more underscore characters (\"_\")."
  521. (let* ((headline (cdr (assoc "headline" item)))
  522. (parts (split-string headline))
  523. (id (org-taskjuggler-clean-id (downcase (pop parts)))))
  524. ; try to add more parts of the headline to make it unique
  525. (while (and (member id unique-ids) (car parts))
  526. (setq id (concat id "_" (org-taskjuggler-clean-id (downcase (pop parts))))))
  527. ; if its still not unique add "_"
  528. (while (member id unique-ids)
  529. (setq id (concat id "_")))
  530. id))
  531. (defun org-taskjuggler-clean-id (id)
  532. "Clean and return ID to make it acceptable for taskjuggler."
  533. (and id
  534. ;; replace non-ascii by _
  535. (replace-regexp-in-string
  536. "[^a-zA-Z0-9_]" "_"
  537. ;; make sure id doesn't start with a number
  538. (replace-regexp-in-string "^\\([0-9]\\)" "_\\1" id))))
  539. (defun org-taskjuggler-open-project (project)
  540. "Insert the beginning of a project declaration. All valid
  541. attributes from the PROJECT alist are inserted. If no end date is
  542. specified it is calculated
  543. `org-export-taskjuggler-default-project-duration' days from now."
  544. (let* ((unique-id (cdr (assoc "unique-id" project)))
  545. (headline (cdr (assoc "headline" project)))
  546. (version (cdr (assoc "version" project)))
  547. (start (cdr (assoc "start" project)))
  548. (end (cdr (assoc "end" project))))
  549. (insert
  550. (format "project %s \"%s\" \"%s\" %s +%sd {\n }\n"
  551. unique-id headline version start
  552. org-export-taskjuggler-default-project-duration))))
  553. (defun org-taskjuggler-filter-and-join (items)
  554. "Filter all nil elements from ITEMS and join the remaining ones
  555. with separator \"\n\"."
  556. (let ((filtered-items (remq nil items)))
  557. (and filtered-items (mapconcat 'identity filtered-items "\n"))))
  558. (defun org-taskjuggler-get-attributes (item attributes)
  559. "Return all attribute as a single formatted string. ITEM is an
  560. alist representing either a resource or a task. ATTRIBUTES is a
  561. list of symbols. Only entries from ITEM are considered that are
  562. listed in ATTRIBUTES."
  563. (org-taskjuggler-filter-and-join
  564. (mapcar
  565. (lambda (attribute)
  566. (org-taskjuggler-filter-and-join
  567. (org-taskjuggler-get-attribute item attribute)))
  568. attributes)))
  569. (defun org-taskjuggler-get-attribute (item attribute)
  570. "Return a list of strings containing the properly formatted
  571. taskjuggler declaration for a given ATTRIBUTE in ITEM (an alist).
  572. If the ATTRIBUTE is not in ITEM return nil."
  573. (cond
  574. ((null item) nil)
  575. ((equal (symbol-name attribute) (car (car item)))
  576. (cons (format "%s %s" (symbol-name attribute) (cdr (car item)))
  577. (org-taskjuggler-get-attribute (cdr item) attribute)))
  578. (t (org-taskjuggler-get-attribute (cdr item) attribute))))
  579. (defun org-taskjuggler-open-resource (resource)
  580. "Insert the beginning of a resource declaration. All valid
  581. attributes from the RESOURCE alist are inserted. If the RESOURCE
  582. defines a property \"resource_id\" it will be used as the id for
  583. this resource. Otherwise it will use the ID property. If neither
  584. is defined it will calculate a unique id for the resource using
  585. `org-taskjuggler-get-unique-id'."
  586. (let ((id (org-taskjuggler-clean-id
  587. (or (cdr (assoc "resource_id" resource))
  588. (cdr (assoc "ID" resource))
  589. (cdr (assoc "unique-id" resource)))))
  590. (headline (cdr (assoc "headline" resource)))
  591. (attributes org-export-taskjuggler-valid-resource-attributes))
  592. (insert
  593. (concat
  594. "resource " id " \"" headline "\" {\n "
  595. (org-taskjuggler-get-attributes resource attributes) "\n"))))
  596. (defun org-taskjuggler-clean-effort (effort)
  597. "Translate effort strings into a format acceptable to taskjuggler,
  598. i.e. REAL UNIT. A valid effort string can be anything that is
  599. accepted by `org-duration-string-to-minutes´."
  600. (cond
  601. ((null effort) effort)
  602. (t (let* ((minutes (org-duration-string-to-minutes effort))
  603. (hours (/ minutes 60.0)))
  604. (format "%.1fh" hours)))))
  605. (defun org-taskjuggler-get-priority (priority)
  606. "Return a priority between 1 and 1000 based on PRIORITY, an
  607. org-mode priority string."
  608. (max 1 (/ (* 1000 (- org-lowest-priority (string-to-char priority)))
  609. (- org-lowest-priority org-highest-priority))))
  610. (defun org-taskjuggler-open-task (task)
  611. (let* ((unique-id (cdr (assoc "unique-id" task)))
  612. (headline (cdr (assoc "headline" task)))
  613. (effort (org-taskjuggler-clean-effort (cdr (assoc org-effort-property task))))
  614. (depends (cdr (assoc "depends" task)))
  615. (allocate (cdr (assoc "allocate" task)))
  616. (priority-raw (cdr (assoc "PRIORITY" task)))
  617. (priority (and priority-raw (org-taskjuggler-get-priority priority-raw)))
  618. (state (cdr (assoc "TODO" task)))
  619. (complete (or (and (member state org-done-keywords) "100")
  620. (cdr (assoc "complete" task))))
  621. (parent-ordered (cdr (assoc "parent-ordered" task)))
  622. (previous-sibling (cdr (assoc "previous-sibling" task)))
  623. (milestone (or (cdr (assoc "milestone" task))
  624. (and (assoc "leaf-node" task)
  625. (not (or effort
  626. (cdr (assoc "duration" task))
  627. (cdr (assoc "end" task))
  628. (cdr (assoc "period" task)))))))
  629. (attributes org-export-taskjuggler-valid-task-attributes))
  630. (insert
  631. (concat
  632. "task " unique-id " \"" headline "\" {\n"
  633. (if (and parent-ordered previous-sibling)
  634. (format " depends %s\n" previous-sibling)
  635. (and depends (format " depends %s\n" depends)))
  636. (and allocate (format " purge %s\n allocate %s\n"
  637. (or (and (org-taskjuggler-targeting-tj3-p) "allocate")
  638. "allocations")
  639. allocate))
  640. (and complete (format " complete %s\n" complete))
  641. (and effort (format " effort %s\n" effort))
  642. (and priority (format " priority %s\n" priority))
  643. (and milestone (format " milestone\n"))
  644. (org-taskjuggler-get-attributes task attributes)
  645. "\n"))))
  646. (defun org-taskjuggler-close-maybe (level)
  647. (while (> org-export-taskjuggler-old-level level)
  648. (insert "}\n")
  649. (setq org-export-taskjuggler-old-level (1- org-export-taskjuggler-old-level)))
  650. (when (= org-export-taskjuggler-old-level level)
  651. (insert "}\n")))
  652. (defun org-taskjuggler-insert-reports ()
  653. (let (report)
  654. (dolist (report org-export-taskjuggler-default-reports)
  655. (insert report "\n"))))
  656. (provide 'org-taskjuggler)
  657. ;;; org-taskjuggler.el ends here