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

Version number comparison & fix chained version renames #134

Merged
merged 14 commits into from
Sep 23, 2024
Merged
4 changes: 2 additions & 2 deletions docs/bitstyles_version_compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ open version_showcase/index.html

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

if version >= "5.0.0" do
if version >= {5,0,0} do
~H"""
<p class={classnames("u-new-class-that-does-everything")}>...</p>
"""
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()} of bitstyles.
BitstylesPhoenix will generate classes #{BitstylesPhoenix.Bitstyles.version(:string)} of bitstyles.
You can set older versions with a configuration:

```elixir
Expand Down
100 changes: 75 additions & 25 deletions lib/bitstyles_phoenix/bitstyles.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ defmodule BitstylesPhoenix.Bitstyles do
@default_version "5.0.1"
@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()}"
"#{@cdn_url}@#{version(:string)}"
end

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

def classname(class, version) when version > "5.0.1" do
IO.warn("Version #{version} of bitstyles is not yet supported")
class
def classname(class, version) when is_tuple(version) do
downgrade_classname(class, version, default_version(:tuple))
end

# Note about class renaming:
Expand All @@ -25,11 +27,24 @@ defmodule BitstylesPhoenix.Bitstyles do
# If it does exist, then doing this renaming in the classname/2 function would make it impossible
# for users of older bitstyles to use the "class-name-A" classname.

def classname(class, version) when version >= "5.0.0" do
defp downgrade_classname(class, target_version, _current_version)
when target_version > {5, 0, 1} do
andreasknoepfle marked this conversation as resolved.
Show resolved Hide resolved
IO.warn("Version #{version_to_string(target_version)} of bitstyles is not yet supported")
class
end

defp downgrade_classname(class, target_version, _current_version)
andreasknoepfle marked this conversation as resolved.
Show resolved Hide resolved
when target_version >= {5, 0, 0} do
class
end

def classname(class, version) when version >= "4.2.0" do
defp downgrade_classname(class, target_version, current_version)
when target_version >= current_version do
class
end

defp downgrade_classname(class, target_version, current_version)
when should_downgrade_from({5, 0, 0}, target_version, current_version) do
sizes_renaming = %{
"3xs" => "xxxs",
"2xs" => "xxs",
Expand Down Expand Up @@ -63,25 +78,27 @@ defmodule BitstylesPhoenix.Bitstyles do
"u-fg-text" -> "u-fg-gray-30"
"u-fg-text-darker" -> "u-fg-gray-50"
"u-bg-gray-darker" -> "u-bg-gray-80"
"u-version-5-0-0" -> "u-version-4"
class -> class
end

classname(class, "5.0.0")
downgrade_classname(class, target_version, {4, 2, 0})
end

def classname(class, version) when version >= "4.0.0" do
defp downgrade_classname(class, target_version, current_version)
when should_downgrade_from({4, 2, 0}, target_version, current_version) do
mapping =
case class do
"u-border-radius-" <> variant -> "u-round-#{variant}"
_ -> class
end

classname(mapping, "4.3.0")
downgrade_classname(mapping, target_version, {4, 0, 0})
end

def classname(class, version) when version >= "2.0.0" do
# credo:disable-for-previous-line Credo.Check.Refactor.CyclomaticComplexity

# credo:disable-for-next-line Credo.Check.Refactor.CyclomaticComplexity
defp downgrade_classname(class, target_version, current_version)
when should_downgrade_from({4, 0, 0}, target_version, current_version) do
mapping =
case class do
# Make sure that we leave the classes in place that are old anyway (update compatibility)
Expand All @@ -101,52 +118,85 @@ defmodule BitstylesPhoenix.Bitstyles do
"u-border-radius-" <> variant -> "u-round--#{variant}"
"u-overflow-x-auto" -> "u-overflow--x"
"u-overflow-y-auto" -> "u-overflow--y"
"u-version-4" -> "u-version-2"
_ -> class
end

classname(mapping, "4.0.0")
downgrade_classname(mapping, target_version, {2, 0, 0})
end

def classname(class, version) when version >= "1.5.0" do
defp downgrade_classname(class, target_version, current_version)
when should_downgrade_from({2, 0, 0}, target_version, current_version) do
mapping =
case class do
"u-flex-shrink-" <> number -> "u-flex__shrink-#{number}"
"u-flex-grow-" <> number -> "u-flex__grow-#{number}"
"u-flex-wrap" -> "u-flex--wrap"
"u-flex-col" -> "u-flex--col"
"u-version-2" -> "u-version-1-5"
_ -> class
end

classname(mapping, "2.0.0")
downgrade_classname(mapping, target_version, {1, 5, 0})
end

def classname(class, version) when version >= "1.3.0" do
defp downgrade_classname(class, target_version, current_version)
when should_downgrade_from({1, 5, 0}, target_version, current_version) do
mapping =
case class do
"u-grid-cols-" <> number -> "u-grid--#{number}-col"
"u-col-span-" <> number -> "u-grid__col-span-#{number}"
"u-col-start-" <> number -> "u-grid__col-#{number}"
"u-version-1-5" -> "u-version-1-3"
_ -> class
end

classname(mapping, "1.5.0")
downgrade_classname(mapping, target_version, {1, 3, 0})
end

def classname(_class, version) 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} 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
def version(format \\ :tuple) do
andreasknoepfle marked this conversation as resolved.
Show resolved Hide resolved
bitstyles_version_override = Process.get(:bitstyles_phoenix_bistyles_version)

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

get_version_in_format(version, format)
end

def default_version(format \\ :tuple) do
get_version_in_format(@default_version, format)
end

defp get_version_in_format(version, format) do
case format do
:tuple -> version_to_tuple(version)
:string -> version_to_string(version)
end
end

def default_version do
@default_version
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
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(:tuple) >= {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(:tuple) >= {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(:tuple) >= {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(:tuple) >= {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(:tuple) >= {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(:tuple) >= {5, 0, 0} do
extra
|> Keyword.put_new(:shape, "menu")
|> Keyword.put_new(:color, "transparent")
Expand Down
4 changes: 3 additions & 1 deletion lib/bitstyles_phoenix/showcase.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ defmodule BitstylesPhoenix.Showcase do
alias Phoenix.HTML.Safe

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

doctest_expected_results =
if is_list(doctest_expected_results) && is_tuple(List.first(doctest_expected_results)) do
Expand Down
Loading
Loading