AstroJS/botanjs/compressor/closure.py

57 lines
1.3 KiB
Python
Raw Normal View History

2015-08-14 10:12:10 +00:00
#!/usr/bin/env python3
import os
2021-09-05 15:16:37 +00:00
from sys import platform
2015-08-14 10:12:10 +00:00
from tempfile import NamedTemporaryFile
2016-05-27 17:39:59 +00:00
from botanjs.config import Config as config
2022-08-02 13:35:37 +00:00
from botanjs.service.jwork import log
2015-08-14 10:12:10 +00:00
2016-05-27 17:39:59 +00:00
COMPILER = config[ "BotanJS" ][ "ClosureCompiler" ]
2015-08-14 10:12:10 +00:00
2022-08-02 13:35:37 +00:00
AVAILABLE = os.path.isfile( COMPILER )
2015-08-14 10:12:10 +00:00
COMPILER_OPTIONS = [
"--compilation_level ADVANCED_OPTIMIZATIONS"
, "--output_wrapper=\"(function(){%output%})();\""
]
class Wrapper:
C = None
# externs
E = ""
def __init__( self ):
2018-12-07 16:28:30 +00:00
self.C = "java -jar -Xmx64M "+ COMPILER + " " + " ".join( COMPILER_OPTIONS )
2015-08-14 10:12:10 +00:00
def scanExterns( self, sdir ):
for root, dirs, files in os.walk( sdir ):
# Split file extensions
files = list( os.path.splitext( x ) for x in files )
files.sort()
for f in files:
files.remove( f ) if f[1] != ".js" else None
self.E = " --externs " + " --externs ".join(
os.path.join( root, x )
# join back extensions
for x in list( "".join( x ) for x in files )
)
break
def compress( self, loc ):
2022-08-02 13:35:37 +00:00
if not AVAILABLE:
log.error( "Compiler not found" )
return
2015-08-14 10:12:10 +00:00
content = ""
with open( loc, "rb" ) as f:
content = f.read()
2021-09-05 15:16:37 +00:00
with NamedTemporaryFile( delete = ( not platform == "win32" ) ) as f:
2015-08-14 10:12:10 +00:00
f.write( content[12:-5] )
os.system( self.C + self.E + " --js " + f.name + " --js_output_file " + loc[:-3] + ".c.js" )