This document is about creating a CLI app written in Dart; for a mobile app follow our Flutter Quick Start. However it's recommended to first give a try to the current document.
ClojureDart needs at least Java 9.
If you already have Dart installed, make sure your version of the sdk is at least 2.12 -- this version introduced a big change (types are not nullable by default) to the language, use dart --version
to check. Code produced by ClojureDart wouldn't be compatible with previous versions of Dart.
If you already have the clj
command installed make sure to upgrade to at least the 1.10.3.814. This release allows to easily use private git deps.
First, create a Clojure project, you need to specify it's a pure Dart (not Flutter) project and where is the main
function (here quickstart.helloworld
):
mkdir helloworld
cd helloworld
cat << EOF > deps.edn
{:paths ["src"] ; where your cljd files will live
:deps {org.clojure/clojure {:mvn/version "1.10.1"}
tensegritics/clojuredart
{:git/url "[email protected]:tensegritics/ClojureDart.git"
; or "https://github.com/tensegritics/ClojureDart.git"
:sha "6127d8ef23156f2be5b10e97a0932868d175e585"}}
:aliases {:cljd {:main-opts ["-m" "cljd.build"]}}
:cljd/opts {:kind :dart
:main quickstart.helloworld}}
EOF
Then, you need to prepare this project to also be a Dart project:
clj -M:cljd init
And add the main namespace:
mkdir -p src/quickstart
cat << EOF > src/quickstart/helloworld.cljd
(ns quickstart.helloworld)
(defn main []
(print "hello, world\n"))
EOF
The src
directory isn't special, you are free to layout your project as you like, as long as you don't
interfere with Dart's project layout (bin
and lib
especially).
By default compilation starts from the main namespace (here quickstart.helloworld
) and transitively compiles dependencies.
clj -M:cljd compile
The above command compiles the project only once and exits. When you are actively working on a piece of code we recommend you use watch
instead of compile
:
clj -M:cljd watch
Compiled Dart files are found under lib/cljd-out
; to execute the program, just type:
dart run
By doing so you have run your program on the Dart VM. To get an actual executable, enter:
dart compile exe -o helloworld bin/helloworld.dart
Without the -o helloworld
option it would have created a helloworld.exe
alongside helloworld.dart
.
Write more clojure code, new namespaces, have fun. The watcher will pick up your changes. Then execute your dart file again.