Skip to content

Commit

Permalink
First import
Browse files Browse the repository at this point in the history
  • Loading branch information
kumarshantanu committed Jul 11, 2011
0 parents commit d3f4824
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 0 deletions.
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# lein-localrepo

Leiningen plugin to work with local Maven repository.


## Usage

Either install as a plugin:

$ lein plugin install lein-localrepo "0.1"

Or, include as a dev-dependency:

:dev-dependencies [lein-localrepo "0.1"]


### Guess Leiningen (Maven) coordinates of a file

Syntax:
$ lein localrepo coords <filename>

Example:
$ lein localrepo coords foo-bar-1.0.6.jar
Output:
foo-bar-1.0.6.jar foo-bar/foo-bar 1.0.6


### Install artifacts to local Maven repository

Syntax:
$ lein localrepo install <filename> [<[groupId/]artifactId> [<version>]]

Examples:
$ lein localrepo install foo-1.0.6.jar com.example/foo 1.0.6
$ lein localrepo install foomatic-1.3.9.jar foomatic 1.3.9


### List artifacts in local Maven repository (NOT YET IMPLEMENTED):

Syntax:
$ lein localrepo list [<[groupId/]artifactId> [<version>]]

Examples:
$ lein localrepo list # lists all artifacts, all versions
$ lein localrepo list com.example/foo # lists all versions
$ lein localrepo list foomatic # lists all versions
$ lein localrepo list com.example/foo 1.3 # lists only specified version


### Remove artifacts from local Maven repository (NOT YET IMPLEMENTED):

Syntax:
$ lein localrepo remove <[groupId/]artifactId> [<version>]

Examples:
$ lein localrepo remove com.example/foo # removes all versions
$ lein localrepo remove foomatic # removes all versions
$ lein localrepo remove com.example/foo 1.0.3 # removes only specified version


## Getting in touch

On Twitter: [@kumarshantanu](http://twitter.com/kumarshantanu)

On Leiningen mailing list: [http://groups.google.com/group/leiningen](http://groups.google.com/group/leiningen)


## License

Copyright (C) 2011 Shantanu Kumar

Distributed under the Eclipse Public License, the same as Clojure.
3 changes: 3 additions & 0 deletions project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(defproject lein-localrepo "0.1"
:description "Leiningen local repository plugin"
:dev-dependencies [[org.clojure/clojure "1.2.1"]])
155 changes: 155 additions & 0 deletions src/leiningen/localrepo.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
(ns leiningen.localrepo
(:require
[leiningen.util.maven :as mvn]
[leiningen.install :as ins]
[leiningen.pom :as pom]
[clojure.java.io :as jio]
[clojure.string :as str])
(:import
(java.io File)
(java.util.jar JarFile)
(org.apache.maven.artifact.installer ArtifactInstaller)))


(defn illegal-arg
"Throw IllegalArgumentException using the args"
[arg & more]
(let [msg (apply str (interpose " " (into [arg] more)))]
(throw (IllegalArgumentException. msg))))


(defn split-artifactid
"Given 'groupIp/artifactId' string split them up and return
as a vector of 2 elements."
[^String artifact-id]
(let [tokens (str/split artifact-id #"/")
tcount (count tokens)
[gi ai] tokens]
(if (or (zero? tcount)
(> tcount 2)
(nil? gi))
(illegal-arg "Invalid groupId/artifactId:" artifact-id)
(if (nil? ai)
[gi gi]
[gi ai]))))


(defn ^String java-filepath
"Accept path (of a file) as argument and return a uniform file path for all
operating systems.
Example: \"C:\\path\\to\\file.txt\" becomes \"C:/path/to/file.txt\"
See also: split-filepath"
[s]
(let [p (if (instance? File s) (.getAbsolutePath ^File s)
(str s))]
(.replace ^String p "\\" "/")))


(defn ^String split-filepath
"Given a complete path, split into filedir and filename and return as vector.
The filedir is normalized as uniform Java filepath.
See also: java-filepath"
[s]
(let [jf (java-filepath s)
sf (str/split jf #"/")]
[(str/join "/" (drop-last sf)) (last sf)]))


(defn ^String pick-filename
"Given a filepath, return the filename portion from it."
[s]
(last (split-filepath s)))


(defn c-coords
"Guess Leiningen coordinates of given filename.
Example:
Input - local/jars/foo-bar-1.0.6.jar
Output - foo-bar-1.0.6.jar foo-bar 1.0.6"
[^String filepath]
(let [filename (pick-filename filepath)
tokens (drop-last
(re-find (re-matcher #"(.+)\-(\d.+)\.(\w+)"
filename)))
[_ artifact-id version] tokens]
(println filename (str artifact-id "/" artifact-id) version)))


(defn c-install
"Install artifact to local repository"
[filename artifact-id version]
(let [the-file (jio/file filename)
[gid aid] (split-artifactid artifact-id)
project {:name aid
:group gid
:version version
:root "."
:source-path ""
:test-path ""
:resources-path ""
:dev-resources-path ""}
model (mvn/make-model project)
artifact (mvn/make-artifact model)
installer (.lookup mvn/container ArtifactInstaller/ROLE)
local-repo (mvn/make-local-repo)]
;(when (not= "pom" (.getPackaging model))
; (mvn/add-metadata artifact (jio/file (pom/pom project))))
(ins/install-shell-wrappers (JarFile. the-file))
(.install installer the-file artifact local-repo)))


(defn c-list
"List artifacts in local Maven repo"
[& args]
(println "Not yet implemented"))


(defn c-remove
"Remove artifacts from local Maven repo"
[& args]
(println "Not yet implemented"))


(defn c-help
"Display help for plugin, or for specified command"
([]
(println "
Leiningen plugin to work with local Maven repository.
coords Guess Leiningen (Maven) coords of a file
install Install artifact to local repository
list List artifacts in local repository (Not Yet Implemented)
remove Remove artifact from local repository (Not Yet Implemented)
help This help screen
"))
([command]
(case command
"coords" (doc c-coords)
"install" (doc c-install)
"list" (doc c-list)
"remove" (doc c-remove)
"help" (doc c-help)
(illegal-arg "Illegal command:" command
", Allowed: coords, install, list, remove, help"))))


(defn apply-cmd
[pred cmd f args]
(if (pred) (apply f args)
(c-help cmd)))


(defn localrepo
"Work with local Maven repository"
([project]
(c-help))
([project command & args]
(let [argc (count args)]
(case command
"coords" (apply-cmd #(= argc 1) command c-coords args)
"install" (apply-cmd #(= argc 3) command c-install args)
"list" (apply-cmd #(>= argc 0) command c-list args)
"remove" (apply-cmd #(>= argc 0) command c-remove args)
"help" (apply-cmd #(or (= argc 0)
(= argc 1)) command c-help args)
(c-help)))))
2 changes: 2 additions & 0 deletions test/leiningen/test_localrepo.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(ns leiningen.test-localrepo
(:use clojure.test))

0 comments on commit d3f4824

Please sign in to comment.