Jetons.Transformer behaviour (jetons v0.2.2)

Copy Markdown View Source

Behaviour for transforming design token configs to output formats.

Transformers receive raw DTCG config maps and output a list of {path, content} tuples representing files to write.

Example

defmodule MyApp.IOSTransformer do
  use Jetons.Transformer

  def init(opts) do
    {:ok, %{class_name: opts[:class_name] || "Tokens"}}
  end

  def transform(raw_config, opts, state) do
    swift = generate_swift(raw_config, state)
    {:ok, [{"lib/#{state.class_name}.swift", swift}]}
  end
end

Config

config :jetons,
  ios: [
    transformer: MyApp.IOSTransformer,
    class_name: "DesignTokens"
  ]

Summary

Callbacks

Initialize transformer state from config options.

Transform raw DTCG config to output files.

Callbacks

init(opts)

@callback init(opts :: keyword()) :: {:ok, state :: term()} | {:error, String.t()}

Initialize transformer state from config options.

Called once when the build task starts. Return {:ok, state} or {:error, reason}.

transform(raw_config, opts, state)

@callback transform(
  raw_config :: map(),
  opts :: keyword(),
  state :: term()
) :: {:ok, [{path :: String.t(), content :: iodata()}]} | {:error, String.t()}

Transform raw DTCG config to output files.

Receives the resolved, flattened config map. Returns a list of {path, content} tuples for files to write.

Examples

# Single file
{:ok, [{"output.css", css_content}]}

# Multiple files  
{:ok, [
  {"Colors.swift", swift_code},
  {"Colors.h", header_code}
]}