init
This commit is contained in:
		
						commit
						8d306ab690
					
				
					 14 changed files with 703 additions and 0 deletions
				
			
		
							
								
								
									
										1
									
								
								.gitignore
									
										
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
										
									
									
										vendored
									
									
										Normal file
									
								
							|  | @ -0,0 +1 @@ | ||||||
|  | build.lua | ||||||
							
								
								
									
										12
									
								
								config.lua
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								config.lua
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,12 @@ | ||||||
|  | return { | ||||||
|  |   entry = 'main.lua', | ||||||
|  |   output = { | ||||||
|  |     name = 'build.lua' | ||||||
|  |   }, | ||||||
|  |   plug = { | ||||||
|  |     --require 'plug.switch' {}, | ||||||
|  |     require 'plug.arrow' {}, | ||||||
|  |     require 'plug.assigns' {}, | ||||||
|  |     require 'plug.num' {}, | ||||||
|  |   }, | ||||||
|  | } | ||||||
							
								
								
									
										14
									
								
								examples/config.lua
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								examples/config.lua
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,14 @@ | ||||||
|  | return { | ||||||
|  |   entry = { | ||||||
|  |     'main.lua', | ||||||
|  |   }, | ||||||
|  |   output = { | ||||||
|  |     name = '$hash.lua' | ||||||
|  |   }, | ||||||
|  |   plug = { | ||||||
|  |     --require 'plug.switch' {}, | ||||||
|  |     require 'plug.arrow' {}, | ||||||
|  |     require 'plug.assigns' {}, | ||||||
|  |     require 'plug.num' {}, | ||||||
|  |   }, | ||||||
|  | } | ||||||
							
								
								
									
										29
									
								
								examples/main.lua
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								examples/main.lua
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,29 @@ | ||||||
|  | 
 | ||||||
|  | local main = () ==> | ||||||
|  | 
 | ||||||
|  |   emp = () => | ||||||
|  | 
 | ||||||
|  |   add = (a, b) => a + b | ||||||
|  | 
 | ||||||
|  |   local a, b = 5, 8 | ||||||
|  |   add(a, b) | ||||||
|  | 
 | ||||||
|  |   a += 0x10_10 | ||||||
|  | 
 | ||||||
|  | --[[ | ||||||
|  |   switch(a) { | ||||||
|  |     5, 8 => error 'cant', | ||||||
|  |     300 => error 'trahtarista' | ||||||
|  |       (() =>)() | ||||||
|  |       , | ||||||
|  |     _ => print 'can', | ||||||
|  |   } | ||||||
|  | --]] | ||||||
|  | 
 | ||||||
|  |   print 'hello world' | ||||||
|  | <== | ||||||
|  | main() | ||||||
|  | 
 | ||||||
|  | { | ||||||
|  |   tst = true | ||||||
|  | } | ||||||
							
								
								
									
										
											BIN
										
									
								
								img/luapack.png
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								img/luapack.png
									
										
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 77 KiB | 
							
								
								
									
										104
									
								
								img/luapack.svg
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										104
									
								
								img/luapack.svg
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,104 @@ | ||||||
|  | <?xml version="1.0" encoding="UTF-8" standalone="no"?> | ||||||
|  | <!-- Generator: Adobe Illustrator 14.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 43363)  --> | ||||||
|  | 
 | ||||||
|  | <svg | ||||||
|  |    version="1.0" | ||||||
|  |    id="Ebene_1" | ||||||
|  |    x="0px" | ||||||
|  |    y="0px" | ||||||
|  |    width="947px" | ||||||
|  |    height="947px" | ||||||
|  |    viewBox="0 0 947 947" | ||||||
|  |    enable-background="new 0 0 947 947" | ||||||
|  |    xml:space="preserve" | ||||||
|  |    sodipodi:docname="luapack.svg" | ||||||
|  |    inkscape:version="1.1.2 (0a00cf5339, 2022-02-04)" | ||||||
|  |    inkscape:export-filename="/home/er2/Desktop/luapack.png" | ||||||
|  |    inkscape:export-xdpi="96" | ||||||
|  |    inkscape:export-ydpi="96" | ||||||
|  |    xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" | ||||||
|  |    xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" | ||||||
|  |    xmlns="http://www.w3.org/2000/svg" | ||||||
|  |    xmlns:svg="http://www.w3.org/2000/svg"><defs | ||||||
|  |    id="defs23"><inkscape:perspective | ||||||
|  |      sodipodi:type="inkscape:persp3d" | ||||||
|  |      inkscape:vp_x="0 : 473.5 : 1" | ||||||
|  |      inkscape:vp_y="0 : 1000 : 0" | ||||||
|  |      inkscape:vp_z="909.38991 : 908.67343 : 1" | ||||||
|  |      inkscape:persp3d-origin="473.5 : 315.66667 : 1" | ||||||
|  |      id="perspective1468" /></defs><sodipodi:namedview | ||||||
|  |    id="namedview21" | ||||||
|  |    pagecolor="#505050" | ||||||
|  |    bordercolor="#eeeeee" | ||||||
|  |    borderopacity="1" | ||||||
|  |    inkscape:pageshadow="0" | ||||||
|  |    inkscape:pageopacity="0" | ||||||
|  |    inkscape:pagecheckerboard="0" | ||||||
|  |    showgrid="false" | ||||||
|  |    inkscape:zoom="0.32" | ||||||
|  |    inkscape:cx="512.5" | ||||||
|  |    inkscape:cy="825" | ||||||
|  |    inkscape:window-width="1920" | ||||||
|  |    inkscape:window-height="1007" | ||||||
|  |    inkscape:window-x="0" | ||||||
|  |    inkscape:window-y="0" | ||||||
|  |    inkscape:window-maximized="1" | ||||||
|  |    inkscape:current-layer="g18" /> | ||||||
|  | 
 | ||||||
|  | <path | ||||||
|  |    id="rect1651-6" | ||||||
|  |    style="display:inline;fill:#002a5f;fill-opacity:1;stroke:none;stroke-width:0;stroke-linecap:round;stroke-miterlimit:0;stroke-dasharray:none" | ||||||
|  |    d="m 0,420 473,256 v 0 L 947,420 473,164 Z" | ||||||
|  |    sodipodi:nodetypes="cccccc" | ||||||
|  |    inkscape:label="boxc" /><path | ||||||
|  |    id="rect7503" | ||||||
|  |    style="display:inline;fill:#003b80;fill-opacity:1;stroke:#4646d7;stroke-width:8;stroke-linecap:round;stroke-miterlimit:0;stroke-dasharray:none;stroke-opacity:1" | ||||||
|  |    d="M 186.55273,0 0,103.54688 V 420 L 473,164 Z M 760.44727,0 473,164 947,420 V 103.54688 Z" | ||||||
|  |    sodipodi:nodetypes="cccccccccc" | ||||||
|  |    inkscape:label="boxb" /><g | ||||||
|  |    id="g18" | ||||||
|  |    style="display:inline" | ||||||
|  |    inkscape:label="logo"> | ||||||
|  | 	<path | ||||||
|  |    fill="#000080" | ||||||
|  |    d="m 835.5,473.6 c 0,-199.8 -162.2,-362 -362,-362 -199.8,0 -362,162.2 -362,362 0,199.8 162.2,362 362,362 199.8,0 362,-162.2 362,-362" | ||||||
|  |    id="path2" | ||||||
|  |    style="display:inline" /> | ||||||
|  | 	<path | ||||||
|  |    fill="#ffffff" | ||||||
|  |    d="m 729.5,323.6 c 0,-58.5 -47.5,-106 -106,-106 -58.5,0 -106,47.5 -106,106 0,58.5 47.5,106 106,106 58.5,0 106,-47.5 106,-106" | ||||||
|  |    id="path4" | ||||||
|  |    style="display:inline" /> | ||||||
|  | 	<path | ||||||
|  |    fill="#000080" | ||||||
|  |    d="m 941.5,111.5 c 0,-58.5 -47.5,-106 -106,-106 -58.5,0 -106,47.5 -106,106 0,58.5 47.5,106 106,106 58.5,0 106,-47.4 106,-106" | ||||||
|  |    id="path6" | ||||||
|  |    style="display:inline" /> | ||||||
|  | 	 | ||||||
|  | 	<path | ||||||
|  |    fill="none" | ||||||
|  |    stroke="#808080" | ||||||
|  |    stroke-width="10.8612" | ||||||
|  |    stroke-miterlimit="10" | ||||||
|  |    stroke-dasharray="40.8475" | ||||||
|  |    d="m 890.6,261 c 33.5,65.8 51,138.6 51,212.5 0,258.4 -209.7,468.1 -468.1,468.1 C 215.1,941.6 5.4,731.9 5.4,473.5 5.4,215.1 215.1,5.4 473.5,5.4 c 83.1,0 164.6,22.1 236.2,63.9" | ||||||
|  |    id="path16" /> | ||||||
|  | </g><path | ||||||
|  |    id="rect1651" | ||||||
|  |    style="display:inline;fill:#003b80;fill-opacity:1;stroke:#4646d7;stroke-width:8;stroke-linecap:round;stroke-miterlimit:0;stroke-dasharray:none;stroke-opacity:1" | ||||||
|  |    d="M 0,420 V 947 H 473.49454 L 473,676 Z" | ||||||
|  |    sodipodi:nodetypes="ccccc" | ||||||
|  |    inkscape:label="boxtl" /><path | ||||||
|  |    id="path8" | ||||||
|  |    style="fill:#ffffff;stroke-width:0.822633" | ||||||
|  |    d="M 23.675681,674.91644 V 870.29109 H 145.09508 V 848.32826 H 48.601807 V 674.91644 Z m 350.276829,50.92133 c -20.4013,0 -37.265,5.92205 -46.64302,16.36949 -6.41654,7.23917 -9.04988,15.30195 -9.62573,29.20444 h 22.54003 c 1.89206,-17.19302 12.094,-24.92612 32.98888,-24.92612 20.07225,0 31.34064,7.48682 31.34064,20.89574 v 5.92261 c 0,9.37801 -5.59324,13.4075 -23.27986,15.54635 -31.58911,4.03091 -36.44334,5.10158 -44.99873,8.55664 -16.3704,6.66332 -24.67819,19.24872 -24.67819,37.51117 0,25.50163 17.68697,41.54354 46.06781,41.54354 17.68661,0 31.91754,-6.16946 47.71211,-20.6478 1.563,14.23156 8.55555,20.6478 23.0339,20.6478 4.60674,0 8.06123,-0.49405 15.3004,-2.38611 v -16.86335 c -2.4679,0.57583 -3.53732,0.57519 -4.85352,0.57519 -7.81502,0 -12.09116,-4.02969 -12.09116,-11.02206 v -82.59321 c 0,-24.92579 -18.26297,-38.33432 -52.81356,-38.33432 z m -205.24634,4.0304 v 108.25916 c 0,23.36277 17.43938,38.3343 44.7508,38.3343 20.6481,0 33.72684,-7.24001 46.88897,-25.74927 v 19.57873 h 20.0726 V 729.86817 h -22.21077 v 79.63189 c 0,28.7099 -15.05379,47.46417 -38.33431,47.46417 -17.68661,0 -28.9565,-10.77552 -28.9565,-27.63949 v -99.45657 z m 235.84714,70.99393 v 25.17208 c 0,7.56823 -2.22058,12.09195 -8.88392,18.26169 -9.13123,8.3086 -20.07269,12.58705 -33.23483,12.58705 -17.43983,0 -27.6395,-8.30848 -27.6395,-22.54003 0,-14.72513 9.95367,-22.2942 33.81004,-25.74926 23.60957,-3.20827 28.46224,-4.27647 35.94821,-7.73153 z" | ||||||
|  |    inkscape:label="lua" /><path | ||||||
|  |    id="rect1651-3" | ||||||
|  |    style="display:inline;fill:#003b80;fill-opacity:1;stroke:#4646d7;stroke-width:8;stroke-linecap:round;stroke-miterlimit:0;stroke-dasharray:none;stroke-opacity:1" | ||||||
|  |    d="M 946,420 V 947 H 472.50546 L 473,676 Z" | ||||||
|  |    sodipodi:nodetypes="ccccc" | ||||||
|  |    inkscape:label="boxtr" /><path | ||||||
|  |    id="path45551" | ||||||
|  |    style="fill:#ffffff" | ||||||
|  |    d="m 490.8125,747.75 c -4.2014,0.38195 -6.49305,2.6736 -6.875,6.875 v 114.58398 c 0.38195,4.2014 2.6736,6.49306 6.875,6.875 4.20139,-0.38194 6.49305,-2.6736 6.875,-6.875 v -34.375 h 34.375 c 12.41323,-0.38194 22.62954,-4.6788 30.65039,-12.89062 8.21183,-8.21183 12.50868,-18.42814 12.89063,-30.65039 -0.38195,-12.22225 -4.6788,-22.44052 -12.89063,-30.65235 C 554.50107,752.4288 544.28475,748.13195 532.0625,747.75 Z m 154.6875,0 c -35.34622,0 -64,28.65378 -64,64 0,35.34622 28.65378,64 64,64 14.56082,-0.0462 28.67059,-5.05633 40,-14.20312 V 863.75 c 0,6.648 5.352,12 12,12 6.648,0 12,-5.352 12,-12 0.1263,-17.12699 0,-52 0,-52 0,-35.34622 -28.65378,-64 -64,-64 z m 134.89062,0 c -13.17711,0.19097 -24.53925,3.43813 -34.08789,9.74023 -9.35766,6.49308 -16.51908,14.61001 -21.48437,24.34961 -5.15626,9.7396 -7.73438,19.85978 -7.73438,30.36328 0,10.5035 2.57812,20.5295 7.73438,30.07813 4.96529,9.73961 12.12671,17.7604 21.48437,24.0625 9.35766,6.3021 20.71981,9.54926 34.08789,9.74023 9.16669,-0.19097 17.28363,-1.81357 24.34961,-4.86914 6.87502,-3.05556 12.3177,-6.39886 16.32813,-10.02734 4.01043,-3.81945 6.01562,-6.77821 6.01562,-8.87891 -0.19097,-2.29167 -1.05033,-4.01041 -2.57812,-5.15625 -1.33681,-1.14584 -2.7691,-1.71875 -4.29688,-1.71875 -2.29167,0.38195 -4.86978,1.90841 -7.73437,4.58203 -2.86459,2.67362 -6.77885,5.34787 -11.74414,8.02149 -5.15627,2.67362 -11.93705,4.10591 -20.33985,4.29687 -15.65976,-0.57291 -27.78513,-6.01559 -36.3789,-16.32812 -8.59377,-10.12155 -12.98676,-21.4837 -13.17774,-34.08789 0.19098,-12.79517 4.58397,-24.25345 13.17774,-34.375 8.59377,-10.12155 20.71914,-15.47006 36.3789,-16.04297 8.4028,0.19097 15.18358,1.62326 20.33985,4.29688 4.96529,2.67362 8.87955,5.34787 11.74414,8.02148 2.86459,2.67362 5.4427,4.20204 7.73437,4.58398 2.48265,-0.19097 4.20139,-1.05034 5.15625,-2.57812 1.14584,-1.52779 1.71875,-2.96007 1.71875,-4.29688 0,-2.1007 -2.00519,-5.0614 -6.01562,-8.88086 -4.01043,-3.62848 -9.54924,-6.96983 -16.61524,-10.02539 -6.87501,-3.05556 -14.89581,-4.68012 -24.0625,-4.87109 z m 69.3086,0 c -4.2014,0.38195 -6.49305,2.6736 -6.875,6.875 v 114.58398 c 0.38195,4.2014 2.6736,6.49306 6.875,6.875 4.20139,-0.38194 6.49305,-2.6736 6.875,-6.875 v -33.51562 l 22.91601,-23.20313 42.39649,60.72852 c 1.52778,1.90973 3.43684,2.86523 5.72851,2.86523 1.33682,0 2.67492,-0.38259 4.01172,-1.14648 3.24654,-2.86459 3.81945,-6.10979 1.71875,-9.73828 l -43.82812,-61.875 -0.28711,-0.28711 43.25586,-43.54102 c 2.67361,-3.24653 2.67361,-6.4937 0,-9.74023 -3.24654,-2.67362 -6.4937,-2.67362 -9.74024,0 l -66.17187,66.45898 V 754.625 c -0.38195,-4.2014 -2.67361,-6.49305 -6.875,-6.875 z M 497.6875,761.5 h 34.375 c 8.4028,0.19097 15.37195,3.05554 20.91016,8.59375 5.72918,5.72918 8.68988,12.79643 8.88086,21.19922 -0.19098,8.4028 -3.05555,15.46808 -8.59375,21.19726 -5.72918,5.53821 -12.79447,8.40278 -21.19727,8.59375 h -34.375 z M 644.5,764.75 a 48,48 0 0 1 48,48 48,48 0 0 1 -48,48 48,48 0 0 1 -48,-48 48,48 0 0 1 48,-48 z" | ||||||
|  |    inkscape:label="pack" /></svg> | ||||||
| After Width: | Height: | Size: 8.1 KiB | 
							
								
								
									
										19
									
								
								license
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								license
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,19 @@ | ||||||
|  | Zlib License | ||||||
|  | 
 | ||||||
|  | Copyright (c) 2022 Er2 <er2@dismail.de> | ||||||
|  | 
 | ||||||
|  | This software is provided 'as-is', without any express or implied | ||||||
|  | warranty. In no event will the authors be held liable for any damages | ||||||
|  | arising from the use of this software. | ||||||
|  | 
 | ||||||
|  | Permission is granted to anyone to use this software for any purpose, | ||||||
|  | including commercial applications, and to alter it and redistribute it | ||||||
|  | freely, subject to the following restrictions: | ||||||
|  | 
 | ||||||
|  | 1. The origin of this software must not be misrepresented; you must not | ||||||
|  |    claim that you wrote the original software. If you use this software | ||||||
|  |    in a product, an acknowledgement in the product documentation would be | ||||||
|  |    appreciated but is not required. | ||||||
|  | 2. Altered source versions must be plainly marked as such, and must not be | ||||||
|  |    misrepresented as being the original software. | ||||||
|  | 3. This notice may not be removed or altered from any source distribution. | ||||||
							
								
								
									
										303
									
								
								main.lua
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										303
									
								
								main.lua
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,303 @@ | ||||||
|  | lp = { | ||||||
|  |   name = 'LuaPack', | ||||||
|  |   version = '0.1', | ||||||
|  |   copyright = '(c) Er2 2022, Zlib License', | ||||||
|  | 
 | ||||||
|  |   path = {}, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | -- String interpolation | ||||||
|  | function lp.strf(str, env) | ||||||
|  |   return str:gsub('%$([%w_]+)%]?', (n) ==> | ||||||
|  |     if env[n] | ||||||
|  |     then return tostring(env[n]) | ||||||
|  |     end | ||||||
|  |   <==) .. '' | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | -- Helper function | ||||||
|  | function lp.insPath(t) | ||||||
|  |   for _, v in pairs(t) | ||||||
|  |   do table.insert(lp.path, v) | ||||||
|  |   end | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | function lp.foreach(e, fn) | ||||||
|  |   for _, v in pairs(e.plug) | ||||||
|  |   do fn(v) end | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | function lp.reqf(file, opts) | ||||||
|  |   if opts.ext[file] | ||||||
|  |   then return opts.ext[file] | ||||||
|  |   end | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | function lp.req(e, opts) | ||||||
|  |   return lp.reqf(e.file, opts) | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | -- Polyfill | ||||||
|  | function table.contains(t, V) | ||||||
|  |   for k, v in pairs(t) do | ||||||
|  |     if v == V | ||||||
|  |     then return k | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | -- Try to find file path | ||||||
|  | function lp.resolve(name, dir) | ||||||
|  |   if path.absolute(name) | ||||||
|  |   then return name | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|  |   name = name:gsub('%.[%w_]+$', '') | ||||||
|  | 
 | ||||||
|  |   for _, v in pairs(lp.path) do | ||||||
|  |     v = v:gsub('%?', name) | ||||||
|  |     v = path.join(dir, v) | ||||||
|  | 
 | ||||||
|  |     local f = io.open(v) | ||||||
|  |     if f then | ||||||
|  |       f:close() | ||||||
|  |       return v | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | -- Use this in plugins to get module | ||||||
|  | function lp.get(d, e, opts) | ||||||
|  |   local f | ||||||
|  |   for _, v in pairs(e.plug) do | ||||||
|  |     f = lp.resolve(d, e.dir) | ||||||
|  |     if f then break end | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|  |   if not f then | ||||||
|  |     print(lp.strf('  [WRN] Cannot find "$file"', {file = d})) | ||||||
|  |     return nil | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|  |   local c = lp.reqf(f, opts) | ||||||
|  |   if not c | ||||||
|  |   then c = lp.make(f, opts, f .. '.out.lua') | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|  |   if not e.req[c.i] | ||||||
|  |   then e.reqs = e.reqs + 1 | ||||||
|  |   end | ||||||
|  |   e.req[c.i] = true | ||||||
|  | 
 | ||||||
|  |   print(lp.strf('  [GOT] $file', c)) | ||||||
|  | 
 | ||||||
|  |   return c | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | -- Alias to makeEnt without entry flag | ||||||
|  | function lp.make(file, opts, out) | ||||||
|  |   return lp.makeEnt(file, opts, out, false) | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | -- Make function | ||||||
|  | function lp.makeEnt(file, opts, out, entry) | ||||||
|  |   local dir = path.abs(path.dir(file)) | ||||||
|  |   local ext  = file:match '%.([%w_]+)$' | ||||||
|  |   local oext =  out:match '%.([%w_]+)$' | ||||||
|  | 
 | ||||||
|  |   file = lp.resolve(file, dir, ext) | ||||||
|  | 
 | ||||||
|  |   local e = lp.reqf(file, opts) | ||||||
|  |   if e then return e end | ||||||
|  | 
 | ||||||
|  |   local f, err = io.open(file) | ||||||
|  |   assert(f, err) | ||||||
|  |   local src = f:read '*a' | ||||||
|  |   f:close() | ||||||
|  | 
 | ||||||
|  |   e = { | ||||||
|  |     i = #opts.ext + 1, | ||||||
|  |     file = file, | ||||||
|  |     dir = dir, | ||||||
|  |     ext = ext, | ||||||
|  |     src = src, | ||||||
|  |     reqs = 0, | ||||||
|  |     req  = {}, | ||||||
|  |     entry = entry, | ||||||
|  |     plug = opts.plug, | ||||||
|  |   } | ||||||
|  |   table.insert(opts.ext, file) | ||||||
|  |   opts.ext[file] = e | ||||||
|  | 
 | ||||||
|  |   if #e.plug == 0 | ||||||
|  |   then error(lp.strf('File "$file" cannot be loaded. Add plugins.', e)) | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|  |   lp.foreach(e, (v) ==> | ||||||
|  |     v.load(e, ext, oext) | ||||||
|  |     e.src = v.deps(e, opts)      or e.src | ||||||
|  |     e.src = v.transform(e, opts) or e.src | ||||||
|  |   <==) | ||||||
|  | 
 | ||||||
|  |   return e | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | local alp = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' | ||||||
|  | function lp.randgen(n) | ||||||
|  |   local g = '' | ||||||
|  |   for i = 1, n do | ||||||
|  |     local v = math.random(#alp) | ||||||
|  |     g = g.. alp:sub(v, v) | ||||||
|  |   end | ||||||
|  |   return g | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | function lp.save(e, out) | ||||||
|  |   out = lp.strf(out, { | ||||||
|  |     name = path.base(e.file):gsub('%.[%w_]+$', ''), | ||||||
|  |     hash = lp.randgen(6), | ||||||
|  |   }) | ||||||
|  |   local f, err = io.open(out, 'w') | ||||||
|  |   assert(f, err) | ||||||
|  |   f:write(e.src) | ||||||
|  |   f:close() | ||||||
|  |   return out | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | function lp.config(conf, dir) | ||||||
|  |   local cfg = { | ||||||
|  |     -- defaults | ||||||
|  |     entry = {}, | ||||||
|  |     output = {}, | ||||||
|  |     ext = {}, | ||||||
|  |     plug = { | ||||||
|  |       require 'plug.lua' {} | ||||||
|  |     }, | ||||||
|  |   } | ||||||
|  |   for k, v in pairs(conf) do | ||||||
|  |     if k == 'entry' | ||||||
|  |     or k == 'output' | ||||||
|  |     then | ||||||
|  |       if type(v) == 'string' | ||||||
|  |       then table.insert(cfg[k], path.abs(v, dir)) | ||||||
|  |       elseif type(v) == 'table' then | ||||||
|  |         for _, v in pairs(v) | ||||||
|  |         do table.insert(cfg[k], path.abs(tostring(v), dir)) | ||||||
|  |         end | ||||||
|  |       else error(lp.strf('Unknown type $type in "$opt"', {type = type(v), opt = k})) | ||||||
|  |       end | ||||||
|  |     elseif k == 'plug' | ||||||
|  |     then for _, v in pairs(v) do | ||||||
|  |       if type(v) == 'table' | ||||||
|  |       then table.insert(cfg[k], v) | ||||||
|  |         v.load =      v.load or function(e, ext, oext) end | ||||||
|  |         v.deps =      v.deps      or function(e, opts) end | ||||||
|  |         v.transform = v.transform or function(e, opts) end | ||||||
|  |         v.postbuild = v.postbuild or function(e, opts) end | ||||||
|  |       else error(lp.strf('Unknown type $type in "$opt"', {type = type(v), opt = k})) | ||||||
|  |       end | ||||||
|  |     end | ||||||
|  |     else error(lp.strf('Unknown option "$opt"', {opt = k})) | ||||||
|  |    end | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|  |   return cfg | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | function lp.build(conf, dir) | ||||||
|  |   local opts = lp.config(conf, dir) | ||||||
|  |   for k, v in pairs(opts.entry) do | ||||||
|  |     local out = opts.output[math.min(k, #opts.output)] | ||||||
|  |     print(lp.strf('[BUILD] $file => $out', {file = v, out = out})) | ||||||
|  |     local e = lp.makeEnt(v, opts, out, true) | ||||||
|  |     lp.foreach(e, function(v) | ||||||
|  |       v.postbuild(e, opts) | ||||||
|  |     end) | ||||||
|  |     local save = lp.save(e, out) | ||||||
|  |     print(lp.strf('[BUILT] $o => $r', {o = e.file, r = save})) | ||||||
|  |   end | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | function main(argv) | ||||||
|  |   print(lp.name ..' '.. lp.version) | ||||||
|  |   print(lp.copyright) | ||||||
|  |   print '' | ||||||
|  | 
 | ||||||
|  |   local int = (argv[0] or ''):match '^lua' -- started using interpreter | ||||||
|  |   if #argv == 0 | ||||||
|  |   or (#argv == 1 and int) then | ||||||
|  |     print(('Usage: %s config.lua'):format(int and (argv[0] ..' '.. argv[1]) or argv[0])) | ||||||
|  |     return -1 | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|  |   local t = os.time() | ||||||
|  | 
 | ||||||
|  |   local ent = path.abs(int and argv[2] or argv[1]) | ||||||
|  |   local dir = path.dir(ent) | ||||||
|  | 
 | ||||||
|  |   local f, err = io.open(ent) | ||||||
|  |   assert(f, err) | ||||||
|  |   local conf = f:read '*a' | ||||||
|  |   f:close() | ||||||
|  | 
 | ||||||
|  |   f, err = load(conf, 'config') | ||||||
|  |   if not f | ||||||
|  |   then error(err) | ||||||
|  |   else conf = f() | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|  |   lp.build(conf, dir) | ||||||
|  | 
 | ||||||
|  |   print(lp.strf('Built under $sec seconds.', {sec = os.time() - t})) | ||||||
|  | 
 | ||||||
|  |   return 0 | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | path = { | ||||||
|  |   absolute = (f) ==> | ||||||
|  |     f = f:sub(1, 1) | ||||||
|  |     return f == '/' or f == '\\' | ||||||
|  |   <== | ||||||
|  | 
 | ||||||
|  |   , relative = (f) => not path.absolute(f) | ||||||
|  | 
 | ||||||
|  |   , abs = (f, pwd) ==> | ||||||
|  |     if path.absolute(f) | ||||||
|  |     then return f end | ||||||
|  | 
 | ||||||
|  |     pwd or= os.getenv 'PWD' | ||||||
|  | 
 | ||||||
|  |     f = pwd .. '/'.. f | ||||||
|  |     f = f:gsub('(.*)[\\/]+%.%.', path.dir) | ||||||
|  |     f = f:gsub('%.[\\/]+', '/') | ||||||
|  |     f = f:gsub('[\\/]+', '/') | ||||||
|  | 
 | ||||||
|  |     return f | ||||||
|  |   <== | ||||||
|  | 
 | ||||||
|  |   , rel = (f) => f | ||||||
|  | 
 | ||||||
|  |   , base = (f) => f:match '([^/\\]*)[/\\]*$' | ||||||
|  | 
 | ||||||
|  |   , dir = (f) => f:match '^(.*)[\\/]+[^\\/]*[\\/]*$' | ||||||
|  | 
 | ||||||
|  |   , file = (f) => f:match '([^\\/]+)[\\/]*$' | ||||||
|  | 
 | ||||||
|  |   , join = function(base, add) | ||||||
|  |     if path.absolute(add) | ||||||
|  |     then return add end | ||||||
|  | 
 | ||||||
|  |     if path.relative(base) | ||||||
|  |     then base = path.abs(base) | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|  |     local p = path.abs(base ..'/'.. add) | ||||||
|  |     if path.relative(p) | ||||||
|  |     then return add end | ||||||
|  | 
 | ||||||
|  |     return p | ||||||
|  |   end, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | math.randomseed(os.time()) | ||||||
|  | os.exit(main(arg)) | ||||||
							
								
								
									
										17
									
								
								plug/arrow.lua
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								plug/arrow.lua
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,17 @@ | ||||||
|  | -- Arrow functions in Lua | ||||||
|  | -- f = (n) => 2 + n | ||||||
|  | 
 | ||||||
|  | return function(op) | ||||||
|  |   return { | ||||||
|  |     name = 'Arrow functions', | ||||||
|  |     transform = function(e, opts) | ||||||
|  |       return e.src | ||||||
|  |       -- multiple-line | ||||||
|  |       :gsub('%(([^%(%)]*)%)%s*==>%s*(.-)%s<==', | ||||||
|  |         'function(%1) %2 end') | ||||||
|  |       -- single-line | ||||||
|  |       :gsub('%(([^%(%)]*)%)%s*=>[ \t\v\f]*([^\n\r]*)', | ||||||
|  |         'function(%1) return %2 end') | ||||||
|  |     end, | ||||||
|  |   } | ||||||
|  | end | ||||||
							
								
								
									
										16
									
								
								plug/assigns.lua
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								plug/assigns.lua
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,16 @@ | ||||||
|  | 
 | ||||||
|  | local reg  = '([%%w_%%.%%[%%]]+)%%s*(%s)=%%s*' | ||||||
|  | local regv = '%1 = %1 %2 ' | ||||||
|  | 
 | ||||||
|  | return function(op) | ||||||
|  |   return { | ||||||
|  |     name = 'Assigments', | ||||||
|  |     transform = function(e, opts) | ||||||
|  |       return e.src | ||||||
|  |       :gsub(reg:format '[%+%-%*%/%%^|&]', regv) | ||||||
|  |       :gsub(reg:format '%.%.', regv) | ||||||
|  |       :gsub(reg:format 'and', regv) | ||||||
|  |       :gsub(reg:format 'or', regv) | ||||||
|  |     end, | ||||||
|  |   } | ||||||
|  | end | ||||||
							
								
								
									
										108
									
								
								plug/lua.lua
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										108
									
								
								plug/lua.lua
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,108 @@ | ||||||
|  | 
 | ||||||
|  | lp.insPath { | ||||||
|  |   '?.lua', | ||||||
|  |   '?/init.lua', | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | local env = { | ||||||
|  |   name = lp.name, | ||||||
|  |   ver = lp.version, | ||||||
|  |   copy = lp.copyright, | ||||||
|  | 
 | ||||||
|  |   cache = '_c_', | ||||||
|  |   req = '_r_', | ||||||
|  |   mreq = '_m_', -- require by map | ||||||
|  |   mtemp = '  function($req) -- $file\n$src\n  end, -- $file\n', | ||||||
|  |   pre = '--[=[=]=]', | ||||||
|  |   regex = '([^\n]-)' .. 'require%s*%(?["\'](.-)["\']%)?%s*', | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | env.require = lp.strf('$req(%d)', env) | ||||||
|  | 
 | ||||||
|  | local base = lp.strf([[ | ||||||
|  | $pre -- $name $ver | ||||||
|  | $pre -- $copy | ||||||
|  | $pre (function(m) | ||||||
|  | $pre   -- Module cache | ||||||
|  | $pre   local $cache = {} | ||||||
|  | %s$pre | ||||||
|  | $pre   -- Load main module | ||||||
|  | $pre   $require | ||||||
|  | $pre | ||||||
|  | $pre end) { | ||||||
|  | %s | ||||||
|  | $pre } | ||||||
|  | ]], env) | ||||||
|  | 
 | ||||||
|  | local main = lp.strf([[ | ||||||
|  | $pre | ||||||
|  | $pre   -- Require function | ||||||
|  | $pre   local function $req(i) | ||||||
|  | $pre     -- Check in cache | ||||||
|  | $pre     local c = $cache[i] | ||||||
|  | $pre     if not c then | ||||||
|  | $pre       -- Execute module function | ||||||
|  | $pre       c = m[i]($req) | ||||||
|  | $pre | ||||||
|  | $pre       -- And put it to cache | ||||||
|  | $pre       $cache[i] = c | ||||||
|  | $pre     end | ||||||
|  | $pre | ||||||
|  | $pre     -- Return cached returned value | ||||||
|  | $pre     return c | ||||||
|  | $pre   end | ||||||
|  | ]], env) | ||||||
|  | 
 | ||||||
|  | local mreq = lp.strf([[ | ||||||
|  | $pre | ||||||
|  | $pre   -- Require by map | ||||||
|  | $pre   local function $mreq(m, r) | ||||||
|  | $pre     -- Check if module exists | ||||||
|  | $pre     if not m[r] | ||||||
|  | $pre     then error('Cannot find module "'..r..'"') | ||||||
|  | $pre     end | ||||||
|  | $pre | ||||||
|  | $pre     -- Using default require | ||||||
|  | $pre     return $req(m[r]) | ||||||
|  | $pre   end | ||||||
|  | ]], env) | ||||||
|  | 
 | ||||||
|  | return function(op) | ||||||
|  |   return { | ||||||
|  |     name = 'Base Lua plugin', | ||||||
|  |     load = function(e, ext, oext) | ||||||
|  |       if ext == 'lua' | ||||||
|  |       then return true -- load every lua file | ||||||
|  |       end | ||||||
|  |     end, | ||||||
|  |     deps = function(e, opts) | ||||||
|  |       return e.src:gsub(env.regex, function(bef, name) | ||||||
|  |         name = name:gsub('%.', '/') -- lua.lua => lua/lua | ||||||
|  |         if bef:match '%-%-' | ||||||
|  |         then return '' end -- under comment | ||||||
|  |         local d = lp.get(name, e, opts) | ||||||
|  |         if d then | ||||||
|  |           return bef.. env.require:format(d.i) | ||||||
|  |         end | ||||||
|  |       end) | ||||||
|  |     end, | ||||||
|  |     transform = function(e, opts) | ||||||
|  |       if not e.entry then return end -- change only entry files | ||||||
|  |       if e.reqs > 0 then | ||||||
|  |         local m = '' | ||||||
|  |         for i = 1, #opts.ext do | ||||||
|  |           local v = opts.ext[opts.ext[i]] -- [i] => [file] => object | ||||||
|  |           m = m.. lp.strf(lp.strf(env.mtemp, env), v) | ||||||
|  |         end | ||||||
|  |         return base:format(main, opts.ext[e.file].i, m) | ||||||
|  |       end | ||||||
|  |     end, | ||||||
|  |     postbuild = function(e, opts) | ||||||
|  |       local f, err = load(e.src, 'lint') | ||||||
|  |       if err then | ||||||
|  |         print(e.src) | ||||||
|  |         error(err) | ||||||
|  |       end | ||||||
|  |     end, | ||||||
|  |   } | ||||||
|  | end | ||||||
							
								
								
									
										28
									
								
								plug/num.lua
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								plug/num.lua
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,28 @@ | ||||||
|  | 
 | ||||||
|  | return function(op) | ||||||
|  |   return { | ||||||
|  |     name = 'Better numbers', | ||||||
|  |     transform = function(e, opts) | ||||||
|  |       return e.src | ||||||
|  |       :gsub('%.?%d[%._0-9A-Fa-fxboXBO]+', function(num) | ||||||
|  |         local base = 10 | ||||||
|  |         local nnum = num | ||||||
|  |         :gsub('_', '') | ||||||
|  |         :gsub('^0([xboXBO])', function(m) | ||||||
|  |           if m == 'x' or m == 'X' | ||||||
|  |           then base = 16 | ||||||
|  |           elseif m == 'b' or m == 'B' | ||||||
|  |           then base = 2 | ||||||
|  |           elseif m == 'o' or m == 'O' | ||||||
|  |           then base = 8 | ||||||
|  |           end | ||||||
|  |           return '' | ||||||
|  |         end) | ||||||
|  |         assert(not nnum:match '[xboXBO]', 'invalid number '..num) | ||||||
|  |         nnum = tonumber(nnum, base) | ||||||
|  |         assert(nnum, 'invalid number '..num) | ||||||
|  |         return tostring(nnum) | ||||||
|  |       end) | ||||||
|  |     end, | ||||||
|  |   } | ||||||
|  | end | ||||||
							
								
								
									
										31
									
								
								plug/switch.lua
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								plug/switch.lua
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,31 @@ | ||||||
|  | 
 | ||||||
|  | -- BROKEN | ||||||
|  | 
 | ||||||
|  | local reg  = 'switch%s*%(([^{}]-)%)%s*{(.-)}' | ||||||
|  | local regv = '%s*([^=>]+)%s*=>%s*(.-);' | ||||||
|  | 
 | ||||||
|  | return function(op) | ||||||
|  |   return { | ||||||
|  |     name = 'Switch-case', | ||||||
|  |     transform = function(e, opts) | ||||||
|  |       return e.src:gsub(reg, function(case, code) | ||||||
|  |         local st | ||||||
|  |         return code:gsub(regv, function(when, c) | ||||||
|  |           local code = st and 'elseif' or 'if' | ||||||
|  |           st = true | ||||||
|  |           if when == '_' | ||||||
|  |           then code = 'else ' | ||||||
|  |           else local st | ||||||
|  |             for v in when:gmatch '%s*([^,]+)' do | ||||||
|  |               code = code .. (st and ' or' or '') | ||||||
|  |               .. (' %s == %s'):format(case, v) | ||||||
|  |               st = true | ||||||
|  |             end | ||||||
|  |             code = code .. ' then ' | ||||||
|  |           end | ||||||
|  |           return code .. c | ||||||
|  |         end) .. ' end' | ||||||
|  |       end) | ||||||
|  |     end, | ||||||
|  |   } | ||||||
|  | end | ||||||
							
								
								
									
										21
									
								
								readme.md
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								readme.md
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,21 @@ | ||||||
|  | <div align="center"> | ||||||
|  | 
 | ||||||
|  | [](https://gitdab.com/er2/luapack) | ||||||
|  | 
 | ||||||
|  | # LuaPack | ||||||
|  | 
 | ||||||
|  | LuaPack is a module bundler. It takes Lua, MoonScript etc. files, transforms and bundles (packing). | ||||||
|  | 
 | ||||||
|  | </div> | ||||||
|  | 
 | ||||||
|  | # Usage | ||||||
|  | 
 | ||||||
|  | Usage is very simple: | ||||||
|  | 
 | ||||||
|  | ```lua build.lua config.lua``` | ||||||
|  | 
 | ||||||
|  | # Installation | ||||||
|  | 
 | ||||||
|  | You can't use main.lua file directly, because it uses non-standard Lua features like arrow functions. | ||||||
|  | 
 | ||||||
|  | Download `build.lua` from releases. | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue