Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: erickg/lein-debian
base: b88d886845
...
head fork: erickg/lein-debian
compare: 2701452261
  • 5 commits
  • 7 files changed
  • 0 commit comments
  • 1 contributor
View
27 README.md
@@ -1,4 +1,25 @@
-lein-debian
-===========
+# lein-debian
-Leiningen plugin to generate Debian packages from build products
+A Leiningen plugin to do many wonderful things.
+
+## Usage
+
+FIXME: Use this for user-level plugins:
+
+Put `[lein-debian "0.1.0-SNAPSHOT"]` into the `:plugins` vector of your
+`:user` profile, or if you are on Leiningen 1.x do `lein plugin install
+lein-debian 0.1.0-SNAPSHOT`.
+
+FIXME: Use this for project-level plugins:
+
+Put `[lein-debian "0.1.0-SNAPSHOT"]` into the `:plugins` vector of your project.clj.
+
+FIXME: and add an example usage that actually makes sense:
+
+ $ lein debian
+
+## License
+
+Copyright © 2012 FIXME
+
+Distributed under the Eclipse Public License, the same as Clojure.
View
7 project.clj
@@ -0,0 +1,7 @@
+(defproject lein-debian "0.1.0-SNAPSHOT"
+ :description "Leiningen plugin to generate Debian packages"
+ :url "http://github.com/erickg/lein-debian"
+ :license {:name "Eclipse Public License"
+ :url "http://www.eclipse.org/legal/epl-v10.html"}
+ :eval-in-leiningen true
+ :dependencies [[midje "1.3.1" :scope "test"]])
View
78 src/lein_debian/common.clj
@@ -0,0 +1,78 @@
+(ns lein-debian.common
+ (: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
+(def vs "_")
+
+(def mkdir "mkdir")
+(def fmt "fmt")
+(def debuild "debuild")
+(def copy "cp")
+(def ls "ls")
+(def rm "rm")
+(def bash "/bin/bash")
+(def apt-move "apt-move")
+(def apt-ftparchive "apt-ftparchive")
+(def debian-cd "debian-cd-3.1.7")
+(def arches "i386")
+
+(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~)")
+(def standards-version "3.9.1" )
+(def homepage "http://google.com")
+(def architecture "all")
+(def description "The Osterhase was too lazy to provide a description")
+(def files "*.jar")
+(def target-subdir "target")
+(def install-dir "/usr/share/java")
+(def apt-config-file "config/apt.conf")
+(def apt-move-config-file "config/apt-move.conf")
+(def dist "squeeze")
+(def pkg-config-file (str "config/" dist "-packages.conf"))
+
+(defn path
+ [element & more-elements]
+ (str/join "/" (conj more-elements element)))
+
+(defn build-debian-version
+ [version build-num]
+ (if version
+ (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)]
+ (build-debian-version version build-num)))
+
+(defn get-artifact-id
+ [dependency]
+ (if-let [dep-str (and dependency (name dependency))]
+ (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
20 src/leiningen/debian.clj
@@ -0,0 +1,20 @@
+(ns leiningen.debian
+ (:use [lein-debian.common]))
+
+(defn debian
+ "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]
+ (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) => "liba-clojure"
+ (get-artifact-id 'a/b) => "libb-clojure"
+ (get-artifact-id 'a.b/c) => "libc-clojure"
+ (get-artifact-id 'a.b.c/d) => "libd-clojure"
+ (get-artifact-id ['a.b.c/d]) => (throws ClassCastException)))
+
View
23 test/lein_debian/test/package.clj
@@ -0,0 +1,23 @@
+(ns lein-debian.test.package
+ (:use [clojure.test :only (deftest)]
+ [midje.sweet]
+ [lein-debian.package]))
+
+(deftest get-dependencies-test
+ (facts
+ (#'lein-debian.package/get-dependencies
+ {:dependencies [['a.b/c "x"]
+ ['a.b/c "x" :debian nil]
+ ['a.b/c "x" :debian ["d"]]
+ ['a.b/c "x" :debian ["d" "y"]]
+ ['a "x"]
+ ['a :debian ["b"]]
+ ['a]]}) => [["libc-clojure" "x"]
+ ["d" nil]
+ ["d" "y"]
+ ["liba-clojure" "x"]
+ ["b" nil]
+ ["liba-clojure" nil]]
+ (#'lein-debian.package/get-dependencies
+ {:dependencies [['a.b/c "x" :scope "test"]
+ ['a.b.c "y"]]}) => [["liba-b-c-clojure" "y"]]))

No commit comments for this range

Something went wrong with that request. Please try again.