poly
Search…
Base
A base is similar to a component except for two things:
  • It doesn't have an interface.
  • It exposes a public API to the outside world.
The lack of an interface makes bases less composable compared to components. This is okay because they serve a different purpose which is to be a bridge between the real world and the components the base delegates to. This gives us the modularity and structure we need to build simple and understandable services and tools.
Let's create the cli base to see how it works, by executing the create base command:
poly create base name:cli
Our workspace should now look like this:
example
├── bases
│ └── cli
│ ├── deps.edn
│ ├── resources
│ │ └── cli
│ ├── src
│ │ └── se
│ │ └── example
│ │ └── cli
│ │ └── core.clj
│ └── test
│ └── se
│ └── example
│ └── cli
│ └── core_test.clj
├── components
│ └── user
│ ├── deps.edn
│ ├── resources
│ │ └── user
│ ├── src
│ │ └── se
│ │ └── example
│ │ └── user
│ │ ├── core.clj
│ │ └── interface.clj
│ └── test
│ └── se
│ └── example
│ └── user
│ └── interface_test.clj
├── deps.edn
├── development
│ └── src
│ └── dev
│ └── lisa.clj
├── logo.png
├── projects
├── readme.md
└── workspace.edn
Now we need to update ./deps.edn with our newly created base:
:aliases {:dev {:extra-paths ["development/src"
"components/user/src"
"components/user/resources"
"bases/cli/src"
"bases/cli/resources"]
:extra-deps {org.clojure/clojure {:mvn/version "1.10.1"}
org.clojure/tools.deps.alpha {:mvn/version "0.12.1003"}}}
:test {:extra-paths ["components/user/test"
"bases/cli/test"]}
...and add some code to it:
(ns se.example.cli.core
(:require [se.example.user.interface :as user])
(:gen-class))
(defn -main [& args]
(println (user/hello (first args)))
(System/exit 0))
Here we added the -main function that will later be called from the command line. The (:gen-class) statement tells the compiler to generate a Java class for us when the code is compiled.
A deps.edn file was also created with this content:
{:paths ["src" "resources"]
:deps {}
:aliases {:test {:extra-paths ["test"]
:extra-deps {}}}}
This config file is identical to the user config file, and will soon be needed when we create a project that includes it.
The next thing we want to do is to build an artifact that will turn the code into something useful, a command-line tool. To do that, we need to start by creating a project.
Export as PDF
Copy link