|
| 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: [], |
| 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 | + Igniter.update_elixir_file(igniter, "mix.exs", fn zipper -> |
| 51 | + with {:ok, zipper} <- Igniter.Code.Function.move_to_def(zipper, :project, 0), |
| 52 | + {:ok, zipper} <- |
| 53 | + Igniter.Code.Keyword.put_in_keyword( |
| 54 | + zipper, |
| 55 | + [:atomvm], |
| 56 | + start: Igniter.Project.Module.module_name_prefix(igniter), |
| 57 | + esp32_flash_offset: Sourceror.parse_string!("0x250000"), |
| 58 | + stm32_flash_offset: Sourceror.parse_string!("0x8080000"), |
| 59 | + chip: "auto", |
| 60 | + port: "auto" |
| 61 | + ) do |
| 62 | + {:ok, zipper} |
| 63 | + end |
| 64 | + end) |
| 65 | + |> Igniter.mkdir("avm_deps") |
| 66 | + |> Igniter.Project.Module.find_and_update_module!( |
| 67 | + Igniter.Project.Module.module_name_prefix(igniter), |
| 68 | + fn zipper -> |
| 69 | + case Igniter.Code.Function.move_to_def(zipper, :start, 0) do |
| 70 | + :error -> |
| 71 | + # start not available |
| 72 | + zipper = |
| 73 | + Igniter.Code.Common.add_code( |
| 74 | + zipper, |
| 75 | + """ |
| 76 | + def start do |
| 77 | + IO.inspect("Hello AtomVM!") |
| 78 | + :ok |
| 79 | + end |
| 80 | + """, |
| 81 | + placement: :before |
| 82 | + ) |
| 83 | + |
| 84 | + {:ok, zipper} |
| 85 | + |
| 86 | + _ -> |
| 87 | + {:ok, zipper} |
| 88 | + end |
| 89 | + end |
| 90 | + ) |
| 91 | + end |
| 92 | +end |
0 commit comments