Browse files

added package task

  • Loading branch information...
1 parent 5114ce9 commit 23b865251675e8f408b9b9d2808ffedb88d7a694 @erickg committed Apr 25, 2012
View
63 src/lein-debian/package.clj
@@ -1,63 +0,0 @@
-(ns lein-debian.package
- (:require [clojure.string :as str])
- (:use [clojure.java.shell :only (sh)]
- [lein-debian.common])
- (:import [java.util Date Properties Map Locale]
- [java.text SimpleDateFormat]))
-
-(defn build
- [project dependencies]
- (let [pkg-name (:deb-name project (get-artifact-id (:name project)))
- version (make-version project)
- base-dir (:out (sh "pwd"))
- target-dir (path base-dir (:target-path project target-subdir))
- package-dir (path target-dir (str pkg-name "-" version))
- debian-dir (path package-dir "debian")
- install-dir (:deb-install-dir project install-dir)]
- (sh mkdir "-p" debian-dir)
- (duck/write-lines
- (path debian-dir "control")
- [(str "Source: " artifact-id)
- (str "Section: " (:section configuration section))
- (str "Priority: " (:priority configuration priority))
- (str "Maintainer: " (:maintainer configuration maintainer))
- (str "Build-Depends: " (:buildDepends configuration build-depends))
- (str "Standards-Version: " (:standardsVersion configuration standards-version))
- (str "Homepage: " (:homepage configuration homepage))
- ""
- (str "Package: " artifact-id)
- (str "Architecture: " (:architecture configuration architecture))
- (str "Depends: " (format-dependencies dependencies))
- (str "Description: " (format-description configuration))])
- (doseq [pkg dependencies :when (not-empty pkg)]
- (.info (.getLog this) (str "Depends on " (package-spec pkg))))
- (duck/write-lines
- (path debian-dir "changelog")
- [(str artifact-id " (" version ") unstable; urgency=low")
- ""
- " * Initial Release."
- ""
- (str " -- "
- (:maintainer configuration maintainer) " "
- (.format (SimpleDateFormat. "EEE, d MMM yyyy HH:mm:ss Z"
- (Locale/CANADA)) (Date.)))])
- (duck/write-lines
- (path debian-dir "rules")
- ["#!/usr/bin/make -f" "%:"
- "\tdh $@"])
- (duck/write-lines
- (path package-dir "Makefile")
- [(str "INSTALLDIR := $(DESTDIR)/" install-dir)
- "build:"
- ""
- "install:"
- "\t@mkdir -p $(INSTALLDIR)"
- (str/join " "
- ["\t@cd" target-dir "&&"
- copy "-a"
- (str/replace (:files configuration files) #"\s+" " ")
- "$(INSTALLDIR)"])])
- ((juxt write-preinst write-postinst write-prerm write-postrm)
- debian-dir configuration)
- (sh rm "-fr" "debhelper.log" :dir debian-dir )
- (.info (.getLog this) (sh debuild :dir debian-dir))))
View
35 src/lein-debian/common.clj → src/lein_debian/common.clj
@@ -1,5 +1,6 @@
(ns lein-debian.common
- (:require [clojure.string :as str]))
+ (:use [clojure.java.io :only (writer)])
+ (:require [clojure.string :as str]))
;;; version separator character
;;; (used to separate the name from the version in a debian dependency property
@@ -17,7 +18,17 @@
(def debian-cd "debian-cd-3.1.7")
(def arches "i386")
-(def maintainer "Oster Hase <osterhase@rapanui.com>")
+(def debfullname
+ (or (System/getenv "DEBFULLNAME")
+ "Oster Hase"))
+
+(def debemail
+ (or (System/getenv "DEBEMAIL")
+ "osterhase@rapanui.com"))
+
+(def maintainer
+ (str debfullname " <" debemail ">"))
+
(def section "contrib")
(def priority "optional")
(def build-depends "debhelper (>= 7.0.50~)")
@@ -37,14 +48,30 @@
[element & more-elements]
(str/join "/" (conj more-elements element)))
+(defn build-debian-version
+ [version build-num]
+ (let [version (str/replace version "-SNAPSHOT" "")]
+ (str/join [version (if build-num (str "." build-num))])))
+
(defn make-version
[project]
(let [version (:version project)
build-num (:build-number project)]
- (str/join [version (if build-num (str "." build-num))])))
+ (build-debian-version version build-num)))
(defn get-artifact-id
[dependency]
(if-let [dep-str (and dependency (name dependency))]
- (last (str/split dep-str #"/"))))
+ (str "lib"
+ (str/replace (last (str/split dep-str #"/")) "." "-")
+ "-clojure")))
+
+(defn err
+ [& args]
+ (binding [*out* *err*]
+ (apply println args)))
+(defn write-lines
+ [file coll]
+ (with-open [stream (writer file)]
+ (.write stream (apply str (map #(str %1 "\n") coll)))))
View
156 src/lein_debian/package.clj
@@ -0,0 +1,156 @@
+(ns lein-debian.package
+ (:require [clojure.string :as str]
+ [leiningen.jar :as jar])
+ (:use [clojure.java.shell :only (sh)]
+ [lein-debian.common])
+ (:import [java.util Date Properties Map Locale]
+ [java.text SimpleDateFormat]))
+
+(defn- package-spec
+ [package]
+ (str (first package)
+ (if-let [version (second package)]
+ (str " (>= " version ")"))))
+
+(defn- format-dependencies
+ [dependencies]
+ (str/join ", " (conj (map package-spec dependencies) "${misc:Depends}")))
+
+(defn- format-description
+ [configuration]
+ (let [description (or (:description configuration) description)
+ lines (str/split-lines description)]
+ (apply str
+ (first lines) "\n"
+ (map #(str " " (str/replace %1 #"\s+" " ") "\n") (rest lines)))))
+
+(defn- scope-allowed?
+ [project m]
+ (or (not= (:scope m) "test")
+ (:package-test-dependencies project)))
+
+(defn- build-debian-name
+ [project dependency version args]
+ (let [artifact-id (get-artifact-id dependency)
+ m (and (not-empty args) (apply assoc {} args))]
+ (if (scope-allowed? project m)
+ (if (contains? m :debian)
+ (if-let [override (:debian m)]
+ [(first override) (second override)])
+ [artifact-id
+ (build-debian-version version (:build-number project))]))))
+
+(defn- get-dependencies
+ [project]
+ (filter (comp not nil?)
+ (for [[dependency version & rest] (:dependencies project)]
+ (let [[version rest] (if (keyword? version)
+ [nil (conj rest version)]
+ [version rest])]
+ (build-debian-name project dependency version rest)))))
+
+(defn maybe-from-script
+ [commands]
+ (let [commands (str/trim commands)]
+ (if (.startsWith commands "!")
+ (str/join "\n" (slurp (.substring commands 1)))
+ commands)))
+
+(defn install-helper
+ [debian-dir project script type cases]
+ (if-let [commands (or (type project))]
+ (let [commands (maybe-from-script commands)]
+ (spit (path debian-dir script)
+ (reduce str ["#!/bin/sh"
+ "set -e"
+ "case \"$1\" in"
+ (str cases ")")
+ commands
+ " ;;"
+ "esac"
+ "#DEBHELPER#"
+ "exit 0"])))))
+
+(defn write-preinst
+ [debian-dir project]
+ (install-helper debian-dir project "preinst" :deb-pre-install "install|upgrade"))
+
+(defn write-postinst
+ [debian-dir project]
+ (install-helper debian-dir project "postinst" :deb-post-install "configure"))
+
+(defn write-prerm
+ [debian-dir project]
+ (install-helper debian-dir project "prerm" :deb-pre-remove "remove|upgrade|deconfigure"))
+
+(defn write-postrm
+ [debian-dir project]
+ (install-helper
+ debian-dir project "postrm" :deb-post-remove
+ "purge|remove|upgrade|failed-upgrade|abort-install|abort-upgrade|disappear"))
+
+
+(defn package
+ [project args]
+ (and (jar/jar project)
+ (let [dependencies (get-dependencies project)
+ pkg-name (:deb-name project (get-artifact-id (:name project)))
+ version (make-version project)
+ base-dir (str/trim (:out (sh "pwd")))
+ target-dir (:target-path project (path base-dir target-subdir))
+ package-dir (path target-dir (str pkg-name "-" version))
+ debian-dir (path package-dir "debian")
+ install-dir (:deb-install-dir project install-dir)
+ arch (:deb-architecture project architecture)]
+ (sh mkdir "-p" debian-dir)
+ (write-lines (path debian-dir "control")
+ [(str "Source: " pkg-name)
+ (str "Section: " (:deb-section project section))
+ (str "Priority: " (:deb-priority project priority))
+ (str "Maintainer: " (:deb-maintainer project maintainer))
+ (str "Build-Depends: " (:deb-build-depends project build-depends))
+ (str "Standards-Version: " (:deb-standards-version project standards-version))
+ (str "Homepage: " (:deb-homepage homepage))
+ ""
+ (str "Package: " pkg-name)
+ (str "Architecture: " arch)
+ (str "Depends: " (format-dependencies dependencies))
+ (str "Description: " (format-description project))])
+
+ (when-not (empty? dependencies)
+ (apply println "Depends on" (map package-spec dependencies)))
+
+ (write-lines (path debian-dir "changelog")
+ [(str pkg-name " (" version ") unstable; urgency=low")
+ ""
+ " * Initial Release."
+ ""
+ (str " -- "
+ (:deb-maintainer project maintainer) " "
+ (.format (SimpleDateFormat. "EEE, d MMM yyyy HH:mm:ss Z"
+ (Locale/CANADA)) (Date.)))])
+ (write-lines (path debian-dir "rules")
+ ["#!/usr/bin/make -f"
+ "%:"
+ "\tdh $@"])
+
+ (write-lines (path package-dir "Makefile")
+ [(str "INSTALLDIR := $(DESTDIR)/" install-dir)
+ "build:"
+ ""
+ "install:"
+ "\t@mkdir -p $(INSTALLDIR)"
+ (str/join " "
+ ["\t@cd" target-dir "&&"
+ copy "-a"
+ (str/replace (:deb-files project files) #"\s+" " ")
+ "$(INSTALLDIR)"])])
+ ((juxt write-preinst write-postinst write-prerm write-postrm)
+ debian-dir project)
+ (sh rm "-fr" "debhelper.log" :dir debian-dir )
+ (let [r (sh debuild "--no-tgz-check" :dir debian-dir)]
+ (if-not (= (:exit r) 0)
+ (err (:out r)
+ "\nFailed to build Debian package. Errors follow:\n"
+ (:err r))
+ (println "Created" (str target-dir "/" pkg-name "_" version "_" arch ".deb")))))))
View
45 src/leiningen/debian.clj
@@ -1,33 +1,20 @@
(ns leiningen.debian
- (:require [clojure.string :as str]))
-
-(defn- scope-allowed?
- [project m]
- (or (not= (:scope m) "test")
- (:package-test-dependencies project)))
-
-(defn- build-debian-name
- [project dependency version args]
- (let [artifactId (get-artifact-id dependency)
- m (and (not-empty args) (apply assoc {} args))]
- (if (scope-allowed? project m)
- (if (contains? m :debian)
- (if-let [override (:debian m)]
- [(first override) (second override)])
- [(str "lib" artifactId "-java") version]))))
-
-(defn- get-dependencies
- [project]
- (filter (comp not nil?)
- (for [[dependency version & rest] (:dependencies project)]
- (let [[version rest] (if (keyword? version)
- [nil (conj rest version)]
- [version rest])]
- (build-debian-name project dependency version rest)))))
+ (:use [lein-debian.common]))
(defn debian
- "Generates a Debian package for build products"
+ "Generates a Debian package for build products
+USAGE: lein debian <TASK>
+where <TASK> can be any of:
+package : creates a Debian package as per the configuration specified
+ in project.clj
+"
[project & args]
- (println (get-dependencies project)))
-
-
+ (let [[task & rest] args]
+ (if task
+ (let [task-ns-sym (symbol (str "lein-debian." task))
+ - (require task-ns-sym)
+ task-ns (find-ns task-ns-sym)]
+ (if-let [funk (and task-ns (->> task symbol (ns-resolve task-ns)))]
+ (funk project args)
+ (err "unknown task" task)))
+ (err "no task was specified. Try lein help debian"))))
View
14 test/lein_debian/test/common.clj
@@ -0,0 +1,14 @@
+(ns lein-debian.test.common
+ (:use [clojure.test :only (deftest)]
+ [midje.sweet]
+ [lein-debian.common]))
+
+(deftest get-artifact-id-test
+ (facts
+ (get-artifact-id nil) => nil
+ (get-artifact-id 'a) => "a"
+ (get-artifact-id 'a/b) => "b"
+ (get-artifact-id 'a.b/c) => "c"
+ (get-artifact-id 'a.b.c/d) => "d"
+ (get-artifact-id ['a.b.c/d]) => (throws ClassCastException)))
+
View
19 test/leiningen/test/debian.clj → test/lein_debian/test/package.clj
@@ -1,20 +1,11 @@
-(ns leiningen.test.debian
+(ns lein-debian.test.package
(:use [clojure.test :only (deftest)]
[midje.sweet]
- [leiningen.debian]))
-
-(deftest get-artifact-id-test
- (facts
- (#'leiningen.debian/get-artifact-id nil) => nil
- (#'leiningen.debian/get-artifact-id 'a) => "a"
- (#'leiningen.debian/get-artifact-id 'a/b) => "b"
- (#'leiningen.debian/get-artifact-id 'a.b/c) => "c"
- (#'leiningen.debian/get-artifact-id 'a.b.c/d) => "d"
- (#'leiningen.debian/get-artifact-id ['a.b.c/d]) => (throws ClassCastException)))
+ [lein-debian.package]))
(deftest get-dependencies-test
(facts
- (#'leiningen.debian/get-dependencies
+ (#'lein-debian.package/get-dependencies
{:dependencies [['a.b/c "x"]
['a.b/c "x" :debian nil]
['a.b/c "x" :debian ["d"]]
@@ -27,6 +18,6 @@
["liba-java" "x"]
["b" nil]
["liba-java" nil]]
- (#'leiningen.debian/get-dependencies
+ (#'lein-debian.package/get-dependencies
{:dependencies [['a.b/c "x" :scope "test"]
- ['a.b.c "y"]]}) => [["liba.b.c-java" "y"]]))
+ ['a.b.c "y"]]}) => [["liba.b.c-java" "y"]]))

0 comments on commit 23b8652

Please sign in to comment.