Skip to content

Commit a092230

Browse files
committed
Igniter installer
(install igniter: mix archive.install hex igniter_new) you can try mix igniter.new my_project --install exatomvm@github:petermm/exatomvm && cd my_project adds dependency and atomvm project config and the start function. early days in igniter world, but should be good to go. Signed-off-by: Peter M <[email protected]>
1 parent 434f730 commit a092230

File tree

2 files changed

+192
-1
lines changed

2 files changed

+192
-1
lines changed

lib/mix/tasks/ExAtomVM.install.ex

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
defmodule Mix.Tasks.Exatomvm.Install do
2+
use Igniter.Mix.Task
3+
4+
@example "mix igniter.new my_project --install exatomvm@github:atomvm/exatomvm && cd my_project"
5+
6+
@shortdoc "Add and config AtomVM"
7+
@moduledoc """
8+
#{@shortdoc}
9+
10+
## Example
11+
12+
```bash
13+
#{@example}
14+
```
15+
16+
"""
17+
18+
@impl Igniter.Mix.Task
19+
def info(_argv, _composing_task) do
20+
%Igniter.Mix.Task.Info{
21+
# Groups allow for overlapping arguments for tasks by the same author
22+
# See the generators guide for more.
23+
group: :exatomvm,
24+
# dependencies to add
25+
adds_deps: [{:pythonx, "~> 0.4.0"}],
26+
# dependencies to add and call their associated installers, if they exist
27+
installs: [],
28+
# An example invocation
29+
example: @example,
30+
# A list of environments that this should be installed in.
31+
only: nil,
32+
# a list of positional arguments, i.e `[:file]`
33+
positional: [],
34+
# Other tasks your task composes using `Igniter.compose_task`, passing in the CLI argv
35+
# This ensures your option schema includes options from nested tasks
36+
composes: [],
37+
# `OptionParser` schema
38+
schema: [],
39+
# Default values for the options in the `schema`
40+
defaults: [],
41+
# CLI aliases
42+
aliases: [],
43+
# A list of options in the schema that are required
44+
required: []
45+
}
46+
end
47+
48+
@impl Igniter.Mix.Task
49+
def igniter(igniter) do
50+
selected_instructions =
51+
Igniter.Util.IO.select(
52+
"What device do you want to show instructions for?\n(Project is configured for all devices - this is just for further flashing instructions):",
53+
["ESP32", "Pico", "STM32", "All"]
54+
)
55+
56+
IO.inspect(selected_instructions)
57+
58+
options = [
59+
start: Igniter.Project.Module.module_name_prefix(igniter),
60+
esp32_flash_offset: Sourceror.parse_string!("0x250000"),
61+
stm32_flash_offset: Sourceror.parse_string!("0x8080000"),
62+
chip: "auto",
63+
port: "auto"
64+
]
65+
66+
Igniter.update_elixir_file(igniter, "mix.exs", fn zipper ->
67+
with {:ok, zipper} <- Igniter.Code.Function.move_to_def(zipper, :project, 0),
68+
{:ok, zipper} <-
69+
Igniter.Code.Keyword.put_in_keyword(
70+
zipper,
71+
[:atomvm],
72+
options
73+
) do
74+
{:ok, zipper}
75+
end
76+
end)
77+
|> Igniter.mkdir("avm_deps")
78+
|> Igniter.Project.Module.find_and_update_module!(
79+
Igniter.Project.Module.module_name_prefix(igniter),
80+
fn zipper ->
81+
case Igniter.Code.Function.move_to_def(zipper, :start, 0) do
82+
:error ->
83+
# start function not available, so let's create one
84+
zipper =
85+
Igniter.Code.Common.add_code(
86+
zipper,
87+
"""
88+
def start do
89+
IO.inspect("Hello AtomVM!")
90+
:ok
91+
end
92+
""",
93+
placement: :before
94+
)
95+
96+
{:ok, zipper}
97+
98+
_ ->
99+
{:ok, zipper}
100+
end
101+
end
102+
)
103+
|> output_instructions(selected_instructions)
104+
end
105+
106+
defp common_intro do
107+
"""
108+
Your AtomVM project is now ready.
109+
Make sure you have installed AtomVM itself on the device:
110+
(make sure to choose the Elixir enabled build)
111+
"""
112+
end
113+
114+
defp output_instructions(igniter, selected_instructions)
115+
when selected_instructions == "ESP32" do
116+
igniter
117+
|> Igniter.add_notice(selected_instructions)
118+
|> Igniter.add_notice("""
119+
#{common_intro()}
120+
https://www.atomvm.net/doc/main/getting-started-guide.html#flashing-a-binary-image-to-esp32 (binary available)
121+
122+
you can also use the web flasher (using Chrome):
123+
https://petermm.github.io/atomvm-web-tools/
124+
""")
125+
|> Igniter.add_notice("""
126+
You are now ready to flash your project to your device using:
127+
128+
mix atomvm.esp32.flash
129+
130+
ExAtomVM will automatically download the flashing tools needed on first flash.
131+
(port is in "auto" mode and will find first connected ESP32)
132+
133+
[https://github.com/atomvm/exatomvm?tab=readme-ov-file#the-atomvmesp32flash-task]
134+
Optionally set port in the command or specify in mix.exs
135+
mix atomvm.esp32.flash --port /dev/ttyUSB0
136+
""")
137+
end
138+
139+
defp output_instructions(igniter, selected_instructions)
140+
when selected_instructions == "Pico" do
141+
igniter
142+
|> Igniter.add_notice(selected_instructions)
143+
|> Igniter.add_notice("""
144+
#{common_intro()}
145+
https://www.atomvm.net/doc/main/getting-started-guide.html#flashing-a-binary-image-to-pico (binary available)
146+
147+
""")
148+
|> Igniter.add_notice("""
149+
150+
You are then ready to flash your project to your device using:
151+
152+
mix atomvm.pico.flash [https://github.com/atomvm/exatomvm?tab=readme-ov-file#the-atomvmpicoflash-task]
153+
""")
154+
end
155+
156+
defp output_instructions(igniter, selected_instructions)
157+
when selected_instructions == "STM32" do
158+
igniter
159+
|> Igniter.add_notice(selected_instructions)
160+
|> Igniter.add_notice("""
161+
#{common_intro()}
162+
You need to build AtomVM for your board:
163+
https://www.atomvm.net/doc/main/build-instructions.html#building-for-stm32
164+
165+
And have st-link installed to flash:
166+
https://github.com/stlink-org/stlink?tab=readme-ov-file#installation
167+
https://www.atomvm.net/doc/main/getting-started-guide.html#flashing-a-binary-image-to-stm32
168+
""")
169+
|> Igniter.add_notice("""
170+
You are then ready to flash your project to your device using:
171+
172+
mix atomvm.stm32.flash [https://github.com/atomvm/exatomvm?tab=readme-ov-file#the-atomvmstm32flash-task]
173+
""")
174+
end
175+
176+
defp output_instructions(igniter, selected_instructions)
177+
when selected_instructions == "All" do
178+
igniter
179+
|> output_instructions("ESP32")
180+
|> output_instructions("Pico")
181+
|> output_instructions("STM32")
182+
end
183+
184+
defp is_mac? do
185+
case :os.type() do
186+
{:unix, :darwin} -> true
187+
_ -> false
188+
end
189+
end
190+
end

mix.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ defmodule ExAtomVM.MixProject do
2121
# Run "mix help deps" to learn about dependencies.
2222
defp deps do
2323
[
24-
{:uf2tool, "1.1.0"}
24+
{:uf2tool, "1.1.0"},
25+
{:igniter, "~> 0.5", runtime: false, optional: true}
2526
# {:dep_from_hexpm, "~> 0.3.0"},
2627
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
2728
]

0 commit comments

Comments
 (0)