Skip to content

Commit

Permalink
feat: add git option
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Timmer committed May 15, 2024
1 parent cf3e231 commit 8bf8f7e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/mix/tasks/edit/add.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule Mix.Tasks.Edit.Add do
OPTS:
--version Set the version for the DEP
--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'
--override Set the override option
--no-runtime Set the runtime option to false
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 @@ -11,6 +11,7 @@ defmodule Mix.Tasks.Edit.Update do
OPTS:
--version Set the version for the DEP
--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'
--override Set the override option
--no-runtime Set the runtime option to false
Expand Down
5 changes: 5 additions & 0 deletions lib/mix_edit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ defmodule MixEdit do
[
{{:__block__, [format: :keyword], [:path]}, {:__block__, [delimiter: "\""], [value]}}
]

{:git, value} ->
[
{{:__block__, [format: :keyword], [:git]}, {:__block__, [delimiter: "\""], [value]}}
]
end)
end

Expand Down
4 changes: 3 additions & 1 deletion lib/mix_edit/general_task.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule MixEdit.GeneralTask do

@options [
version: :string,
git: :string,
sorted: :boolean,
out: :string,
in: :string,
Expand Down Expand Up @@ -98,7 +99,7 @@ defmodule MixEdit.GeneralTask do
|> Enum.flat_map(fn {:remove, _, removed} -> removed end)
|> Enum.map(&to_string/1)

unless removed == [] do
if removed != [] do
Mix.Task.run("deps.unlock", removed)
Mix.Task.run("deps.clean", removed)
end
Expand Down Expand Up @@ -192,6 +193,7 @@ defmodule MixEdit.GeneralTask do
)
end

defp version_or_path(%{git: git}), do: "git `#{git}`"
defp version_or_path(%{version: version}), do: "version `#{version}`"
defp version_or_path(%{path: path}), do: "path `#{path}`"

Expand Down
53 changes: 53 additions & 0 deletions test/mix_add_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,58 @@ defmodule MixEditTest do
end
end

test "sorting the dependency list works" do
deps = """
defmodule Test.MixProject do
use Mix.Project
def project do
[
version: "0.1.0",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end
defp deps do
[
{:jason, "~> 0.0.0"},
{:ex_doc, ">= 0.0.0"},
{:sweet_xml, ">= 0.0.0"}
]
end
end
"""

opts = %{
dependencies: [testing: [version: ">= 0.0.0"]],
sorted: true,
app: :test,
method: :add
}

{quoted, comments} = MixEdit.quote_string!(deps)

{new_file, info} =
Macro.prewalk(
quoted,
nil,
&MixEdit.deps_walker(&1, &2, opts)
)

assert {:add, [testing: [version: ">= 0.0.0"]]} == info

stringified =
new_file
|> Code.Formatter.to_algebra(comments: comments)
|> Inspect.Algebra.format(:infinity)
|> IO.iodata_to_binary()
|> String.replace("\n", " ")
|> String.replace(~r/\s+/, " ")

assert stringified =~
"defp deps do [{:ex_doc, \">= 0.0.0\"}, {:jason, \"~> 0.0.0\"}, {:sweet_xml, \">= 0.0.0\"}, {:testing, \">= 0.0.0\"}] end"
end

defp unwrap_deps({{:__block__, _, [deps]}, _}), do: deps
end

0 comments on commit 8bf8f7e

Please sign in to comment.