gecko/browser/components/loop/build-jsx
2014-09-16 11:38:59 +01:00

115 lines
3.6 KiB
Python
Executable File

#! /usr/bin/env python
import os
import re
from distutils import spawn
import subprocess
from threading import Thread
import argparse
def find_react_version(lib_dir):
"Finds the React library version number currently used."
for filename in os.listdir(lib_dir):
match = re.match(r"react-(.*)-prod\.js", filename)
if (match and match.group(1)):
return match.group(1)
print 'Unable to find the current react version used in content.'
print 'Please checked the %s directory.' % lib_dir
exit(1)
SHARED_LIBS_DIR=os.path.join(os.path.dirname(__file__), "content", "shared", "libs")
REACT_VERSION=find_react_version(SHARED_LIBS_DIR)
src_files = [] # files to be compiled
# search for react-tools install
jsx_path = spawn.find_executable('jsx')
if jsx_path is None:
print 'You do not have the react-tools installed'
print 'Please do $ npm install -g react-tools'
exit(1)
p = subprocess.Popen([jsx_path, '-V'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
for line in iter(p.stdout.readline, b''):
info = line.rstrip()
if not info == REACT_VERSION:
print 'You have the wrong version of react-tools installed'
print 'Please use version %s' % REACT_VERSION
exit(1)
# parse the CLI arguments
description = 'Loop build tool for JSX files. ' + \
'Will scan entire loop directory and compile them in place. ' + \
'Must be executed from browser/components/loop directory.'
parser = argparse.ArgumentParser(description=description)
parser.add_argument('--watch', '-w', action='store_true', help='continuous' +
'build based on file changes (optional)')
args = parser.parse_args()
# loop through all tuples and get unique dirs only
unique_jsx_dirs = []
# find all .jsx files
for dirname, dirnames, filenames in os.walk('.'):
for filename in filenames:
if '.jsx' == os.path.splitext(filename)[1]: # (root, ext)
src_files.append(filename)
if dirname not in unique_jsx_dirs:
unique_jsx_dirs.append(dirname)
def jsx_run_watcher(path):
subprocess.call(['jsx', '-w', '-x', 'jsx', path, path])
def start_jsx_watcher_threads(dirs):
"""
starts a thread with a jsx watch process
for every dir in the dirs argument
"""
threads = []
for udir in dirs:
t = Thread(target=jsx_run_watcher, args=(udir,))
threads.append(t)
t.start()
for t in threads:
t.join()
def check_file_packaging(srcs):
"""
get all lines from jar.mn file
check against the files we are going to compile.
Note that this only looks at filenames, and will miss
the one of two identically named files.
"""
# get all lines from jar.mn
packaged_files = [line.strip() for line in open('./jar.mn')]
# loop through our compiled files and compare against jar.mn
# XXX fix to use paths so that identically named files don't get overlooked
# and update the docstring for this function
missing_jar_files = [] # initially empty
for src_file in srcs:
transpiled_file = os.path.splitext(src_file)[0] + ".js" # (root, ext)
if not any(transpiled_file in x for x in packaged_files):
missing_jar_files.append(transpiled_file)
# output a message to the user
if len(missing_jar_files):
for f in missing_jar_files:
print f + ' not in jar.mn file'
check_file_packaging(src_files)
if args.watch:
start_jsx_watcher_threads(unique_jsx_dirs)
else:
for d in unique_jsx_dirs:
out = subprocess.call(['jsx', '-x', 'jsx', d, d])