Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Nevers committed Jan 1, 2020
0 parents commit 0f3065a
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
*.annot
*.cmo
*.cma
*.cmi
*.a
*.o
*.cmx
*.cmxs
*.cmxa

# ocamlbuild working directory
_build/

# ocamlbuild targets
*.byte
*.native

# oasis generated files
setup.data
setup.log

# Merlin configuring file for Vim and Emacs
.merlin

# Dune generated files
*.install

# Local OPAM switch
_opam/
notes
*DS_Store
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
all: build

build:
@dune build @all
cp -f _build/default/mkocaml.exe /usr/local/bin/mkocaml

install:
@dune install

test: build
@dune runtest

doc: build
@opam install odoc
@dune build @doc

clean:
@dune clean

22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 🐪 Mkocaml

A simple helper I use to generate OCaml projects.

This tool generates:
* Git repository
* Git ignore
* Executable/Library
* Makefile with no nonsense commands
* Copy of the executable to `/usr/local/bin`

I constantly find myself struggling to remember `dune` commands for
various tasks. This project is intended to solve that.


# Example
Creating a new executable

> mkocaml -e new
> make
> new
Hello, World!
2 changes: 2 additions & 0 deletions dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(executable
(name mkocaml))
1 change: 1 addition & 0 deletions dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 1.11)
156 changes: 156 additions & 0 deletions mkocaml.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
let lib_name = ref ""
let exe_name = ref ""

let bad_arg arg =
raise @@ Arg.Bad (Format.sprintf "Unrecognized argument: %s" arg)

let usage =
Format.sprintf "Usage: %s [-l name] [-e name]" Sys.argv.(0)

let specs = [
"-l", Arg.Set_string lib_name, ": Creates a library with the given name";
"-e", Arg.Set_string exe_name, ": Creates an executable with the given name";
]

let makefile project project_type =
let copy_exe = match project_type with
| `Exec -> "\t@cp -f _build/default/bin/main.exe /usr/local/bin/" ^ project
| _ -> ""
in
Format.sprintf
"all: build
build:
\t@dune build @all
%s
install:
\t@dune install
test: build
\t@dune runtest
doc: build
\t@opam install odoc
\t@dune build @doc
clean:
\t@dune clean
publish:
\t@opam pin .
\t@opam publish .
"
copy_exe

let opam project =
Format.sprintf
"
opam-version: \"2.0\"
version: \"1.0\"
authors: \"Chris Nevers <[email protected]>\"
maintainer: \"Chris Nevers <[email protected]>\"
homepage: \"https://github.com/chrisnevers/%s\"
bug-reports: \"https://github.com/chrisnevers/%s/issues\"
dev-repo: \"git://github.com/chrisnevers/%s.git\"
synopsis: \"\"
build: [
[\"dune\" \"subst\"] {pinned}
[\"dune\" \"build\" \"-p\" name \"-j\" jobs]
]
depends: [
\"dune\" {build}
\"alcotest\" {with-test}
]
"
project project project

let gitignore =
"
*.annot
*.cmo
*.cma
*.cmi
*.a
*.o
*.cmx
*.cmxs
*.cmxa
# ocamlbuild working directory
_build/
# ocamlbuild targets
*.byte
*.native
# oasis generated files
setup.data
setup.log
# Merlin configuring file for Vim and Emacs
.merlin
# Dune generated files
*.install
# Local OPAM switch
_opam/
notes
*DS_Store
"

let setup_opam project =
match Sys.file_exists (project ^ ".opam") with
| true -> ()
| false ->
let chan = open_out (project ^ ".opam") in
output_string chan (opam project);
close_out chan;
let _ = Sys.command "dune build @install" in
()

let setup_exe project =
let _ = Format.sprintf "mkdir -p bin && cd bin && dune init exec %s" project
|> Sys.command in
begin match Sys.file_exists "Makefile" with
| true -> ()
| false ->
let chan = open_out "Makefile" in
output_string chan (makefile project `Exec);
close_out chan
end;
setup_opam project

let setup_lib project =
let _ = Format.sprintf
"mkdir -p lib && cd lib && dune init lib %s && mv ../dune . && touch %s.ml"
project project
|> Sys.command in
begin match Sys.file_exists "Makefile" with
| true -> ()
| false ->
let chan = open_out "Makefile" in
output_string chan (makefile project `Lib);
close_out chan
end;
setup_opam project

let setup_git () =
let _ = Sys.command ("git init"); in
let chan = open_out ".gitignore" in
output_string chan gitignore;
close_out chan

let () =
Arg.parse specs bad_arg usage;
if String.length !lib_name > 0 then
let _ = setup_git () in
setup_lib !lib_name
else
if String.length !exe_name > 0 then
let _ = setup_git () in
setup_exe !exe_name
else
print_endline usage

0 comments on commit 0f3065a

Please sign in to comment.