comparison src/luan/modules/Boot.luan @ 1280:781ec0a92bb5

add Boot.luan
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 20 Dec 2018 13:38:16 -0700
parents
children 503bde9a7c80
comparison
equal deleted inserted replaced
1279:323743a7f317 1280:781ec0a92bb5
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
7 local StringLuan = require "java:luan.modules.StringLuan"
8 local match = StringLuan.match -- String.match
9 local IoLuan = require "java:luan.modules.IoLuan"
10 local LuanUrl = require "java:luan.modules.url.LuanUrl"
11
12
13 local Boot = {}
14
15
16 local function error(message)
17 new_error(message).throw()
18 end
19 Boot.error = error
20
21
22 local function new_LuanIn(io)
23 local this = {}
24 this.java = io
25 this.to_string = io.to_string
26 this.to_uri_string = io.to_uri_string
27 this.read_text = io.read_text
28 this.read_binary = io.read_binary
29 this.read_lines = io.read_lines
30 this.read_blocks = io.read_blocks
31 this.exists = io.exists
32 this.checksum = io.checksum
33 this.charset = io.charset
34 this.set_charset = io.set_charset
35 return this
36 end
37 Boot.new_LuanIn = new_LuanIn
38
39 local function new_writer(writer)
40 local this = {}
41 this.java = writer
42 this.write = writer.write
43 this.close = writer.close
44 return this
45 end
46
47 function Boot.text_writer(out)
48 return new_writer( IoLuan.luanWriter(out) )
49 end
50
51 Boot.binary_writer = new_writer
52
53 local function new_LuanIO(io)
54 local this = new_LuanIn(io)
55 this.write = io.write
56 this.write_text = io.write_text
57
58 function this.text_writer()
59 return new_writer( io.text_writer() )
60 end
61
62 function this.binary_writer()
63 return new_writer( io.binary_writer() )
64 end
65
66 return this
67 end
68
69 local schemes = {}
70
71 function schemes.null(path)
72 return new_LuanIO( IoLuan.nullIO )
73 end
74
75 function schemes.string(path)
76 return new_LuanIO( IoLuan.LuanString.new(path) )
77 end
78
79 function schemes.classpath(path)
80 local cp = IoLuan.classpath(path)
81 return cp and new_LuanIn(cp)
82 end
83
84 function schemes.luan(path)
85 return schemes.classpath("luan/modules/"..path)
86 end
87
88 function schemes.stdin(path)
89 local Io = require "luan:Io.luan"
90 return Io.stdin
91 end
92
93 local function url(path,options)
94 return new_LuanIn( LuanUrl.new(URL.new(path),options) )
95 end
96
97 function schemes.http(path,options)
98 return url( "http:"..path, options )
99 end
100
101 function schemes.https(path,options)
102 return url( "https:"..path, options )
103 end
104
105 local function new_BaseOs(io)
106 local this = new_LuanIO(io)
107 this.wait_for = io.wait_for
108 return this
109 end
110
111 function schemes.os(path,options)
112 return new_BaseOs( IoLuan.LuanOs.new(path,options) )
113 end
114
115 function schemes.bash(path,options)
116 return new_BaseOs( IoLuan.LuanBash.new(path,options) )
117 end
118
119 local function new_LuanFile(io)
120 local this = new_LuanIO(io)
121 this.name = io.file.getName
122 this.is_directory = io.file.isDirectory
123 this.is_file = io.file.isFile
124 this.delete = io.delete
125 this.delete_on_exit = io.file.deleteOnExit
126 this.mkdir = io.mkdir
127 this.last_modified = io.file.lastModified
128 this.set_last_modified = io.set_last_modified
129 this.length = io.file.length
130 this.rename_to = io.rename_to
131
132 function this.child(name)
133 return new_LuanFile( io.child(name) )
134 end
135
136 function this.children()
137 local raw = io.children()
138 if raw == nil then
139 return nil
140 end
141 local rtn = {}
142 for _, child in ipairs(raw) do
143 rtn[#rtn+1] = new_LuanFile(child)
144 end
145 return rtn
146 end
147
148 function this.parent()
149 return new_LuanFile( io.parent() )
150 end
151
152 function this.canonical()
153 return new_LuanFile( io.canonical() )
154 end
155
156 function this.create_temp_file(prefix,suffix)
157 return new_LuanFile( io.create_temp_file(prefix,suffix) )
158 end
159
160 return this
161 end
162
163 function schemes.file(path)
164 return new_LuanFile( IoLuan.LuanFile.new(path) )
165 end
166
167 Boot.schemes = schemes
168
169
170 local function uri(name,options)
171 local scheme, location = match( name, "^([^:]+):(.*)$" )
172 scheme or error( "invalid Io.uri name '"..name.."', missing scheme" )
173 local opener = schemes[scheme] or error( "invalid scheme '"..scheme.."' in '"+name+"'" )
174 return opener(location,options)
175 end
176 Boot.uri = uri
177
178
179 function Boot.read(uri_str) -- for PackageLuan.java
180 local u = uri(uri_str)
181 if u==nil or not u.exists() then
182 return nil
183 end
184 return u.read_text()
185 end
186
187
188 return Boot