Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mix gleam.toml [--replace] #39

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.6.3 - 2024-01-28

- Added `mix gleam.toml [--replace]` task. It generates gleam.toml from mix.exs. This might be useful for Gleam tooling support in Mix projects, such as Gleam LSP.

## v0.6.2 - 2023-11-16

- Updated for Elixir v1.15 and Gleam v0.32 compatibility. Make sure to set
Expand Down
92 changes: 92 additions & 0 deletions lib/mix/tasks/gleam/toml.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
defmodule Mix.Tasks.Gleam.Toml do
use Mix.Task
@shortdoc "Generates gleam.toml from mix.exs."
@moduledoc """
#{@shortdoc}

This might be useful for Gleam tooling support in Mix projects, such as Gleam LSP.

Print gleam.toml into stdout:

mix gleam.toml

Replace gleam.toml file:

mix gleam.toml --replace

Automate gleam.toml sync using mix.exs project aliases:

aliases: [
"deps.get": ["deps.get", "gleam.deps.get", "gleam.toml --replace"]
]
"""
@impl true
@shell Mix.shell()
def run(argv) do
replace =
argv
|> OptionParser.parse!(switches: [replace: :boolean])
|> elem(0)
|> Keyword.get(:replace, false)

Mix.Project.get!()
cfg = Mix.Project.config()

deps =
Mix.Dep.load_and_cache()
|> Enum.flat_map(fn %Mix.Dep{app: dep, opts: opts} ->
dst = Keyword.fetch!(opts, :dest)

if dst |> Path.join("gleam.toml") |> File.regular?() do
[{dep, [path: dst]}]
else
[]
end
end)

toml =
[
name: Keyword.fetch!(cfg, :app),
version: Keyword.fetch!(cfg, :version),
dependencies: deps
]
|> mk_toml

if replace do
Mix.Project.project_file()
|> Path.dirname()
|> Path.join("gleam.toml")
|> File.write!(toml)
else
@shell.info(toml)
end
end

defp mk_toml(x) do
x
|> Enum.map(&mk_toml_row/1)
|> Enum.join("\n")
end

defp mk_toml_row({k, v}), do: "#{mk_toml_key(k)} = #{mk_toml_val(v)}"

defp mk_toml_key(x) when is_atom(x) or is_binary(x) or is_number(x) do
x |> to_string |> inspect
end

defp mk_toml_val(x) do
cond do
is_map(x) or Keyword.keyword?(x) ->
"{#{x |> Enum.map(&mk_toml_row/1) |> Enum.join(",")}}"

is_list(x) ->
"[#{x |> Enum.map(&mk_toml_val/1) |> Enum.join(",")}]"

is_number(x) ->
to_string(x)

true ->
mk_toml_key(x)
end
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule MixGleam.MixProject do
def project do
[
app: :mix_gleam,
version: "0.6.2",
version: "0.6.3",
elixir: "~> 1.9",
start_permanent: Mix.env() == :prod,
name: "mix_gleam",
Expand Down