From ae275c6396545defc80a765a6b835d01f4c76599 Mon Sep 17 00:00:00 2001 From: alisdair sullivan Date: Mon, 14 Sep 2015 11:46:53 -0700 Subject: function name changes: `reduce_path/1` -> `canonical_path/1` `relative_path/2` -> `path_from_ancestor/2` --- src/rebar_dir.erl | 12 ++++++------ src/rebar_file_utils.erl | 36 ++++++++++++++++++------------------ test/rebar_file_utils_SUITE.erl | 36 ++++++++++++++++++------------------ 3 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/rebar_dir.erl b/src/rebar_dir.erl index bbc7394..364e197 100644 --- a/src/rebar_dir.erl +++ b/src/rebar_dir.erl @@ -175,15 +175,15 @@ retarget_path(State, Path) -> %% not relative to any apps in project, check to see it's relative to %% project root retarget_path(State, Path, []) -> - case rebar_file_utils:relative_path(rebar_file_utils:reduce_path(Path), rebar_state:dir(State)) of - {ok, NewPath} -> filename:join([base_dir(State), NewPath]); + case rebar_file_utils:path_from_ancestor(rebar_file_utils:canonical_path(Path), rebar_state:dir(State)) of + {ok, NewPath} -> filename:join([base_dir(State), NewPath]); %% not relative to project root, don't modify - {error, not_relative} -> Path + {error, badparent} -> Path end; %% relative to current app, retarget to the same dir relative to %% the app's out_dir retarget_path(State, Path, [App|Rest]) -> - case rebar_file_utils:relative_path(rebar_file_utils:reduce_path(Path), rebar_app_info:dir(App)) of - {ok, NewPath} -> filename:join([rebar_app_info:out_dir(App), NewPath]); - {error, not_relative} -> retarget_path(State, Path, Rest) + case rebar_file_utils:path_from_ancestor(rebar_file_utils:canonical_path(Path), rebar_app_info:dir(App)) of + {ok, NewPath} -> filename:join([rebar_app_info:out_dir(App), NewPath]); + {error, badparent} -> retarget_path(State, Path, Rest) end. diff --git a/src/rebar_file_utils.erl b/src/rebar_file_utils.erl index 26d4da0..ea1a6a2 100644 --- a/src/rebar_file_utils.erl +++ b/src/rebar_file_utils.erl @@ -38,8 +38,8 @@ system_tmpdir/1, reset_dir/1, touch/1, - relative_path/2, - reduce_path/1]). + path_from_ancestor/2, + canonical_path/1]). -include("rebar.hrl"). @@ -247,29 +247,29 @@ touch(Path) -> atime = calendar:local_time()}). %% for a given path return the path relative to a base directory --spec relative_path(string(), string()) -> {ok, string()} | {error, not_relative}. +-spec path_from_ancestor(string(), string()) -> {ok, string()} | {error, badparent}. -relative_path(Target, To) -> - relative_path1(filename:split(reduce_path(Target)), - filename:split(reduce_path(To))). +path_from_ancestor(Target, To) -> + path_from_ancestor_(filename:split(canonical_path(Target)), + filename:split(canonical_path(To))). -relative_path1([Part|Target], [Part|To]) -> relative_path1(Target, To); -relative_path1([], []) -> {ok, ""}; -relative_path1(Target, []) -> {ok, filename:join(Target)}; -relative_path1(_, _) -> {error, not_relative}. +path_from_ancestor_([Part|Target], [Part|To]) -> path_from_ancestor_(Target, To); +path_from_ancestor_([], []) -> {ok, ""}; +path_from_ancestor_(Target, []) -> {ok, filename:join(Target)}; +path_from_ancestor_(_, _) -> {error, badparent}. %% reduce a filepath by removing all incidences of `.' and `..' --spec reduce_path(string()) -> string(). +-spec canonical_path(string()) -> string(). -reduce_path(Dir) -> reduce_path([], filename:split(filename:absname(Dir))). +canonical_path(Dir) -> canonical_path([], filename:split(filename:absname(Dir))). -reduce_path([], []) -> filename:nativename("/"); -reduce_path(Acc, []) -> filename:join(lists:reverse(Acc)); -reduce_path(Acc, ["."|Rest]) -> reduce_path(Acc, Rest); -reduce_path([_|Acc], [".."|Rest]) -> reduce_path(Acc, Rest); -reduce_path([], [".."|Rest]) -> reduce_path([], Rest); -reduce_path(Acc, [Component|Rest]) -> reduce_path([Component|Acc], Rest). +canonical_path([], []) -> filename:nativename("/"); +canonical_path(Acc, []) -> filename:join(lists:reverse(Acc)); +canonical_path(Acc, ["."|Rest]) -> canonical_path(Acc, Rest); +canonical_path([_|Acc], [".."|Rest]) -> canonical_path(Acc, Rest); +canonical_path([], [".."|Rest]) -> canonical_path([], Rest); +canonical_path(Acc, [Component|Rest]) -> canonical_path([Component|Acc], Rest). %% =================================================================== %% Internal functions diff --git a/test/rebar_file_utils_SUITE.erl b/test/rebar_file_utils_SUITE.erl index ba34723..0f444d5 100644 --- a/test/rebar_file_utils_SUITE.erl +++ b/test/rebar_file_utils_SUITE.erl @@ -11,8 +11,8 @@ reset_nonexistent_dir/1, reset_empty_dir/1, reset_dir/1, - relative_path/1, - reduce_path/1]). + path_from_ancestor/1, + canonical_path/1]). -include_lib("common_test/include/ct.hrl"). -include_lib("eunit/include/eunit.hrl"). @@ -22,7 +22,7 @@ all() -> [{group, tmpdir}, {group, reset_dir}, - relative_path, reduce_path]. + path_from_ancestor, canonical_path]. groups() -> [{tmpdir, [], [raw_tmpdir, empty_tmpdir, simple_tmpdir, multi_tmpdir]}, @@ -88,19 +88,19 @@ reset_dir(Config) -> ?assert(filelib:is_dir(TmpDir)), {ok, []} = file:list_dir(TmpDir). -relative_path(_Config) -> - ?assertEqual({ok, "foo/bar/baz"}, rebar_file_utils:relative_path("/foo/bar/baz", "/")), - ?assertEqual({ok, "bar/baz"}, rebar_file_utils:relative_path("/foo/bar/baz", "/foo")), - ?assertEqual({ok, "bar"}, rebar_file_utils:relative_path("foo/bar", "foo")), - ?assertEqual({ok, "bar"}, rebar_file_utils:relative_path("foo/bar/", "foo/")), - ?assertEqual({error, not_relative}, rebar_file_utils:relative_path("/foo/bar/baz", "/qux")), - ?assertEqual({error, not_relative}, rebar_file_utils:relative_path("/foo/bar/baz", "/foo/bar/baz/qux")). +path_from_ancestor(_Config) -> + ?assertEqual({ok, "foo/bar/baz"}, rebar_file_utils:path_from_ancestor("/foo/bar/baz", "/")), + ?assertEqual({ok, "bar/baz"}, rebar_file_utils:path_from_ancestor("/foo/bar/baz", "/foo")), + ?assertEqual({ok, "bar"}, rebar_file_utils:path_from_ancestor("foo/bar", "foo")), + ?assertEqual({ok, "bar"}, rebar_file_utils:path_from_ancestor("foo/bar/", "foo/")), + ?assertEqual({error, badparent}, rebar_file_utils:path_from_ancestor("/foo/bar/baz", "/qux")), + ?assertEqual({error, badparent}, rebar_file_utils:path_from_ancestor("/foo/bar/baz", "/foo/bar/baz/qux")). -reduce_path(_Config) -> - ?assertEqual("/", rebar_file_utils:reduce_path("/")), - ?assertEqual("/", rebar_file_utils:reduce_path("/../../..")), - ?assertEqual("/foo", rebar_file_utils:reduce_path("/foo/bar/..")), - ?assertEqual("/foo", rebar_file_utils:reduce_path("/foo/../foo")), - ?assertEqual("/foo", rebar_file_utils:reduce_path("/foo/.")), - ?assertEqual("/foo", rebar_file_utils:reduce_path("/foo/./.")), - ?assertEqual("/foo/bar", rebar_file_utils:reduce_path("/foo/./bar")). \ No newline at end of file +canonical_path(_Config) -> + ?assertEqual("/", rebar_file_utils:canonical_path("/")), + ?assertEqual("/", rebar_file_utils:canonical_path("/../../..")), + ?assertEqual("/foo", rebar_file_utils:canonical_path("/foo/bar/..")), + ?assertEqual("/foo", rebar_file_utils:canonical_path("/foo/../foo")), + ?assertEqual("/foo", rebar_file_utils:canonical_path("/foo/.")), + ?assertEqual("/foo", rebar_file_utils:canonical_path("/foo/./.")), + ?assertEqual("/foo/bar", rebar_file_utils:canonical_path("/foo/./bar")). \ No newline at end of file -- cgit v1.1