Skip to content

Commit

Permalink
Reduce duplication in code and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jan 16, 2023
1 parent 16c52bf commit 2a9be7e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 54 deletions.
53 changes: 25 additions & 28 deletions lib/mix/tasks/tailwind.install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,31 @@ defmodule Mix.Tasks.Tailwind.Install do
def run(args) do
valid_options = [runtime_config: :boolean, if_missing: :boolean]

case OptionParser.parse_head!(args, strict: valid_options) do
{opts, []} ->
if opts[:runtime_config], do: Mix.Task.run("app.config")

if opts[:if_missing] && latest_version?() do
:ok
else
Tailwind.install()
end

{opts, [base_url]} ->
if opts[:runtime_config], do: Mix.Task.run("app.config")

if opts[:if_missing] && latest_version?() do
:ok
else
Tailwind.install(base_url)
end

{_, _} ->
Mix.raise("""
Invalid arguments to tailwind.install, expected one of:
mix tailwind.install
mix tailwind.install 'https://github.com/tailwindlabs/tailwindcss/releases/download/v$version/tailwindcss-$target'
mix tailwind.install --runtime-config
mix tailwind.install --if-missing
""")
{opts, base_url} =
case OptionParser.parse_head!(args, strict: valid_options) do
{opts, []} ->
{opts, Tailwind.default_base_url()}

{opts, [base_url]} ->
{opts, base_url}

{_, _} ->
Mix.raise("""
Invalid arguments to tailwind.install, expected one of:
mix tailwind.install
mix tailwind.install 'https://github.com/tailwindlabs/tailwindcss/releases/download/v$version/tailwindcss-$target'
mix tailwind.install --runtime-config
mix tailwind.install --if-missing
""")
end

if opts[:runtime_config], do: Mix.Task.run("app.config")

if opts[:if_missing] && latest_version?() do
:ok
else
Tailwind.install(base_url)
end
end

Expand Down
11 changes: 7 additions & 4 deletions lib/tailwind.ex
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@ defmodule Tailwind do
run(profile, args)
end

@doc """
The default URL to install Tailwind from.
"""
def default_base_url do
"https://github.com/tailwindlabs/tailwindcss/releases/download/v$version/tailwindcss-$target"
end

@doc """
Installs tailwind with `configured_version/0`.
"""
Expand Down Expand Up @@ -386,8 +393,4 @@ defmodule Tailwind do
|> String.replace("$version", configured_version())
|> String.replace("$target", target())
end

defp default_base_url do
"https://github.com/tailwindlabs/tailwindcss/releases/download/v$version/tailwindcss-$target"
end
end
1 change: 0 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ defmodule Tailwind.MixProject do
app: :tailwind,
version: @version,
elixir: "~> 1.10",
otp: ">= 22.0",
deps: deps(),
description: "Mix tasks for installing and invoking tailwind",
package: [
Expand Down
26 changes: 5 additions & 21 deletions test/tailwind_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,10 @@ defmodule TailwindTest do
assert String.trim(File.read!("assets/js/app.js")) == expected_js
end

describe "Download custom binary" do
test "Fills the URL with the configured version and computed target" do
assert :ok =
Mix.Task.rerun("tailwind.install", [
"https://github.com/tailwindlabs/tailwindcss/releases/download/v$version/tailwindcss-$target"
])
end

test "Fills the URL with the computed target" do
assert :ok =
Mix.Task.rerun("tailwind.install", [
"https://github.com/tailwindlabs/tailwindcss/releases/download/v3.2.2/tailwindcss-$target"
])
end

test "Fills the URL with the configured version" do
assert :ok =
Mix.Task.rerun("tailwind.install", [
"https://github.com/tailwindlabs/tailwindcss/releases/download/v$version/tailwindcss-linux-arm64"
])
end
test "installs with custom URL" do
assert :ok =
Mix.Task.rerun("tailwind.install", [
"https://github.com/tailwindlabs/tailwindcss/releases/download/v$version/tailwindcss-$target"
])
end
end

0 comments on commit 2a9be7e

Please sign in to comment.