Skip to content

Commit

Permalink
Merge pull request #6 from aptinio/fix-release-priv_dir
Browse files Browse the repository at this point in the history
Fix IanaDataDir.maybe_copy_iana_files_to_custom_dir() crashing
  • Loading branch information
mathieuprog authored Jul 27, 2021
2 parents 8b6a243 + 8a6ee6f commit 571482c
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions lib/iana_data_dir.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
defmodule Tz.IanaDataDir do
@dir Application.get_env(:tz, :data_dir, :code.priv_dir(:tz))
@regex_tzdata_dir_name ~r/^tzdata20[0-9]{2}[a-z]$/

def dir_path(), do: @dir
defp dir, do: Application.get_env(:tz, :data_dir, :code.priv_dir(:tz))

def tzdata_dir_name(parent_dir \\ @dir) do
def tzdata_dir_name(parent_dir \\ dir()) do
tz_data_dirs =
File.ls!(parent_dir)
|> Enum.filter(&Regex.match?(@regex_tzdata_dir_name, &1))
Expand All @@ -18,7 +17,7 @@ defmodule Tz.IanaDataDir do

def tzdata_dir_path() do
if dir_name = tzdata_dir_name() do
Path.join(@dir, dir_name)
Path.join(dir(), dir_name)
end
end

Expand All @@ -34,20 +33,20 @@ defmodule Tz.IanaDataDir do
tzdata_version() ->
nil

:code.priv_dir(:tz) == @dir ->
:code.priv_dir(:tz) == dir() ->
raise "tzdata files not found"

true ->
if dir_name = tzdata_dir_name(:code.priv_dir(:tz)) do
File.cp_r!(Path.join(:code.priv_dir(:tz), dir_name), Path.join(@dir, dir_name))
File.cp_r!(Path.join(:code.priv_dir(:tz), dir_name), Path.join(dir(), dir_name))
else
raise "tzdata files not found"
end
end
end

def extract_tzdata_into_dir(version, content) do
tmp_archive_path = Path.join(@dir, "tzdata#{version}.tar.gz")
tmp_archive_path = Path.join(dir(), "tzdata#{version}.tar.gz")
tzdata_dir_name = "tzdata#{version}"
:ok = File.write!(tmp_archive_path, content)

Expand All @@ -64,17 +63,18 @@ defmodule Tz.IanaDataDir do
'iso3166.tab',
'zone1970.tab'
]

:ok = :erl_tar.extract(tmp_archive_path, [
:compressed,
{:cwd, Path.join(@dir, tzdata_dir_name)},
{:cwd, Path.join(dir(), tzdata_dir_name)},
{:files, files_to_extract}
])

:ok = File.rm!(tmp_archive_path)
end

def delete_tzdata_dir(version) do
Path.join(@dir, "tzdata#{version}")
Path.join(dir(), "tzdata#{version}")
|> File.rm_rf!()
end
end

0 comments on commit 571482c

Please sign in to comment.