Permalink
Browse files

Initial commit.

  • Loading branch information...
fukamachi committed Oct 9, 2013
0 parents commit 6caa3f000fb3568550d8761f00c3a1549e95c3a1
Showing with 295 additions and 0 deletions.
  1. +8 −0 .gitignore
  2. +23 −0 LICENSE
  3. +78 −0 README.markdown
  4. +20 −0 envy-test.asd
  5. +35 −0 envy.asd
  6. +49 −0 src/envy.lisp
  7. +82 −0 t/envy.lisp
@@ -0,0 +1,8 @@
*.fasl
*.dx32fsl
*.dx64fsl
*.lx32fsl
*.lx64fsl
*.x86f
*~
.#*
23 LICENSE
@@ -0,0 +1,23 @@
Copyright (c) 2013, Eitarow Fukamachi
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,78 @@
# Envy
Configuration switcher by an environment variable inspired by Perl's [Config::ENV](http://search.cpan.org/~satoh/Config-ENV/lib/Config/ENV.pm).
## Usage
```common-lisp
(defpackage :myapp.config
(:use :cl
:envy))
(in-package :myapp.config)
(setf (config-env-var) "APP_ENV")
(defconfig :common
`(:application-root ,(asdf:component-pathname (asdf:find-system :myapp))))
(defconfig |development|
'(:debug T
:database-type :sqlite
:database-connection-spec '()))
(defconfig |production|
'(:database-type :mysql
:database-connection-spec '()))
(defconfig |staging|
`(:debug T
,@|production|))
```
```common-lisp
(defpackage :myapp
(:use :cl)
(:import-from :envy
:config)
(in-package :myapp)
(getf (config :myapp.config) :database-type)
```
## Tips
```common-lisp
(defpackage myapp.config
(:use :cl
:envy)
(:shadow :envy
:config)
(:export :config))
(in-package :myapp.config)
(defconfig :common
...)
;;
;; ... Configurations ...
;;
(defun config ()
(envy:config #.(package-name *package*)))
```
## See Also
- [Config::ENV](http://search.cpan.org/~satoh/Config-ENV/lib/Config/ENV.pm)
## Author
* Eitarow Fukamachi (e.arrows@gmail.com)
## Copyright
Copyright (c) 2013 Eitarow Fukamachi (e.arrows@gmail.com)
# License
Licensed under the BSD 2-Clause License.
@@ -0,0 +1,20 @@
#|
This file is a part of Envy project.
Copyright (c) 2013 Eitarow Fukamachi (e.arrows@gmail.com)
|#
(in-package :cl-user)
(defpackage envy-test-asd
(:use :cl :asdf))
(in-package :envy-test-asd)
(defsystem envy-test
:author "Eitarow Fukamachi"
:license "BSD 2-Clause"
:depends-on (:envy
:cl-test-more
:osicat)
:components ((:module "t"
:components
((:file "envy"))))
:perform (load-op :after (op c) (asdf:clear-system c)))
@@ -0,0 +1,35 @@
#|
This file is a part of Envy project.
Copyright (c) 2013 Eitarow Fukamachi (e.arrows@gmail.com)
|#
#|
Author: Eitarow Fukamachi (e.arrows@gmail.com)
|#
(in-package :cl-user)
(defpackage envy-asd
(:use :cl :asdf))
(in-package :envy-asd)
(defsystem envy
:version "0.1"
:author "Eitarow Fukamachi"
:license "BSD 2-Clause"
:components ((:module "src"
:components
((:file "envy"))))
:description "Configuration switcher by an environment variable."
:long-description
#.(with-open-file (stream (merge-pathnames
#p"README.markdown"
(or *load-pathname* *compile-file-pathname*))
:if-does-not-exist nil
:direction :input)
(when stream
(let ((seq (make-array (file-length stream)
:element-type 'character
:fill-pointer t)))
(setf (fill-pointer seq) (read-sequence seq stream))
seq)))
:in-order-to ((test-op (load-op envy-test))))
@@ -0,0 +1,49 @@
#|
This file is a part of Envy project.
Copyright (c) 2013 Eitarow Fukamachi (e.arrows@gmail.com)
|#
(in-package :cl-user)
(defpackage envy
(:use :cl)
(:export :config-env-var
:defconfig
:config))
(in-package :envy)
(defvar *config-env-map* (make-hash-table :test 'equal))
(defvar *package-common-configurations* (make-hash-table :test 'equal))
(defun config-env-var (&optional (package-name (package-name *package*)))
(gethash (package-name (find-package package-name)) *config-env-map*))
(defun (setf config-env-var) (val &optional (package-name (package-name *package*)))
(setf (gethash (package-name (find-package package-name)) *config-env-map*) val))
(defmacro defconfig (name configurations)
(if (eq name :common)
(let ((package-name (package-name *package*)))
`(setf (gethash ,package-name *package-common-configurations*)
,configurations))
`(progn
(defparameter ,name ,configurations)
(setf (get ',name 'configurationp) t))))
(defun config (package-name)
(let* ((package (find-package package-name))
(package-name (package-name package))
(env-var (config-env-var package-name)))
(unless env-var
(error "Package \"~A\" is not configured. Set config-env-var."
package-name))
(let ((env (asdf::getenv env-var)))
(if env
(let ((symbol (find-symbol env package)))
(if (and symbol
(get symbol 'configurationp)
(boundp symbol))
(values (append (symbol-value symbol)
(gethash package-name *package-common-configurations* nil))
t)
(values nil nil)))
(values nil nil)))))
@@ -0,0 +1,82 @@
#|
This file is a part of Envy project.
Copyright (c) 2013 Eitarow Fukamachi (e.arrows@gmail.com)
|#
(in-package :cl-user)
(defpackage :envy.myapp.config
(:use :cl)
(:import-from :envy
:config-env-var
:defconfig))
(in-package :envy.myapp.config)
(setf (config-env-var) "APP_ENV")
(defconfig :common
`(:application-root #P"/path/to/application/"))
(defconfig |development|
'(:a 1
:b 2))
(defconfig |production|
'(:a 10
:b 200))
(defconfig |staging|
`(:a 0
:c 3000
,@|production|))
(in-package :cl-user)
(defpackage envy-test
(:use :cl
:envy
:cl-test-more)
(:import-from :osicat
:environment-variable
:makunbound-environment-variable))
(in-package :envy-test)
(plan 9)
(defparameter *env-var* "APP_ENV")
(defparameter *env-backup*
(environment-variable *env-var*))
(diag "development")
(setf (environment-variable *env-var*) "development")
(is (getf (config :envy.myapp.config) :a) 1)
(is (getf (config :envy.myapp.config) :b) 2)
(is (getf (config :envy.myapp.config) :application-root)
#P"/path/to/application/"
"Has a common configuration")
(diag "production")
(setf (environment-variable *env-var*) "production")
(is (getf (config :envy.myapp.config) :a) 10)
(is (getf (config :envy.myapp.config) :b) 200)
(is (getf (config :envy.myapp.config) :application-root)
#P"/path/to/application/"
"Has a common configuration")
(diag "staging")
(setf (environment-variable *env-var*) "staging")
(is (getf (config :envy.myapp.config) :a) 0
"Can override a parent configuration")
(is (getf (config :envy.myapp.config) :c) 3000
"Can add another configuration")
(is (getf (config :envy.myapp.config) :application-root)
#P"/path/to/application/"
"Has a common configuration")
(finalize)
(if *env-backup*
(setf (environment-variable *env-var*) *env-backup*)
(makunbound-environment-variable *env-var*))

0 comments on commit 6caa3f0

Please sign in to comment.