Skip to content

Commit

Permalink
Make Tesla an optional dependency (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
zediogoviana authored Apr 24, 2023
1 parent 63b8314 commit 7f04bfd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 46 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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+.
Expand Down
92 changes: 49 additions & 43 deletions lib/ex_phone_number/mix/tasks/update_metadata.ex
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 7f04bfd

Please sign in to comment.