Implicit modules and script style plugins

I want to work on a DSL that looks very similar to unit tests, define some steps but the steps are linked

step "step 1" do
    IO.puts("hi")
end

step "step 2", depends: ["step 1"] do
    IO.puts("bye")
end

Forcing a user to define a module at the top feels possibly distracting.

Two questions, one how should I go about loading an exs at runtime and calling it (I assume Code.compile_file

Second more important question, how might I go about dynamically generating a module or infering one maybe from the filename.

1 Like

Dynamically generating modules is easier than you’d expect,

def make_module(stuff) do
  Module.create(derived_module_name(stuff), quote do
    # your module body goes here
  end,
  Macro.Env.location(__ENV__))
end

As for running a .exs file, I think your best bet would be Code.eval_file/2. Also, depending on what you’re looking to do, you may want to look into Reactor: Read Me — reactor v0.4.1, as it may do what you’re looking to do already. And for defining the DSL you may look into Spark: Get Started With Spark — spark v1.1.53, which is a DSL building toolkit. Still sorely under-documented, but very feature rich. If you’re willing to read the tests to figure out how it works, then you’ll be A-ok. It’s very battle tested.

1 Like

Id probably still have to wrap the content of the .exs file so I might end up using some sort of string eval.