Skip to content

Commit ee3cdde

Browse files
authored
Merge pull request #1302 from liveview-native/bc-mix-task
Rewrite of xcodegen task
2 parents e791deb + 19fc5a8 commit ee3cdde

31 files changed

+153
-203
lines changed

config/config.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ import Config
33
config :logger, :level, :debug
44
config :logger, :backends, []
55

6+
config :live_view_native, plugins: [
7+
LiveViewNative.SwiftUI
8+
]
9+
610
import_config "#{config_env()}.exs"

config/test.exs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@ config :live_view_native_stylesheet, :parsers, swiftui: LiveViewNative.SwiftUI.R
55
config :live_view_native_stylesheet,
66
annotations: false
77

8-
config :live_view_native, plugins: [
9-
LiveViewNative.SwiftUI
10-
]
11-
12-
config :live_view_native, plugins: [
13-
LiveViewNative.SwiftUI
14-
]
15-
168
config :phoenix_template, format_encoders: [
179
swiftui: Phoenix.HTML.Engine
1810
]
@@ -39,7 +31,7 @@ config :live_view_native_stylesheet,
3931
content: [
4032
swiftui: [
4133
"test/**/*.*"
42-
34+
4335
]
4436
],
45-
output: "priv/static/assets"
37+
output: "priv/static/assets"

lib/mix/tasks/lvn.gen.swiftui.ex

Lines changed: 0 additions & 179 deletions
This file was deleted.

lib/mix/tasks/generate_documentation.ex renamed to lib/mix/tasks/lvn.swiftui.gen.docs.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule Mix.Tasks.Lvn.SwiftUi.GenerateDocumentation do
1+
defmodule Mix.Tasks.Lvn.Swiftui.Gen.Docs do
22
@moduledoc "Generates ex doc files for all SwiftUI views"
33

44
use Mix.Task

lib/mix/tasks/lvn.swiftui.gen.ex

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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

Comments
 (0)