Change minifier calling

This commit is contained in:
JoseluCross
2017-06-29 19:14:07 +02:00
parent 15898d10e5
commit f39594aa63
2 changed files with 37 additions and 26 deletions

View File

@@ -1,3 +0,0 @@
{
"supported": ["js","css","htm","html","twig"]
}

View File

@@ -1,19 +1,20 @@
#!/usr/bin/python3 #!/usr/bin/python3
import os import os
import json
from sys import argv as _ from sys import argv as _
from shutil import copyfile from shutil import copyfile
from jsmin import jsmin from jsmin import jsmin
from rcssmin import cssmin from rcssmin import cssmin
VERSION="0.1.0.0" VERSION = "0.1.1.0"
FORMATS={} verbose = False
with open('../data/supported.json') as data: FORMATS = {
FORMATS = json.load(data) "supported": ["js", "css", "htm", "html", "twig"]
}
def copyStructure(originalPath, finalPath): def copyStructure(originalPath, finalPath):
"""Copy folder structure and start copy and minimize"""
if not os.path.isdir(originalPath): if not os.path.isdir(originalPath):
raise Exception("The origin directory " + originalPath + " not exists") raise Exception("The origin directory " + originalPath + " not exists")
@@ -27,30 +28,34 @@ def copyStructure(originalPath, finalPath):
def startCopy(origin, final): def startCopy(origin, final):
"""Copy files which do not support minifer"""
originList = os.listdir(origin) originList = os.listdir(origin)
for i in originList: for i in originList:
part = i.split('.') part = i.split('.')
if os.path.isdir(origin+"/"+i): if os.path.isdir(origin+"/"+i):
if verbose:
print(final + "/" + i + " has been created")
os.makedirs(final+"/"+i) os.makedirs(final+"/"+i)
startCopy(origin+"/"+i, final+"/"+i) startCopy(origin+"/"+i, final+"/"+i)
elif not part[len(part)-1] in FORMATS["supported"]: elif not part[len(part)-1] in FORMATS["supported"]:
copyfile(origin+"/"+i, final+"/"+i) copyfile(origin+"/"+i, final+"/"+i)
if verbose:
print(origin + "/" + i + " has been copied")
else: else:
minimizeFile(origin+"/"+i,final+"/"+i,part[len(part)-1]) minimizeFile(origin+"/"+i,final+"/"+i,part[len(part)-1])
def minimizeFile(source, production, format): def minimizeFile(source, production, format):
"""Minify files"""
input = open(source, "r") input = open(source, "r")
output = open(production, "w") output = open(production, "w")
if format == "js": output.write(globals()[format+"min"](input.read()))
output.write(jsmin(input.read()))
if format == "css":
output.write(cssmin(input.read(), keep_bang_comments=False))
if format == "html" or format == "htm" or format == "twig":
output.write(htmlmin(input.read()))
input.close() input.close()
output.close() output.close()
if verbose:
print(source + " has been minified")
def htmlmin(source): def htmlmin(source):
"""Minify html"""
ret = "" ret = ""
codePre = False #Is inside a code or pre label codePre = False #Is inside a code or pre label
lines = source.split("\n") lines = source.split("\n")
@@ -62,7 +67,6 @@ def htmlmin(source):
codePre = False codePre = False
elif "<pre>" in i or "<code>" in i: elif "<pre>" in i or "<code>" in i:
codePre = True codePre = True
if codePre: if codePre:
ret += "\n"+i ret += "\n"+i
else: else:
@@ -76,8 +80,16 @@ def htmlmin(source):
ret += i ret += i
return ret return ret
def htmmin(source):
"""Minify htm"""
return htmlmin(source)
def twigmin(source):
"""Minify twig"""
return htmlmin(source)
def main(): def main():
"""Main activity"""
if len(_) == 1 or _[1] == "-h": if len(_) == 1 or _[1] == "-h":
help() help()
elif _[1] == "-v": elif _[1] == "-v":
@@ -88,6 +100,7 @@ def main():
help() help()
def help(): def help():
"""Help"""
print("Usage of minithunder:") print("Usage of minithunder:")
print("---------------------") print("---------------------")
print("-h: show help") print("-h: show help")
@@ -95,6 +108,7 @@ def help():
print("-m <Source path> <Production path>: Minimize project to production") print("-m <Source path> <Production path>: Minimize project to production")
def version(): def version():
"""version"""
print("MiniThunder - Prepare your software to production") print("MiniThunder - Prepare your software to production")
print("Version: "+VERSION) print("Version: "+VERSION)