Skip to content

Commit 0f3065a

Browse files
author
Chris Nevers
committed
Initial commit
0 parents  commit 0f3065a

File tree

6 files changed

+231
-0
lines changed

6 files changed

+231
-0
lines changed

.gitignore

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
*.annot
2+
*.cmo
3+
*.cma
4+
*.cmi
5+
*.a
6+
*.o
7+
*.cmx
8+
*.cmxs
9+
*.cmxa
10+
11+
# ocamlbuild working directory
12+
_build/
13+
14+
# ocamlbuild targets
15+
*.byte
16+
*.native
17+
18+
# oasis generated files
19+
setup.data
20+
setup.log
21+
22+
# Merlin configuring file for Vim and Emacs
23+
.merlin
24+
25+
# Dune generated files
26+
*.install
27+
28+
# Local OPAM switch
29+
_opam/
30+
notes
31+
*DS_Store

Makefile

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
all: build
2+
3+
build:
4+
@dune build @all
5+
cp -f _build/default/mkocaml.exe /usr/local/bin/mkocaml
6+
7+
install:
8+
@dune install
9+
10+
test: build
11+
@dune runtest
12+
13+
doc: build
14+
@opam install odoc
15+
@dune build @doc
16+
17+
clean:
18+
@dune clean
19+

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# 🐪 Mkocaml
2+
3+
A simple helper I use to generate OCaml projects.
4+
5+
This tool generates:
6+
* Git repository
7+
* Git ignore
8+
* Executable/Library
9+
* Makefile with no nonsense commands
10+
* Copy of the executable to `/usr/local/bin`
11+
12+
I constantly find myself struggling to remember `dune` commands for
13+
various tasks. This project is intended to solve that.
14+
15+
16+
# Example
17+
Creating a new executable
18+
19+
> mkocaml -e new
20+
> make
21+
> new
22+
Hello, World!

dune

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
(executable
2+
(name mkocaml))

dune-project

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(lang dune 1.11)

mkocaml.ml

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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

Comments
 (0)