|
| 1 | +defmodule Mix.Tasks.Lvn.Swiftui.Gen do |
| 2 | + use Mix.Task |
| 3 | + |
| 4 | + alias Mix.LiveViewNative.Context |
| 5 | + |
| 6 | + @shortdoc "Generates the SwiftUI Project for LiveView Native" |
| 7 | + def run(args) do |
| 8 | + args = List.insert_at(args, 0, "swiftui") |
| 9 | + |
| 10 | + context = Context.build(args, __MODULE__) |
| 11 | + |
| 12 | + files = files_to_be_generated(context) |
| 13 | + |
| 14 | + context |
| 15 | + |> install_xcodegen() |
| 16 | + |> copy_new_files(files) |
| 17 | + |> run_xcodegen() |
| 18 | + |> remove_xcodegen_files() |
| 19 | + |
| 20 | + :ok |
| 21 | + end |
| 22 | + |
| 23 | + def switches, do: [ |
| 24 | + context_app: :string, |
| 25 | + web: :string |
| 26 | + ] |
| 27 | + |
| 28 | + def validate_args!([format]), do: [format] |
| 29 | + def validate_args!(_args) do |
| 30 | + Mix.raise(""" |
| 31 | + mix lvn.swiftui.gen does not take any arguments, only the following switches: |
| 32 | +
|
| 33 | + --context-app |
| 34 | + --web |
| 35 | + """) |
| 36 | + end |
| 37 | + |
| 38 | + defp install_xcodegen(context) do |
| 39 | + unless System.find_executable("xcodegen") do |
| 40 | + cond do |
| 41 | + # Install with Mint |
| 42 | + System.find_executable("mint") -> |
| 43 | + status_message("running", "mint install yonaskolb/xcodegen") |
| 44 | + System.cmd("mint", ["install", "yonaskolb/xcodegen"]) |
| 45 | + |
| 46 | + # Install with Homebrew |
| 47 | + System.find_executable("brew") -> |
| 48 | + status_message("running", "brew install xcodegen") |
| 49 | + System.cmd("brew", ["install", "xcodegen"]) |
| 50 | + |
| 51 | + # Clone from GitHub (fallback) |
| 52 | + true -> |
| 53 | + File.mkdir_p("_build/tmp/xcodegen") |
| 54 | + status_message("running", "git clone https://github.com/yonaskolb/XcodeGen.git") |
| 55 | + System.cmd("git", ["clone", "https://github.com/yonaskolb/XcodeGen.git", "_build/tmp/xcodegen"]) |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + context |
| 60 | + end |
| 61 | + |
| 62 | + def files_to_be_generated(context) do |
| 63 | + root = |
| 64 | + Mix.Project.deps_paths[:live_view_native_swiftui] |
| 65 | + |> Path.join("priv/templates/lvn.swiftui.gen/xcodegen/") |
| 66 | + |
| 67 | + Path.wildcard(Path.join([root, "**/*"])) |
| 68 | + |> Enum.filter(&(!File.dir?(&1))) |
| 69 | + |> Enum.map(fn(path) -> |
| 70 | + type = |
| 71 | + path |
| 72 | + |> Path.extname() |
| 73 | + |> case do |
| 74 | + ".swift" -> :eex |
| 75 | + _any -> :text |
| 76 | + end |
| 77 | + |
| 78 | + path = Path.relative_to(path, root) |
| 79 | + |
| 80 | + {type, path, rewrite_file_path(path, context)} |
| 81 | + end) |
| 82 | + end |
| 83 | + |
| 84 | + defp rewrite_file_path(file_path, %{base_module: base_module, native_path: native_path}) do |
| 85 | + file_path = String.replace(file_path, "TemplateApp", inspect(base_module)) |
| 86 | + Path.join(native_path, file_path) |
| 87 | + end |
| 88 | + |
| 89 | + defp copy_new_files(%Context{} = context, files) do |
| 90 | + binding = [ |
| 91 | + context: context, |
| 92 | + assigns: %{ |
| 93 | + app_namespace: inspect(context.base_module) |
| 94 | + } |
| 95 | + ] |
| 96 | + |
| 97 | + apps = Context.apps(context.format) |
| 98 | + |
| 99 | + Mix.Phoenix.copy_from(apps, "priv/templates/lvn.swiftui.gen/xcodegen", binding, files) |
| 100 | + |
| 101 | + context |
| 102 | + end |
| 103 | + |
| 104 | + defp run_xcodegen(%{base_module: base_module, native_path: native_path} = context) do |
| 105 | + xcodegen_env = [ |
| 106 | + {"LVN_APP_NAME", inspect(base_module)}, |
| 107 | + {"LVN_BUNDLE_IDENTIFIER", "com.example.#{inspect(base_module)}"} |
| 108 | + ] |
| 109 | + |
| 110 | + if File.exists?("_build/tmp/xcodegen") do |
| 111 | + xcodegen_spec_path = Path.join(native_path, "project.yml") |
| 112 | + |
| 113 | + System.cmd("swift", ["run", "xcodegen", "generate", "-s", xcodegen_spec_path], cd: "_build/tmp/xcodegen", env: xcodegen_env) |
| 114 | + else |
| 115 | + System.cmd("xcodegen", ["generate"], cd: native_path, env: xcodegen_env) |
| 116 | + end |
| 117 | + |
| 118 | + context |
| 119 | + end |
| 120 | + |
| 121 | + defp remove_xcodegen_files(%{native_path: native_path} = context) do |
| 122 | + ["base_spec.yml", "project_watchos.yml", "project.yml", "skip_spec.yml"] |
| 123 | + |> Enum.map(&(Path.join(native_path, &1))) |
| 124 | + |> Enum.map(&File.rm/1) |
| 125 | + |
| 126 | + context |
| 127 | + end |
| 128 | + |
| 129 | + defp status_message(label, message) do |
| 130 | + formatted_message = IO.ANSI.green() <> "* #{label} " <> IO.ANSI.reset() <> message |
| 131 | + |
| 132 | + IO.puts(formatted_message) |
| 133 | + end |
| 134 | +end |
0 commit comments