(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]]
   [control
    ["[0]" try (.only Try)]
    ["[0]" exception]
    [concurrency
     ["[0]" async (.only Async)]]]
   [data
    ["[0]" binary
     [\ (.only Binary)]
     ["[1]" \\format]]
    [text
     [encoding
      ["[0]" utf8]]]
    [collection
     ["[0]" set]]
    [format
     ["[0]" tar]
     ["[0]" xml]]]
   [meta
    [compiler
     [meta
      ["[0]" export]]]]
   [world
    ["[0]" file]
    ["[0]" console (.only Console)]]]]
 ["[0]" //
  ["[1][0]" clean]
  ["/[1]" //
   ["/" profile (.only Profile)]
   ["[1][0]" action (.only Action)]
   ["[1][0]" command (.only Command)]
   ["[1][0]" local]
   ["[1][0]" pom]
   ["[1][0]" package]
   [repository (.only Repository)
    ["[1][0]" origin]]
   ["[1][0]" dependency
    ["[1]/[0]" deployment]
    ["[1]/[0]" status]]
   ["[1][0]" artifact (.only Artifact)
    ["[1]/[0]" type]]]])

(def .public success
  "Successfully installed the project locally.")

(def .public failure
  "Failure: No 'identity' defined for the project.")

(def .public (do! console fs local profile)
  (-> (Console Async) (file.System Async) (Repository Async) (Command Any))
  (when (the /.#identity profile)
    {.#Some identity}
    (do ///action.monad
      [package (|> profile
                   (the /.#sources)
                   set.list
                   (export.library fs))
       pom (|> profile
               ///pom.write
               (at async.monad in))
       _ (///dependency/deployment.one local
                                       [identity ///artifact/type.lux_library]
                                       (let [pom_data (|> pom
                                                          (at xml.codec encoded)
                                                          (at utf8.codec encoded))]
                                         [///package.#origin {///origin.#Local ""}
                                          ///package.#library (let [library (binary.result tar.format package)]
                                                                [library (///dependency/status.verified library)])
                                          ///package.#pom [pom
                                                           pom_data
                                                           (///dependency/status.verified pom_data)]]))]
      (is (Async (Try Any))
          (console.write_line ..success console)))

    _
    (is (Async (Try Any))
        (console.write_line ..failure console))))
