Advertisement
awsumben13

Annex Installer

Oct 2nd, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 23.12 KB | None | 0 0
  1. local files = {
  2.   [ "Annex/build" ] = "\
  3. local args = { ... }\
  4. \
  5. local path = _G._ANNEX_PATH or shell.getRunningProgram():gsub( \"/build\", \"\" )\
  6. local env = setmetatable( { _ANNEX_PATH = path }, { __index = _ENV } )\
  7. local preprocessor\
  8. \
  9. local help = [[HELP TEXT]]\
  10. \
  11. local paths = {}\
  12. local main = \"main\"\
  13. local penv = {}\
  14. local outputs = {}\
  15. \
  16. local mode = \"path\"\
  17. \
  18. local h = fs.open( path .. \"/preprocessor.lua\", \"r\" )\
  19. if h then\
  20. \9local content = h.readAll()\
  21. \9h.close()\
  22. \
  23. \9local f, err = load( content, \"preprocessor\", nil, env )\
  24. \9if f then\
  25. \9\9preprocessor = f()\
  26. \9else\
  27. \9\9error( err, 0 )\
  28. \9end\
  29. else\
  30. \9return error( \"Cannot find file 'preprocessor'\", 0 )\
  31. end\
  32. \
  33. env.preprocessor = preprocessor\
  34. \
  35. local prev\
  36. for i = 1, #args do\
  37. \9prev = mode ~= \"main\" and mode or prev\
  38. \9if args[i] == \"help\" or args[i] == \"-h\" or args[i] == \"-help\" then\
  39. \9\9print( help )\
  40. \9elseif args[i] == \"-m\" then\
  41. \9\9mode = \"main\"\
  42. \9elseif args[i] == \"-d\" then\
  43. \9\9mode = \"path\"\
  44. \9elseif args[i] == \"-o\" then\
  45. \9\9mode = \"output\"\
  46. \9elseif args[i] == \"-f\" then\
  47. \9\9mode = \"flag\"\
  48. \9elseif mode == \"main\" then\
  49. \9\9main = args[i]:gsub( \"%.\", \"/\" )\
  50. \9\9mode = prev\
  51. \9elseif mode == \"path\" then\
  52. \9\9paths[#paths + 1] = args[i]:gsub( \"%.\", \"/\" )\
  53. \9elseif mode == \"output\" then\
  54. \9\9outputs[#outputs + 1] = args[i]\
  55. \9elseif mode == \"flag\" then\
  56. \9\9penv[args[i]] = true\
  57. \9end\
  58. end\
  59. \
  60. outputs[1] = outputs[1] or \"?/output\"\
  61. \
  62. if #paths == 0 then\
  63. \9return error( \"Expected one or more build paths\", 0 )\
  64. end\
  65. \
  66. local p = preprocessor()\
  67. p.include_paths = paths\
  68. p.env = penv\
  69. p.active_include = main .. \".lua\"\
  70. \
  71. paths[#paths + 1] = \"\"\
  72. paths[#paths + 1] = \"Annex/lib\"\
  73. \
  74. for i = 1, #paths do\
  75. \9local h = fs.open( paths[i] .. \"/\" .. main .. \".lua\", \"r\" )\
  76. \9if h then\
  77. \9\9local content = h.readAll()\
  78. \9\9h.close()\
  79. \
  80. \9\9p:push( content )\
  81. \
  82. \9\9local r = p:build()\
  83. \
  84. \9\9for i = 1, #outputs do\
  85. \9\9\9local file = outputs[i]:gsub( \"%?\", paths[1] ):gsub( \"%.\", \"/\" ) .. \".lua\"\
  86. \9\9\9local h = fs.open( file, \"w\" )\
  87. \9\9\9if h then\
  88. \9\9\9\9h.write( r )\
  89. \9\9\9\9h.close()\
  90. \9\9\9else\
  91. \9\9\9\9return error( \"Failed to write to output file '\" .. outputs[i] .. \"'\", 0 )\
  92. \9\9\9end\
  93. \9\9end\
  94. \
  95. \9\9return\
  96. \9end\
  97. end\
  98. \
  99. return error( \"Failed to find main file (\" .. main .. \".lua) in paths given\", 0 )",
  100.   [ "Annex/module/conditional.lua" ] = "\
  101. local open = { [\"if\"] = true, [\"ifn\"] = true, [\"ifdef\"] = true, [\"ifndef\"] = true }\
  102. local stop = { [\"elif\"] = true, [\"elifn\"] = true, [\"elifdef\"] = true, [\"elifndef\"] = true, [\"else\"] = true, [\"endif\"] = true }\
  103. \
  104. local function checker( self, mode, data )\
  105. \9if mode == 0 then\
  106. \9\9return self.env[data]\
  107. \9elseif mode == 1 then\
  108. \9\9return not self.env[data]\
  109. \9elseif mode == 2 then\
  110. \9\9return self.env[data] ~= nil\
  111. \9elseif mode == 3 then\
  112. \9\9return self.env[data] == nil\
  113. \9end\
  114. end\
  115. \
  116. local function getnext( self )\
  117. \9local l, d, i = 0, self:fetch()\
  118. \9while d do\
  119. \9\9self:write \"\"\
  120. \9\9if open[i] then\
  121. \9\9\9l = l + 1\
  122. \9\9elseif stop[i] and l == 0 then\
  123. \9\9\9return i, d\
  124. \9\9elseif i == \"endif\" then\
  125. \9\9\9l = l - 1\
  126. \9\9end\
  127. \9\9d, i = self:fetch()\
  128. \9end\
  129. end\
  130. \
  131. local function skip_to_next( self )\
  132. \9local i, d = getnext( self )\
  133. \9return i == \"elif\" and 0 or i == \"elifn\" and 1 or i == \"elifdef\" and 2 or i == \"elifndef\" and 3 or i == \"else\" and 4 or i == \"endif\" and 5, d\
  134. end\
  135. \
  136. local function skip_to_end( self )\
  137. \9local i = getnext( self )\
  138. \9while i do\
  139. \9\9if i == \"endif\" then return end\
  140. \9\9i = getnext( self )\
  141. \9end\
  142. \9return error( \"Expected '@endif'\", 0 )\
  143. end\
  144. \
  145. local function execute( self )\
  146. \9local d, i = self:fetch()\
  147. \9while d do\
  148. \9\9if stop[i] then\
  149. \9\9\9self:write \"\"\
  150. \9\9\9return i ~= \"endif\" and skip_to_end( self )\
  151. \9\9elseif i then\
  152. \9\9\9self:execute( i, d )\
  153. \9\9else\
  154. \9\9\9self:write( d )\
  155. \9\9end\
  156. \9\9d, i = self:fetch()\
  157. \9end\
  158. \9return error( \"Expected '@endif'\", 0 )\
  159. end\
  160. \
  161. local function block( self, mode, data )\
  162. \9self:write \"\"\
  163. \9if checker( self, mode, data ) then -- successful check\
  164. \9\9return execute( self ) -- execute and return\
  165. \9else\
  166. \9\9while true do\
  167. \9\9\9local mode, data = skip_to_next( self ) -- gets next valid instruction\
  168. \9\9\9if mode == 4 then -- else statement\
  169. \9\9\9\9return execute( self ) -- execute and return\
  170. \9\9\9elseif mode == 5 then -- got to an end with no successful branch\
  171. \9\9\9\9return\
  172. \9\9\9elseif not mode then -- no valid instruction\
  173. \9\9\9\9return error( \"Expected '@endif'\", 0 ) -- therefore missing an endif\
  174. \9\9\9else -- got an instruction, check\
  175. \9\9\9\9if checker( self, mode, data ) then -- successful check\
  176. \9\9\9\9\9return execute( self ) -- execute and return\
  177. \9\9\9\9end\
  178. \9\9\9end\
  179. \9\9end\
  180. \9end\
  181. end\
  182. \
  183. local module = {}\
  184. \
  185. module[\"if\"] = function( self, data )\
  186. \9return block( self, 0, data )\
  187. end\
  188. \
  189. function module:ifn( data )\
  190. \9return block( self, 1, data )\
  191. end\
  192. \
  193. function module:ifdef( data )\
  194. \9return block( self, 2, data )\
  195. end\
  196. \
  197. function module:ifndef( data )\
  198. \9return block( self, 3, data )\
  199. end\
  200. \
  201. function module:elif( data )\
  202. \9return error( \"Unexpected '@elif' with no initial '@if'\", 0 )\
  203. end\
  204. \
  205. function module:elifdef( data )\
  206. \9return error( \"Unexpected '@elifn' with no initial '@if'\", 0 )\
  207. end\
  208. \
  209. function module:elifn( data )\
  210. \9return error( \"Unexpected '@elifdef' with no initial '@if'\", 0 )\
  211. end\
  212. \
  213. function module:elifndef( data )\
  214. \9return error( \"Unexpected '@elifndef' with no initial '@if'\", 0 )\
  215. end\
  216. \
  217. module[\"else\"] = function( self, data )\
  218. \9return error( \"Unexpected '@else' with no initial '@if'\", 0 )\
  219. end\
  220. \
  221. function module:endif( data )\
  222. \9return error( \"Unexpected '@endif' with no initial '@if'\", 0 )\
  223. end\
  224. \
  225. return module",
  226.   [ "Annex/module/include.lua" ] = "\
  227. local state = ...\
  228. \
  229. local module = {}\
  230. \
  231. local header = \"local __f,__err=load(\"\
  232. \
  233. state.included = {}\
  234. \
  235. local function isFile( file )\
  236. \9return fs.exists( file ) and not fs.isDir( file )\
  237. end\
  238. \
  239. local function includefile( self, file )\
  240. \9if not self.included[file] then\
  241. \9\9local h = fs.open( file, \"r\" )\
  242. \9\9local content = h.readAll()\
  243. \9\9h.close()\
  244. \
  245. \9\9self:push( content )\
  246. \9\9self.active_include = file\
  247. \9\9self:write( self:build():gsub( \"%s+$\", \"\" ) )\
  248. \9end\
  249. end\
  250. \
  251. local function requirefile( self, file, name, lib )\
  252. \9if not self.included[file] then\
  253. \9\9local h = fs.open( file, \"r\" )\
  254. \9\9local content = h.readAll()\
  255. \9\9h.close()\
  256. \
  257. \9\9self:push( content )\
  258. \9\9self.active_include = file\
  259. \9\9local str = header .. (\"%q\"):format( self:build():gsub( \"%s+$\", \"\" ) ) .. \",\" .. (\"%q\"):format( name ) .. \",nil,_ENV)if not __f then error(__err,0)end\"\
  260. \9\9self:write( str .. ( lib and \" local \" .. name .. \"=__f()\" or \" __f()\" ) )\
  261. \9end\
  262. end\
  263. \
  264. function module:include( data )\
  265. \9local file = data:gsub( \"%.\", \"/\" )\
  266. \9for i = 1, #self.include_paths do\
  267. \9\9if isFile( self.include_paths[i] .. \"/\" .. file .. \".lua\" ) then\
  268. \9\9\9return includefile( self, self.include_paths[i] .. \"/\" .. file .. \".lua\" )\
  269. \9\9elseif isFile( self.include_paths[i] .. \"/\" .. file .. \"/\" .. fs.getName( file ) .. \".lua\" ) then\
  270. \9\9\9return includefile( self, self.include_paths[i] .. \"/\" .. file .. \"/\" .. fs.getName( file ) .. \".lua\" )\
  271. \9\9elseif isFile( self.include_paths[i] .. \"/\" .. data ) then\
  272. \9\9\9return includefile( self, self.include_paths[i] .. \"/\" .. data )\
  273. \9\9end\
  274. \9end\
  275. \9return error( \"Cannot find file '\" .. data .. \"'\", 0 )\
  276. end\
  277. \
  278. function module:require( data )\
  279. \9local data, lib = data\
  280. \9if data:find \"^.-%sas%s[%w_]+$\" then\
  281. \9\9data, lib = data:match \"^(.-)%sas%s([%w_]+)$\"\
  282. \9end\
  283. \9local file = data:gsub( \"%.\", \"/\" )\
  284. \9for i = 1, #self.include_paths do\
  285. \9\9if isFile( self.include_paths[i] .. \"/\" .. file .. \".lua\" ) then\
  286. \9\9\9return requirefile( self, self.include_paths[i] .. \"/\" .. file .. \".lua\", lib or data, lib ~= nil )\
  287. \9\9elseif isFile( self.include_paths[i] .. \"/\" .. file .. \"/\" .. fs.getName( file ) .. \".lua\" ) then\
  288. \9\9\9return requirefile( self, self.include_paths[i] .. \"/\" .. file .. \"/\" .. fs.getName( file ) .. \".lua\", lib or data, lib ~= nil )\
  289. \9\9elseif isFile( self.include_paths[i] .. \"/\" .. data ) then\
  290. \9\9\9return requirefile( self, self.include_paths[i] .. \"/\" .. data, lib or data, lib ~= nil )\
  291. \9\9end\
  292. \9end\
  293. \9return error( \"Cannot find file '\" .. data .. \"'\", 0 )\
  294. end\
  295. \
  296. function module:once()\
  297. \9self:write \"\"\
  298. \9self.included[self.active_include] = true\
  299. end\
  300. \
  301. return module",
  302.   [ "Annex/lib/colour.lua" ] = "\
  303. -- @print Including colour\
  304. \
  305. -- @define TRANSPARENT 0\
  306. -- @define WHITE 1\
  307. -- @define ORANGE 2\
  308. -- @define MAGENTA 4\
  309. -- @define LIGHTBLUE 8\
  310. -- @define YELLOW 16\
  311. -- @define LIME 32\
  312. -- @define PINK 64\
  313. -- @define GREY 128\
  314. -- @define LIGHTGREY 256\
  315. -- @define CYAN 512\
  316. -- @define PURPLE 1024\
  317. -- @define BLUE 2048\
  318. -- @define BROWN 4096\
  319. -- @define GREEN 8192\
  320. -- @define RED 16384\
  321. -- @define BLACK 32768\
  322. \
  323. colour = {\
  324. \9transparent = TRANSPARENT;\
  325. \9white = WHITE;\
  326. \9orange = ORANGE;\
  327. \9magenta = MAGENTA;\
  328. \9lightBlue = LIGHTBLUE;\
  329. \9yellow = YELLOW;\
  330. \9lime = LIME;\
  331. \9pink = PINK;\
  332. \9grey = GREY;\
  333. \9lightGrey = LIGHTGREY;\
  334. \9cyan = CYAN;\
  335. \9purple = PURPLE;\
  336. \9blue = BLUE;\
  337. \9brown = BROWN;\
  338. \9green = GREEN;\
  339. \9red = RED;\
  340. \9black = BLACK;\
  341. }",
  342.   [ "Annex/module/console.lua" ] = "\
  343. local module = {}\
  344. \
  345. function module:error( data )\
  346. \9error( data:gsub( \"$([%w_]+)\", function( v )\
  347. \9\9return tostring( self.macros[v] or self.env[v] or \"UNDEFINED\" )\
  348. \9end ), 0 )\
  349. end\
  350. \
  351. function module:print( data )\
  352. \9self:write \"\"\
  353. \9print( data:gsub( \"$([%w_]+)\", function( v )\
  354. \9\9return tostring( self.macros[v] or self.env[v] or \"UNDEFINED\" )\
  355. \9end ), nil )\
  356. end\
  357. \
  358. return module",
  359.   [ "Annex/debug" ] = "\
  360. local args = { ... }\
  361. \
  362. local path = _G._ANNEX_PATH or shell.getRunningProgram():gsub( \"/debug\", \"\" )\
  363. local env = setmetatable( { _ANNEX_PATH = path }, { __index = _ENV } )\
  364. local preprocessor\
  365. \
  366. local help = [[HELP TEXT]]\
  367. \
  368. local paths = {}\
  369. local main = \"main\"\
  370. local penv = {}\
  371. local outputs = {}\
  372. \
  373. local mode = \"path\"\
  374. \
  375. local h = fs.open( path .. \"/preprocessor.lua\", \"r\" )\
  376. if h then\
  377. \9local content = h.readAll()\
  378. \9h.close()\
  379. \
  380. \9local f, err = load( content, \"preprocessor\", nil, env )\
  381. \9if f then\
  382. \9\9preprocessor = f()\
  383. \9else\
  384. \9\9error( err, 0 )\
  385. \9end\
  386. else\
  387. \9return error( \"Cannot find file 'preprocessor'\", 0 )\
  388. end\
  389. \
  390. env.preprocessor = preprocessor\
  391. \
  392. local prev\
  393. for i = 1, #args do\
  394. \9prev = mode ~= \"main\" and mode or prev\
  395. \9if args[i] == \"help\" or args[i] == \"-h\" or args[i] == \"-help\" then\
  396. \9\9print( help )\
  397. \9elseif args[i] == \"-m\" then\
  398. \9\9mode = \"main\"\
  399. \9elseif args[i] == \"-d\" then\
  400. \9\9mode = \"path\"\
  401. \9elseif args[i] == \"-o\" then\
  402. \9\9mode = \"output\"\
  403. \9elseif args[i] == \"-f\" then\
  404. \9\9mode = \"flag\"\
  405. \9elseif mode == \"main\" then\
  406. \9\9main = args[i]:gsub( \"%.\", \"/\" )\
  407. \9\9mode = prev\
  408. \9elseif mode == \"path\" then\
  409. \9\9paths[#paths + 1] = args[i]:gsub( \"%.\", \"/\" )\
  410. \9elseif mode == \"output\" then\
  411. \9\9outputs[#outputs + 1] = args[i]\
  412. \9elseif mode == \"flag\" then\
  413. \9\9penv[args[i]] = true\
  414. \9end\
  415. end\
  416. \
  417. outputs[1] = outputs[1] or \"?/output\"\
  418. \
  419. if #paths == 0 then\
  420. \9return error( \"Expected one or more build paths\", 0 )\
  421. end\
  422. \
  423. local p = preprocessor()\
  424. p.include_paths = paths\
  425. p.env = penv\
  426. p.active_include = main .. \".lua\"\
  427. \
  428. paths[#paths + 1] = \"\"\
  429. paths[#paths + 1] = \"Annex/lib\"\
  430. \
  431. for i = 1, #paths do\
  432. \9local h = fs.open( paths[i] .. \"/\" .. main .. \".lua\", \"r\" )\
  433. \9if h then\
  434. \9\9local content = h.readAll()\
  435. \9\9h.close()\
  436. \
  437. \9\9p:push( content )\
  438. \
  439. \9\9local r = p:build()\
  440. \
  441. \9\9local f, err = load( r, \"output\", nil, _ENV )\
  442. \9\9if not f then return error( err, 0 ) end\
  443. \9\9return f()\
  444. \
  445. \9end\
  446. end\
  447. \
  448. return error( \"Failed to find main file (\" .. main .. \".lua) in paths given\", 0 )",
  449.   [ "Annex/lib/class.lua" ] = "\
  450. -- @print Including Annex.lib.class\
  451. \
  452. class = {}\
  453. local classobj = setmetatable( {}, { __index = class } )\
  454. local names = {}\
  455. local last_created\
  456. \
  457. local supportedMetaMethods = {\
  458. \9__add = true;\
  459. \9__sub = true;\
  460. \9__mul = true;\
  461. \9__div = true;\
  462. \9__mod = true;\
  463. \9__pow = true;\
  464. \9__unm = true;\
  465. \9__len = true;\
  466. \9__eq = true;\
  467. \9__lt = true;\
  468. \9__lte = true;\
  469. \9__tostring = true;\
  470. \9__concat = true;\
  471. }\
  472. \
  473. local function _tostring( self )\
  474. \9return \"[Class] \" .. self:type()\
  475. end\
  476. local function _concat( a, b )\
  477. \9return tostring( a ) .. tostring( b )\
  478. end\
  479. \
  480. local function newSuper( object, super )\
  481. \
  482. \9local superProxy = {}\
  483. \
  484. \9if super.super then\
  485. \9\9superProxy.super = newSuper( object, super.super )\
  486. \9end\
  487. \
  488. \9setmetatable( superProxy, { __index = function( t, k )\
  489. \
  490. \9\9if type( super[k] ) == \"function\" then\
  491. \9\9\9return function( self, ... )\
  492. \
  493. \9\9\9\9if self == superProxy then\
  494. \9\9\9\9\9self = object\
  495. \9\9\9\9end\
  496. \9\9\9\9object.super = superProxy.super\
  497. \9\9\9\9local v = { super[k]( self, ... ) }\
  498. \9\9\9\9object.super = superProxy\
  499. \9\9\9\9return unpack( v )\
  500. \
  501. \9\9\9end\
  502. \9\9else\
  503. \9\9\9return super[k]\
  504. \9\9end\
  505. \
  506. \9end, __newindex = super, __tostring = function( self )\
  507. \9\9return \"[Super] \" .. tostring( super ) .. \" of \" .. tostring( object )\
  508. \9end } )\
  509. \
  510. \9return superProxy\
  511. \
  512. end\
  513. \
  514. function classobj:new( ... )\
  515. \
  516. \9local mt = { __index = self, __INSTANCE = true }\
  517. \9local instance = setmetatable( { class = self, meta = mt }, mt )\
  518. \
  519. \9if self.super then\
  520. \9\9instance.super = newSuper( instance, self.super )\
  521. \9end\
  522. \
  523. \9for k, v in pairs( self.meta ) do\
  524. \9\9if supportedMetaMethods[k] then\
  525. \9\9\9mt[k] = v\
  526. \9\9end\
  527. \9end\
  528. \
  529. \9if mt.__tostring == _tostring then\
  530. \9\9function mt:__tostring()\
  531. \9\9\9return self:tostring()\
  532. \9\9end\
  533. \9end\
  534. \
  535. \9function instance:type()\
  536. \9\9return self.class:type()\
  537. \9end\
  538. \
  539. \9function instance:typeOf( class )\
  540. \9\9return self.class:typeOf( class )\
  541. \9end\
  542. \
  543. \9if not self.tostring then\
  544. \9\9function instance:tostring()\
  545. \9\9\9return \"[Instance] \" .. self:type()\
  546. \9\9end\
  547. \9end\
  548. \
  549. \9local ob = self\
  550. \9while ob do\
  551. \9\9if ob[ob.meta.__type] then\
  552. \9\9\9ob[ob.meta.__type]( instance, ... )\
  553. \9\9\9break\
  554. \9\9end\
  555. \9\9ob = ob.super\
  556. \9end\
  557. \
  558. \9return instance\
  559. \
  560. end\
  561. \
  562. function classobj:extends( super )\
  563. \
  564. \9self.super = super\
  565. \9self.meta.__index = super\
  566. \
  567. end\
  568. \
  569. function classobj:type()\
  570. \
  571. \9return tostring( self.meta.__type )\
  572. \
  573. end\
  574. \
  575. function classobj:typeOf( super )\
  576. \
  577. \9return super == self or ( self.super and self.super:typeOf( super ) ) or false\
  578. \
  579. end\
  580. \
  581. function classobj:implement( t )\
  582. \
  583. \9for k, v in pairs( t ) do\
  584. \9\9self[k] = v\
  585. \9end\
  586. \9return self\
  587. \
  588. end\
  589. \
  590. function class:new( name )\
  591. \
  592. \9if type( name or self ) ~= \"string\" then\
  593. \9\9return error( \"expected string class name, got \" .. type( name or self ) )\
  594. \9end\
  595. \
  596. \9local mt = { __index = classobj, __CLASS = true, __tostring = _tostring, __concat = _concat, __call = classobj.new, __type = name or self }\
  597. \9local obj = setmetatable( { meta = mt }, mt )\
  598. \
  599. \9names[name] = obj\
  600. \9last_created = obj\
  601. \
  602. \9_ENV[name] = obj\
  603. \
  604. \9return function( t )\
  605. \9\9if not last_created then\
  606. \9\9\9return error \"no class to define\"\
  607. \9\9end\
  608. \
  609. \9\9for k, v in pairs( t ) do\
  610. \9\9\9last_created[k] = v\
  611. \9\9end\
  612. \9\9last_created = nil\
  613. \9end\
  614. \
  615. end\
  616. \
  617. function class.type( object )\
  618. \
  619. \9local _type = type( object )\
  620. \
  621. \9if _type == \"table\" then\
  622. \9\9pcall( function()\
  623. \9\9\9local mt = getmetatable( object )\
  624. \9\9\9_type = ( ( mt.__CLASS or mt.__INSTANCE ) and object:type() ) or _type\
  625. \9\9end )\
  626. \9end\
  627. \
  628. \9return _type\
  629. \
  630. end\
  631. \
  632. function class.typeOf( object, class )\
  633. \
  634. \9if type( object ) == \"table\" then\
  635. \9\9local ok, v = pcall( function() return getmetatable( object ).__CLASS or getmetatable( object ).__INSTANCE or error() end )\
  636. \9\9return ok and v\
  637. \9end\
  638. \
  639. \9return false\
  640. \
  641. end\
  642. \
  643. function class.isClass( object )\
  644. \
  645. \9return pcall( function() if not getmetatable( object ).__CLASS then error() end end ), nil\
  646. \
  647. end\
  648. \
  649. function class.isInstance( object )\
  650. \
  651. \9return pcall( function() if not getmetatable( object ).__INSTANCE then error() end end ), nil\
  652. \
  653. end\
  654. \
  655. setmetatable( class, {\
  656. \9__call = class.new;\
  657. } )\
  658. \
  659. function extends( name )\
  660. \
  661. \9if not last_created then\
  662. \9\9return error \"no class to extend\"\
  663. \9end\
  664. \
  665. \9if not names[name] then\
  666. \9\9return error( \"no such class '\" .. tostring( name ) .. \"'\" )\
  667. \9end\
  668. \
  669. \9last_created:extends( names[name] )\
  670. \9\
  671. \9return function( t )\
  672. \9\9if not last_created then\
  673. \9\9\9return error \"no class to define\"\
  674. \9\9end\
  675. \
  676. \9\9for k, v in pairs( t ) do\
  677. \9\9\9last_created[k] = v\
  678. \9\9end\
  679. \9\9last_created = nil\
  680. \9end\
  681. \
  682. end\
  683. \
  684. function implements( name )\
  685. \9\
  686. \9if not last_created then\
  687. \9\9return error \"no class to modify\"\
  688. \9end\
  689. \
  690. \9if type( name ) == \"string\" then\
  691. \9\9if not names[name] then\
  692. \9\9\9return error( \"no such class '\" .. tostring( name ) .. \"'\" )\
  693. \9\9end\
  694. \9\9last_created:implements( names[name] )\
  695. \9elseif type( name ) == \"table\" then\
  696. \9\9last_created:implements( name )\
  697. \9end\
  698. \9\
  699. \9return function( t )\
  700. \9\9if not last_created then\
  701. \9\9\9return error \"no class to define\"\
  702. \9\9end\
  703. \
  704. \9\9for k, v in pairs( t ) do\
  705. \9\9\9last_created[k] = v\
  706. \9\9end\
  707. \9\9last_created = nil\
  708. \9end\
  709. \
  710. end",
  711.   [ "Annex/preprocessor.lua" ] = "\
  712. local preprocessor = {}\
  713. \
  714. local function loadm( self, path, name )\
  715. \9if self.module_loaded[path] then return end\
  716. \9self.module_loaded[path] = true\
  717. \
  718. \9local h = fs.open( path, \"r\")\
  719. \9if h then\
  720. \9\9local content = h.readAll()\
  721. \9\9h.close()\
  722. \
  723. \9\9local f, err = load( content, name, nil, _ENV )\
  724. \9\9if f then\
  725. \9\9\9f, err = pcall( f, self )\
  726. \9\9end\
  727. \9\9if not f then\
  728. \9\9\9return error( err, 0 )\
  729. \9\9end\
  730. \9\9if type( err ) == \"table\" then\
  731. \9\9\9for k, v in pairs( err ) do\
  732. \9\9\9\9if type( v ) ~= \"function\" then\
  733. \9\9\9\9\9return error( \"Module tried to add non-function instruction\", 0 )\
  734. \9\9\9\9elseif self.instruction_list[k] then\
  735. \9\9\9\9\9return error( \"Conflicting instruction name '\" .. k .. \"'\", 0 )\
  736. \9\9\9\9end\
  737. \9\9\9\9self.instruction_list[k] = v\
  738. \9\9\9end\
  739. \9\9end\
  740. \9end\
  741. end\
  742. \
  743. local function load( self, data )\
  744. \9self:write \"\"\
  745. \9if fs.exists( data:gsub( \"%.\", \"/\" ) .. \".lua\" ) then\
  746. \9\9loadm( self, data:gsub( \"%.\", \"/\" ) .. \".lua\", data )\
  747. \9elseif fs.exists( _ANNEX_PATH .. \"/module/\" .. data:gsub( \"%.\", \"/\" ) .. \".lua\" ) then\
  748. \9\9loadm( self, _ANNEX_PATH .. \"/module/\" .. data:gsub( \"%.\", \"/\" ) .. \".lua\", data )\
  749. \9else\
  750. \9\9return error( \"No such module '\" .. data .. \"'\", 0 )\
  751. \9end\
  752. end\
  753. \
  754. function preprocessor:fetch()\
  755. \9local s = self.stack[#self.stack]\
  756. \9if not s then return end\
  757. \9s.line = s.line + 1\
  758. \9if s.instructions[s.iptr] and s.instructions[s.iptr].line == s.line then\
  759. \9\9s.iptr = s.iptr + 1\
  760. \9\9return s.instructions[s.iptr - 1].data, s.instructions[s.iptr - 1].instruction\
  761. \9end\
  762. \9return s.lines[s.line], nil\
  763. end\
  764. \
  765. function preprocessor:write( data )\
  766. \9local s = self.stack[#self.stack]\
  767. \9if not s then return end\
  768. \9local c, _c = 1\
  769. \9while c ~= 0 do\
  770. \9\9c = 0\
  771. \9\9for k, v in pairs( self.macros ) do\
  772. \9\9\9data, _c = ( \" \" .. data .. \" \" ):gsub( \"([^%w_])\" .. k .. \"([^%w_])\", \"%1\" .. v .. \"%2\" )\
  773. \9\9\9data = data:sub( 2, -2 )\
  774. \9\9\9c = c + _c\
  775. \9\9end\
  776. \9end\
  777. \9s.output[#s.output + 1] = data\
  778. end\
  779. \
  780. function preprocessor:execute( i, d )\
  781. \9if self.instruction_list[i] then\
  782. \9\9self.instruction_list[i]( self, d )\
  783. \9else\
  784. \9\9return error( \"Unknown instruction '\" .. i .. \"'\", 0 )\
  785. \9end\
  786. end\
  787. \
  788. function preprocessor:push( str )\
  789. \9local lines, instructions, line = {}, {}\
  790. \
  791. \9while str do\
  792. \9\9line = str:match \"^(.-)\\n\" or str\
  793. \9\9str = str:match \"^.-\\n(.+)\"\
  794. \9\9if line and line:find \"^%s*%-?%-?%s*@%s*%w\" then\
  795. \9\9\9instructions[#instructions + 1] = { line = #lines + 1, instruction = line:match \"^%s*%-?%-?%s*@%s*(%w+)\":lower(), data = line:gsub( \"^%s*%-?%-?%s*@%s*%w+%s*\", \"\" ) }\
  796. \9\9\9line = \"\"\
  797. \9\9end\
  798. \
  799. \9\9lines[#lines + 1] = line\
  800. \9end\
  801. \
  802. \9self.stack[#self.stack + 1] = {\
  803. \9\9lines = lines;\
  804. \9\9instructions = instructions;\
  805. \9\9line = 0;\
  806. \9\9iptr = 1;\
  807. \9\9output = {};\
  808. \9}\
  809. end\
  810. \
  811. function preprocessor:build()\
  812. \9local d, i = self:fetch()\
  813. \9while d do\
  814. \9\9if i then\
  815. \9\9\9self:execute( i, d )\
  816. \9\9else\
  817. \9\9\9self:write( d )\
  818. \9\9end\
  819. \9\9d, i = self:fetch()\
  820. \9end\
  821. \9return table.concat( table.remove( self.stack, #self.stack ).output, \"\\n\" )\
  822. end\
  823. \
  824. return function()\
  825. \
  826. \9local s = {}\
  827. \
  828. \9s.env = {}\
  829. \9s.include_paths = { \"\" }\
  830. \9s.instruction_list = { load = load }\
  831. \
  832. \9s.module_loaded = {}\
  833. \9s.macros = {}\
  834. \
  835. \9s.stack = {}\
  836. \
  837. \9loadm( s, _ANNEX_PATH .. \"/module/conditional.lua\", \"conditional\" )\
  838. \9loadm( s, _ANNEX_PATH .. \"/module/console.lua\", \"error\" )\
  839. \9loadm( s, _ANNEX_PATH .. \"/module/define.lua\", \"define\" )\
  840. \9loadm( s, _ANNEX_PATH .. \"/module/include.lua\", \"include\" )\
  841. \
  842. \9return setmetatable( s, { __index = preprocessor } )\
  843. \
  844. end",
  845.   [ "Annex/module/define.lua" ] = "\
  846. local module = {}\
  847. \
  848. function module:define( data )\
  849. \9self:write \"\"\
  850. \9local w = data:match \"^[%w_]+\"\
  851. \9if not w then\
  852. \9\9return error( \"Expected word to define in '@define', got \" .. (\"%q\"):format( data ) )\
  853. \9end\
  854. \9local r = data:match \"^[%w_]+%s*(.-)$\"\
  855. \9if #r > 0 then\
  856. \9\9self.macros[w] = r\
  857. \9\9self.env[w] = r ~= \"false\"\
  858. \9else\
  859. \9\9self.macros[w] = \"true\"\
  860. \9\9self.env[w] = true\
  861. \9end\
  862. end\
  863. \
  864. function module:undef( data )\
  865. \9self:write \"\"\
  866. \9self.env[data] = nil\
  867. \9self.macros[data] = nil\
  868. end\
  869. \
  870. function module:defineifndef( data )\
  871. \9self:write \"\"\
  872. \9local w = data:match \"^[%w_]+\"\
  873. \9if not w then\
  874. \9\9return error( \"Expected word to define in '@define', got \" .. (\"%q\"):format( data ) )\
  875. \9end\
  876. \9if self.env[w] == nil then\
  877. \9\9local r = data:match \"^[%w_]+%s*(.-)$\"\
  878. \9\9if #r > 0 then\
  879. \9\9\9self.macros[w] = r\
  880. \9\9\9self.env[w] = r ~= \"false\"\
  881. \9\9else\
  882. \9\9\9self.macros[w] = \"true\"\
  883. \9\9\9self.env[w] = true\
  884. \9\9end\
  885. \9end\
  886. end\
  887. \
  888. return module",
  889. }fs.makeDir "Annex"
  890. for k, v in pairs( files ) do
  891.     print( "Writing to file " .. k )
  892.     local h = fs.open( k, "w" )
  893.     if h then
  894.         h.write( v )
  895.         h.close()
  896.     else
  897.         return error( "Failed to open file", 0 )
  898.     end
  899. end
  900.  
  901. print "Would you like to patch the startup file to aid development? (enter to confirm)"
  902. if select( 2, os.pullEvent "key" ) == keys.enter then
  903.     local h = fs.open( "startup", "r" )
  904.     local content = h and h.readAll() or ""
  905.     if h then h.close() end
  906.  
  907.     content = "shell.setPath( \"Annex:\" .. shell.path() )\n" .. content
  908.  
  909.     local h = fs.open( "startup", "w" )
  910.     if h then
  911.         h.write( content )
  912.         h.close()
  913.     else
  914.         print "Failed to open startup, is it a folder?"
  915.     end
  916. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement