Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/mix/tasks/edit/add.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule Mix.Tasks.Edit.Add do

OPTS:
--version Set the version for the DEP
--lossy Fetch the latest version without patch (example '~> 1.2')
--path Set the path for the DEP
--git Set the git repo for the DEP
--only Setting the only flag, example 'test' or 'test+dev'
Expand Down
1 change: 1 addition & 0 deletions lib/mix/tasks/edit/update.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Mix.Tasks.Edit.Update do

OPTS:
--version Set the version for the DEP
--lossy Fetch the latest version without patch (example '~> 1.2')
--path Set the path for the DEP
--git Set the git repo for the DEP
--only Setting the only flag, example 'test' or 'test+dev'
Expand Down
13 changes: 12 additions & 1 deletion lib/mix_edit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ defmodule MixEdit do
auth = Mix.Tasks.Hex.auth_info(:read, auth_inline: false)

hex_version =
get_version_fetcher().get(extra_requirements[:org], package, auth)
get_version_fetcher().get(opts[:org], package, auth)
|> get_latest_package_version!(package)
|> remove_patch_version(opts[:lossy])
|> add_prefix()

[version: hex_version]
Expand All @@ -49,6 +50,13 @@ defmodule MixEdit do
"~> #{version}"
end

defp remove_patch_version(version, true) do
version_struct = Version.parse!(version)
"#{version_struct.major}.#{version_struct.minor}"
end

defp remove_patch_version(version, _), do: version

@doc """
Convert elixir text to quoted term
"""
Expand Down Expand Up @@ -229,6 +237,9 @@ defmodule MixEdit do
{:only, []} ->
[]

{:lossy, _} ->
[]

{:only, value} ->
[
{{:__block__, [format: :keyword], [:only]},
Expand Down
5 changes: 4 additions & 1 deletion lib/mix_edit/general_task.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ defmodule MixEdit.GeneralTask do
only: :string,
org: :string,
override: :boolean,
lossy: :boolean,
runtime: :boolean
]

Expand Down Expand Up @@ -48,7 +49,9 @@ defmodule MixEdit.GeneralTask do
[{String.to_atom(dep), MixEdit.fetch_version_or_option(dep, opts)}]

deps ->
Enum.map(deps, fn dep -> {String.to_atom(dep), MixEdit.fetch_version_or_option(dep)} end)
Enum.map(deps, fn dep ->
{String.to_atom(dep), MixEdit.fetch_version_or_option(dep, lossy: opts[:lossy])}
end)
end

app_paths =
Expand Down
20 changes: 19 additions & 1 deletion test/mix_add_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule MixEditTest do

describe "fetch_version_or_option" do
test "fetches from hex" do
assert [version: "~> 735.7"] == MixEdit.fetch_version_or_option("testing")
assert [version: "~> 735.7.6"] == MixEdit.fetch_version_or_option("testing")
end

test "not exists raises" do
Expand All @@ -14,6 +14,23 @@ defmodule MixEditTest do
end

test "fetches from hex with options" do
assert [
version: "~> 1.23.6",
override: true,
runtime: false,
only: [:test, :dev, :prod],
org: "myorg"
] ==
MixEdit.fetch_version_or_option("testing",
override: true,
runtime: false,
only: "test+dev+prod",
org: "myorg",
extra_fields: :ignored
)
end

test "fetches from hex with options, lossy" do
assert [
version: "~> 1.23",
override: true,
Expand All @@ -26,6 +43,7 @@ defmodule MixEditTest do
runtime: false,
only: "test+dev+prod",
org: "myorg",
lossy: true,
extra_fields: :ignored
)
end
Expand Down
4 changes: 2 additions & 2 deletions test/support/fake_package.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
defmodule MixEdit.FakePackage do
def get(nil, "testing", _) do
{:ok, {200, %{"latest_stable_version" => "735.7"}, []}}
{:ok, {200, %{"latest_stable_version" => "735.7.6"}, []}}
end

def get("myorg", "testing", _) do
{:ok, {200, %{"latest_stable_version" => "1.23"}, []}}
{:ok, {200, %{"latest_stable_version" => "1.23.6"}, []}}
end

def get(nil, "not_existing", _) do
Expand Down