diff --git a/packages/elixir-client/mix.exs b/packages/elixir-client/mix.exs index 53c3dd0d0a..a6d4714a98 100644 --- a/packages/elixir-client/mix.exs +++ b/packages/elixir-client/mix.exs @@ -3,10 +3,17 @@ defmodule Electric.Client.MixProject do @github_repo "https://github.com/electric-sql/electric" + # Project version is obtained by evaluating version.exs in development. Before publishing to + # hex.pm, the line below is replaced with a static version string via the + # `mix:write-static-version` script in package.json. + {version, _bindings} = Code.eval_file("version.exs") + @version version || "0.0.0" + @docs_source_ref_version version || "main" + def project do [ app: :electric_client, - version: version(), + version: @version, elixir: "~> 1.17", elixirc_paths: elixirc_paths(Mix.env()), start_permanent: Mix.env() == :prod, @@ -59,8 +66,7 @@ defmodule Electric.Client.MixProject do defp deps_for(_), do: [] defp docs do - version = version("main") - tag = URI.encode("@core/elixir-client@#{version}", &(&1 != ?@)) + tag = URI.encode("@core/elixir-client@#{@docs_source_ref_version}", &(&1 != ?@)) [ main: "Electric.Client", @@ -84,26 +90,4 @@ defmodule Electric.Client.MixProject do defp description do "Elixir client for ElectricSQL" end - - defp version(default \\ "0.0.0") do - with :error <- version_from_env(), - :error <- version_from_package_json() do - default - end - end - - defp version_from_env do - with {:ok, version} <- System.fetch_env("ELECTRIC_CLIENT_VERSION"), - trimmed = String.trim(version), - {:ok, _} <- Version.parse(trimmed) do - trimmed - end - end - - defp version_from_package_json do - case File.read("./package.json") do - {:ok, binary} -> binary |> :json.decode() |> Map.fetch!("version") - {:error, _} -> :error - end - end end diff --git a/packages/elixir-client/package.json b/packages/elixir-client/package.json index 9294633356..52891712a2 100644 --- a/packages/elixir-client/package.json +++ b/packages/elixir-client/package.json @@ -3,6 +3,7 @@ "private": true, "version": "0.2.5", "scripts": { - "publish:hex": "mix do deps.get, hex.publish --yes || true" + "mix:write-static-version": "sed -i \"/version.exs/c\\ \\ version = $(elixir -r version.exs -e 'IO.inspect Electric.Version.version()')\" mix.exs", + "publish:hex": "pnpm mix:write-static-version && mix do deps.get, hex.publish --yes || true" } } diff --git a/packages/elixir-client/version.exs b/packages/elixir-client/version.exs new file mode 100644 index 0000000000..442c3c0e4d --- /dev/null +++ b/packages/elixir-client/version.exs @@ -0,0 +1,31 @@ +# This helper script is evaluated in mix.exs to obtain the Mix project version. + +defmodule Electric.Version do + def version(default \\ nil) do + with :error <- version_from_env(), + :error <- version_from_package_json() do + default + end + end + + def write_static_version(file) do + File.write!(file, inspect(version())) + end + + defp version_from_env do + with {:ok, version} <- System.fetch_env("ELECTRIC_VERSION"), + trimmed = String.trim(version), + {:ok, _} <- Version.parse(trimmed) do + trimmed + end + end + + defp version_from_package_json do + case File.read("./package.json") do + {:ok, binary} -> binary |> :json.decode() |> Map.fetch!("version") + {:error, _} -> :error + end + end +end + +Electric.Version.version() diff --git a/packages/sync-service/mix.exs b/packages/sync-service/mix.exs index 6f2803bdea..fa45a3259f 100644 --- a/packages/sync-service/mix.exs +++ b/packages/sync-service/mix.exs @@ -3,10 +3,17 @@ defmodule Electric.MixProject do @github_repo "https://github.com/electric-sql/electric" + # Project version is obtained by evaluating version.exs in development. Before publishing to + # hex.pm, the line below is replaced with a static version string via the + # `mix:write-static-version` script in package.json. + {version, _bindings} = Code.eval_file("version.exs") + @version version || "0.0.0" + @docs_source_ref_version version || "main" + def project do [ app: :electric, - version: version(), + version: @version, elixir: "~> 1.17", elixirc_paths: elixirc_paths(Mix.env()), start_permanent: Mix.env() == :prod, @@ -121,28 +128,6 @@ defmodule Electric.MixProject do ] end - defp version(default \\ "0.0.0") do - with :error <- version_from_env(), - :error <- version_from_package_json() do - default - end - end - - defp version_from_env do - with {:ok, version} <- System.fetch_env("ELECTRIC_VERSION"), - trimmed = String.trim(version), - {:ok, _} <- Version.parse(trimmed) do - trimmed - end - end - - defp version_from_package_json do - case File.read("./package.json") do - {:ok, binary} -> binary |> :json.decode() |> Map.fetch!("version") - {:error, _} -> :error - end - end - defp description do "Postgres sync engine. Sync little subsets of your Postgres data into local apps and services. " end @@ -158,8 +143,7 @@ defmodule Electric.MixProject do end defp docs do - version = version("main") - tag = URI.encode("@core/sync-service@#{version}", &(&1 != ?@)) + tag = URI.encode("@core/sync-service@#{@docs_source_ref_version}", &(&1 != ?@)) [ main: "readme", diff --git a/packages/sync-service/package.json b/packages/sync-service/package.json index 42bac7932a..ea0fab5cdc 100644 --- a/packages/sync-service/package.json +++ b/packages/sync-service/package.json @@ -3,6 +3,7 @@ "private": true, "version": "1.0.0-beta.1", "scripts": { - "publish:hex": "mix do deps.get, hex.publish --yes || true" + "mix:write-static-version": "sed -i \"/version.exs/c\\ \\ version = $(elixir -r version.exs -e 'IO.inspect Electric.Version.version()')\" mix.exs", + "publish:hex": "pnpm mix:write-static-version && mix do deps.get, hex.publish --yes || true" } } diff --git a/packages/sync-service/version.exs b/packages/sync-service/version.exs new file mode 100644 index 0000000000..442c3c0e4d --- /dev/null +++ b/packages/sync-service/version.exs @@ -0,0 +1,31 @@ +# This helper script is evaluated in mix.exs to obtain the Mix project version. + +defmodule Electric.Version do + def version(default \\ nil) do + with :error <- version_from_env(), + :error <- version_from_package_json() do + default + end + end + + def write_static_version(file) do + File.write!(file, inspect(version())) + end + + defp version_from_env do + with {:ok, version} <- System.fetch_env("ELECTRIC_VERSION"), + trimmed = String.trim(version), + {:ok, _} <- Version.parse(trimmed) do + trimmed + end + end + + defp version_from_package_json do + case File.read("./package.json") do + {:ok, binary} -> binary |> :json.decode() |> Map.fetch!("version") + {:error, _} -> :error + end + end +end + +Electric.Version.version()