Skip to content

Commit 6d0a928

Browse files
committed
refactor tasks so they show up in mix help
1 parent efe1889 commit 6d0a928

11 files changed

Lines changed: 117 additions & 20 deletions

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ The data for cities comes from the [geonames](http://www.geonames.org/) project.
1919
### Postal Codes
2020

2121
The data for postal codes comes from the [geonames](http://www.geonames.org/) project. This project has scripts to download all or individual postal code files via the --source option.
22+
23+
### Mix Tasks
24+
one must first refresh the applications task lists by running `mix compile` tasks can then be viewed via `mix help`
File renamed without changes.

lib/location/postalcode.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ defmodule Location.PostalCode do
6868
@doc """
6969
Finds postal_code information by postal code.
7070
"""
71-
@spec get_postal_code(string()) :: %__MODULE__{} | nil
71+
@spec get_postal_code(String.t()) :: %__MODULE__{} | nil
7272
def get_postal_code(code) do
7373
case :ets.lookup(@ets_table_by_id, code) do
7474
[{postal_code, {country_code, state_code, city_name, latitude, longitude}}] ->
@@ -85,7 +85,7 @@ defmodule Location.PostalCode do
8585
This function returns all postal code founds when the country has multiple
8686
cities with the same name.
8787
"""
88-
@spec get_postal_codes(string(), string(), string()) :: %__MODULE__{} | nil
88+
@spec get_postal_codes(String.t(), String.t(), String.t()) :: %__MODULE__{} | nil
8989
def get_postal_codes(country_code, state_code, city_name) do
9090
case :ets.lookup(@ets_table_by_lookup, {country_code, state_code, city_name}) do
9191
data when is_list(data) ->
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ defmodule Location.Scraper do
1919
result = Floki.find(result, "a") |> Enum.drop(5)
2020

2121
Enum.map(result, fn x ->
22-
[{_, [{_, href}], [name]}] = Floki.find(x, "a")
22+
[{_, [{_, _href}], [name]}] = Floki.find(x, "a")
2323
String.replace(name, ".zip", "")
2424
end)
2525
|> Enum.join(", ")

mix_tasks/update_english_translations.ex renamed to lib/mix/tasks/update_english_translations.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
defmodule Mix.Tasks.UpdateEnglishTranslations do
1+
defmodule Mix.Tasks.Location.UpdateEnglishTranslations do
22
use Mix.Task
3+
@shortdoc "Updates english translations for locations"
34

45
@cldr_url "https://raw.githubusercontent.com/unicode-org/cldr/main/common/subdivisions/en.xml"
56
@translations_dest Application.app_dir(:location, "/priv/iso_3166-2.en-translations.json")
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
defmodule Mix.Tasks.UpdateGeonameData do
1+
defmodule Mix.Tasks.Location.UpdateGeonameData do
22
use Mix.Task
3-
4-
# @allcountries_src "https://download.geonames.org/export/dump/allCountries.zip"
3+
@shortdoc "Updates the geonamedata for locations"
4+
@allcountries_src "https://download.geonames.org/export/dump/allCountries.zip"
55
@allcountries_dest Application.app_dir(:location, "/priv/geonames.csv")
66

77
@doc """
88
The data source allCountries.txt clocks in at 1.5GB. Expect this to take a while.
99
"""
1010
def run(_) do
11-
# System.cmd("wget", [@allcountries_src, "-O", "/tmp/allCountries.zip"])
12-
# System.cmd("unzip", ["/tmp/allCountries.zip", "-d", "/tmp"])
11+
System.cmd("wget", [@allcountries_src, "-O", "/tmp/allCountries.zip"])
12+
zip_file = Unzip.LocalFile.open("/tmp/allCountries.zip")
13+
{:ok, unzip} = Unzip.new(zip_file)
14+
Unzip.file_stream!(unzip, "allCountries.txt")
15+
|> Stream.into(File.stream!("/tmp/allCountries.txt"))
16+
|> Stream.run()
1317

1418
process_geonames_file("/tmp/allCountries.txt")
1519
end
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
defmodule Mix.Tasks.UpdateIsoData do
1+
defmodule Mix.Tasks.Location.UpdateIsoData do
22
use Mix.Task
3+
@shortdoc "Updates the isocodes for locations"
34

45
@countries_src "https://salsa.debian.org/iso-codes-team/iso-codes/-/raw/main/data/iso_3166-1.json"
56
@subdivisions_src "https://salsa.debian.org/iso-codes-team/iso-codes/-/raw/main/data/iso_3166-2.json"
Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
defmodule Mix.Tasks.UpdatePostalCodeData do
1+
defmodule Mix.Tasks.Location.UpdatePostalCodeData do
22
use Mix.Task
3+
@shortdoc "Updates the postal code data from source"
34

4-
default = Application.app_dir(:location, "/priv/postal_codes.csv")
5-
@destination_filename Application.get_env(:location, :postal_codes_source_file, default)
5+
@destination_filename Application.compile_env(:location, :postal_codes_source_file, "priv/postal_codes.csv")
66

77
@doc """
88
The data source clocks in at 16mb. Expect this to take a while.
@@ -11,10 +11,12 @@ defmodule Mix.Tasks.UpdatePostalCodeData do
1111

1212
def run(args) do
1313
{options, _, _} =
14-
OptionParser.parse(["--source", "allCountries", "--list", "--append", "--help"],
14+
OptionParser.parse(args,
1515
strict: [source: :string, list: :boolean, append: :boolean, help: :boolean]
1616
)
1717

18+
options = Keyword.merge([help: false, list: false, append: false], options)
19+
1820
case(Keyword.get(options, :help) || Keyword.get(options, :list)) do
1921
false ->
2022
Keyword.get(options, :source)
@@ -41,7 +43,12 @@ defmodule Mix.Tasks.UpdatePostalCodeData do
4143
def main(name, append \\ false) do
4244
src = "https://download.geonames.org/export/zip/#{name}.zip"
4345
System.cmd("wget", [src, "-O", "/tmp/#{name}.zip"])
44-
System.cmd("unzip", ["/tmp/#{name}.zip", "-d", "/tmp"])
46+
47+
zip_file = Unzip.LocalFile.open("/tmp/#{name}.zip")
48+
{:ok, unzip} = Unzip.new(zip_file)
49+
Unzip.file_stream!(unzip, "#{name}.txt")
50+
|> Stream.into(File.stream!("/tmp/#{name}.txt"))
51+
|> Stream.run()
4552

4653
process_file("/tmp/#{name}.txt", append)
4754
end
@@ -60,11 +67,12 @@ defmodule Mix.Tasks.UpdatePostalCodeData do
6067

6168
IO.puts("Writing result to #{@destination_filename}")
6269

70+
Location.Scraper.write_date_to_version()
71+
6372
case append do
6473
false -> File.write!(@destination_filename, Enum.join(result, "\n"))
6574
true -> File.write!(@destination_filename, Enum.join(result, "\n"), :append)
6675
end
6776

68-
Location.Scraper.write_date_to_version()
6977
end
7078
end

location.iml

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="ELIXIR_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<excludeFolder url="file://$MODULE_DIR$/_build/test/lib/floki" />
7+
<excludeFolder url="file://$MODULE_DIR$/_build/test/lib/flow" />
8+
<excludeFolder url="file://$MODULE_DIR$/_build/test/lib/gen_stage" />
9+
<excludeFolder url="file://$MODULE_DIR$/_build/test/lib/html_entities" />
10+
<excludeFolder url="file://$MODULE_DIR$/_build/test/lib/idna" />
11+
<excludeFolder url="file://$MODULE_DIR$/_build/test/lib/jason" />
12+
<excludeFolder url="file://$MODULE_DIR$/_build/test/lib/mimerl" />
13+
<excludeFolder url="file://$MODULE_DIR$/_build/test/lib/nimble_csv" />
14+
<excludeFolder url="file://$MODULE_DIR$/_build/test/lib/ssl_verify_fun" />
15+
<excludeFolder url="file://$MODULE_DIR$/_build/test/lib/unicode_util_compat" />
16+
<excludeFolder url="file://$MODULE_DIR$/_build/dev/lib/floki" />
17+
<excludeFolder url="file://$MODULE_DIR$/_build/dev/lib/flow" />
18+
<excludeFolder url="file://$MODULE_DIR$/_build/dev/lib/gen_stage" />
19+
<excludeFolder url="file://$MODULE_DIR$/_build/dev/lib/html_entities" />
20+
<excludeFolder url="file://$MODULE_DIR$/_build/dev/lib/idna" />
21+
<excludeFolder url="file://$MODULE_DIR$/_build/dev/lib/jason" />
22+
<excludeFolder url="file://$MODULE_DIR$/_build/dev/lib/mimerl" />
23+
<excludeFolder url="file://$MODULE_DIR$/_build/dev/lib/nimble_csv" />
24+
<excludeFolder url="file://$MODULE_DIR$/_build/dev/lib/ssl_verify_fun" />
25+
<excludeFolder url="file://$MODULE_DIR$/_build/dev/lib/unicode_util_compat" />
26+
<excludeFolder url="file://$MODULE_DIR$/_build/dev/lib/certifi" />
27+
<excludeFolder url="file://$MODULE_DIR$/_build/dev/lib/hackney" />
28+
<excludeFolder url="file://$MODULE_DIR$/_build/dev/lib/metrics" />
29+
<excludeFolder url="file://$MODULE_DIR$/_build/dev/lib/mime" />
30+
<excludeFolder url="file://$MODULE_DIR$/_build/dev/lib/parse_trans" />
31+
<excludeFolder url="file://$MODULE_DIR$/_build/dev/lib/tesla" />
32+
<excludeFolder url="file://$MODULE_DIR$/_build/dev/lib/unzip" />
33+
<excludeFolder url="file://$MODULE_DIR$/_build/test/lib/certifi" />
34+
<excludeFolder url="file://$MODULE_DIR$/_build/test/lib/hackney" />
35+
<excludeFolder url="file://$MODULE_DIR$/_build/test/lib/metrics" />
36+
<excludeFolder url="file://$MODULE_DIR$/_build/test/lib/mime" />
37+
<excludeFolder url="file://$MODULE_DIR$/_build/test/lib/parse_trans" />
38+
<excludeFolder url="file://$MODULE_DIR$/_build/test/lib/tesla" />
39+
<excludeFolder url="file://$MODULE_DIR$/_build/test/lib/unzip" />
40+
</content>
41+
<orderEntry type="inheritedJdk" />
42+
<orderEntry type="sourceFolder" forTests="false" />
43+
<orderEntry type="library" name="jason" level="project" />
44+
<orderEntry type="library" name="nimble_csv" level="project" />
45+
<orderEntry type="library" name="floki" level="project" />
46+
<orderEntry type="library" name="httpoison" level="project" />
47+
<orderEntry type="library" name="flow" level="project" />
48+
<orderEntry type="library" name="ex_doc" level="project" />
49+
<orderEntry type="library" name="hackney" level="project" />
50+
<orderEntry type="library" name="mimic" level="project" />
51+
<orderEntry type="library" name="httparrot" level="project" />
52+
<orderEntry type="library" name="earmark" level="project" />
53+
<orderEntry type="library" name="dialyxir" level="project" />
54+
<orderEntry type="library" name="gen_stage" level="project" />
55+
<orderEntry type="library" name="tesla" level="project" />
56+
<orderEntry type="library" name="mime" level="project" />
57+
<orderEntry type="library" name="ibrowse" level="project" />
58+
<orderEntry type="library" name="gun" level="project" />
59+
<orderEntry type="library" name="finch" level="project" />
60+
<orderEntry type="library" name="castore" level="project" />
61+
<orderEntry type="library" name="mint" level="project" />
62+
<orderEntry type="library" name="poison" level="project" />
63+
<orderEntry type="library" name="exjsx" level="project" />
64+
<orderEntry type="library" name="msgpax" level="project" />
65+
<orderEntry type="library" name="fuse" level="project" />
66+
<orderEntry type="library" name="telemetry" level="project" />
67+
<orderEntry type="library" name="opentelemetry_process_propagator" level="project" />
68+
<orderEntry type="library" name="excoveralls" level="project" />
69+
<orderEntry type="library" name="mix_test_watch" level="project" />
70+
<orderEntry type="library" name="inch_ex" level="project" />
71+
<orderEntry type="library" name="cowlib" level="project" />
72+
<orderEntry type="library" name="ranch" level="project" />
73+
<orderEntry type="library" name="benchee" level="project" />
74+
<orderEntry type="library" name="credo" level="project" />
75+
<orderEntry type="library" name="html5ever" level="project" />
76+
<orderEntry type="library" name="fast_html" level="project" />
77+
<orderEntry type="library" name="unzip" level="project" />
78+
<orderEntry type="library" name="benchee_html" level="project" />
79+
<orderEntry type="library" name="mox" level="project" />
80+
</component>
81+
</module>

mix.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ defmodule Location.MixProject do
4040
defp extra_applications(_env), do: []
4141

4242
# Specifies which paths to compile per environment.
43-
defp elixirc_paths(env) when env in [:dev, :test], do: ["lib", "mix_tasks"]
43+
defp elixirc_paths(:test), do: ["lib"]
4444
defp elixirc_paths(_env), do: ["lib"]
4545

4646
# Run "mix help deps" to learn about dependencies.
4747
defp deps do
4848
[
4949
{:jason, "~> 1.3"},
5050
{:nimble_csv, "~> 1.2"},
51-
{:floki, "~> 0.35.2", only: [:dev, :test]},
51+
{:floki, "~> 0.35.2"},
5252
{:tesla, "~> 1.8"},
5353
{:hackney, "~> 1.20"},
54-
{:flow, "~> 1.2", only: [:dev, :test]},
54+
{:flow, "~> 1.2"},
5555
{:unzip, "0.11.0"}
5656
]
5757
end

0 commit comments

Comments
 (0)