From 7f04bfd13490c553bafbc08e4ae9befab0b08db4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Z=C3=A9=20Diogo?= Date: Mon, 24 Apr 2023 21:02:24 +0100 Subject: [PATCH] Make Tesla an optional dependency (#65) --- CHANGELOG.md | 4 + .../mix/tasks/update_metadata.ex | 92 ++++++++++--------- mix.exs | 6 +- 3 files changed, 56 insertions(+), 46 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba78266..eb92594 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project aims to adhere to [Semantic Versioning](https://semver.org/spec ## [Unreleased] +### Fixed + +- Stop failing if Tesla is not in the depencencies + ## [0.4.0] - 2023-04-18 Require Elixir v1.10+. diff --git a/lib/ex_phone_number/mix/tasks/update_metadata.ex b/lib/ex_phone_number/mix/tasks/update_metadata.ex index 0fadaa4..653d5a2 100644 --- a/lib/ex_phone_number/mix/tasks/update_metadata.ex +++ b/lib/ex_phone_number/mix/tasks/update_metadata.ex @@ -1,52 +1,58 @@ -defmodule Mix.Tasks.UpdateMetadata do - @moduledoc "Downloads the latest libphonenumber metadata from GitHub" - @shortdoc "Update libphonenumber metadata" - - use Mix.Task - @raw_files_url "https://raw.githubusercontent.com/google/libphonenumber" - @files_to_download ["resources/PhoneNumberMetadata.xml", "resources/PhoneNumberMetadataForTesting.xml"] - @resources_directory "resources" - - defmodule GitHub do - use Tesla - - plug(Tesla.Middleware.BaseUrl, "https://api.github.com") - plug(Tesla.Middleware.Headers, [{"User-Agent", "ex_phone_number"}]) - plug(Tesla.Middleware.JSON) - - def latest_release(repo) do - get("/repos/" <> repo <> "/releases/latest") +if Code.ensure_loaded?(Tesla) do + defmodule Mix.Tasks.UpdateMetadata do + @moduledoc "Downloads the latest libphonenumber metadata from GitHub" + @shortdoc "Update libphonenumber metadata" + + use Mix.Task + @raw_files_url "https://raw.githubusercontent.com/google/libphonenumber" + @files_to_download ["resources/PhoneNumberMetadata.xml", "resources/PhoneNumberMetadataForTesting.xml"] + @resources_directory "resources" + + defmodule GitHub do + def latest_release(repo) do + Tesla.get(client(), "/repos/" <> repo <> "/releases/latest") + end + + defp client do + middleware = [ + {Tesla.Middleware.BaseUrl, "https://api.github.com"}, + Tesla.Middleware.JSON, + {Tesla.Middleware.Headers, [{"User-Agent", "ex_phone_number"}]} + ] + + Tesla.client(middleware) + end end - end - @impl Mix.Task - def run(_args) do - latest_tag = fetch_latest_tag() - Enum.each(@files_to_download, &download(latest_tag, &1)) - update_readme(latest_tag) - end + @impl Mix.Task + def run(_args) do + latest_tag = fetch_latest_tag() + Enum.each(@files_to_download, &download(latest_tag, &1)) + update_readme(latest_tag) + end - defp fetch_latest_tag() do - {:ok, %{body: body}} = GitHub.latest_release("google/libphonenumber") - body["tag_name"] - end + defp fetch_latest_tag() do + {:ok, %{body: body}} = GitHub.latest_release("google/libphonenumber") + body["tag_name"] + end - defp download(tag, path) do - filename = Path.basename(path) - local_path = Path.join([File.cwd!(), @resources_directory, filename]) - file_url = "#{@raw_files_url}/#{tag}/#{path}" - {:ok, %{body: body}} = Tesla.get(file_url) - File.write!(local_path, body) - end + defp download(tag, path) do + filename = Path.basename(path) + local_path = Path.join([File.cwd!(), @resources_directory, filename]) + file_url = "#{@raw_files_url}/#{tag}/#{path}" + {:ok, %{body: body}} = Tesla.get(file_url) + File.write!(local_path, body) + end - defp update_readme(tag) do - readme_path = Path.join([File.cwd!(), "README.md"]) + defp update_readme(tag) do + readme_path = Path.join([File.cwd!(), "README.md"]) - updated_content = - readme_path - |> File.read!() - |> String.replace(~r{Current metadata version: v[\d+].[\d+].[\d+]\.}, "Current metadata version: " <> tag <> ".") + updated_content = + readme_path + |> File.read!() + |> String.replace(~r{Current metadata version: v[\d+].[\d+].[\d+]\.}, "Current metadata version: " <> tag <> ".") - File.write!(readme_path, updated_content) + File.write!(readme_path, updated_content) + end end end diff --git a/mix.exs b/mix.exs index 510a419..56b5ec1 100644 --- a/mix.exs +++ b/mix.exs @@ -28,9 +28,9 @@ defmodule ExPhoneNumber.Mixfile do {:sweet_xml, "~> 0.7"}, {:ex_doc, ">= 0.0.0", only: :dev, runtime: false}, {:ex_spec, "~> 2.0", only: :test}, - {:tesla, "~> 1.4", only: [:dev, :test]}, - {:hackney, "~> 1.17", only: [:dev, :test]}, - {:jason, ">= 1.0.0", only: [:dev, :test]} + {:tesla, "~> 1.4", only: [:dev, :test], optional: true}, + {:hackney, "~> 1.17", only: [:dev, :test], optional: true}, + {:jason, ">= 1.0.0", only: [:dev, :test], optional: true} ] end