Created
October 21, 2012 05:20
-
-
Save mnzk/3925966 to your computer and use it in GitHub Desktop.
ClojureでJavaのpackage-info.javaを一括生成
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;-coding: utf-8 -*- | |
;; leiningen プロジェクトの core.cljです。 | |
(ns pkginfo-gen.core | |
(:gen-class) | |
(:require [clojure.java.io :as io]) | |
(:require [clojure.string :as string])) | |
(def coding "utf-8") | |
(def info-filename "package-info.java") | |
(def line-separator (System/getProperty "line.separator")) | |
(def template | |
(string/join | |
line-separator | |
["/**" | |
" * %sパッケージ。" | |
" *" | |
" * <pre>" | |
" * // TODO パッケージ内容の詳細を記述してください" | |
" * </pre>" | |
" * " | |
" */" | |
"package %s;"])) | |
(defn insert-to-template | |
[package-name] | |
(format template package-name package-name)) | |
(def dir-sep java.io.File/separator) | |
(def re-tail-sep (re-pattern (str dir-sep "+$"))) | |
(defn path-join | |
[path1 path2] | |
(.getPath (io/file path1 path2))) | |
(defn- normalize-tail* | |
[pat sep dirname] | |
(str (string/replace dirname pat "") sep)) | |
(defn normalize-tail | |
"ディレクトリ名末尾にセパレータ文字が無い場合、付加する" | |
[dirname] | |
(normalize-tail* re-tail-sep dir-sep dirname)) | |
(defn to-package-name | |
"ディレクトリ名からパッケージ名を作成" | |
[sep base-len dir] | |
(-> (subs dir base-len) | |
(string/replace sep "."))) | |
(defn gen-dir-package-pairs | |
"すべての対象ディレクトリに対する [ディレクトリ名 パッケージ名] ベクタを作成し、それらを要素とするシーケンスを返す" | |
[base-dirname] | |
(let [base-dir (normalize-tail base-dirname) | |
base-len (count base-dir) | |
to-package-name #(to-package-name dir-sep base-len %) | |
make-pairs #(map (juxt identity to-package-name) %) | |
dirnames (->> (rest (file-seq (io/file base-dir))) | |
(filter #(.isDirectory %)) | |
(map #(.getPath %)))] | |
(make-pairs dirnames))) | |
(defn gen-info-file | |
"package info ファイルの生成" | |
[filename package] | |
(spit filename | |
(insert-to-template package) | |
:encoding coding)) | |
(defn -main | |
([] | |
(-main ".")) | |
([base-dir & args] | |
(doseq [[dir pkg] (gen-dir-package-pairs base-dir)] | |
(let [filename (path-join dir info-filename)] | |
(println (format "Generating... [%s]" filename)) | |
(gen-info-file filename pkg))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment