From ea853e5171e129884003ed4240d05006314b2317 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Mon, 11 Mar 2024 22:08:25 -0400 Subject: [PATCH] improvement: properly format generated migrations --- .../migration_generator.ex | 20 +++++-------------- mix.exs | 2 +- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/lib/migration_generator/migration_generator.ex b/lib/migration_generator/migration_generator.ex index e34a3f9..b358fb8 100644 --- a/lib/migration_generator/migration_generator.ex +++ b/lib/migration_generator/migration_generator.ex @@ -305,7 +305,7 @@ defmodule AshPostgres.MigrationGenerator do pretty: true ) - contents = format(contents, opts) + contents = String.trim(format(migration_file, contents, opts)) create_file(snapshot_file, snapshot_contents, force: true) create_file(migration_file, contents) end @@ -869,7 +869,7 @@ defmodule AshPostgres.MigrationGenerator do """ try do - contents = format(contents, opts) + contents = String.trim(format(migration_file, contents, opts)) if opts.dry_run do Mix.shell().info(contents) @@ -980,9 +980,10 @@ defmodule AshPostgres.MigrationGenerator do defp maybe_comment(text, _), do: text - defp format(string, opts) do + defp format(path, string, opts) do if opts.format do - Code.format_string!(string, locals_without_parens: ecto_sql_locals_without_parens()) + {func, _} = Mix.Tasks.Format.formatter_for_file(path) + func.(string) else string end @@ -999,17 +1000,6 @@ defmodule AshPostgres.MigrationGenerator do reraise exception, __STACKTRACE__ end - defp ecto_sql_locals_without_parens do - path = File.cwd!() |> Path.join("deps/ecto_sql/.formatter.exs") - - if File.exists?(path) do - {opts, _} = Code.eval_file(path) - Keyword.get(opts, :locals_without_parens, []) - else - [] - end - end - defp streamline(ops, acc \\ []) defp streamline([], acc), do: Enum.reverse(acc) diff --git a/mix.exs b/mix.exs index c9a4855..4e79e3e 100644 --- a/mix.exs +++ b/mix.exs @@ -12,7 +12,7 @@ defmodule AshPostgres.MixProject do [ app: :ash_postgres, version: @version, - elixir: "~> 1.11", + elixir: "~> 1.13", start_permanent: Mix.env() == :prod, deps: deps(), description: @description,