org-taskjuggler.el 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. ;;; org-taskjuggler.el --- TaskJuggler exporter for org-mode
  2. ;;
  3. ;; Copyright (C) 2007-2013 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. ;; - Look at org-file-properties, org-global-properties and
  137. ;; org-global-properties-fixed
  138. ;; - What about property inheritance and org-property-inherit-p?
  139. ;; - Use TYPE_TODO as an way to assign resources
  140. ;; - Make sure multiple dependency definitions (i.e. BLOCKER on
  141. ;; previous-sibling and on a specific task_id) in multiple
  142. ;; attributes are properly exported.
  143. ;;
  144. ;;; Code:
  145. (eval-when-compile
  146. (require 'cl))
  147. (require 'org)
  148. (require 'org-exp)
  149. ;;; User variables:
  150. (defgroup org-export-taskjuggler nil
  151. "Options for exporting Org-mode files to TaskJuggler."
  152. :tag "Org Export TaskJuggler"
  153. :group 'org-export)
  154. (defcustom org-export-taskjuggler-extension ".tjp"
  155. "Extension of TaskJuggler files."
  156. :group 'org-export-taskjuggler
  157. :version "24.1"
  158. :type 'string)
  159. (defcustom org-export-taskjuggler-project-tag "taskjuggler_project"
  160. "Tag, property or todo used to find the tree containing all
  161. the tasks for the project."
  162. :group 'org-export-taskjuggler
  163. :version "24.1"
  164. :type 'string)
  165. (defcustom org-export-taskjuggler-resource-tag "taskjuggler_resource"
  166. "Tag, property or todo used to find the tree containing all the
  167. resources for the project."
  168. :group 'org-export-taskjuggler
  169. :version "24.1"
  170. :type 'string)
  171. (defcustom org-export-taskjuggler-report-tag "taskjuggler_report"
  172. "Tag, property or todo used to find the tree containing all the
  173. reports for the project."
  174. :group 'org-export-taskjuggler
  175. :version "24.1"
  176. :type 'string)
  177. (defcustom org-export-taskjuggler-target-version 2.4
  178. "Which version of TaskJuggler the exporter is targeting."
  179. :group 'org-export-taskjuggler
  180. :version "24.1"
  181. :type 'number)
  182. (defcustom org-export-taskjuggler-default-project-version "1.0"
  183. "Default version string for the project."
  184. :group 'org-export-taskjuggler
  185. :version "24.1"
  186. :type 'string)
  187. (defcustom org-export-taskjuggler-default-project-duration 280
  188. "Default project duration if no start and end date have been defined
  189. in the root node of the task tree, i.e. the tree that has been marked
  190. with `org-export-taskjuggler-project-tag'"
  191. :group 'org-export-taskjuggler
  192. :version "24.1"
  193. :type 'integer)
  194. (defcustom org-export-taskjuggler-default-reports
  195. '("taskreport \"Gantt Chart\" {
  196. headline \"Project Gantt Chart\"
  197. columns hierarchindex, name, start, end, effort, duration, completed, chart
  198. timeformat \"%Y-%m-%d\"
  199. hideresource 1
  200. loadunit shortauto
  201. }"
  202. "resourcereport \"Resource Graph\" {
  203. headline \"Resource Allocation Graph\"
  204. columns no, name, utilization, freeload, chart
  205. loadunit shortauto
  206. sorttasks startup
  207. hidetask ~isleaf()
  208. }")
  209. "Default reports for the project."
  210. :group 'org-export-taskjuggler
  211. :version "24.1"
  212. :type '(repeat (string :tag "Report")))
  213. (defcustom org-export-taskjuggler-default-global-header
  214. ""
  215. "Default global header for the project. This goes before
  216. project declaration, and might be useful for early macros"
  217. :group 'org-export-taskjuggler
  218. :version "24.1"
  219. :type '(string :tag "Preamble"))
  220. (defcustom org-export-taskjuggler-default-global-properties
  221. "shift s40 \"Part time shift\" {
  222. workinghours wed, thu, fri off
  223. }
  224. "
  225. "Default global properties for the project. Here you typically
  226. define global properties such as shifts, accounts, rates,
  227. vacation, macros and flags. Any property that is allowed within
  228. the TaskJuggler file can be inserted. You could for example
  229. include another TaskJuggler file.
  230. The global properties are inserted after the project declaration
  231. but before any resource and task declarations."
  232. :group 'org-export-taskjuggler
  233. :version "24.1"
  234. :type '(string :tag "Preamble"))
  235. (defcustom org-export-taskjuggler-valid-task-attributes
  236. '(account start note duration endbuffer endcredit end
  237. flags journalentry length limits maxend maxstart minend
  238. minstart period reference responsible scheduling
  239. startbuffer startcredit statusnote chargeset charge)
  240. "Valid attributes for Taskjuggler tasks. If one of these
  241. appears as a property for a headline, it will be exported with
  242. the corresponding task."
  243. :group 'org-export-taskjuggler)
  244. (defcustom org-export-taskjuggler-valid-resource-attributes
  245. '(limits vacation shift booking efficiency journalentry rate
  246. workinghours flags)
  247. "Valid attributes for Taskjuggler resources. If one of these
  248. appears as a property for a headline, it will be exported with
  249. the corresponding resource."
  250. :group 'org-export-taskjuggler)
  251. (defcustom org-export-taskjuggler-valid-report-attributes
  252. '(headline columns definitions timeformat hideresource hidetask
  253. loadunit sorttasks formats period)
  254. "Valid attributes for Taskjuggler reports. If one of these
  255. appears as a property for a headline, it will be exported with
  256. the corresponding report."
  257. :group 'org-export-taskjuggler)
  258. (defcustom org-export-taskjuggler-keep-project-as-task t
  259. "Whether to keep the project headline as an umbrella task for
  260. all declared tasks. Setting this to nil will allow maintaining
  261. completely separated task buckets, while still sharing the same
  262. resources pool."
  263. :group 'org-export-taskjuggler
  264. :type 'boolean)
  265. ;;; Hooks
  266. (defvar org-export-taskjuggler-final-hook nil
  267. "Hook run at the end of TaskJuggler export, in the new buffer.")
  268. ;;; Autoload functions:
  269. ;; avoid compiler warning about free variable
  270. (defvar org-export-taskjuggler-old-level)
  271. ;;;###autoload
  272. (defun org-export-as-taskjuggler (&optional arg hidden ext-plist
  273. to-buffer body-only pub-dir)
  274. "Export parts of the current buffer as a TaskJuggler file.
  275. The exporter looks for a tree with tag, property or todo that
  276. matches `org-export-taskjuggler-project-tag' and takes this as
  277. the tasks for this project. The first node of this tree defines
  278. the project properties such as project name and project period.
  279. If there is a tree with tag, property or todo that matches
  280. `org-export-taskjuggler-resource-tag' this three is taken as
  281. resources for the project. If no resources are specified, a
  282. default resource is created and allocated to the project. Also
  283. the taskjuggler project will be created with default reports as
  284. defined in `org-export-taskjuggler-default-reports'."
  285. (interactive "P")
  286. (message "Exporting...")
  287. (setq-default org-done-keywords org-done-keywords)
  288. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  289. ext-plist
  290. (org-infile-export-plist)))
  291. (org-export-opt-plist opt-plist)
  292. (tasks
  293. (org-taskjuggler-resolve-dependencies
  294. (org-taskjuggler-assign-task-ids
  295. (org-taskjuggler-compute-task-leafiness
  296. (org-map-entries
  297. 'org-taskjuggler-components
  298. org-export-taskjuggler-project-tag nil 'archive 'comment)))))
  299. (resources
  300. (org-taskjuggler-assign-resource-ids
  301. (org-map-entries
  302. 'org-taskjuggler-components
  303. org-export-taskjuggler-resource-tag nil 'archive 'comment)))
  304. (reports
  305. (org-map-entries
  306. 'org-taskjuggler-components
  307. org-export-taskjuggler-report-tag nil 'archive 'comment))
  308. (filename (if to-buffer
  309. nil
  310. (concat (file-name-as-directory
  311. (or pub-dir
  312. (org-export-directory :tj opt-plist)))
  313. (file-name-sans-extension
  314. (file-name-nondirectory buffer-file-name))
  315. org-export-taskjuggler-extension)))
  316. (buffer (if to-buffer
  317. (cond
  318. ((eq to-buffer 'string)
  319. (get-buffer-create "*Org Taskjuggler Export*"))
  320. (t (get-buffer-create to-buffer)))
  321. (find-file-noselect filename)))
  322. (old-buffer (current-buffer))
  323. (org-export-taskjuggler-old-level 0)
  324. task resource)
  325. (unless tasks
  326. (error "No tasks specified"))
  327. ;; add a default resource
  328. (unless resources
  329. (setq resources
  330. `((("resource_id" . ,(user-login-name))
  331. ("HEADLINE" . ,user-full-name)
  332. ("level" . 1)))))
  333. ;; add a default allocation to the first task if none was given
  334. (unless (assoc "allocate" (car tasks))
  335. (let ((task (car tasks))
  336. (resource-id (cdr (assoc "resource_id" (car resources)))))
  337. (setcar tasks (push (cons "allocate" resource-id) task))))
  338. ;; add a default start date to the first task if none was given
  339. (unless (assoc "start" (car tasks))
  340. (let ((task (car tasks))
  341. (time-string (format-time-string "%Y-%m-%d")))
  342. (setcar tasks (push (cons "start" time-string) task))))
  343. ;; add a default version if none was given
  344. (unless (assoc "version" (car tasks))
  345. (let ((task (car tasks))
  346. (version org-export-taskjuggler-default-project-version))
  347. (setcar tasks (push (cons "version" version) task))))
  348. (with-current-buffer buffer
  349. (erase-buffer)
  350. (org-install-letbind)
  351. ;; create local variables for all options, to make sure all called
  352. ;; functions get the correct information
  353. (mapc (lambda (x)
  354. (set (make-local-variable (nth 2 x))
  355. (plist-get opt-plist (car x))))
  356. org-export-plist-vars)
  357. (org-clone-local-variables old-buffer "^org-")
  358. (insert org-export-taskjuggler-default-global-header)
  359. (org-taskjuggler-open-project
  360. (if org-export-taskjuggler-keep-project-as-task
  361. (car tasks)
  362. (pop tasks)))
  363. (insert org-export-taskjuggler-default-global-properties)
  364. (insert "\n")
  365. (dolist (resource resources)
  366. (let ((level (cdr (assoc "level" resource))))
  367. (org-taskjuggler-close-maybe level)
  368. (org-taskjuggler-open-resource resource)
  369. (setq org-export-taskjuggler-old-level level)))
  370. (org-taskjuggler-close-maybe 1)
  371. (setq org-export-taskjuggler-old-level 0)
  372. (dolist (task tasks)
  373. (let ((level (cdr (assoc "level" task))))
  374. (org-taskjuggler-close-maybe level)
  375. (org-taskjuggler-open-task task)
  376. (setq org-export-taskjuggler-old-level level)))
  377. (org-taskjuggler-close-maybe
  378. (if org-export-taskjuggler-keep-project-as-task
  379. 1 2))
  380. (org-taskjuggler-insert-reports reports)
  381. (or to-buffer (save-buffer))
  382. (or (org-export-push-to-kill-ring "TaskJuggler")
  383. (message "Exporting... done"))
  384. (if (eq to-buffer 'string)
  385. (prog1 (buffer-substring (point-min) (point-max))
  386. (kill-buffer (current-buffer)))
  387. (current-buffer)))))
  388. ;;;###autoload
  389. (defun org-export-as-taskjuggler-and-open ()
  390. "Export the current buffer as a TaskJuggler file and open it
  391. with the TaskJuggler GUI."
  392. (interactive)
  393. (let* ((file-name (buffer-file-name (org-export-as-taskjuggler)))
  394. (process-name "TaskJugglerUI")
  395. (command (concat process-name " " file-name)))
  396. (start-process-shell-command process-name nil command)))
  397. (defun org-taskjuggler-targeting-tj3-p ()
  398. "Return true if we are targeting TaskJuggler III."
  399. (>= org-export-taskjuggler-target-version 3.0))
  400. (defun org-taskjuggler-parent-is-ordered-p ()
  401. "Return true if the parent of the current node has a property
  402. \"ORDERED\". Return nil otherwise."
  403. (save-excursion
  404. (and (org-up-heading-safe) (org-entry-get (point) "ORDERED"))))
  405. (defun org-taskjuggler-date (date)
  406. (let ((time (parse-time-string date)))
  407. (format "%d-%02d-%02d" (nth 5 time) (nth 4 time) (nth 3 time))))
  408. (defun org-taskjuggler-components ()
  409. "Return an alist containing all the pertinent information for
  410. the current node such as the headline, the level, todo state
  411. information, all the properties, etc."
  412. (let* ((props (org-entry-properties))
  413. (components (org-heading-components))
  414. (level (nth 1 components))
  415. (headline
  416. (replace-regexp-in-string
  417. "\"" "\\\"" (nth 4 components) t t)) ; quote double quotes in headlines
  418. (parent-ordered (org-taskjuggler-parent-is-ordered-p)))
  419. (let ((scheduled (assoc "SCHEDULED" props))
  420. (deadline (assoc "DEADLINE" props)))
  421. (when scheduled
  422. (push (cons "start" (org-taskjuggler-date (cdr scheduled))) props))
  423. (when deadline
  424. (push (cons "end" (org-taskjuggler-date (cdr deadline))) props)))
  425. (push (cons "level" level) props)
  426. (push (cons "HEADLINE" headline) props)
  427. (push (cons "parent-ordered" parent-ordered) props)))
  428. (defun org-taskjuggler-assign-task-ids (tasks)
  429. "Given a list of tasks return the same list assigning a unique id
  430. and the full path to each task. Taskjuggler takes hierarchical ids.
  431. For that reason we have to make ids locally unique and we have to keep
  432. a path to the current task."
  433. (let ((previous-level 0)
  434. unique-ids unique-id
  435. path
  436. task resolved-tasks tmp)
  437. (dolist (task tasks resolved-tasks)
  438. (let ((level (cdr (assoc "level" task))))
  439. (cond
  440. ((< previous-level level)
  441. (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
  442. (dotimes (tmp (- level previous-level))
  443. (push (list unique-id) unique-ids)
  444. (push unique-id path)))
  445. ((= previous-level level)
  446. (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
  447. (push unique-id (car unique-ids))
  448. (setcar path unique-id))
  449. ((> previous-level level)
  450. (dotimes (tmp (- previous-level level))
  451. (pop unique-ids)
  452. (pop path))
  453. (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
  454. (push unique-id (car unique-ids))
  455. (setcar path unique-id)))
  456. (push (cons "unique-id" unique-id) task)
  457. (push (cons "path"
  458. (mapconcat 'identity
  459. (if org-export-taskjuggler-keep-project-as-task
  460. (reverse path)
  461. (cdr (reverse path))) ".")) task)
  462. (setq previous-level level)
  463. (setq resolved-tasks (append resolved-tasks (list task)))))))
  464. (defun org-taskjuggler-compute-task-leafiness (tasks)
  465. "Figure out if each task is a leaf by looking at it's level,
  466. and the level of its successor. If the successor is higher (ie
  467. deeper), then it's not a leaf."
  468. (let (new-list)
  469. (while (car tasks)
  470. (let ((task (car tasks))
  471. (successor (car (cdr tasks))))
  472. (cond
  473. ;; if a task has no successors it is a leaf
  474. ((null successor)
  475. (push (cons (cons "leaf-node" t) task) new-list))
  476. ;; if the successor has a lower level than task it is a leaf
  477. ((<= (cdr (assoc "level" successor)) (cdr (assoc "level" task)))
  478. (push (cons (cons "leaf-node" t) task) new-list))
  479. ;; otherwise examine the rest of the tasks
  480. (t (push task new-list))))
  481. (setq tasks (cdr tasks)))
  482. (nreverse new-list)))
  483. (defun org-taskjuggler-assign-resource-ids (resources)
  484. "Given a list of resources return the same list, assigning a
  485. unique id to each resource."
  486. (let (unique-ids new-list)
  487. (dolist (resource resources new-list)
  488. (let ((unique-id (org-taskjuggler-get-unique-id resource unique-ids)))
  489. (push (cons "unique-id" unique-id) resource)
  490. (push unique-id unique-ids)
  491. (push resource new-list)))
  492. (nreverse new-list)))
  493. (defun org-taskjuggler-resolve-dependencies (tasks)
  494. (let ((previous-level 0)
  495. siblings
  496. task resolved-tasks)
  497. (dolist (task tasks resolved-tasks)
  498. (let* ((level (cdr (assoc "level" task)))
  499. (depends (cdr (assoc "depends" task)))
  500. (parent-ordered (cdr (assoc "parent-ordered" task)))
  501. (blocker (cdr (assoc "BLOCKER" task)))
  502. (blocked-on-previous
  503. (and blocker (string-match "previous-sibling" blocker)))
  504. (dependencies
  505. (org-taskjuggler-resolve-explicit-dependencies
  506. (append
  507. (and depends (org-taskjuggler-tokenize-dependencies depends))
  508. (and blocker (org-taskjuggler-tokenize-dependencies blocker)))
  509. tasks))
  510. previous-sibling)
  511. ; update previous sibling info
  512. (cond
  513. ((< previous-level level)
  514. (dotimes (tmp (- level previous-level))
  515. (push task siblings)))
  516. ((= previous-level level)
  517. (setq previous-sibling (car siblings))
  518. (setcar siblings task))
  519. ((> previous-level level)
  520. (dotimes (tmp (- previous-level level))
  521. (pop siblings))
  522. (setq previous-sibling (car siblings))
  523. (setcar siblings task)))
  524. ; insert a dependency on previous sibling if the parent is
  525. ; ordered or if the tasks has a BLOCKER attribute with value "previous-sibling"
  526. (when (or (and previous-sibling parent-ordered) blocked-on-previous)
  527. (push (format "!%s" (cdr (assoc "unique-id" previous-sibling))) dependencies))
  528. ; store dependency information
  529. (when dependencies
  530. (push (cons "depends" (mapconcat 'identity dependencies ", ")) task))
  531. (setq previous-level level)
  532. (setq resolved-tasks (append resolved-tasks (list task)))))))
  533. (defun org-taskjuggler-tokenize-dependencies (dependencies)
  534. "Split a dependency property value DEPENDENCIES into the
  535. individual dependencies and return them as a list while keeping
  536. the optional arguments (such as gapduration) for the
  537. dependencies. A dependency will have to match `[-a-zA-Z0-9_]+'."
  538. (cond
  539. ((string-match "^ *$" dependencies) nil)
  540. ((string-match "^[ \t]*\\([-a-zA-Z0-9_]+\\([ \t]*{[^}]+}\\)?\\)[ \t,]*" dependencies)
  541. (cons
  542. (substring dependencies (match-beginning 1) (match-end 1))
  543. (org-taskjuggler-tokenize-dependencies (substring dependencies (match-end 0)))))
  544. (t (error (format "invalid dependency id %s" dependencies)))))
  545. (defun org-taskjuggler-resolve-explicit-dependencies (dependencies tasks)
  546. "For each dependency in DEPENDENCIES try to find a
  547. corresponding task with a matching property \"task_id\" in TASKS.
  548. Return a list containing the resolved links for all DEPENDENCIES
  549. where a matching tasks was found. If the dependency is
  550. \"previous-sibling\" it is ignored (as this is dealt with in
  551. `org-taskjuggler-resolve-dependencies'). If there is no matching
  552. task the dependency is ignored and a warning is displayed ."
  553. (unless (null dependencies)
  554. (let*
  555. ;; the dependency might have optional attributes such as "{
  556. ;; gapduration 5d }", so only use the first string as id for the
  557. ;; dependency
  558. ((dependency (car dependencies))
  559. (id (car (split-string dependency)))
  560. (optional-attributes
  561. (mapconcat 'identity (cdr (split-string dependency)) " "))
  562. (path (org-taskjuggler-find-task-with-id id tasks)))
  563. (cond
  564. ;; ignore previous sibling dependencies
  565. ((equal (car dependencies) "previous-sibling")
  566. (org-taskjuggler-resolve-explicit-dependencies (cdr dependencies) tasks))
  567. ;; if the id is found in another task use its path
  568. ((not (null path))
  569. (cons (mapconcat 'identity (list path optional-attributes) " ")
  570. (org-taskjuggler-resolve-explicit-dependencies
  571. (cdr dependencies) tasks)))
  572. ;; warn about dangling dependency but otherwise ignore it
  573. (t (display-warning
  574. 'org-export-taskjuggler
  575. (format "No task with matching property \"task_id\" found for id %s" id))
  576. (org-taskjuggler-resolve-explicit-dependencies (cdr dependencies) tasks))))))
  577. (defun org-taskjuggler-find-task-with-id (id tasks)
  578. "Find ID in tasks. If found return the path of task. Otherwise
  579. return nil."
  580. (let ((task-id (cdr (assoc "task_id" (car tasks))))
  581. (path (cdr (assoc "path" (car tasks)))))
  582. (cond
  583. ((null tasks) nil)
  584. ((equal task-id id) path)
  585. (t (org-taskjuggler-find-task-with-id id (cdr tasks))))))
  586. (defun org-taskjuggler-get-unique-id (item unique-ids)
  587. "Return a unique id for an ITEM which can be a task or a resource.
  588. The id is derived from the headline and made unique against
  589. UNIQUE-IDS. If the (downcased) first token of the headline is not
  590. unique try to add more (downcased) tokens of the headline or
  591. finally add more underscore characters (\"_\")."
  592. (let* ((headline (cdr (assoc "HEADLINE" item)))
  593. (parts (split-string headline))
  594. (id (org-taskjuggler-clean-id (downcase (pop parts)))))
  595. ; try to add more parts of the headline to make it unique
  596. (while (and (member id unique-ids) (car parts))
  597. (setq id (concat id "_" (org-taskjuggler-clean-id (downcase (pop parts))))))
  598. ; if its still not unique add "_"
  599. (while (member id unique-ids)
  600. (setq id (concat id "_")))
  601. id))
  602. (defun org-taskjuggler-clean-id (id)
  603. "Clean and return ID to make it acceptable for taskjuggler."
  604. (and id
  605. ;; replace non-ascii by _
  606. (replace-regexp-in-string
  607. "[^a-zA-Z0-9_]" "_"
  608. ;; make sure id doesn't start with a number
  609. (replace-regexp-in-string "^\\([0-9]\\)" "_\\1" id))))
  610. (defun org-taskjuggler-open-project (project)
  611. "Insert the beginning of a project declaration. All valid
  612. attributes from the PROJECT alist are inserted. If no end date is
  613. specified it is calculated
  614. `org-export-taskjuggler-default-project-duration' days from now."
  615. (let* ((unique-id (cdr (assoc "unique-id" project)))
  616. (headline (cdr (assoc "HEADLINE" project)))
  617. (version (cdr (assoc "version" project)))
  618. (start (cdr (assoc "start" project)))
  619. (end (cdr (assoc "end" project))))
  620. (insert
  621. (format "project %s \"%s\" \"%s\" %s %s {\n }\n"
  622. unique-id headline version start
  623. (or (and end (format "- %s" end))
  624. (format "+%sd"
  625. org-export-taskjuggler-default-project-duration))))))
  626. (defun org-taskjuggler-filter-and-join (items)
  627. "Filter all nil elements from ITEMS and join the remaining ones
  628. with separator \"\n\"."
  629. (let ((filtered-items (remq nil items)))
  630. (and filtered-items (mapconcat 'identity filtered-items "\n"))))
  631. (defun org-taskjuggler-get-attributes (item attributes)
  632. "Return all attribute as a single formatted string. ITEM is an
  633. alist representing either a resource or a task. ATTRIBUTES is a
  634. list of symbols. Only entries from ITEM are considered that are
  635. listed in ATTRIBUTES."
  636. (org-taskjuggler-filter-and-join
  637. (mapcar
  638. (lambda (attribute)
  639. (org-taskjuggler-filter-and-join
  640. (org-taskjuggler-get-attribute item attribute)))
  641. attributes)))
  642. (defun org-taskjuggler-get-attribute (item attribute)
  643. "Return a list of strings containing the properly formatted
  644. taskjuggler declaration for a given ATTRIBUTE in ITEM (an alist).
  645. If the ATTRIBUTE is not in ITEM return nil."
  646. (cond
  647. ((null item) nil)
  648. ((equal (symbol-name attribute) (car (car item)))
  649. (cons (format "%s %s" (symbol-name attribute) (cdr (car item)))
  650. (org-taskjuggler-get-attribute (cdr item) attribute)))
  651. (t (org-taskjuggler-get-attribute (cdr item) attribute))))
  652. (defun org-taskjuggler-open-resource (resource)
  653. "Insert the beginning of a resource declaration. All valid
  654. attributes from the RESOURCE alist are inserted. If the RESOURCE
  655. defines a property \"resource_id\" it will be used as the id for
  656. this resource. Otherwise it will use the ID property. If neither
  657. is defined it will calculate a unique id for the resource using
  658. `org-taskjuggler-get-unique-id'."
  659. (let ((id (org-taskjuggler-clean-id
  660. (or (cdr (assoc "resource_id" resource))
  661. (cdr (assoc "ID" resource))
  662. (cdr (assoc "unique-id" resource)))))
  663. (headline (cdr (assoc "HEADLINE" resource)))
  664. (attributes org-export-taskjuggler-valid-resource-attributes))
  665. (insert
  666. (concat
  667. "resource " id " \"" headline "\" {\n "
  668. (org-taskjuggler-get-attributes resource attributes) "\n"))))
  669. (defun org-taskjuggler-clean-effort (effort)
  670. "Translate effort strings into a format acceptable to taskjuggler,
  671. i.e. REAL UNIT. A valid effort string can be anything that is
  672. accepted by `org-duration-string-to-minutes´."
  673. (cond
  674. ((null effort) effort)
  675. (t (let* ((minutes (org-duration-string-to-minutes effort))
  676. (hours (/ minutes 60.0)))
  677. (format "%.1fh" hours)))))
  678. (defun org-taskjuggler-get-priority (priority)
  679. "Return a priority between 1 and 1000 based on PRIORITY, an
  680. org-mode priority string."
  681. (max 1 (/ (* 1000 (- org-lowest-priority (string-to-char priority)))
  682. (- org-lowest-priority org-highest-priority))))
  683. (defun org-taskjuggler-open-task (task)
  684. (let* ((unique-id (cdr (assoc "unique-id" task)))
  685. (headline (cdr (assoc "HEADLINE" task)))
  686. (effort (org-taskjuggler-clean-effort (cdr (assoc org-effort-property task))))
  687. (depends (cdr (assoc "depends" task)))
  688. (allocate (cdr (assoc "allocate" task)))
  689. (priority-raw (cdr (assoc "PRIORITY" task)))
  690. (priority (and priority-raw (org-taskjuggler-get-priority priority-raw)))
  691. (state (cdr (assoc "TODO" task)))
  692. (complete (or (and (member state org-done-keywords) "100")
  693. (cdr (assoc "complete" task))))
  694. (parent-ordered (cdr (assoc "parent-ordered" task)))
  695. (previous-sibling (cdr (assoc "previous-sibling" task)))
  696. (milestone (or (cdr (assoc "milestone" task))
  697. (and (assoc "leaf-node" task)
  698. (not (or effort
  699. (cdr (assoc "length" task))
  700. (cdr (assoc "duration" task))
  701. (and (cdr (assoc "start" task))
  702. (cdr (assoc "end" task)))
  703. (cdr (assoc "period" task)))))))
  704. (attributes org-export-taskjuggler-valid-task-attributes))
  705. (insert
  706. (concat
  707. "task " unique-id " \"" headline "\" {\n"
  708. (if (and parent-ordered previous-sibling)
  709. (format " depends %s\n" previous-sibling)
  710. (and depends (format " depends %s\n" depends)))
  711. (and allocate (format " purge %s\n allocate %s\n"
  712. (or (and (org-taskjuggler-targeting-tj3-p) "allocate")
  713. "allocations")
  714. allocate))
  715. (and complete (format " complete %s\n" complete))
  716. (and effort (format " effort %s\n" effort))
  717. (and priority (format " priority %s\n" priority))
  718. (and milestone (format " milestone\n"))
  719. (org-taskjuggler-get-attributes task attributes)
  720. "\n"))))
  721. (defun org-taskjuggler-open-report (report)
  722. (let* ((kind (or (cdr (assoc "report-kind" report)) "taskreport"))
  723. (headline (cdr (assoc "HEADLINE" report)))
  724. (attributes org-export-taskjuggler-valid-report-attributes))
  725. (insert
  726. (concat
  727. kind " \"" headline "\" {\n"
  728. (org-taskjuggler-get-attributes report attributes)
  729. "\n}\n"))))
  730. (defun org-taskjuggler-close-maybe (level)
  731. (while (> org-export-taskjuggler-old-level level)
  732. (insert "}\n")
  733. (setq org-export-taskjuggler-old-level (1- org-export-taskjuggler-old-level)))
  734. (when (= org-export-taskjuggler-old-level level)
  735. (insert "}\n")))
  736. (defun org-taskjuggler-insert-reports (reports)
  737. (if reports
  738. (dolist (report (cdr reports))
  739. (org-taskjuggler-open-report report))
  740. (let (report)
  741. (dolist (report org-export-taskjuggler-default-reports)
  742. (insert report "\n")))))
  743. (provide 'org-taskjuggler)
  744. ;; Local variables:
  745. ;; generated-autoload-file: "org-loaddefs.el"
  746. ;; End:
  747. ;;; org-taskjuggler.el ends here