Packages

Transform SwiftUI code into LiveViewNative templates

Current section

Files

Jump to
swiftui_2_lvn lib swiftui_2_lvn cli.ex
Raw

lib/swiftui_2_lvn/cli.ex

defmodule Swiftui2Lvn.CLI do
@moduledoc """
Simple CLI for SwiftUI to LiveViewNative transformer.
Reads SwiftUI code from stdin, file path, or command line args and outputs LVN code to stdout.
## Usage
# From stdin
echo "VStack { Text('Hello') }" | swiftui2lvn
# From file
swiftui2lvn --file path/to/swiftui_code.swift
swiftui2lvn -f path/to/swiftui_code.swift
# From command line args
swiftui2lvn "VStack { Text('Hello') }"
"""
def main(args) do
try do
input = get_input(args)
# Use the library's public API to convert SwiftUI to LVN
output = Swiftui2Lvn.convert(input)
# Write to stdout
IO.write(output)
rescue
error ->
IO.puts(:stderr, "Error: #{inspect(error)}")
System.halt(1)
end
end
defp get_input([]), do: IO.read(:all) |> String.trim()
defp get_input(["--file", file_path | _rest]) do
read_file(file_path)
end
defp get_input(["-f", file_path | _rest]) do
read_file(file_path)
end
defp get_input(args) do
# Args provided, join them as the input
Enum.join(args, " ")
end
defp read_file(file_path) do
case File.read(file_path) do
{:ok, content} -> String.trim(content)
{:error, reason} ->
raise "Failed to read file '#{file_path}': #{:file.format_error(reason)}"
end
end
end