Skip to content

Commit fd25c88

Browse files
committed
Fix quoting; simplify and clean up code
Use quote/unquote to wire dynamic values into generated code. Make the installer idempotent, safe to run twice. Remove unused bits.
1 parent bdc5c31 commit fd25c88

1 file changed

Lines changed: 117 additions & 108 deletions

File tree

lib/mix/tasks/desktop.install.ex

Lines changed: 117 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,7 @@ defmodule Mix.Tasks.Desktop.Install do
1212
%Igniter.Mix.Task.Info{
1313
# Groups allow for overlapping arguments for tasks by the same author
1414
# See the generators guide for more.
15-
group: :desktop,
16-
# *other* dependencies to add
17-
# i.e `{:foo, "~> 2.0"}`
18-
adds_deps: [],
19-
# *other* dependencies to add and call their associated installers, if they exist
20-
# i.e `{:foo, "~> 2.0"}`
21-
installs: [],
22-
# An example invocation
23-
# example: __MODULE__.Docs.example(),
24-
# A list of environments that this should be installed in.
25-
only: nil,
26-
# a list of positional arguments, i.e `[:file]`
27-
positional: [],
28-
# Other tasks your task composes using `Igniter.compose_task`, passing in the CLI argv
29-
# This ensures your option schema includes options from nested tasks
30-
composes: [],
31-
# `OptionParser` schema
32-
schema: [],
33-
# Default values for the options in the `schema`
34-
defaults: [],
35-
# CLI aliases
36-
aliases: [],
37-
# A list of options in the schema that are required
38-
required: []
15+
group: :desktop
3916
}
4017
end
4118

@@ -50,96 +27,128 @@ defmodule Mix.Tasks.Desktop.Install do
5027

5128
igniter
5229
|> Igniter.compose_task("igniter.add", ["desktop"])
53-
|> Igniter.Project.Module.create_module(menubar, """
54-
@moduledoc """
55-
Menu bar that is shown as part of the main window on Windows/Linux. In
56-
MacOS this menu bar appears at the very top of the screen.
57-
\"""
58-
use Gettext, backend: #{gettext}
59-
use Desktop.Menu
60-
alias Desktop.Window
61-
62-
def render(assigns) do
63-
~H"""
64-
<menubar>
65-
<menu label={gettext "File"}>
66-
<item onclick="quit">{gettext "Quit"}</item>
67-
</menu>
68-
<menu label={gettext "Extra"}>
69-
<item onclick="observer">{gettext "Show Observer"}</item>
70-
<item onclick="browser">{gettext "Open Browser"}</item>
71-
</menu>
72-
</menubar>
30+
|> add_menu_bar(menubar, gettext, endpoint)
31+
|> add_menu(menu, gettext, app, main_window)
32+
|> Igniter.Project.Application.add_new_child(
33+
{
34+
Desktop.Window,
35+
{:code,
36+
quote do
37+
[
38+
app: unquote(app),
39+
id: unquote(Igniter.Project.Module.module_name(igniter, MainWindow)),
40+
title: unquote(to_string(app)),
41+
size: {600, 500},
42+
# icon: "icon.png", # TODO: ship an example taskbar icon here
43+
menubar: unquote(menubar),
44+
icon_menu: unquote(menu),
45+
url: &unquote(endpoint).url/0
46+
]
47+
end}
48+
},
49+
after: [endpoint]
50+
)
51+
52+
# TODO: Add runtime_tools observer if available, or optionall add the dependency
53+
end
54+
55+
defp add_menu_bar(igniter, menubar, gettext, endpoint) do
56+
Igniter.Project.Module.find_and_update_or_create_module(
57+
igniter,
58+
menubar,
59+
"""
60+
@moduledoc \"""
61+
Menu bar that is shown as part of the main window on Windows/Linux. In
62+
MacOS this menu bar appears at the very top of the screen.
7363
\"""
74-
end
75-
76-
def handle_event("quit", menu) do
77-
Window.quit()
78-
{:noreply, menu}
79-
end
80-
81-
def handle_event("observer", menu) do
82-
:observer.start()
83-
{:noreply, menu}
84-
end
85-
86-
def handle_event("browser", menu) do
87-
Window.prepare_url(#{endpoint}.url())
88-
|> :wx_misc.launchDefaultBrowser()
89-
90-
{:noreply, menu}
91-
end
92-
93-
def mount(menu) do
94-
{:ok, menu}
95-
end
96-
""")
97-
|> Igniter.Project.Module.create_module(menu, """
98-
@moduledoc """
99-
Menu that is shown when a user clicks on the taskbar icon of the #{app}
100-
\"""
101-
use Gettext, backend: #{gettext}
102-
use Desktop.Menu
103-
104-
def render(assigns) do
105-
~H"""
106-
<menu>
107-
<item onclick="edit">{gettext "Open"}</item>
108-
<hr/>
109-
<item onclick="quit">{gettext "Quit"}</item>
110-
</menu>
64+
use Gettext, backend: #{inspect(gettext)}
65+
use Desktop.Menu
66+
alias Desktop.Window
67+
68+
@impl true
69+
def render(assigns) do
70+
~H\"""
71+
<menubar>
72+
<menu label={gettext "File"}>
73+
<item onclick="quit">{gettext "Quit"}</item>
74+
</menu>
75+
<menu label={gettext "Extra"}>
76+
<item onclick="browser">{gettext "Open Browser"}</item>
77+
</menu>
78+
</menubar>
79+
\"""
80+
end
81+
82+
@impl true
83+
def handle_event("quit", menu) do
84+
Window.quit()
85+
{:noreply, menu}
86+
end
87+
88+
def handle_event("browser", menu) do
89+
Window.prepare_url(#{inspect(endpoint)}.url())
90+
|> :wx_misc.launchDefaultBrowser()
91+
92+
{:noreply, menu}
93+
end
94+
95+
@impl true
96+
def handle_info(_, menu) do
97+
{:noreply, menu}
98+
end
99+
100+
@impl true
101+
def mount(menu) do
102+
{:ok, menu}
103+
end
104+
""",
105+
fn zipper -> {:ok, zipper} end
106+
)
107+
end
108+
109+
defp add_menu(igniter, menu, gettext, app, main_window) do
110+
Igniter.Project.Module.find_and_update_or_create_module(
111+
igniter,
112+
menu,
113+
"""
114+
@moduledoc \"""
115+
Menu that is shown when a user clicks on the taskbar icon of the #{app}
111116
\"""
112-
end
117+
use Gettext, backend: #{inspect(gettext)}
118+
use Desktop.Menu
113119
114-
def handle_event(command, menu) do
115-
case command do
116-
<<"quit">> -> Desktop.Window.quit()
117-
<<"edit">> -> Desktop.Window.show(#{main_window})
120+
@impl true
121+
def render(assigns) do
122+
~H\"""
123+
<menu>
124+
<item onclick="edit">{gettext "Open"}</item>
125+
<hr/>
126+
<item onclick="quit">{gettext "Quit"}</item>
127+
</menu>
128+
\"""
118129
end
119130
120-
{:noreply, menu}
121-
end
131+
@impl true
132+
def handle_event(command, menu) do
133+
case command do
134+
<<"quit">> -> Desktop.Window.quit()
135+
<<"edit">> -> Desktop.Window.show(#{inspect(main_window)})
136+
end
122137
123-
def mount(menu) do
124-
{:ok, menu}
125-
end
126-
""")
127-
|> Igniter.Project.Application.add_new_child(
128-
{
129-
Desktop.Window,
130-
[
131-
app: app,
132-
id: Igniter.Project.Module.module_name(igniter, MainWindow),
133-
title: to_string(app),
134-
size: {600, 500},
135-
# icon: "icon.png", # TODO: ship an example taskbar icon here
136-
menubar: menubar,
137-
icon_menu: menu,
138-
url: fn -> apply(endpoint, :url, []) end
139-
]
140-
},
141-
after: [endpoint]
138+
{:noreply, menu}
139+
end
140+
141+
@impl true
142+
def handle_info(_, menu) do
143+
{:noreply, menu}
144+
end
145+
146+
@impl true
147+
def mount(menu) do
148+
{:ok, menu}
149+
end
150+
""",
151+
fn zipper -> {:ok, zipper} end
142152
)
143-
# TODO: detect and warn if the project assumes pgsql
144153
end
145154
end

0 commit comments

Comments
 (0)