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

fix(electric,elixir-client): Write static version string to mix.exs prior to publishing #2180

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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()
Loading