This repository was archived by the owner on Jan 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdexd-socket.rb
More file actions
executable file
·250 lines (206 loc) · 6.1 KB
/
Copy pathdexd-socket.rb
File metadata and controls
executable file
·250 lines (206 loc) · 6.1 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env ruby
# encoding: utf-8
=begin
6 6 ____ __ _ ___ __ __ ____,
||_ || | // ' ||_/ ||_ '
|| \\__/ \\__/ || \\ ||__,
_ _ _ ____, ____ ____ ____ ___ __ __ 9 9
\ \\ / ||__' ||__) ||__) || // ' ||_/
\/\/ ||__, ||__) || \, _||_ \\__/ || \\, o
=end
require "socket"
require "optparse"
require "json"
require "yaml"
require "shellwords"
DAEMON_NICENAME = "Dex"
DAEMON_VERSION = "2.0.0"
PORT = 2345
# Splat ensures that the args are joinable
def poots(*p); STDERR.puts [*p].join("\n"); end
def __poots(*p); STDERR.puts [*p].join("\n").split("\n").map{|e| " #{e}" }.join("\n"); end
# Pretty colours
class String
def console_red; colorise(self, "\e[31m"); end
def console_green; colorise(self, "\e[32m"); end
def console_grey; colorise(self, "\e[30m"); end
def console_bold; colorise(self, "\e[1m"); end
def console_underline; colorise(self, "\e[4m"); end
def colorise(text, color_code) "#{color_code}#{text}\e[0m" end
def markitdown
# TODO: Use lookahead/lookbehind magic to match pairs
self.gsub! %r{\*\*(\w|[^\s][^*]*?[^\s])\*\*}x, "<strong>\\1</strong>"
self.gsub! %r{ \*(\w|[^\s][^*]*?[^\s])\* }x, "<em>\\1</em>"
self.gsub! %r{ `(\w|[^\s][^`]*?[^\s])` }x, "<code>\\1</code>"
self
end
end
# Show a nice message on exit
%w(INT TERM).each{|s| trap(s){poots "\ntake care out there \u{1f44b}"; abort}}
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename __FILE__} [options]"
# TODO: A more sensible default?
options[:src] = nil
opts.on("-s", "--src [PATH]", "Dexfile source path") {|p| options[:src] = p}
options[:dest] = nil
opts.on("-d", "--dest [PATH]", "Compiled dexfile destination path") {|p| options[:src] = p}
options[:verbose] = false
opts.on("-v", "--verbose", "Run verbosely") {|v| options[:verbose] = v}
end.parse!
abort "Dexfile source path is required. See '#{File.basename __FILE__} --help' for more" unless options[:src]
# abort "Dexfile destination path is required. See '#{File.basename __FILE__} --help' for more" unless options[:dest]
# Config method
def getLatestConfig(srcDir)
filesByModule = { global: [] }
modulesByHost = { global: {}, utilities: {} }
metadataByModule = {}
contentsByFile = {}
Dir.chdir File.realpath File.expand_path(srcDir)
Dir.glob("*/{*,*/*}.{css,sass,scss,js,coffee}").reject do |f|
!File.file?(f)
end.each do |modPath|
mod, slash, filename = modPath.rpartition("/")
basename, dot, ext = filename.rpartition(".")
parts = modPath.split("/")
filesByModule[mod] ||= { css: [], js: [] }
# Exclude host-wide files (like "global/setup.js")
if parts.size > 2
modulesByHost[parts[0]] ||= []
modulesByHost[parts[0]] |= [mod]
metadataByModule[mod] = {
"Author" => nil,
"Title" => "#{parts[1]}",
"Category" => "#{parts[0] }",
"Description" => nil,
"URL" => nil
}
info_yaml = File.join(mod, "info.yaml")
if File.exists? info_yaml
__poots "Loading '#{info_yaml}'"
(YAML::load_file(info_yaml) || {}).each do |k, v|
case k
when "Title", "Category" then next
when String
metadataByModule[mod][k] = v.markitdown
else
metadataByModule[mod][k] = v
end
end
end
end
contents = "/* Error: #{modPath} */"
action = "Compiled"
coffee = `which coffee`.strip
sass = `which sass`.strip
case ext
when "coffee"
unless coffee === ""
contents = `cat #{Shellwords.escape modPath} | #{coffee} -c -s`.strip
else
contents = "/* coffeescript is not installed */"
end
when "scss", "sass"
unless sass === ""
contents = `#{sass} #{Shellwords.escape modPath}`.strip
else
contents = "/* sass gem is not installed */"
end
else
contents = IO.read(modPath)
action = "Copied"
end
poots [
"#{Time.now.strftime "%H:%M:%S"}".console_bold,
action,
modPath
].join(" ")
case ext
when "js", "coffee"
filesByModule[mod][:js].push modPath
contents = [
"console.groupCollapsed(\"#{modPath}\");",
contents,
"console.groupEnd();"
].join("\n\n")
when "css", "sass", "scss"
filesByModule[mod][:css].push modPath
contents = [
"/* @start #{modPath} */",
contents,
"/* @end #{modPath} */"
].join("\n\n")
end
contentsByFile[modPath] = contents
end
{
:metadata => metadataByModule,
:modulesByHost => modulesByHost,
:filesByModule => filesByModule,
:contentsByFile => contentsByFile
}
end
server = TCPServer.new("localhost", PORT)
poots "#{DAEMON_NICENAME} #{DAEMON_VERSION}".console_green
poots "Serving from '#{options[:src].to_s}' on port #{PORT}"
poots "==========".console_grey
loop do
client = server.accept
request_string = client.gets.to_s.strip
# TODO: why the empty requests?
if request_string === ""
poots [
"#{Time.now.strftime "%H:%M:%S"}".console_bold,
"[Blank request]".console_red
].join(" ")
poots "==========".console_grey
client.close
next
end
# TODO: Parse this more intelligently (?)
method, path, protocol = request_string.split(" ", 3)
# Print out a nice message
poots [
"#{Time.now.strftime "%H:%M:%S"}".console_bold,
"#{method} #{path}".console_green
].join(" ")
# Default returned stuff
body = "<div style='font-size: 32px'>\u{1f60e}\u{1f44d}</div>"
status = 200
filetype = "html"
case path
when /^\/getdata[\/]?$/
filetype = "json"
body = getLatestConfig options[:src]
else
body = "Doesn’t look like “#{path}” even exists."
status = 404
end
statusString = case status
when 200 then "200 OK"
when 404 then "404 Page Not Found"
when 500 then "500 Internal Server Error"
else "501 Not Implemented"
end
contentType = case filetype
when "css" then "text/css; charset=utf-8"
when "json", "js" then "application/javascript; charset=utf-8"
else "text/plain; charset=utf-8"
end
body = case filetype
when "json" then body.to_json
else "#{body}"
end
client.print [
"HTTP/1.1 #{statusString}",
"Content-Type: #{contentType}",
"Content-Length: #{body.bytesize}",
"#{DAEMON_NICENAME}-Version: #{DAEMON_VERSION}",
"Connection: close",
"",
body,
""
].join("\r\n")
poots "==========".console_grey
client.close
end