Skip to content

Commit

Permalink
Add 'Prefix' tag support
Browse files Browse the repository at this point in the history
  • Loading branch information
carlzhc committed Apr 17, 2019
1 parent 8993a2d commit fee475d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 33 deletions.
15 changes: 15 additions & 0 deletions example-project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
;; Example project

(defproject lein-rpmbuild "0.1.0"
:description "Leiningen plugin for building an RPM package"
:url "http://github.com/carlzhc/lein-rpmbuild"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
:url "https://www.eclipse.org/legal/epl-2.0/"}
:dependencies [[lein-tar "3.3.0"]]
:tar {:ubergar true
:format :tar-gz}
:rpmbuild {:%define [["__os_install_post" "%{nil}"]]
:Requires ["java-1.8.0-openjdk"]
:Prefix "/opt/%name"
:%changelog :gitlog})

11 changes: 3 additions & 8 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
(defproject lein-rpmbuild "0.1.0"
:description "Leiningen plugin for building RPM package"
(defproject lein-rpmbuild "0.1.1"
:description "Leiningen plugin for building an RPM package"
:url "http://github.com/carlzhc/lein-rpmbuild"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
:url "https://www.eclipse.org/legal/epl-2.0/"}
:eval-in-leiningen true
:dependencies [[lein-tar "3.3.0"]]
:deploy-repositories [["releases" :clojars]
["snapshots" :clojars]]
:tar {:ubergar true
:format :tar-gz}
:rpmbuild {:%define [["__os_install_post" "%{nil}"]]
:Requires ["java-1.8.0-openjdk"]
:%changelog :gitlog})
["snapshots" :clojars]])

76 changes: 51 additions & 25 deletions src/leiningen/rpmbuild.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@
(let [result (shell/sh "git" "log" "--pretty=format:* %ad %an <%ae> - %h %n- %s%n" "--date=format:%a %b %d %Y")]
(if (zero? (:exit result)) (:out result) nil)))

(defn- newlinejoin
"Use newline to join elements in collection"
[acol]
(if (coll? acol)
(str/join "\n" acol)
acol))

(defn- tagformat
[tagname tagval]
(format "%-16s%s\n" (str tagname ":") tagval))

(defn- genspec
"Generate a RPM spec file"
[project
{:keys [Name Version Release Summary Group License URL BuildArch BuildRoot Requires
{:keys [Name Version Release Summary Group License URL BuildArch BuildRoot Prefix Requires
Source0 Source1 Source2 Source3 Source4 Source5 Source6 Source7 Source8 Source9
%description %prep %install %clean %post %files %changelog
%description %prep %install %clean %post %files %changelog %doc
%define %undefine %global]
:as options}]
(let [pkg (io/file (:root project) "pkg")
Expand All @@ -33,56 +43,72 @@
(when %undefine
(doseq [line %undefine]
(.write spec (format "%%undefine %s %s\n" (first line) (second line)))))
(.newLine spec)
(.write spec (format "Name: %s\n" (or Name (:name project))))
(.write spec (format "Version: %s\n" (or Version (:version project))))
(.write spec (format "Release: %s\n" (or Release "1%{?dist}")))
(.write spec (format "Summary: %s\n" (or Summary (:description project))))
(.write spec (format "Group: %s\n" (or Group "Application")))
(.write spec (format "License: %s\n" (or License (:name (:license project)))))
(.write spec (format "URL: %s\n" (or URL (:url project))))
(.write spec (format "Source0: %s\n" (or Source0 "%{name}-%{version}.tar.gz")))
(when (or %global %define %undefine) (.newLine spec))
(.write spec (tagformat "Name" (or Name (:name project))))
(.write spec (tagformat "Version" (or Version (:version project))))
(.write spec (tagformat "Release" (or Release "1%{?dist}")))
(.write spec (tagformat "Summary" (or Summary (newlinejoin (:description project)))))
(.write spec (tagformat "Group" (or Group "Application")))
(.write spec (tagformat "License" (or License (:name (:license project)))))
(.write spec (tagformat "URL" (or URL (:url project))))
(.write spec (tagformat "Source0" (or Source0 "%{name}-%{version}.tar.gz")))
(doseq [n (range 1 10)]
(when-let [v (get options (str "Source" n))]
(.write spec (format "Source%d: %s\n" n v))))
(.write spec (format "BuildRoot: %s\n"
(.write spec (tagformat (str "Source" n) v))))
(.write spec (tagformat "BuildRoot"
(or BuildRoot "%{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)")))
(.write spec (format "BuildArch: %s\n" "noarch"))
(.write spec (tagformat "BuildArch" (or BuildArch "noarch")))

(when Prefix
(.write spec (tagformat "Prefix" Prefix)))

(when Requires
(doseq [r Requires]
(.write spec (format "Requires: %s\n" r))))
(.write spec (tagformat "Requires" r))))

(.newLine spec)
(.write spec (format "%%description\n%s\n" (or %description (:description project))))
(.write spec (format "%%description\n%s\n"
(newlinejoin (or %description (:description project)))))

(.newLine spec)
(.write spec (format "%%prep\n%s\n" (or %prep "%autosetup -v")))
(.write spec (format "%%prep\n%s\n"
(newlinejoin (or %prep ["%autosetup -v"]))))

(.newLine spec)
(.write spec (format "%%install\n%s\n" (or %install "rm -f *.spec build.clj; cp -r . $RPM_BUILD_ROOT/")))
(.write spec (format "%%install\n%s\n"
(newlinejoin (or %install
["rm -f *.spec build.clj"
(str "mkdir -p $RPM_BUILD_ROOT" Prefix)
(str "cp -ar . $RPM_BUILD_ROOT" Prefix)]))))

(.newLine spec)
(.write spec (format "%%clean\n%s\n" (or %clean "rm -rf $RPM_BUILD_ROOT")))
(.write spec (format "%%clean\n%s\n"
(newlinejoin (or %clean
["rm -rf $RPM_BUILD_ROOT"]))))

(when %post
(.newLine spec)
(.write spec (format "%%post\n%s\n" %post)))
(.write spec (format "%%post\n%s\n" (newlinejoin %post))))

(.newLine spec)
(.write spec (format "%%files\n%%defattr(-,root,root,-)\n%s\n" (or (not= "" (str/join "\n" %files)) "/*")))
(.write spec (format "%%files\n%s\n"
(newlinejoin (or %files
["%defattr(-,root,root,-)"
(str Prefix "/*")]))))
(when %doc
(.write spec (format "%%doc %s\n"
(newlinejoin %doc))))

(when %changelog
(.newLine spec)
(if (= :gitlog %changelog)
(.write spec (format "%%changelog\n%s\n" (gitlog)))
(.write spec (format "%%changelog\n%s\n" (str/join "\n\n" %changelog)))))

)
(.write spec (format "%%changelog\n%s\n" (newlinejoin %changelog))))))

(lein/info "Wrote" (.getCanonicalPath specfile))
(assoc-in project [:rpmbuild :spec] (.getCanonicalPath specfile))))



(defn- maketarball
[project & args]
(let [tarball (apply tar project args)]
Expand Down

0 comments on commit fee475d

Please sign in to comment.