Skip to content

Commit 9d697ab

Browse files
authored
elixir: remove include_vm (#541)
1 parent 44001ed commit 9d697ab

File tree

9 files changed

+29
-713
lines changed

9 files changed

+29
-713
lines changed
Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
defmodule Mix.Tasks.Popcorn.Cook do
2-
@shortdoc "Generates static artifacts to run the project in the browser."
2+
@shortdoc "Builds the `.avm` bundle for a Popcorn project."
33
@moduledoc """
44
#{@shortdoc}
55
66
Accepts the following options:
77
- `out_dir` - the directory to write artifacts to
8-
- `target` - `wasm` (default) or `unix`. If `unix` is chosed, you need to build the runtime
9-
first with `mix popcorn.build_runtime --target unix`
10-
- `--include-vm` - include the VM and supporting files in the output directory.
11-
Without this flag, only the `.avm` bundle is generated.
8+
- `start_module` - optional module with `start/0` used as the bundle entrypoint
129
1310
`out_dir` is mandatory, unless provided via `config.exs`,
1411
for example `config :popcorn, out_dir: "dist/wasm"`
1512
"""
1613
use Mix.Task
1714

1815
@requirements "compile"
16+
@parser_config [
17+
strict: [out_dir: :string, start_module: :string],
18+
aliases: [d: :out_dir]
19+
]
1920

2021
@impl true
2122
def run(args) do
22-
parser_config = [
23-
strict: [out_dir: :string, target: :string, include_vm: :boolean],
24-
aliases: [d: :out_dir]
25-
]
23+
{options, _rest} = OptionParser.parse!(args, @parser_config)
2624

27-
{options, _rest} = OptionParser.parse!(args, parser_config)
25+
options
26+
|> Keyword.update(:start_module, nil, &as_module/1)
27+
|> Popcorn.cook()
28+
end
2829

29-
Popcorn.cook(options)
30+
defp as_module(module_name) when is_binary(module_name) do
31+
module_name
32+
|> String.split(".")
33+
|> Module.safe_concat()
3034
end
3135
end

popcorn/elixir/lib/popcorn.ex

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ defmodule Popcorn do
44
"""
55

66
alias Popcorn.Build
7-
alias Popcorn.Utils.FetchArtifacts
87

98
@popcorn_path Mix.Project.app_path()
109
@popcorn_generated_path Path.join(@popcorn_path, "popcorn_generated_ebin")
11-
@priv_dir :code.priv_dir(:popcorn)
1210
@api_dir Path.join(["popcorn", "api"])
1311

1412
defmodule CookingError do
@@ -17,27 +15,19 @@ defmodule Popcorn do
1715
end
1816

1917
@doc """
20-
Generates static artifacts to run the project in the browser.
18+
Builds a Popcorn `.avm` bundle.
2119
2220
Options:
2321
- `out_dir` - The directory to write artifacts to. Required, unless provided via `config.exs`.
2422
- `start_module` - Optional; a module with `start/0` function that will be called after applications start.
25-
- `target` - `wasm` (default) or `unix`. If `unix` is chosed, you need to build the runtime
26-
first with `mix popcorn.build_runtime --target unix`
2723
- `extra_beams` - Compiled BEAMs that should be included in the generated bundle.
28-
- `include_vm` - If `true`, includes the VM and supporting files in the output directory (e.g. AtomVM.wasm, AtomVM.mjs and JS glue for wasm; AtomVM binary for unix).
29-
30-
Instead of calling `cook/1`, you can call `ingredients/1` and then `bundle/1`.
3124
"""
3225
@spec cook([
3326
{:out_dir, String.t()}
3427
| {:start_module, module}
35-
| {:target, :wasm | :unix}
3628
| {:extra_beams, [String.t()]}
37-
| {:include_vm, boolean()}
3829
]) :: :ok
3930
def cook(options \\ []) do
40-
ingredients(Keyword.take(options, [:out_dir, :target, :include_vm]))
4131
bundle(Keyword.take(options, [:out_dir, :start_module, :extra_beams]))
4232
end
4333

@@ -72,29 +62,6 @@ defmodule Popcorn do
7262
end
7363
end
7464

75-
@doc """
76-
Generates artifacts needed to run any Popcorn-based project.
77-
78-
Options have the same semantics as in `cook/1`.
79-
"""
80-
@spec ingredients([{:out_dir, String.t()} | {:target, :wasm | :unix} | {:include_vm, boolean()}]) ::
81-
:ok
82-
def ingredients(options \\ []) do
83-
default_options = [
84-
out_dir: Popcorn.Config.get(:out_dir),
85-
target: :wasm,
86-
include_vm: false
87-
]
88-
89-
options = options |> Keyword.validate!(default_options) |> Map.new()
90-
ensure_option_present(options, :out_dir, "Output directory")
91-
92-
File.mkdir_p!(options.out_dir)
93-
maybe_copy_runtime_artifacts(options)
94-
95-
:ok
96-
end
97-
9865
defp bundled_artifacts(applications) do
9966
builtin_apps = Build.builtin_app_names() |> MapSet.new()
10067

@@ -173,25 +140,6 @@ defmodule Popcorn do
173140
String.contains?(src_path, @api_dir)
174141
end
175142

176-
defp maybe_copy_runtime_artifacts(%{include_vm: false}), do: :ok
177-
178-
defp maybe_copy_runtime_artifacts(options) do
179-
if options.target == :wasm do
180-
wasm_template_files = Path.wildcard(Path.join(@priv_dir, "static-template/wasm/**"))
181-
cp_gzip(wasm_template_files, options.out_dir)
182-
end
183-
184-
paths = FetchArtifacts.fetch_artifacts(options.target)
185-
186-
cp_gzip(paths, options.out_dir, fn path ->
187-
raise CookingError, """
188-
Couldn't find runtime artifact #{Path.basename(path)} at #{path} for target `#{options.target}`. \
189-
To build artifacts from source, run \
190-
`mix popcorn.build_runtime --target #{options.target}`.
191-
"""
192-
end)
193-
end
194-
195143
defp ensure_option_present(options, key, name) do
196144
if options[key] == nil do
197145
raise CookingError, """
@@ -320,17 +268,6 @@ defmodule Popcorn do
320268
gather_app_specs(deps, Map.merge(specs, new_specs))
321269
end
322270

323-
defp cp_gzip(paths, out_dir, handle_missing_file \\ fn _path -> :ok end) do
324-
for path <- List.wrap(paths) do
325-
dest = Path.join(out_dir, Path.basename(path))
326-
if not File.exists?(path), do: handle_missing_file.(path)
327-
File.cp!(path, dest)
328-
gzip(dest)
329-
end
330-
331-
:ok
332-
end
333-
334271
defp gzip(path) do
335272
File.read!(path) |> :zlib.gzip() |> then(&File.write!("#{path}.gz", &1))
336273
end

popcorn/elixir/mix.exs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ defmodule Popcorn.MixProject do
2222
elixirc_paths: elixirc_paths(Mix.env()),
2323
aliases: [
2424
docs: ["docs", &generate_js_docs/1],
25-
compile: ["compile", &download_artifacts/1, &patch/1],
25+
compile: ["compile", &patch/1],
2626
lint: [
2727
"format --check-formatted",
2828
"deps.unlock --check-unused",
@@ -128,13 +128,18 @@ defmodule Popcorn.MixProject do
128128
end
129129

130130
defp generate_js_docs(_) do
131-
Mix.shell().cmd(
132-
"npx documentation build priv/static-template/wasm/popcorn.js -f html -o doc/js-api"
133-
)
131+
js_dir = Path.expand("../js", __DIR__)
132+
js_entry = Path.join(js_dir, "dist/popcorn.mjs")
133+
134+
run_cmd!("pnpm run build", cd: js_dir)
135+
run_cmd!("npx documentation build #{js_entry} -f html -o doc/js-api", [])
134136
end
135137

136-
defp download_artifacts(_args) do
137-
Popcorn.Utils.FetchArtifacts.download_artifacts(force: "--force" in System.argv())
138+
defp run_cmd!(command, opts) do
139+
case Mix.shell().cmd(command, opts) do
140+
0 -> :ok
141+
status -> Mix.raise("command failed with status #{status}: #{command}")
142+
end
138143
end
139144

140145
defp patch(_args) do

popcorn/elixir/pages/getting_started/first_steps.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ import Config
7474
config :popcorn, out_dir: "dist/wasm"
7575
```
7676

77-
Next, fetch dependencies and compile your Elixir code to WebAssembly:
77+
Next, fetch dependencies and compile your Elixir code into a Popcorn `.avm` bundle:
7878

7979
```console
8080
$ mix deps.get
@@ -114,7 +114,7 @@ $ npm install --prefix assets
114114
$ npm run build --prefix assets
115115
```
116116

117-
The build step bundles your JavaScript, copies the WebAssembly runtime assets, and outputs everything to the `dist/` directory.
117+
The JavaScript build step bundles your JavaScript, copies the WebAssembly runtime assets, and outputs everything to the `dist/` directory. `mix popcorn.cook` only produces the `.avm` bundle.
118118

119119
> #### Other bundlers {: .info}
120120
>

popcorn/elixir/priv/static-template/index.html

Whitespace-only changes.

popcorn/elixir/priv/static-template/script.js

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

popcorn/elixir/priv/static-template/style.css

Whitespace-only changes.

0 commit comments

Comments
 (0)