35
36:- module(prolog_version,
37 [ check_prolog_version/1, 38 register_git_module/2, 39 git_module_property/2, 40 git_update_versions/1 41 ]). 42:- use_module(library(process)). 43:- use_module(library(option)). 44:- use_module(library(readutil)). 45:- use_module(library(git)). 46
47
56
57:- multifile
58 git_module_hook/3. 59
64
65check_prolog_version(Required) :-
66 prolog_version_ok(Required),
67 !.
68check_prolog_version(Required) :-
69 print_message(error,
70 required_prolog_version(Required)),
71 format(user_error, '~nPress any key to exit> ', []),
72 get_single_char(_), nl(user_error),
73 halt(1).
74
75prolog_version_ok(or(V1, V2)) :-
76 !,
77 ( prolog_version_ok(V1)
78 -> true
79 ; prolog_version_ok(V2)
80 ).
81prolog_version_ok(Required) :-
82 current_prolog_flag(version, MyVersion),
83 MyVersion >= Required.
84
85:- multifile
86 prolog:message/3. 87
88prolog:message(required_prolog_version(Required)) -->
89 { current_prolog_flag(version, MyVersion),
90 user_version(MyVersion, MyV),
91 user_version(Required, Req)
92 },
93 [ 'This program requires SWI-Prolog ~w'-[Req], nl,
94 'while you are running version ~w.'-[MyV], nl,
95 'Please visit http://www.swi-prolog.org and', nl,
96 'upgrade your version of SWI-Prolog.'
97 ].
98prolog:message(git(no_version)) -->
99 [ 'Sorry, cannot retrieve version stamp from GIT.' ].
100prolog:message(git(update_versions)) -->
101 [ 'Updating GIT version stamps in the background.' ].
102
103
104user_version(or(V1,V2), Version) :-
105 !,
106 user_version(V1, A1),
107 user_version(V2, A2),
108 format(atom(Version), '~w or ~w', [A1, A2]).
109user_version(N, Version) :-
110 Major is N // 10000,
111 Minor is (N // 100) mod 100,
112 Patch is N mod 100,
113 atomic_list_concat([Major, Minor, Patch], '.', Version).
114
115
116 119
120:- dynamic
121 git_module/3, 122 git_module_version/2. 123
138
139register_git_module(Name, Options) :-
140 ( prolog_load_context(directory, BaseDir)
141 -> true
142 ; working_directory(BaseDir, BaseDir)
143 ),
144 select_option(directory(Dir), Options, RestOptions, '.'),
145 absolute_file_name(Dir, AbsDir,
146 [ file_type(directory),
147 relative_to(BaseDir),
148 access(read)
149 ]),
150 retractall(git_module(Name, _, _)),
151 assert(git_module(Name, AbsDir, RestOptions)).
152
153git_update_versions(Name) :-
154 catch(forall(current_git_module(Name, _, _),
155 update_version(Name)),
156 _,
157 print_message(warning, git(no_version))).
158
159update_version(Name) :-
160 current_git_module(Name, Dir, Options),
161 ( catch(git_describe(GitVersion, [directory(Dir)|Options]), _, fail)
162 -> true
163 ; GitVersion = unknown
164 ),
165 retractall(git_module_version(Name, _)),
166 assert(git_module_version(Name, GitVersion)).
167
168current_git_module(Name, Dir, Options) :-
169 git_module(Name, Dir, Options).
170current_git_module(Name, Dir, Options) :-
171 git_module_hook(Name, Dir, Options).
172
173
185
186git_module_property(Name, Property) :-
187 ( var(Name)
188 -> current_git_module(Name, _, _),
189 git_module_property(Name, Property)
190 ; compound(Property)
191 -> once(gen_module_property(Name, Property))
192 ; gen_module_property(Name, Property)
193 ).
194
195gen_module_property(Name, version(Version)) :-
196 ( git_module_version(Name, Version0)
197 -> true
198 ; git_update_versions(Name),
199 git_module_version(Name, Version0)
200 ),
201 Version0 \== unknown,
202 Version = Version0.
203gen_module_property(Name, directory(Dir)) :-
204 current_git_module(Name, Dir, _).
205gen_module_property(Name, remote(Alias, Remote)) :-
206 ( ground(Alias)
207 -> true
208 ; Alias = origin
209 ),
210 current_git_module(Name, Dir, _),
211 git_remote_url(Alias, Remote, [directory(Dir)]).
212gen_module_property(Name, Term) :-
213 current_git_module(Name, _, Options),
214 member(Term, Options).
215
216
217
218 221
222bg_git_update_versions :-
223 print_message(informational, git(update_versions)),
224 thread_create(git_update_versions(_), _,
225 [ detached(true)
226 ]).
227
228:- multifile
229 user:message_hook/3. 230
231user:message_hook(make(done(_)), _, _) :-
232 bg_git_update_versions,
233 fail.
234
236:- if(current_predicate(http_unix_daemon:http_daemon/0)). 237:- initialization git_update_versions(_). 238:- else. 239:- initialization bg_git_update_versions. 240:- endif.