Преглед на файлове

Merge pull request #2247 from pablocostass/export_more_path_functions_in_rebar3_file_utils

Move `absolute_path/2` and `normalized_path` to `rebar_file_utils`
pull/2249/head
Fred Hebert преди 5 години
committed by GitHub
родител
ревизия
13426a32d5
No known key found for this signature in database GPG ключ ID: 4AEE18F83AFDEB23
променени са 3 файла, в които са добавени 83 реда и са изтрити 44 реда
  1. +4
    -44
      src/rebar_dir.erl
  2. +44
    -0
      src/rebar_file_utils.erl
  3. +35
    -0
      test/rebar_file_utils_SUITE.erl

+ 4
- 44
src/rebar_dir.erl Целия файл

@ -191,42 +191,6 @@ processing_base_dir(State, Dir) ->
AbsDir = filename:absname(Dir),
AbsDir =:= rebar_state:get(State, base_dir).
%% @doc make a path absolute
-spec make_absolute_path(file:filename()) -> file:filename().
make_absolute_path(Path) ->
case filename:pathtype(Path) of
absolute ->
Path;
relative ->
{ok, Dir} = file:get_cwd(),
filename:join([Dir, Path]);
volumerelative ->
Volume = hd(filename:split(Path)),
{ok, Dir} = file:get_cwd(Volume),
filename:join([Dir, Path])
end.
%% @doc normalizing a path removes all of the `..' and the
%% `.' segments it may contain.
-spec make_normalized_path(file:filename()) -> file:filename().
make_normalized_path(Path) ->
AbsPath = make_absolute_path(Path),
Components = filename:split(AbsPath),
make_normalized_path(Components, []).
%% @private drops path fragments for normalization
-spec make_normalized_path([string()], [string()]) -> file:filename().
make_normalized_path([], NormalizedPath) ->
filename:join(lists:reverse(NormalizedPath));
make_normalized_path([H|T], NormalizedPath) ->
case H of
"." when NormalizedPath == [], T == [] -> make_normalized_path(T, ["."]);
"." -> make_normalized_path(T, NormalizedPath);
".." when NormalizedPath == [] -> make_normalized_path(T, [".."]);
".." when hd(NormalizedPath) =/= ".." -> make_normalized_path(T, tl(NormalizedPath));
_ -> make_normalized_path(T, [H|NormalizedPath])
end.
%% @doc take a source and a target path, and relativize the target path
%% onto the source.
%%
@ -239,8 +203,8 @@ make_normalized_path([H|T], NormalizedPath) ->
%% '''
-spec make_relative_path(file:filename(), file:filename()) -> file:filename().
make_relative_path(Source, Target) ->
AbsSource = make_normalized_path(Source),
AbsTarget = make_normalized_path(Target),
AbsSource = rebar_file_utils:normalized_path(Source),
AbsTarget = rebar_file_utils:normalized_path(Target),
do_make_relative_path(filename:split(AbsSource), filename:split(AbsTarget)).
%% @private based on fragments of paths, replace the number of common
@ -285,8 +249,8 @@ extra_src_dirs(Opts, Default) ->
src_dirs(Type, Opts, Default) ->
lists:usort([
case D0 of
{D,_} -> normalize_relative_path(D);
_ -> normalize_relative_path(D0)
{D,_} -> rebar_file_utils:normalize_relative_path(D);
_ -> rebar_file_utils:normalize_relative_path(D0)
end || D0 <- raw_src_dirs(Type,Opts,Default)]).
%% @private extracts the un-formatted src_dirs or extra_src_dirs
@ -299,10 +263,6 @@ raw_src_dirs(Type, Opts, Default) ->
Dirs -> Dirs
end.
%% @private normalizes relative paths so that ./a/b/c/ => a/b/c
normalize_relative_path(Path) ->
make_normalized_path(filename:split(Path), []).
%% @doc returns all the source directories (`src_dirs' and
%% `extra_src_dirs').
-spec all_src_dirs(rebar_dict()) -> list(file:filename_all()).

+ 44
- 0
src/rebar_file_utils.erl Целия файл

@ -43,6 +43,9 @@
touch/1,
path_from_ancestor/2,
canonical_path/1,
absolute_path/1,
normalized_path/1,
normalize_relative_path/1,
resolve_link/1,
split_dirname/1,
ensure_dir/1]).
@ -453,6 +456,47 @@ canonical_path([_|Acc], [".."|Rest]) -> canonical_path(Acc, Rest);
canonical_path([], [".."|Rest]) -> canonical_path([], Rest);
canonical_path(Acc, [Component|Rest]) -> canonical_path([Component|Acc], Rest).
%% @doc make a path absolute
-spec absolute_path(file:filename()) -> file:filename().
absolute_path(Path) ->
case filename:pathtype(Path) of
absolute ->
Path;
relative ->
{ok, Dir} = file:get_cwd(),
filename:join([Dir, Path]);
volumerelative ->
Volume = hd(filename:split(Path)),
{ok, Dir} = file:get_cwd(Volume),
filename:join([Dir, Path])
end.
%% @doc normalizing a path removes all of the `..' and the
%% `.' segments it may contain.
-spec normalized_path(file:filename()) -> file:filename().
normalized_path(Path) ->
AbsPath = absolute_path(Path),
Components = filename:split(AbsPath),
normalized_path(Components, []).
%% @private drops path fragments for normalization
-spec normalized_path([string()], [string()]) -> file:filename().
normalized_path([], NormalizedPath) ->
filename:join(lists:reverse(NormalizedPath));
normalized_path([H|T], NormalizedPath) ->
case H of
"." when NormalizedPath == [], T == [] -> normalized_path(T, ["."]);
"." -> normalized_path(T, NormalizedPath);
".." when NormalizedPath == [] -> normalized_path(T, [".."]);
".." when hd(NormalizedPath) =/= ".." -> normalized_path(T, tl(NormalizedPath));
_ -> normalized_path(T, [H|NormalizedPath])
end.
%% @doc normalizes relative paths so that ./a/b/c/ => a/b/c
-spec normalize_relative_path(string()) -> file:filename().
normalize_relative_path(Path) ->
normalized_path(filename:split(Path), []).
%% @doc returns canonical target of path if path is a link, otherwise returns path
-spec resolve_link(string()) -> string().
resolve_link(Path) ->

+ 35
- 0
test/rebar_file_utils_SUITE.erl Целия файл

@ -15,6 +15,8 @@
reset_dir/1,
path_from_ancestor/1,
canonical_path/1,
absolute_path/1,
normalized_path/1,
resolve_link/1,
split_dirname/1,
mv_warning_is_ignored/1,
@ -36,6 +38,8 @@ all() ->
{group, mv},
path_from_ancestor,
canonical_path,
absolute_path,
normalized_path,
resolve_link,
split_dirname,
mv_warning_is_ignored].
@ -144,6 +148,37 @@ canonical_path(_Config) ->
?assertEqual(filename:nativename(Root ++ "foo/bar"),
rebar_file_utils:canonical_path("/foo/./bar")).
absolute_path(_Config) ->
%% We find the root so that the name works both on unix-likes and
%% with Windows.
Root = case os:type() of
{win32, _} -> filename:nativename(filename:absname("/")); % C:\, with proper drive
_ -> "/"
end,
?assertEqual(filename:absname(Root), rebar_file_utils:absolute_path("/")),
?assertEqual(Root ++ "foo", rebar_file_utils:absolute_path("/foo")),
% casoscon ../ o ./
{ok, Cwd} = file:get_cwd(),
?assertEqual(Cwd, rebar_file_utils:absolute_path("./")),
?assertEqual(Cwd ++ "/foo", rebar_file_utils:absolute_path("./foo")),
?assertEqual(Cwd ++ "/foo/bar", rebar_file_utils:absolute_path("foo/bar")).
normalized_path(_Config) ->
%% We find the root so that the name works both on unix-likes and
%% with Windows.
Root = case os:type() of
{win32, _} -> filename:nativename(filename:absname("/")); % C:\, with proper drive
_ -> "/"
end,
?assertEqual(filename:nativename(Root), rebar_file_utils:normalized_path("/")),
?assertEqual("../..", rebar_file_utils:normalized_path("/../../..")),
?assertEqual(Root ++ "foo", rebar_file_utils:normalized_path("/foo/bar/..")),
?assertEqual(Root ++ "foo", rebar_file_utils:normalized_path("/foo/../foo")),
?assertEqual(Root ++ "foo", rebar_file_utils:normalized_path("/foo/.")),
?assertEqual(Root ++ "foo", rebar_file_utils:normalized_path("/foo/./.")),
?assertEqual(filename:nativename(Root ++ "foo/bar"),
rebar_file_utils:normalized_path("/foo/./bar")).
resolve_link(_Config) ->
TmpDir = rebar_file_utils:system_tmpdir(
["rebar_file_utils_SUITE", "resolve_link"]),

Зареждане…
Отказ
Запис