;; Task Runner Example
;; Demonstrates simple dependency resolution and execution ordering

(import assert :refer :all)
(import binding :refer :all)
(import control :refer :all)
(import dict :refer :all)
(import display :refer :all)
(import looping :refer :all)
(import threading :refer :all)

(define contains?
  (lambda (lst value)
    (any? (lambda (x) (= x value)) lst)))

(define tasks
  (list
   (dict :name "clean" :deps (list))
   (dict :name "compile" :deps (list "clean"))
   (dict :name "test" :deps (list "compile"))
   (dict :name "package" :deps (list "test"))
   (dict :name "deploy" :deps (list "package"))))

(define task-by-name
  (lambda (name)
    (car (filter (lambda (t) (= (get "name" t) name)) tasks))))

(define visited (list))
(define run-order (list))

(define visit
  (lambda (name)
    (if (contains? visited name)
        #nil
        (begin
          (set! visited (append visited (list name)))
          (define task (task-by-name name))
          (define deps (get "deps" task))
          (do-list (dep deps)
            (visit dep))
          (set! run-order (append run-order (list name)))))))

(println "=== Task Runner ===")
(do-list (t tasks)
  (visit (get "name" t)))
(assert-equal (list "clean" "compile" "test" "package" "deploy") run-order)
(assert-equal 5 (length run-order))
(println (string-concat "Execution order: " run-order))

(println "\nExample complete!")
