|
| 1 | +let lib_name = ref "" |
| 2 | +let exe_name = ref "" |
| 3 | + |
| 4 | +let bad_arg arg = |
| 5 | + raise @@ Arg.Bad (Format.sprintf "Unrecognized argument: %s" arg) |
| 6 | + |
| 7 | +let usage = |
| 8 | + Format.sprintf "Usage: %s [-l name] [-e name]" Sys.argv.(0) |
| 9 | + |
| 10 | +let specs = [ |
| 11 | + "-l", Arg.Set_string lib_name, ": Creates a library with the given name"; |
| 12 | + "-e", Arg.Set_string exe_name, ": Creates an executable with the given name"; |
| 13 | +] |
| 14 | + |
| 15 | +let makefile project project_type = |
| 16 | + let copy_exe = match project_type with |
| 17 | + | `Exec -> "\t@cp -f _build/default/bin/main.exe /usr/local/bin/" ^ project |
| 18 | + | _ -> "" |
| 19 | + in |
| 20 | + Format.sprintf |
| 21 | +"all: build |
| 22 | +
|
| 23 | +build: |
| 24 | +\t@dune build @all |
| 25 | +%s |
| 26 | +
|
| 27 | +install: |
| 28 | +\t@dune install |
| 29 | +
|
| 30 | +test: build |
| 31 | +\t@dune runtest |
| 32 | +
|
| 33 | +doc: build |
| 34 | +\t@opam install odoc |
| 35 | +\t@dune build @doc |
| 36 | + |
| 37 | +clean: |
| 38 | +\t@dune clean |
| 39 | +
|
| 40 | +publish: |
| 41 | +\t@opam pin . |
| 42 | +\t@opam publish . |
| 43 | +" |
| 44 | + copy_exe |
| 45 | + |
| 46 | +let opam project = |
| 47 | + Format.sprintf |
| 48 | +" |
| 49 | +opam-version: \"2.0\" |
| 50 | +version: \"1.0\" |
| 51 | +authors: \"Chris Nevers <[email protected]>\" |
| 52 | +maintainer: \"Chris Nevers <[email protected]>\" |
| 53 | +homepage: \"https://github.com/chrisnevers/%s\" |
| 54 | +bug-reports: \"https://github.com/chrisnevers/%s/issues\" |
| 55 | +dev-repo: \"git://github.com/chrisnevers/%s.git\" |
| 56 | +synopsis: \"\" |
| 57 | +build: [ |
| 58 | + [\"dune\" \"subst\"] {pinned} |
| 59 | + [\"dune\" \"build\" \"-p\" name \"-j\" jobs] |
| 60 | +] |
| 61 | +
|
| 62 | +depends: [ |
| 63 | + \"dune\" {build} |
| 64 | + \"alcotest\" {with-test} |
| 65 | +] |
| 66 | +" |
| 67 | +project project project |
| 68 | + |
| 69 | +let gitignore = |
| 70 | +" |
| 71 | +*.annot |
| 72 | +*.cmo |
| 73 | +*.cma |
| 74 | +*.cmi |
| 75 | +*.a |
| 76 | +*.o |
| 77 | +*.cmx |
| 78 | +*.cmxs |
| 79 | +*.cmxa |
| 80 | +
|
| 81 | +# ocamlbuild working directory |
| 82 | +_build/ |
| 83 | +
|
| 84 | +# ocamlbuild targets |
| 85 | +*.byte |
| 86 | +*.native |
| 87 | +
|
| 88 | +# oasis generated files |
| 89 | +setup.data |
| 90 | +setup.log |
| 91 | +
|
| 92 | +# Merlin configuring file for Vim and Emacs |
| 93 | +.merlin |
| 94 | +
|
| 95 | +# Dune generated files |
| 96 | +*.install |
| 97 | +
|
| 98 | +# Local OPAM switch |
| 99 | +_opam/ |
| 100 | +notes |
| 101 | +*DS_Store |
| 102 | +" |
| 103 | + |
| 104 | +let setup_opam project = |
| 105 | + match Sys.file_exists (project ^ ".opam") with |
| 106 | + | true -> () |
| 107 | + | false -> |
| 108 | + let chan = open_out (project ^ ".opam") in |
| 109 | + output_string chan (opam project); |
| 110 | + close_out chan; |
| 111 | + let _ = Sys.command "dune build @install" in |
| 112 | + () |
| 113 | + |
| 114 | +let setup_exe project = |
| 115 | + let _ = Format.sprintf "mkdir -p bin && cd bin && dune init exec %s" project |
| 116 | + |> Sys.command in |
| 117 | + begin match Sys.file_exists "Makefile" with |
| 118 | + | true -> () |
| 119 | + | false -> |
| 120 | + let chan = open_out "Makefile" in |
| 121 | + output_string chan (makefile project `Exec); |
| 122 | + close_out chan |
| 123 | + end; |
| 124 | + setup_opam project |
| 125 | + |
| 126 | +let setup_lib project = |
| 127 | + let _ = Format.sprintf |
| 128 | + "mkdir -p lib && cd lib && dune init lib %s && mv ../dune . && touch %s.ml" |
| 129 | + project project |
| 130 | + |> Sys.command in |
| 131 | + begin match Sys.file_exists "Makefile" with |
| 132 | + | true -> () |
| 133 | + | false -> |
| 134 | + let chan = open_out "Makefile" in |
| 135 | + output_string chan (makefile project `Lib); |
| 136 | + close_out chan |
| 137 | + end; |
| 138 | + setup_opam project |
| 139 | + |
| 140 | +let setup_git () = |
| 141 | + let _ = Sys.command ("git init"); in |
| 142 | + let chan = open_out ".gitignore" in |
| 143 | + output_string chan gitignore; |
| 144 | + close_out chan |
| 145 | + |
| 146 | +let () = |
| 147 | + Arg.parse specs bad_arg usage; |
| 148 | + if String.length !lib_name > 0 then |
| 149 | + let _ = setup_git () in |
| 150 | + setup_lib !lib_name |
| 151 | + else |
| 152 | + if String.length !exe_name > 0 then |
| 153 | + let _ = setup_git () in |
| 154 | + setup_exe !exe_name |
| 155 | + else |
| 156 | + print_endline usage |
0 commit comments