aboutsummaryrefslogtreecommitdiffstats
path: root/system/doc/top/src/erlresolvelinks.erl
blob: a891b67421d9f1b965c41abc03e401a831338d66 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
-module(erlresolvelinks). 

%% ------ VERY IMPORTANT ------
%%
%% Original location for this file: 
%% /clearcase/otp/internal_tools/integration/scripts/resolve_links/
%% When updating this file, copy the source to 
%% /usr/local/otp/patch/share/program/
%% and place .beam files (compiled with correct release) in all 
%% /usr/local/otp/patch/share/program/<release>
%% for releases >= R10B
%%
%% ----------------------------

-export([make/1, do_make/1, do_make/2, do_make/3]).
-include_lib("kernel/include/file.hrl").

-define(JAVASCRIPT_NAME, "erlresolvelinks.js").

make([RootDir]) ->
    do_make(RootDir);
make([RootDir, DestDir]) ->
    do_make(RootDir, DestDir);
make([RootDir, DestDir, Name]) ->
    do_make(RootDir, DestDir, Name).

do_make(RootDir) ->
    DestDir = filename:join(RootDir, "doc"),
    do_make(RootDir, DestDir).

do_make(RootDir, DestDir) ->
    do_make(RootDir, DestDir, ?JAVASCRIPT_NAME).

do_make(RootDir, DestDir, Name) ->
    %% doc/Dir
    %% erts-Vsn
    %% lib/App-Vsn
    DocDirs0 = get_dirs(filename:join([RootDir, "doc"])),
    DocDirs = lists:map(fun(Dir) -> 
				D = filename:join(["doc", Dir]),
				{D, D} end, DocDirs0),

    ErtsDirs = latest_app_dirs(RootDir, ""), 
    AppDirs = latest_app_dirs(RootDir, "lib"),
    
    AllAppDirs = 
	lists:map(
	  fun({App, AppVsn}) -> {App, filename:join([AppVsn, "doc", "html"])}
	  end, ErtsDirs ++ AppDirs),

    AllDirs = DocDirs ++ AllAppDirs,
    {ok, Fd} = file:open(filename:join([DestDir, Name]), [write]),
    UTC = calendar:universal_time(),
    io:fwrite(Fd, "/* Generated by ~s at ~w UTC */\n", 
	      [atom_to_list(?MODULE), UTC]),
    io:fwrite(Fd, "function erlhref(ups, app, rest) {\n", []),
    io:fwrite(Fd, "    switch(app) {\n", []),
    lists:foreach(
      fun({Tag, Dir}) ->
	      io:fwrite(Fd, "    case ~p:\n", [Tag]),
	      io:fwrite(Fd, "        location.href=ups + \"~s/\" + rest;\n",
			[Dir]),
	      io:fwrite(Fd, "        break;\n",	[])
      end, AllDirs),
    io:fwrite(Fd, "    default:\n", []),
    io:fwrite(Fd, "        location.href=ups + \"Unresolved\";\n", []),
    io:fwrite(Fd, "    }\n", []),
    io:fwrite(Fd, "}\n", []),
    file:close(Fd),
    ok.
    
get_dirs(Dir) ->
    {ok, Files} = file:list_dir(Dir),
    AFiles = 
	lists:map(fun(File) -> {File, filename:join([Dir, File])} end, Files),
    lists:zf(fun is_dir/1, AFiles).

is_dir({File, AFile}) ->
    {ok, FileInfo} = file:read_file_info(AFile),
    case FileInfo#file_info.type of
	directory ->
	    {true, File};
	_  ->
	    false
    end.

latest_app_dirs(RootDir, Dir) ->
    ADir = filename:join(RootDir, Dir),
    RDirs0 = get_dirs(ADir),
    RDirs1 = lists:filter(fun is_app_dir/1, RDirs0),
    %% Build a list of {{App, VsnNumList}, AppVsn}
    SDirs0 = 
	lists:map(fun(AppVsn) ->
			  [App, VsnStr] = string:tokens(AppVsn, "-"),
			  VsnNumList = vsnstr_to_numlist(VsnStr),
			  {{App, VsnNumList}, AppVsn} end,
		  RDirs1),
    SDirs1 = lists:keysort(1, SDirs0),
    App2Dirs = lists:foldr(fun({{App, _VsnNumList}, AppVsn}, Acc) ->
				   case lists:keymember(App, 1, Acc) of
				       true ->
					   Acc;
				       false ->
					   [{App, AppVsn}| Acc]
				   end
			   end, [], SDirs1),
    lists:map(fun({App, AppVsn}) -> {App, filename:join([Dir, AppVsn])} end,
	      App2Dirs).

is_app_dir(Dir) ->
    case string:tokens(Dir, "-") of
	[_Name, Rest] ->
	    is_vsnstr(Rest);
	_  ->
	    false
    end.

is_vsnstr(Str) ->	
    case string:tokens(Str, ".") of
	[_] ->
	    false;
	Toks  ->
	    lists:all(fun is_numstr/1, Toks)
    end.

is_numstr(Cs) ->
    lists:all(fun(C) when $0 =< C, C =< $9 -> 
		      true;
		 (_) ->
		      false
	      end, Cs).

%% We know:

vsnstr_to_numlist(VsnStr) ->	    
    lists:map(fun(NumStr) -> list_to_integer(NumStr) end,
	      string:tokens(VsnStr, ".")).