Skip to content

Commit

Permalink
Update to elixir 1.16 and remove custom relative_to
Browse files Browse the repository at this point in the history
  • Loading branch information
pnezis committed Oct 21, 2024
1 parent c17f0c6 commit bc3fc06
Show file tree
Hide file tree
Showing 10 changed files with 12 additions and 92 deletions.
2 changes: 1 addition & 1 deletion workspace/lib/mix/tasks/workspace.check.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ defmodule Mix.Tasks.Workspace.Check do
end

defp print_result(result) do
path = Workspace.Utils.Path.relative_to(result.project.path, File.cwd!())
path = Path.relative_to(result.project.path, File.cwd!(), force: true)

log([
highlight(status_text(result.status), status_color(result.status)),
Expand Down
2 changes: 1 addition & 1 deletion workspace/lib/mix/tasks/workspace.test.coverage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ defmodule Mix.Tasks.Workspace.Test.Coverage do
end

defp import_cover_result(workspace_path, cover_path, app) do
path = Workspace.Utils.Path.relative_to(cover_path, workspace_path)
path = Path.relative_to(cover_path, workspace_path, force: true)

log([
" ",
Expand Down
2 changes: 1 addition & 1 deletion workspace/lib/mix/workspace_utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule Mix.WorkspaceUtils do
:ok

{:error, _reason} ->
path = Workspace.Utils.Path.relative_to(workspace.workspace_path, File.cwd!())
path = Path.relative_to(workspace.workspace_path, File.cwd!(), force: true)

Mix.raise("status related operations require a git repo, #{path} is not a valid git repo")
end
Expand Down
4 changes: 2 additions & 2 deletions workspace/lib/workspace.ex
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ defmodule Workspace do
def new(path, config_or_path \\ [])

def new(path, config_or_path) when is_binary(config_or_path) do
config_path = Workspace.Utils.Path.relative_to(config_or_path, Path.expand(path))
config_path = Path.relative_to(config_or_path, Path.expand(path), force: true)
config_path = Path.join(path, config_path)

with {:ok, config} <- Workspace.Config.load(config_path) do
Expand Down Expand Up @@ -408,7 +408,7 @@ defmodule Workspace do
Enum.map_join(
projects,
", ",
&Workspace.Utils.Path.relative_to(&1.mix_path, &1.workspace_path)
&Path.relative_to(&1.mix_path, &1.workspace_path, force: true)
)

"* #{inspect(app)} is defined under: #{paths}"
Expand Down
6 changes: 3 additions & 3 deletions workspace/lib/workspace/checks/validate_config_path.ex
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ defmodule Workspace.Checks.ValidateConfigPath do
}) do
attribute = check[:opts][:config_attribute]

expected = Workspace.Utils.Path.relative_to(meta[:expected], project.path)
expected = Path.relative_to(meta[:expected], project.path, force: true)

configured =
case meta[:configured] do
nil -> "nil"
configured -> Workspace.Utils.Path.relative_to(configured, project.path)
configured -> Path.relative_to(configured, project.path, force: true)
end

[
Expand All @@ -140,7 +140,7 @@ defmodule Workspace.Checks.ValidateConfigPath do
status: :ok
}) do
attribute = check[:opts][:config_attribute]
expected = Workspace.Utils.Path.relative_to(meta[:expected], project.path)
expected = Path.relative_to(meta[:expected], project.path, force: true)

[
:light_cyan,
Expand Down
2 changes: 1 addition & 1 deletion workspace/lib/workspace/checks/workspace_deps_paths.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ defmodule Workspace.Checks.WorkspaceDepsPaths do
project_path = project.path
dependency_path = Workspace.project!(workspace, app).path

Workspace.Utils.Path.relative_to(dependency_path, project_path)
Path.relative_to(dependency_path, project_path, force: true)
end

defp sanitize(path) do
Expand Down
4 changes: 2 additions & 2 deletions workspace/lib/workspace/project.ex
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ defmodule Workspace.Project do
defp maybe_relative(path, _workspace_path, false), do: path

defp maybe_relative(path, workspace_path, true),
do: Workspace.Utils.Path.relative_to(path, workspace_path)
do: Path.relative_to(path, workspace_path, force: true)

@valid_statuses [:undefined, :modified, :affected, :unaffected]

Expand Down Expand Up @@ -334,7 +334,7 @@ defmodule Workspace.Project do
"""
@spec relative_to_workspace(project :: t()) :: binary()
def relative_to_workspace(%Project{path: path, workspace_path: workspace_path}),
do: Workspace.Utils.Path.relative_to(path, workspace_path)
do: Path.relative_to(path, workspace_path, force: true)

# returns an "app name" for the given mix.exs file, it is the
# folder name containing the project. We need a consistent app name
Expand Down
45 changes: 0 additions & 45 deletions workspace/lib/workspace/utils/path.ex
Original file line number Diff line number Diff line change
@@ -1,51 +1,6 @@
defmodule Workspace.Utils.Path do
@moduledoc false

@doc """
Returns `true` if the given `path` is relative, `false` otherwise.
## Examples
iex> Workspace.Utils.Path.relative?("../local")
true
iex> Workspace.Utils.Path.relative?("./path/to/cli.ex")
true
iex> Workspace.Utils.Path.relative?("/usr/local/cli.ex")
false
"""
@spec relative?(path :: Path.t()) :: boolean()
def relative?(path), do: Path.type(path) == :relative

# TODO: Remove once we upgrade to elixir 1.16.0
@doc """
Returns the relative path of `path` with respect to `cwd`
Both paths are expanded before returning the relative path.
"""
@spec relative_to(path :: Path.t(), cwd :: Path.t()) :: binary()
def relative_to(path, cwd) do
cond do
relative?(path) ->
IO.chardata_to_string(path)

true ->
split_path = path |> Path.expand() |> Path.split()
split_cwd = cwd |> Path.expand() |> Path.split()

relative_to(split_path, split_cwd, split_path)
end
end

defp relative_to(path, path, _original), do: "."
defp relative_to([h | t1], [h | t2], original), do: relative_to(t1, t2, original)

defp relative_to(l1, l2, _original) do
base = List.duplicate("..", length(l2))
Path.join(base ++ l1)
end

@doc """
Checks if `base` is a parent directory of `path`
Expand Down
2 changes: 1 addition & 1 deletion workspace/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Workspace.MixProject do
[
app: @app,
version: @version,
elixir: "~> 1.15",
elixir: "~> 1.16",
start_permanent: Mix.env() == :prod,
deps: deps(),
deps_path: "../artifacts/deps",
Expand Down
35 changes: 0 additions & 35 deletions workspace/test/workspace/utils/path_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,6 @@ defmodule Workspace.Utils.Path.PathTest do

alias Workspace.Utils

describe "relative_to/2" do
test "with absolute paths" do
assert Utils.Path.relative_to("/usr/local/foo", "/usr/local") == "foo"
assert Utils.Path.relative_to("/usr/local/foo", "/") == "usr/local/foo"
assert Utils.Path.relative_to("/usr/local/foo", "/etc") == "../usr/local/foo"
assert Utils.Path.relative_to("/usr/local/foo", "/usr/local/foo") == "."
assert Utils.Path.relative_to("/usr/local/foo/", "/usr/local/foo") == "."
assert Utils.Path.relative_to("/usr/local/foo", "/usr/local/foo/") == "."

assert Utils.Path.relative_to("/etc", "/usr/local/foo") == "../../../etc"
assert Utils.Path.relative_to(~c"/usr/local/foo", "/etc") == "../usr/local/foo"
assert Utils.Path.relative_to("/usr/local", "/usr/local/foo") == ".."
assert Utils.Path.relative_to("/usr/local/..", "/usr/local") == ".."

assert Utils.Path.relative_to("/usr/../etc/foo/../../bar", "/log/foo/../../usr/") ==
"../bar"
end

test "with relative paths" do
assert Utils.Path.relative_to("usr/local/foo", "usr/local") == "usr/local/foo"
assert Utils.Path.relative_to("usr/local/foo", "etc") == "usr/local/foo"

assert Utils.Path.relative_to("usr/local/foo", "usr/local") == "usr/local/foo"

# on cwd
assert Utils.Path.relative_to("foo", File.cwd!()) == "foo"
assert Utils.Path.relative_to("./foo", File.cwd!()) == "./foo"
assert Utils.Path.relative_to("../foo", File.cwd!()) == "../foo"

# both relative
assert Utils.Path.relative_to("usr/local/foo", "usr/local") == "usr/local/foo"
assert Utils.Path.relative_to("usr/local/foo", "etc") == "usr/local/foo"
end
end

describe "parent_dir?" do
test "with absolute dirs" do
assert Utils.Path.parent_dir?("/usr/local/workspace", "/usr/local/workspace/foo")
Expand Down

0 comments on commit bc3fc06

Please sign in to comment.