From 6c35953e10265d72a3b46bdb551fb51b3a1bcbbc Mon Sep 17 00:00:00 2001 From: Angelika Tyborska Date: Fri, 26 Jul 2024 10:18:35 +0200 Subject: [PATCH] Extract Bitstyles.Version --- docs/bitstyles_version_compatibility.md | 2 +- lib/bitstyles_phoenix.ex | 2 +- lib/bitstyles_phoenix/bitstyles.ex | 52 ++++--------------- lib/bitstyles_phoenix/bitstyles/version.ex | 39 ++++++++++++++ lib/bitstyles_phoenix/component/button.ex | 6 +-- .../component/description_list.ex | 2 +- lib/bitstyles_phoenix/component/dropdown.ex | 4 +- lib/bitstyles_phoenix/showcase.ex | 2 +- 8 files changed, 57 insertions(+), 52 deletions(-) create mode 100644 lib/bitstyles_phoenix/bitstyles/version.ex diff --git a/docs/bitstyles_version_compatibility.md b/docs/bitstyles_version_compatibility.md index 39498b3..bb59518 100644 --- a/docs/bitstyles_version_compatibility.md +++ b/docs/bitstyles_version_compatibility.md @@ -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""" diff --git a/lib/bitstyles_phoenix.ex b/lib/bitstyles_phoenix.ex index f120e11..1485211 100644 --- a/lib/bitstyles_phoenix.ex +++ b/lib/bitstyles_phoenix.ex @@ -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 diff --git a/lib/bitstyles_phoenix/bitstyles.ex b/lib/bitstyles_phoenix/bitstyles.ex index cd13bdf..5b9633c 100644 --- a/lib/bitstyles_phoenix/bitstyles.ex +++ b/lib/bitstyles_phoenix/bitstyles.ex @@ -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: @@ -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 @@ -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 diff --git a/lib/bitstyles_phoenix/bitstyles/version.ex b/lib/bitstyles_phoenix/bitstyles/version.ex new file mode 100644 index 0000000..9ee0ec8 --- /dev/null +++ b/lib/bitstyles_phoenix/bitstyles/version.ex @@ -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 diff --git a/lib/bitstyles_phoenix/component/button.ex b/lib/bitstyles_phoenix/component/button.ex index f4f36bb..358bfff 100644 --- a/lib/bitstyles_phoenix/component/button.ex +++ b/lib/bitstyles_phoenix/component/button.ex @@ -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 @@ -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]) @@ -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 diff --git a/lib/bitstyles_phoenix/component/description_list.ex b/lib/bitstyles_phoenix/component/description_list.ex index cbddfab..da989e2 100644 --- a/lib/bitstyles_phoenix/component/description_list.ex +++ b/lib/bitstyles_phoenix/component/description_list.ex @@ -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" diff --git a/lib/bitstyles_phoenix/component/dropdown.ex b/lib/bitstyles_phoenix/component/dropdown.ex index eda22b0..bfbc198 100644 --- a/lib/bitstyles_phoenix/component/dropdown.ex +++ b/lib/bitstyles_phoenix/component/dropdown.ex @@ -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 @@ -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") diff --git a/lib/bitstyles_phoenix/showcase.ex b/lib/bitstyles_phoenix/showcase.ex index daf9723..49f9b40 100644 --- a/lib/bitstyles_phoenix/showcase.ex +++ b/lib/bitstyles_phoenix/showcase.ex @@ -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 =