This repository has been archived by the owner on Jun 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
tar.clj
100 lines (86 loc) · 2.96 KB
/
tar.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
(ns leiningen.tar
"Create a tarball suitable for rpm packaging, stolen from riemann."
(:use [clojure.java.shell :only [sh with-sh-dir]]
[clojure.java.io :only [file delete-file writer copy]]
[clojure.string :only [join capitalize trim-newline split trim]]
[leiningen.uberjar :only [uberjar]]))
(defn delete-file-recursively
"Delete file f. If it's a directory, recursively delete all its contents.
Raise an exception if any deletion fails unless silently is true."
[f & [silently]]
(System/gc) ; This sometimes helps release files for deletion on windows.
(let [f (file f)]
(if (.isDirectory f)
(doseq [child (.listFiles f)]
(delete-file-recursively child silently)))
(delete-file f silently)))
(defn tar-dir
"Tar package working directory."
[project]
(file (:root project) "target" "tar" (str (:name project) "-"
(:version project))))
(defn cleanup
[project]
; Delete working dir.
(when (.exists (file (:root project) "target" "tar"))
(delete-file-recursively (file (:root project) "target" "tar"))))
(defn reset
[project]
(cleanup project)
(sh "rm" (str (:root project) "/target/*.tar.bz2")))
(defn make-tar-dir
"Creates the tarball package structure in a new directory."
[project]
(let [dir (tar-dir project)]
(.mkdirs dir)
; Jar
(.mkdirs (file dir "lib"))
(copy (file (:root project) "target"
(str "pithos-" (:version project) "-standalone.jar"))
(file dir "lib" "pithos.jar"))
; Binary
(.mkdirs (file dir "bin"))
(copy (file (:root project) "pkg" "tar" "pithos")
(file dir "bin" "pithos"))
(.setExecutable (file dir "bin" "pithosn") true false)
; Config
(.mkdirs (file dir "etc"))
(copy (file (:root project) "pkg" "tar" "pithos.config")
(file dir "etc" "pithos.config"))
dir))
(defn write
"Write string to file, plus newline"
[file string]
(with-open [w (writer file)]
(.write w (str (trim-newline string) "\n"))))
(defn md5
"Computes the md5 checksum of a file. Returns a hex string."
[file]
(-> (->> file
str
(sh "md5sum")
:out)
(split #" ")
first
trim))
(defn compress
"Convert given package directory to a .tar.bz2."
[project tar-dir]
(let [filename (str (:name project)
"-"
(:version project)
".tar.bz2")
tarball (str (file (:root project)
"target"
filename))]
(with-sh-dir (.getParent tar-dir)
(print (:err (sh "tar" "cvjf" tarball (.getName tar-dir)))))
(write (str tarball ".md5")
(str (md5 tarball) " " filename))))
(defn tar
([project] (tar project true))
([project uberjar?]
(reset project)
(when uberjar? (uberjar project))
(compress project (make-tar-dir project))
(cleanup project)))