(merged_path)
| 45 | |
| 46 | # Generates: all.js: merge *.js, compile coffeescript, all.css: merge *.css, vendor prefix features |
| 47 | def merge(merged_path): |
| 48 | merged_path = merged_path.replace("\\", "/") |
| 49 | merge_dir = os.path.dirname(merged_path) |
| 50 | s = time.time() |
| 51 | ext = merged_path.split(".")[-1] |
| 52 | if ext == "js": # If merging .js find .coffee too |
| 53 | find_ext = ["js", "coffee"] |
| 54 | else: |
| 55 | find_ext = [ext] |
| 56 | |
| 57 | # If exist check the other files modification date |
| 58 | if os.path.isfile(merged_path): |
| 59 | merged_mtime = os.path.getmtime(merged_path) |
| 60 | else: |
| 61 | merged_mtime = 0 |
| 62 | |
| 63 | changed = {} |
| 64 | for file_path in findfiles(merge_dir, find_ext): |
| 65 | if os.path.getmtime(file_path) > merged_mtime + 1: |
| 66 | changed[file_path] = True |
| 67 | if not changed: |
| 68 | return # Assets not changed, nothing to do |
| 69 | |
| 70 | old_parts = {} |
| 71 | if os.path.isfile(merged_path): # Find old parts to avoid unncessary recompile |
| 72 | merged_old = open(merged_path, "rb").read() |
| 73 | for match in re.findall(rb"(/\* ---- (.*?) ---- \*/(.*?)(?=/\* ----|$))", merged_old, re.DOTALL): |
| 74 | old_parts[match[1].decode()] = match[2].strip(b"\n\r") |
| 75 | |
| 76 | logging.debug("Merging %s (changed: %s, old parts: %s)" % (merged_path, changed, len(old_parts))) |
| 77 | # Merge files |
| 78 | parts = [] |
| 79 | s_total = time.time() |
| 80 | for file_path in findfiles(merge_dir, find_ext): |
| 81 | file_relative_path = file_path.replace(merge_dir + "/", "") |
| 82 | parts.append(b"\n/* ---- %s ---- */\n\n" % file_relative_path.encode("utf8")) |
| 83 | if file_path.endswith(".coffee"): # Compile coffee script |
| 84 | if file_path in changed or file_relative_path not in old_parts: # Only recompile if changed or its not compiled before |
| 85 | if config.coffeescript_compiler is None: |
| 86 | config.coffeescript_compiler = findCoffeescriptCompiler() |
| 87 | if not config.coffeescript_compiler: |
| 88 | logging.error("No coffeescript compiler defined, skipping compiling %s" % merged_path) |
| 89 | return False # No coffeescript compiler, skip this file |
| 90 | |
| 91 | # Replace / with os separators and escape it |
| 92 | file_path_escaped = helper.shellquote(file_path.replace("/", os.path.sep)) |
| 93 | |
| 94 | if "%s" in config.coffeescript_compiler: # Replace %s with coffeescript file |
| 95 | command = config.coffeescript_compiler.replace("%s", file_path_escaped) |
| 96 | else: # Put coffeescript file to end |
| 97 | command = config.coffeescript_compiler + " " + file_path_escaped |
| 98 | |
| 99 | # Start compiling |
| 100 | s = time.time() |
| 101 | compiler = subprocess.Popen(command, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE) |
| 102 | out = compiler.stdout.read() |
| 103 | compiler.wait() |
| 104 | logging.debug("Running: %s (Done in %.2fs)" % (command, time.time() - s)) |
no test coverage detected