-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
ide.pl
159 lines (126 loc) · 4.6 KB
/
ide.pl
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* Part of SWISH
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2015-2018, VU University Amsterdam
CWI, Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
:- module(swish_ide,
[ swish/0,
swish/1 % ?Port
]).
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_path)).
:- use_module(library(www_browser)).
:- if(exists_source(library(uid))).
:- use_module(library(uid)).
:- endif.
/** <module>
Open SWISH as an IDE for developing a local application.
*/
/*******************************
* CONFIG *
*******************************/
:- multifile
swish_config:config/2, % Name, Value
swish_config:source_alias/2, % Alias, Options
swish_config:verify_write_access/3, % Request, File, Options
pengines:authentication_hook/3, % Request, Application, User
pengines:not_sandboxed/2, % User, Application
user:file_search_path/2, % Alias, Path
http:location/3. % Alias, Path, Options
user:file_search_path(project, '.').
swish_config:config(ide, true).
swish_config:config(show_beware, false).
swish_config:config(community_examples, true).
swish_config:source_alias(project, [access(both), search('*.pl')]).
swish_config:source_alias(library, []).
swish_config:verify_write_access(_Request, _File, _Options).
pengines:authentication_hook(_Request, swish, User) :-
current_user(User).
pengines:not_sandboxed(_User, _Application).
:- if(current_predicate(getuid/1)).
current_user(User) :- !,
getuid(UID),
user_info(UID, Info),
user_data(name, Info, User).
:- endif.
current_user(default).
http:location(swish, root(swish), [priority(100)]).
:- create_prolog_flag(swish_ide, true, []).
:- use_module(swish).
%% swish
%
% Start the SWISH server and open the main page in your browser.
swish :-
reuse_port(Port),
swish(Port).
swish(Port) :-
http_server_property(Port, goal(swish_ide:http_dispatch)), !,
save_port(Port),
open_browser(Port).
swish(Host:Port) :-
integer(Port),
http_server_property(Port, goal(swish_ide:http_dispatch)), !,
save_port(Host:Port),
open_browser(Port).
swish(Port) :-
http_server(http_dispatch,
[ port(Port),
workers(16)
]),
save_port(Port),
open_browser(Port).
reuse_port(Port) :-
exists_file('data/settings.pl'),
read_file_to_terms('data/settings.pl', Terms, []),
memberchk(port(Port), Terms),
print_message(informational, swish(reuse_port(Port))),
!.
reuse_port(localhost:_).
save_port(Port) :-
setup_call_cleanup(
open('data/settings.pl', write, Out),
format(Out, '~q.~n', [port(Port)]),
close(Out)).
open_browser(Address) :-
host_port(Address, Host, Port),
http_server_property(Port, scheme(Scheme)),
http_absolute_location(swish(.), Path, []),
format(atom(URL), '~w://~w:~w~w', [Scheme, Host, Port, Path]),
www_open_url(URL).
host_port(Host:Port, Host, Port) :- !.
host_port(Port, localhost, Port).
/*******************************
* LOAD IDE RENDERERS *
*******************************/
:- use_module(swish(lib/render/ide), []).
/*******************************
* MESSAGES *
*******************************/
:- multifile prolog:message//1.
prolog:message(swish(reuse_port(Port))) -->
[ 'Trying to reuse old address ~p'-[Port] ].