             [leiningen.leinsync.namespaces :as ns]
             [leiningen.leinsync.project-reader :as pr]
             [leiningen.leinsync.utils :as u])
-  (:import (java.io File)))
+  (:import (java.io File)
+           (org.apache.commons.io FileUtils)))
 
 (def package-def [:ns-sync :packages])
 

(defn write-package-files-from-source-to-target-project [folder-name package-path src-package-files target-project]
  (try
    (doseq [^File src-package-file src-package-files]
      (update-file target-project folder-name package-path src-package-file))
    (catch Exception e
      (m/info "**** [Error] when updating a file of the folder:" folder-name (.getMessage e))
      (if @u/DEBUG-MODE (m/info e)))))

  (io/make-parents to-file)
  (spit to-file (slurp from-file)))

(defn write-to-folder! [to-folder from-folder]
  (io/make-parents to-folder)
  (FileUtils/copyDirectory from-folder to-folder))

(defn update-package-file! [target-project folder-name package-path ^File src-package-file]
  (let [src-package-file-name (.getName src-package-file)]
    (m/info "*** Update namespace" src-package-file-name)
    (-> (pr/->target-project-path target-project)
        (str "/" folder-name "/" package-path "/" src-package-file-name)
        (io/file)
        (write-to-file! src-package-file))))

(defn update-sub-package! [target-project folder-name package-path ^File src-package-folder]
  (m/info "*** Update sub package" (.getName src-package-folder) "on the folder" folder-name)
  (-> (pr/->target-project-path target-project)
      (str "/" folder-name "/" package-path "/" (.getName src-package-folder))
      (io/file)
      (write-to-folder! src-package-folder)))

(defn update-package-entry! [target-project folder-name package-path ^File src-package-file]
  (if (.isDirectory src-package-file)
    (update-sub-package! target-project folder-name package-path ^File src-package-file)
    (update-package-file! target-project folder-name package-path ^File src-package-file)))

(defn delete-package-files-of-target-project [target-project folder-name package-path]
  (try
