Skip to content

Commit

Permalink
Extract Bitstyles.Version
Browse files Browse the repository at this point in the history
  • Loading branch information
angelikatyborska committed Jul 26, 2024
1 parent 9760cc5 commit 6c35953
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 52 deletions.
2 changes: 1 addition & 1 deletion docs/bitstyles_version_compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ open version_showcase/index.html

```elixir
def ui_tricky_component(assigns) do
version = BitstylesPhoenix.Bitstyles.version()
version = BitstylesPhoenix.Bitstyles.Version.version()

if version >= {5,0,0} do
~H"""
Expand Down
2 changes: 1 addition & 1 deletion lib/bitstyles_phoenix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule BitstylesPhoenix do
### Bitstyles version
BitstylesPhoenix will generate classes #{BitstylesPhoenix.Bitstyles.version_string()} of bitstyles.
BitstylesPhoenix will generate classes #{BitstylesPhoenix.Bitstyles.Version.version_string()} of bitstyles.
You can set older versions with a configuration:
```elixir
Expand Down
52 changes: 9 additions & 43 deletions lib/bitstyles_phoenix/bitstyles.ex
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
defmodule BitstylesPhoenix.Bitstyles do
@moduledoc false

@default_version "5.0.1"
require BitstylesPhoenix.Bitstyles.Version
alias BitstylesPhoenix.Bitstyles.Version

@cdn_url "https://cdn.jsdelivr.net/npm/bitstyles"

defguard should_downgrade_from(version, target_version, current_version)
when target_version < version and current_version >= version

def cdn_url do
"#{@cdn_url}@#{version_string()}"
"#{@cdn_url}@#{Version.version_string()}"
end

@doc """
Returns the classnames for the configured version.
Input classnames are assumed to be from the #{@default_version} version of bitstyles.
Input classnames are assumed to be from the #{Version.default_version_string()} version of bitstyles.
"""
def classname(name), do: classname(name, version())
def classname(name), do: classname(name, Version.version())

def classname(class, version) when is_tuple(version) do
downgrade_classname(class, version, default_version())
downgrade_classname(class, version, Version.default_version())
end

# Note about class renaming:
Expand All @@ -29,7 +31,7 @@ defmodule BitstylesPhoenix.Bitstyles do

defp downgrade_classname(class, target_version, _current_version)
when target_version > {5, 0, 1} do
IO.warn("Version #{version_to_string(target_version)} of bitstyles is not yet supported")
IO.warn("Version #{Version.to_string(target_version)} of bitstyles is not yet supported")
class
end

Expand Down Expand Up @@ -157,44 +159,8 @@ defmodule BitstylesPhoenix.Bitstyles do
defp downgrade_classname(_class, target_version, current_version)
when should_downgrade_from({1, 3, 0}, target_version, current_version) do
raise("""
The version #{version_to_string(target_version)} of bitstyles is not supported. The helpers will produce incorrect classes.
The version #{Version.to_string(target_version)} of bitstyles is not supported. The helpers will produce incorrect classes.
Please upgrade bitsyles and set the `bitsyles_version` to the updated version.
""")
end

def version do
version_to_tuple(version_string())
end

def version_string do
bitstyles_version_override = Process.get(:bitstyles_phoenix_bistyles_version)

bitstyles_version_override ||
Application.get_env(:bitstyles_phoenix, :bitstyles_version, @default_version)
end

def default_version do
version_to_tuple(@default_version)
end

def default_version_string do
@default_version
end

defp version_to_tuple(version) when is_tuple(version), do: version

defp version_to_tuple(version) when is_binary(version) do
version
|> String.split(".")
|> Enum.map(&String.to_integer/1)
|> List.to_tuple()
end

defp version_to_string(version) when is_binary(version), do: version

defp version_to_string(version) when is_tuple(version) do
version
|> Tuple.to_list()
|> Enum.map_join(".", &to_string/1)
end
end
39 changes: 39 additions & 0 deletions lib/bitstyles_phoenix/bitstyles/version.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
defmodule BitstylesPhoenix.Bitstyles.Version do
@moduledoc false

@default_version "5.0.1"

def version do
to_tuple(version_string())
end

def version_string do
bitstyles_version_override = Process.get(:bitstyles_phoenix_bistyles_version)

bitstyles_version_override ||
Application.get_env(:bitstyles_phoenix, :bitstyles_version, @default_version)
end

def default_version do
to_tuple(@default_version)
end

def default_version_string, do: @default_version

def to_tuple(version) when is_tuple(version), do: version

def to_tuple(version) when is_binary(version) do
version
|> String.split(".")
|> Enum.map(&String.to_integer/1)
|> List.to_tuple()
end

def to_string(version) when is_binary(version), do: version

def to_string(version) when is_tuple(version) do
version
|> Tuple.to_list()
|> Enum.map_join(".", &Kernel.to_string/1)
end
end
6 changes: 3 additions & 3 deletions lib/bitstyles_phoenix/component/button.ex
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ defmodule BitstylesPhoenix.Component.Button do
extra = assigns_to_attributes(assigns, [:icon, :class, :color, :shape, :variant])

assigns =
if Bitstyles.version() >= {5, 0, 0} && assigns[:variant] do
if Bitstyles.Version.version() >= {5, 0, 0} && assigns[:variant] do
IO.warn("Attribute `variant` is deprecated in ui_button/1! Change to `color` and `shape`")

assigns
Expand All @@ -298,7 +298,7 @@ defmodule BitstylesPhoenix.Component.Button do
end

classes =
if Bitstyles.version() >= {5, 0, 0} do
if Bitstyles.Version.version() >= {5, 0, 0} do
color_and_shape_classes(assigns[:color], assigns[:shape])
else
variant_classes(assigns[:variant])
Expand Down Expand Up @@ -538,7 +538,7 @@ defmodule BitstylesPhoenix.Component.Button do
extra = assigns_to_attributes(assigns, [:icon, :label, :reversed, :color, :title])

extra =
if Bitstyles.version() >= {5, 0, 0} do
if Bitstyles.Version.version() >= {5, 0, 0} do
if assigns[:reversed] do
Keyword.put_new(extra, :"data-theme", "dark")
else
Expand Down
2 changes: 1 addition & 1 deletion lib/bitstyles_phoenix/component/description_list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ defmodule BitstylesPhoenix.Component.DescriptionList do

# u-gap-l and u-gap-xl in 5.0.0 are equivalent to respectively u-gap-m and u-gap-l in 4.3.0
gap_class =
if Bitstyles.version() >= {5, 0, 0} do
if Bitstyles.Version.version() >= {5, 0, 0} do
"u-gap-l"
else
"u-gap-m"
Expand Down
4 changes: 2 additions & 2 deletions lib/bitstyles_phoenix/component/dropdown.ex
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ defmodule BitstylesPhoenix.Component.Dropdown do
assigns_from_single_slot(assigns, :button, exclude: [:label, :icon_file])

button_extra =
if Bitstyles.version() >= {5, 0, 0} do
if Bitstyles.Version.version() >= {5, 0, 0} do
button_extra
|> Keyword.put_new(:color, :secondary)
else
Expand Down Expand Up @@ -516,7 +516,7 @@ defmodule BitstylesPhoenix.Component.Dropdown do
|> assigns_to_attributes()

extra =
if Bitstyles.version() >= {5, 0, 0} do
if Bitstyles.Version.version() >= {5, 0, 0} do
extra
|> Keyword.put_new(:shape, "menu")
|> Keyword.put_new(:color, "transparent")
Expand Down
2 changes: 1 addition & 1 deletion lib/bitstyles_phoenix/showcase.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule BitstylesPhoenix.Showcase do

defmacro story(name, doctest_iex_code, doctest_expected_results, opts \\ []) do
default_version =
BitstylesPhoenix.Bitstyles.default_version_string()
BitstylesPhoenix.Bitstyles.Version.default_version_string()
|> String.to_atom()

doctest_expected_results =
Expand Down

0 comments on commit 6c35953

Please sign in to comment.