Shadix.Generator (shadix v0.0.1)

Copy Markdown View Source

Pure helpers for the Shadix generator tooling.

Provides namespace rewriting, default namespace derivation, and default target directory derivation. Kept separate from Mix tasks so it is unit-testable without invoking Mix machinery.

Summary

Functions

Returns the default target directory for a given namespace.

Returns the default target namespace for the current Mix project.

Rewrites canonical Shadix.* module references in source to use namespace as the new base.

Functions

default_dir(namespace)

@spec default_dir(String.t()) :: String.t()

Returns the default target directory for a given namespace.

Derived as lib/<underscored_namespace>.

iex> Shadix.Generator.default_dir("MyAppWeb.Components.UI")
"lib/my_app_web/components/ui"

default_namespace()

@spec default_namespace() :: String.t()

Returns the default target namespace for the current Mix project.

Derived as <CamelizedApp>Web.Components.UI.

iex> Shadix.Generator.default_namespace()
"ShadixWeb.Components.UI"

rewrite_namespace(source, namespace)

@spec rewrite_namespace(String.t(), String.t()) :: String.t()

Rewrites canonical Shadix.* module references in source to use namespace as the new base.

Three ordered replacements are applied:

  1. Shadix.Componentsnamespace
  2. Shadix.Cnnamespace <> ".Cn"
  3. Shadix.Formnamespace <> ".Form"

The order matters: Shadix.Components is replaced first so the substring Shadix.Cn is never accidentally matched inside a Shadix.Components.* name.

iex> Shadix.Generator.rewrite_namespace("import Shadix.Cn", "Demo.UI")
"import Demo.UI.Cn"