Skip to content

Commit

Permalink
fix(electric,elixir-client): Write static version string to mix.exs p…
Browse files Browse the repository at this point in the history
…rior to publishing

Fixes #2179
  • Loading branch information
alco committed Dec 18, 2024
1 parent 5a844d9 commit 4cb9dd5
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 52 deletions.
34 changes: 9 additions & 25 deletions packages/elixir-client/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand All @@ -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
3 changes: 2 additions & 1 deletion packages/elixir-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
31 changes: 31 additions & 0 deletions packages/elixir-client/version.exs
Original file line number Diff line number Diff line change
@@ -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()
34 changes: 9 additions & 25 deletions packages/sync-service/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion packages/sync-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
31 changes: 31 additions & 0 deletions packages/sync-service/version.exs
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 4cb9dd5

Please sign in to comment.