Mercurial Hosting > luan
annotate src/luan/modules/Boot.luan @ 1321:307e76ccd0d6
generalize separate logging
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 05 Feb 2019 22:36:55 -0700 |
parents | ca742d51b31f |
children | f41919741100 |
rev | line source |
---|---|
1280 | 1 java() |
2 local System = require "java:java.lang.System" | |
3 local URL = require "java:java.net.URL" | |
4 local BasicLuan = require "java:luan.modules.BasicLuan" | |
5 local new_error = BasicLuan.new_error | |
6 local ipairs = BasicLuan.ipairs | |
1283
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
7 local set_metatable = BasicLuan.set_metatable |
1280 | 8 local StringLuan = require "java:luan.modules.StringLuan" |
9 local match = StringLuan.match -- String.match | |
10 local IoLuan = require "java:luan.modules.IoLuan" | |
11 local LuanUrl = require "java:luan.modules.url.LuanUrl" | |
1321
307e76ccd0d6
generalize separate logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1311
diff
changeset
|
12 IoLuan.unrestricted() -- not right |
1280 | 13 |
14 | |
15 local Boot = {} | |
16 | |
17 | |
18 local function error(message) | |
19 new_error(message).throw() | |
20 end | |
21 Boot.error = error | |
22 | |
23 | |
24 local function new_LuanIn(io) | |
25 local this = {} | |
26 this.java = io | |
27 this.to_string = io.to_string | |
28 this.to_uri_string = io.to_uri_string | |
29 this.read_text = io.read_text | |
30 this.read_binary = io.read_binary | |
31 this.read_lines = io.read_lines | |
32 this.read_blocks = io.read_blocks | |
33 this.exists = io.exists | |
34 this.checksum = io.checksum | |
35 this.charset = io.charset | |
36 this.set_charset = io.set_charset | |
37 return this | |
38 end | |
39 Boot.new_LuanIn = new_LuanIn | |
40 | |
41 local function new_writer(writer) | |
42 local this = {} | |
43 this.java = writer | |
44 this.write = writer.write | |
45 this.close = writer.close | |
46 return this | |
47 end | |
48 | |
49 function Boot.text_writer(out) | |
50 return new_writer( IoLuan.luanWriter(out) ) | |
51 end | |
52 | |
53 Boot.binary_writer = new_writer | |
54 | |
55 local function new_LuanIO(io) | |
56 local this = new_LuanIn(io) | |
57 this.write = io.write | |
58 this.write_text = io.write_text | |
59 | |
60 function this.text_writer() | |
61 return new_writer( io.text_writer() ) | |
62 end | |
63 | |
64 function this.binary_writer() | |
65 return new_writer( io.binary_writer() ) | |
66 end | |
67 | |
68 return this | |
69 end | |
70 | |
71 local schemes = {} | |
72 | |
73 function schemes.null(path) | |
74 return new_LuanIO( IoLuan.nullIO ) | |
75 end | |
76 | |
77 function schemes.string(path) | |
78 return new_LuanIO( IoLuan.LuanString.new(path) ) | |
79 end | |
80 | |
81 function schemes.classpath(path) | |
82 local cp = IoLuan.classpath(path) | |
83 return cp and new_LuanIn(cp) | |
84 end | |
85 | |
86 function schemes.luan(path) | |
87 return schemes.classpath("luan/modules/"..path) | |
88 end | |
89 | |
90 function schemes.stdin(path) | |
91 local Io = require "luan:Io.luan" | |
92 return Io.stdin | |
93 end | |
94 | |
95 local function url(path,options) | |
96 return new_LuanIn( LuanUrl.new(URL.new(path),options) ) | |
97 end | |
98 | |
99 function schemes.http(path,options) | |
100 return url( "http:"..path, options ) | |
101 end | |
102 | |
103 function schemes.https(path,options) | |
104 return url( "https:"..path, options ) | |
105 end | |
106 | |
107 local function new_BaseOs(io) | |
108 local this = new_LuanIO(io) | |
109 this.wait_for = io.wait_for | |
110 return this | |
111 end | |
112 | |
113 function schemes.os(path,options) | |
114 return new_BaseOs( IoLuan.LuanOs.new(path,options) ) | |
115 end | |
116 | |
117 function schemes.bash(path,options) | |
118 return new_BaseOs( IoLuan.LuanBash.new(path,options) ) | |
119 end | |
120 | |
121 local function new_LuanFile(io) | |
122 local this = new_LuanIO(io) | |
123 this.name = io.file.getName | |
124 this.is_directory = io.file.isDirectory | |
125 this.is_file = io.file.isFile | |
126 this.delete = io.delete | |
127 this.delete_on_exit = io.file.deleteOnExit | |
128 this.mkdir = io.mkdir | |
129 this.last_modified = io.file.lastModified | |
130 this.set_last_modified = io.set_last_modified | |
131 this.length = io.file.length | |
132 this.rename_to = io.rename_to | |
1311
ca742d51b31f
don't delete into symbolic links
Franklin Schmidt <fschmidt@gmail.com>
parents:
1286
diff
changeset
|
133 this.is_symbolic_link = io.is_symbolic_link |
1280 | 134 |
135 function this.child(name) | |
136 return new_LuanFile( io.child(name) ) | |
137 end | |
138 | |
139 function this.children() | |
140 local raw = io.children() | |
141 if raw == nil then | |
142 return nil | |
143 end | |
144 local rtn = {} | |
145 for _, child in ipairs(raw) do | |
146 rtn[#rtn+1] = new_LuanFile(child) | |
147 end | |
148 return rtn | |
149 end | |
150 | |
151 function this.parent() | |
152 return new_LuanFile( io.parent() ) | |
153 end | |
154 | |
155 function this.canonical() | |
156 return new_LuanFile( io.canonical() ) | |
157 end | |
158 | |
159 function this.create_temp_file(prefix,suffix) | |
160 return new_LuanFile( io.create_temp_file(prefix,suffix) ) | |
161 end | |
162 | |
163 return this | |
164 end | |
165 | |
166 function schemes.file(path) | |
167 return new_LuanFile( IoLuan.LuanFile.new(path) ) | |
168 end | |
169 | |
170 Boot.schemes = schemes | |
171 | |
172 | |
173 local function uri(name,options) | |
1286 | 174 local scheme, location = match( name, "(?s)^([^:]+):(.*)$" ) |
1280 | 175 scheme or error( "invalid Io.uri name '"..name.."', missing scheme" ) |
1286 | 176 local opener = schemes[scheme] or error( "invalid scheme '"..scheme.."' in '"..name.."'" ) |
1280 | 177 return opener(location,options) |
178 end | |
179 Boot.uri = uri | |
180 | |
181 | |
182 function Boot.read(uri_str) -- for PackageLuan.java | |
183 local u = uri(uri_str) | |
184 if u==nil or not u.exists() then | |
185 return nil | |
186 end | |
187 return u.read_text() | |
188 end | |
189 | |
190 | |
1283
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
191 local error_mt = {} |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
192 |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
193 function error_mt.__to_string(t) |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
194 return t.java.getLuanStackTraceString() |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
195 end |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
196 |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
197 function Boot.new_error_table(ex) -- for LuanException.java |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
198 local this = {} |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
199 set_metatable(this,error_mt) |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
200 this.java = ex |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
201 this.get_message = ex.getMessage |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
202 this.throw = ex.throwThis |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
203 this.get_stack_trace_string = ex.getLuanStackTraceString |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
204 this.get_java_stack_trace_string = ex.getLuanStackTraceString |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
205 return this |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
206 end |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
207 |
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1280
diff
changeset
|
208 |
1280 | 209 return Boot |