├── .gitignore ├── CHANGES.md ├── LICENSE.txt ├── Makefile ├── README.md ├── dune-project ├── ppx_getenv.opam ├── src ├── dune └── ppx_getenv.ml └── src_test ├── dune └── test_ppx_getenv.ml /.gitignore: -------------------------------------------------------------------------------- 1 | _build 2 | *.install 3 | .merlin 4 | -------------------------------------------------------------------------------- /CHANGES.md: -------------------------------------------------------------------------------- 1 | v2.1 (23/10/2020) 2 | ----------------- 3 | 4 | - Upgrade to ppxlib 0.18.0 (#8, @NathanReb) 5 | 6 | v2.0 (12/08/2020) 7 | ----------------- 8 | 9 | - Switch from `topkg`/`ocamlbuild` to `dune` (#6, @kit-ty-kate) 10 | - Switch from `ppx_tools` to `ppxlib` (#6, #7, @kit-ty-kate) 11 | 12 | These changes bring OCaml 4.11 support as well as the ability to use `ppx_getenv` directly in `dune`. 13 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | build: 2 | dune build 3 | 4 | test: 5 | dune runtest -f 6 | 7 | clean: 8 | dune clean 9 | 10 | .PHONY: build test clean 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ppx_getenv 2 | ========== 3 | 4 | A sample syntax extension that uses OCaml's new extension points API. 5 | 6 | Take a look at [the guide to extension points in OCaml][guide]. 7 | 8 | [guide]: http://whitequark.org/blog/2014/04/16/a-guide-to-extension-points-in-ocaml/ 9 | 10 | License 11 | ------- 12 | 13 | [Public domain](LICENSE.txt) 14 | -------------------------------------------------------------------------------- /dune-project: -------------------------------------------------------------------------------- 1 | (lang dune 2.0) 2 | (name ppx_getenv) 3 | (version 2.1) 4 | 5 | (formatting disabled) 6 | (generate_opam_files true) 7 | 8 | (maintainers "whitequark ") 9 | (authors "whitequark ") 10 | (license "Public domain") 11 | (source (github ocaml-ppx/ppx_getenv)) 12 | 13 | (package 14 | (name ppx_getenv) 15 | (synopsis "A sample syntax extension using OCaml's new extension points API") 16 | (tags ("syntax")) 17 | (depends 18 | (ocaml (>= 4.04.0)) 19 | (ppxlib (>= 0.18.0)) 20 | (ounit2 :with-test) 21 | (odoc :with-doc))) 22 | -------------------------------------------------------------------------------- /ppx_getenv.opam: -------------------------------------------------------------------------------- 1 | # This file is generated by dune, edit dune-project instead 2 | opam-version: "2.0" 3 | version: "2.1" 4 | synopsis: "A sample syntax extension using OCaml's new extension points API" 5 | maintainer: ["whitequark "] 6 | authors: ["whitequark "] 7 | license: "Public domain" 8 | tags: ["syntax"] 9 | homepage: "https://github.com/ocaml-ppx/ppx_getenv" 10 | bug-reports: "https://github.com/ocaml-ppx/ppx_getenv/issues" 11 | depends: [ 12 | "dune" {>= "2.0"} 13 | "ocaml" {>= "4.04.0"} 14 | "ppxlib" {>= "0.18.0"} 15 | "ounit2" {with-test} 16 | "odoc" {with-doc} 17 | ] 18 | build: [ 19 | ["dune" "subst"] {pinned} 20 | [ 21 | "dune" 22 | "build" 23 | "-p" 24 | name 25 | "-j" 26 | jobs 27 | "@install" 28 | "@runtest" {with-test} 29 | "@doc" {with-doc} 30 | ] 31 | ] 32 | dev-repo: "git+https://github.com/ocaml-ppx/ppx_getenv.git" 33 | -------------------------------------------------------------------------------- /src/dune: -------------------------------------------------------------------------------- 1 | (library 2 | (name ppx_getenv) 3 | (public_name ppx_getenv) 4 | (kind ppx_rewriter) 5 | (libraries ppxlib)) 6 | -------------------------------------------------------------------------------- /src/ppx_getenv.ml: -------------------------------------------------------------------------------- 1 | open Ppxlib 2 | 3 | let getenv s = try Sys.getenv s with Not_found -> "" 4 | 5 | let expander ~loc ~path:_ = function 6 | | (* Should have a single structure item, which is evaluation of a constant string. *) 7 | PStr [{ pstr_desc = 8 | Pstr_eval ({ pexp_loc = loc; 9 | pexp_desc = Pexp_constant (Pconst_string (sym, _, None)); _ }, _); _ }] -> 10 | (* Replace with a constant string with the value from the environment. *) 11 | Ast_builder.Default.estring ~loc (getenv sym) 12 | | _ -> 13 | Location.raise_errorf ~loc "[%%getenv] accepts a string, e.g. [%%getenv \"USER\"]" 14 | 15 | let extension = 16 | Context_free.Rule.extension 17 | (Extension.declare "getenv" Expression Ast_pattern.(__) expander) 18 | 19 | let () = Ppxlib.Driver.register_transformation ~rules:[extension] "ppx_getenv" 20 | -------------------------------------------------------------------------------- /src_test/dune: -------------------------------------------------------------------------------- 1 | (env (_ (env-vars (PPX_GETENV_CHECK 42)))) 2 | 3 | (test 4 | (name test_ppx_getenv) 5 | (preprocess (pps ppx_getenv)) 6 | (libraries ounit2)) 7 | -------------------------------------------------------------------------------- /src_test/test_ppx_getenv.ml: -------------------------------------------------------------------------------- 1 | open OUnit2 2 | 3 | let test_ppx_getenv _ = 4 | (* set in myocamlbuild.ml *) 5 | assert_equal "42" [%getenv "PPX_GETENV_CHECK"] 6 | 7 | let suite = "Test ppx_getenv" >::: [ 8 | "test_ppx_getenv" >:: test_ppx_getenv; 9 | ] 10 | 11 | let _ = 12 | run_test_tt_main suite 13 | --------------------------------------------------------------------------------