Dune how to define custom build task

dune has very powerful extensions, but the documentation doesn’t tell you directly. Today I’ll share a specific example of how we can make dune do many things with a dune configuration.

For example

Publish compiled documents to our documentation server
Sending email notifications to email groups
Sending SMS notifications to administrators
Build a document and open a browser to preview the document page

Let’s start with an example, we create a dune file in the root directory of our project, which you may not have originally, you have to create a new one, we enter the following

; now we tell you how to define a custom rule 
; rule start with (rule )
(rule 
; (alias is point  the command name , so you can run this rule by call  dune build @docopen
 (alias docopen) 
 ; following line is very important, it tell dune do not cache this build command, so it will running every call without any cache
 (deps (universe))
 ; action  (system  to told system run command by `sh` in your Linux/MacOS, windows user may running cmd.exe 
 ; cd ../.. is change the base directory of the running command ,or the default directory will be _build/default  
 (action (system "cd ../.. && pwd &&  dune build @doc && open _build/default/_doc/_html/index.html" ))
)
; end of one piece of rule

; and we define more and more rule as we want 
(rule
  (alias whoami)
  (deps (universe))
  (action (system "uname -a;whoami"))
)

In this example, we define two rules, the rules are the tasks that dune can recognize, in dune, it is called rules

Because it is a custom build command, we use alias to take a unique and non-repeating alias.

The first build command is to build the document and open the browser preview.

Our alias is docopen

Then deps we add universe to tell dune that you don’t want to cache and give me a new build every time. If you don’t add this line, dune will only give you one build, and then because of the cache, you won’t be able to execute it later.

action following by system here, action is the command to start, system means to use the system shell (windows is cmd, linux macos is sh) to give you the execution of the code you specify.

You can see the first we are first change the directory to the project root directory [because the default directory is _build/default], and then we perform the build document generation, and then open open the generated html page.

The first build command is this, if you want to perform the first build task, you can type

dune build @docopen

Then our second build command, relatively simple, with reference to the first, we can add a lot of build commands we want to add inside this dune configuration file.

We just need to specify different alias aliases for them, no duplication.

The official documentation also specifies some other available commands, I won’t go into them one by one. Since I prefer to use shell scripts, I really only need the system to execute my shell scripts for me.

2 Likes

Due to i was blocked by Ocaml github , i can not add issue nor contribute.
But i would like share my experience with some body, if you got some thing in trouble , my experience may help you out.

For something like that just create a page in your blog or your personal website, you can also host html web pages on github for your tutorials.

right,i post much more like this tutorial,but i am not telling and link here,respect community rules.