-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathspringboard
909 lines (630 loc) · 26 KB
/
springboard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
#!/usr/bin/env python
# Copyright (c) 2011 Justin Windle
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
import os
import re
import sys
import getopt
import fnmatch
import logging
import platform
import fileinput
import subprocess
import webbrowser
from shutil import copy, copyfileobj, copytree, ignore_patterns, rmtree
# --------------------------------------------------
#
# Config
#
# --------------------------------------------------
class Config:
# Determine operating system
sys = platform.system()
# Set system alias
if sys in ('Windows', 'Microsoft'):
OS = 'win'
elif sys in ('Darwin'):
OS = 'mac'
elif sys in ('Linux'):
OS = 'linux'
else:
OS = 'other'
# User directory
USER_HOME = os.getenv('USERPROFILE') or os.getenv('HOME')
# Springboard install directory
SPRINGBOARD_HOME = os.path.join(USER_HOME, 'local', 'springboard')
# Path to Closure compiler
PATH_COMPILER = os.path.join(SPRINGBOARD_HOME, 'tools', 'compiler.jar')
# Path to YUI compressor
PATH_COMPRESSOR = os.path.join(SPRINGBOARD_HOME, 'tools', 'compressor.jar')
# Default path to config file
CONFIG_PATH = os.path.join(os.getcwd(), '.sbconfig')
# Closure externs
EXTERNS_FILES = None
# Path to project source
SOURCE_PATH = 'source'
# Path to built project
DEPLOY_PATH = 'deploy'
# Path to project templates
TEMPLATE_PATH = 'templates'
# Prefix for tool commands
COMMAND_PREFIX = 'sb'
# Break lines at this column
CSS_LINE_BREAK = 0
# Project template to clone
PROJECT_TEMPLATE = 'default'
# Closure compiler compilation level
COMPILATION_LEVEL = 'ADVANCED_OPTIMIZATIONS'
# Closure / YUI Warning level
WARNING_LEVEL = 'VERBOSE'
# Additional Closure Compiler arguments
COMPILER_ARGS = ''
# Pattern for junk files to remove from deploy.
REMOVE = ''
# --------------------------------------------------
#
# Sets arguments from config file (if found) and
# extends them with any command line arguments
#
# --------------------------------------------------
def init(arguments):
# Load custom config file if specified
try:
options, more = getopt.getopt(arguments, 'c:s:d:t:', ['config=', 'source=', 'deploy=', 'template='])
for option, value in options:
if option in ('-c', '--config'):
Config.CONFIG_PATH = os.path.join(os.getcwd(), value)
except getopt.GetoptError, error:
console.info(str(error))
# Override default options with loaded config
if os.path.exists(Config.CONFIG_PATH):
lines = open(Config.CONFIG_PATH, 'r')
for line in lines:
result = RegEx.CONFIG_OPTION.search(line)
if result:
setattr(Config, result.group(1), result.group(2))
# Override loaded options with command line arguments
for option, value in options:
if option in ('-s', '--source'):
Config.SOURCE_PATH = value
if option in ('-d', '--deploy'):
Config.DEPLOY_PATH = value
if option in ('-t', '--template'):
Config.PROJECT_TEMPLATE = value
# --------------------------------------------------
#
# Define API
#
# --------------------------------------------------
init = staticmethod(init)
# --------------------------------------------------
#
# Regular Expressions
#
# --------------------------------------------------
class RegEx():
# --------------------------------------------------
#
# Compiles all available regular expressions
#
# --------------------------------------------------
def compile():
# Files to parse for blocks during build
RegEx.BUILD_TYPES = re.compile('\.(html|html|js|php)$')
# Matches options in the config file
RegEx.CONFIG_OPTION = re.compile('([^\=\s]+)\s+\=\s+(.+)')
# Matches an embedded / linked file pattern
RegEx.LINKED_FILE = re.compile('([\w\/\-\.]+\.\w{2,4})')
# The start of a task block
RegEx.BLOCK_START = re.compile('\W+%s:\s+(%s|show|hide)(\s+([\w\/\-\.]+\.\w{2,4}))?' % (Config.COMMAND_PREFIX, r'|'.join(Tools.API)))
# The end of a task block
RegEx.BLOCK_END = re.compile('^%s|\W+%s:\s+end' % (Config.COMMAND_PREFIX, Config.COMMAND_PREFIX))
# --------------------------------------------------
#
# Define API
#
# --------------------------------------------------
compile = staticmethod(compile)
# --------------------------------------------------
#
# Utils
#
# --------------------------------------------------
class Utils():
# --------------------------------------------------
#
# Formats and prints an action which operates on
# input files to produce one output file
#
# --------------------------------------------------
def logAction(action, files, output):
files = Utils.normalisePaths(files, Config.SOURCE_PATH)
output = os.path.basename(output)
console.info('%s:\t%s\n\t\t--> %s\n' % (action, files, output))
# --------------------------------------------------
#
# Calls a method on a class using a string
# representation of the method and returns whether
# or not the call was successful
#
# --------------------------------------------------
def callMethod(owner, name, *args):
method = None
try:
method = getattr(owner, name)
except AttributeError:
console.error('%s has no method "%s"' % (owner, name))
return False
# If method exists and is a function
if method and hasattr(method, '__call__'):
method(*args)
return True
# --------------------------------------------------
#
# Generator to recursively finds files whose
# extension matches a certain pattern regex
#
# --------------------------------------------------
def findFiles(directory, pattern):
for root, dirs, files in os.walk(directory):
for basename in files:
if pattern.search(basename):
filename = os.path.join(root, basename)
yield filename
# --------------------------------------------------
#
# Returns a list of paths, normalised to their own
# directory or a specified base path
#
# --------------------------------------------------
def normalisePaths(paths, base=None):
result = []
for path in paths:
if base:
result.append(re.sub('^/|/$', '', path.replace(base, '')))
else:
result.append(os.path.basename(path))
return r', '.join(result)
# --------------------------------------------------
#
# Recursively copies files from one location to
# another with optional include or exclude patterns
#
# --------------------------------------------------
def copyTree(source, target, includes=[], excludes=[]):
# Always exclude these file types
excludes.append('.DS_Store')
# transform glob patterns to regular expressions
includes = r'|'.join([fnmatch.translate(x) for x in includes])
excludes = r'|'.join([fnmatch.translate(x) for x in excludes]) or r'$.'
for root, dirs, files in os.walk(source):
# exclude directories matching exclude patterns
dirs[:] = [os.path.join(root, d) for d in dirs]
dirs[:] = [d for d in dirs if not re.match(excludes, os.path.basename(d))]
# exclude / include files
files = [os.path.join(root, f) for f in files]
files = [f for f in files if not re.match(excludes, os.path.basename(f))]
files = [f for f in files if re.match(includes, os.path.basename(f))]
# Merge paths and copy
for fname in files:
# Find relative paths in target
fpath = os.path.join(target, os.path.relpath(fname, source))
dpath = os.path.dirname(fpath)
# Create directory structure if required
if not os.path.exists(dpath):
os.makedirs(dpath)
# Copy the file
copy(fname, fpath)
# --------------------------------------------------
#
# Define API
#
# --------------------------------------------------
normalisePaths = staticmethod(normalisePaths)
callMethod = staticmethod(callMethod)
findFiles = staticmethod(findFiles)
logAction = staticmethod(logAction)
copyTree = staticmethod(copyTree)
# --------------------------------------------------
#
# Tools
#
# --------------------------------------------------
class Tools():
# List of available method names
API = ['concat', 'minify', 'compile']
# --------------------------------------------------
#
# Concatenates a list of files into a single output
#
# --------------------------------------------------
def concat(paths, output):
Utils.logAction('Concatenating', paths, output)
# Open the destination file
merged = open(output, 'wb')
# Loop through all target files
for path in paths:
# Write the file contents into destination
copyfileobj(open(path, 'rb'), merged)
merged.write('\n')
# Close the merged file
merged.close()
# --------------------------------------------------
#
# Minifies a list of files into a single output
# using YUI Compressor
#
# --------------------------------------------------
def minify(paths, output):
Utils.logAction('Minifying', paths, output)
# Loop through all target files
for path in paths:
# Pipe file into output and minify
process = subprocess.Popen('java -jar %s --type css --line-break %s %s >> %s' % (
Config.PATH_COMPRESSOR,
Config.CSS_LINE_BREAK,
path,
output
), shell=True)
# Show output
process.communicate()
# --------------------------------------------------
#
# Compiles a list of files into a single output
# using Google Closure Compiler
#
# --------------------------------------------------
def compile(paths, output):
Utils.logAction('Compiling', paths, output)
# Formatted list
scripts = ' --js '.join(paths)
# Construct arguments
options = [
'java',
'-jar', Config.PATH_COMPILER,
'--js', scripts,
'--js_output_file', output,
'--compilation_level', Config.COMPILATION_LEVEL,
'--warning_level', Config.WARNING_LEVEL,
Config.COMPILER_ARGS
]
# Add externs if specified
if Config.EXTERNS_FILES:
for extern in Config.EXTERNS_FILES.split(','):
options += ['--externs', os.path.join(Config.SOURCE_PATH, extern)]
# Tell Closure Compiler to process all scripts
process = subprocess.Popen(' '.join(options), shell=True)
# Show output
process.communicate()
# --------------------------------------------------
#
# Define API
#
# --------------------------------------------------
concat = staticmethod(concat)
minify = staticmethod(minify)
compile = staticmethod(compile)
# --------------------------------------------------
#
# Springboard
#
# --------------------------------------------------
class Springboard():
LOGO = '''
___ _ _ _
/ __|_ __ _ _(_)_ _ __ _| |__ ___ __ _ _ _ __| |
\__ \ '_ \ '_| | ' \/ _` | '_ \/ _ \/ _` | '_/ _` |
|___/ .__/_| |_|_||_\__, |_.__/\___/\__,_|_| \__,_|
|_| |___/
'''
HELP = LOGO + '''
Usage:
$ sb command [options]
Commands:
install Installs Springboard and command line aliases
config Opens the project templates directory for customising
init Initialises a project in the current directory (copies in template files)
-t, --template The name of the template to use (optional) (Defaults to 'default')
build Builds the current project
-c, --config Springboard config path (optional) (Defaults to '.sbconfig')
-s, --source Project source location (optional) (Defaults to 'source')
-d, --deploy Project output location (optional) (Defaults to 'deploy')
help Displays help
'''
# --------------------------------------------------
#
# Outputs the help documentation for Springboard
#
# --------------------------------------------------
def help():
console.info(Springboard.HELP)
# --------------------------------------------------
#
# Installs Springboard & create command line aliases
#
# --------------------------------------------------
def install():
console.info(Springboard.LOGO)
console.info('Installing Springboard to "%s"...' % Config.SPRINGBOARD_HOME)
# Try to detect subsequent installs
firstInstall = True
# Output messages
messages = []
# Install from here
source = os.getcwd()
# Path to installed script (assumes same name as current script)
script = os.path.join(Config.SPRINGBOARD_HOME, os.path.basename(__file__))
# Copy Springboard files
if os.path.exists(os.path.join(Config.SPRINGBOARD_HOME, Config.TEMPLATE_PATH)):
# Copy files but ignore existing templates
Utils.copyTree(source, Config.SPRINGBOARD_HOME, [], [Config.TEMPLATE_PATH, '.git'])
# Not the first install if templates exist
firstInstall = False
else:
# Copy all files and templates
Utils.copyTree(source, Config.SPRINGBOARD_HOME, [], ['.git'])
# For Mac, add alias to BASH profile
if Config.OS == 'mac':
# Make script executable
subprocess.call(['chmod', 'a+x', script])
# Path top bash_profile
bash = os.path.join(Config.USER_HOME, '.bash_profile')
if not os.path.exists(bash):
doc = open(bash, 'w+')
doc.close()
if os.path.exists(bash):
# Read the current contents
doc = open(bash, 'r')
txt = doc.read()
doc.close()
# Check for existing aliases
if not re.compile(r'alias (sb|springboard)').search(txt):
console.info('Adding aliases (%s, %s) to "%s"' % ('springboard', 'sb', bash))
# Create bash session aliases
subprocess.call(['alias', 'springboard=%s' % script])
subprocess.call(['alias', 'sb=%s' % script])
# Add Springboard aliases
doc = open(bash, 'a')
doc.write('\n# Springboard')
doc.write('\nalias springboard="%s"' % script)
doc.write('\nalias sb="%s"' % script)
doc.write('\nexport SPRINGBOARD_HOME="%s"' % Config.SPRINGBOARD_HOME)
doc.write('\nexport PATH=$PATH:$SPRINGBOARD_HOME')
doc.close()
# Aliases availbale in new BASH session
messages.append('Start a new BASH session to start using Springboard :)')
elif Config.OS == 'win':
console.info('Install for windows')
# subprocess.call(['setx', 'sb', script])
# Write a bat file to run Springboard
# Add to PATH variable
# Print status
for message in ['', 'Springboard successful installed!'] + messages + ['']:
console.info('\t%s' % message)
# Open the default template in a browser.
if firstInstall:
target = os.path.join(Config.SPRINGBOARD_HOME, Config.TEMPLATE_PATH, 'default', 'source', 'index.html')
webbrowser.open('file://' + target)
# --------------------------------------------------
#
#
#
#
# --------------------------------------------------
def config():
target = os.path.join(Config.SPRINGBOARD_HOME, Config.TEMPLATE_PATH)
subprocess.call(["open", "-R", target])
# --------------------------------------------------
#
# Initialises a Springboard project in the current
# directory using a given template if specified
#
# --------------------------------------------------
def init():
console.info('\nSpringboard >> init\n')
# Determine paths
root = os.path.join(Config.SPRINGBOARD_HOME, Config.TEMPLATE_PATH)
path = os.path.join(root, Config.PROJECT_TEMPLATE)
dest = os.getcwd()
# If the template exists
if os.path.exists(path):
# Clone template into the current working directory
console.info('Cloning template "%s" into "%s"' % (Config.PROJECT_TEMPLATE, dest))
Utils.copyTree(path, dest)
console.info('Ok, start coding! :)')
else:
# If there's no tempate matching the specified name
console.info('Cannot find template "%s" in "%s"' % (Config.PROJECT_TEMPLATE, root))
# --------------------------------------------------
#
# Builds the current project
#
# --------------------------------------------------
def build():
console.info('\nSpringboard >> build\n')
# Return if source path doesn't exist
if not os.path.exists(Config.SOURCE_PATH):
console.info('Source directory "%s" not found in "%s"' % (Config.SOURCE_PATH, os.getcwd()))
return
# Create / empty deploy directory
if not os.path.exists(Config.DEPLOY_PATH):
os.makedirs(Config.DEPLOY_PATH)
# Files for processing
stack = {}
# Process queue
queue = []
# Current output path
output = None
# Current task
process = None
# If we're stacking files
stacking = False
# Processed files
processed = []
# Redundant files
redundancies = []
# Format to embed resources
embed_format = None
# Remove current output
rmtree(Config.DEPLOY_PATH)
# Copy all files to deploy
copytree(Config.SOURCE_PATH, Config.DEPLOY_PATH, ignore=ignore_patterns('*.svn', '.DS_Store'))
# Parse each target marked for building
for target in Utils.findFiles(Config.DEPLOY_PATH, RegEx.BUILD_TYPES):
# Current line number
line_num = 0
# Current block start line
open_line_num = 0
# Step through the file
for line in fileinput.input(target, inplace=1):
line_num += 1
# Are we collecting files?
if stacking:
# Check for an ending block
if RegEx.BLOCK_END.match(line):
stacking = False
# Create process if output is specified
if output:
# Use the last embed code as a template to link the output
embed = re.sub(RegEx.LINKED_FILE, output, embed_format)
# Get the list of inputs
files = stack[output]
# Map output to the deploy directory
output = Config.DEPLOY_PATH + '/' + output
# If the block isn't empty
if files:
# Write in the embed tag after any indentation
sys.stdout.write(embed)
# For targets that haven't been processed
if not output in processed:
if files:
if process in Tools.API:
# Queue this block for processing
queue.append({
'process': process,
'output': output,
'files': files
})
# Mark this target processed
processed.append(output)
else:
# Warn about empty blocks
console.info('Empty %s block found in "%s" on line %s' % (process, target, open_line_num))
else:
if process == 'show':
# Write the line back in
sys.stdout.write(line)
elif output:
# Extract path from linked resource
path = RegEx.LINKED_FILE.search(line)
if path:
# This line is now the embed template
embed_format = line
if path:
# Remap the path to deploy directory
path_deploy = Config.DEPLOY_PATH + '/' + path.group(1)
# Store the path in this blocks list
stack[output].append(path_deploy)
# Remove processed files after build
if not path_deploy in redundancies:
redundancies.append(path_deploy)
else:
# Does a block start on this line
block_open = RegEx.BLOCK_START.match(line)
if block_open:
# Store the current line number
open_line_num = line_num
# The process to perform in this file list
process = block_open.group(1)
if block_open.group(3):
# The path of the output
output = block_open.group(3)
# Trim whitespace
output = output.strip()
else:
output = None
# We haven't processed this file yet, so collect it's contents
stacking = True
# Create a new list of collected files
stack[output] = []
else:
# Keep line content unmodified
sys.stdout.write(line)
fileinput.close()
# Execute all queued tasks
for item in queue:
Utils.callMethod(Tools, item['process'], item['files'], item['output'])
# Delete redundant files
for path in redundancies:
if not path in processed:
if os.path.exists(path):
os.remove(path)
# Remove files matching a certain pattern
for target in Utils.findFiles(Config.DEPLOY_PATH, re.compile(Config.REMOVE)):
os.remove(target)
# Delete externs
if Config.EXTERNS_FILES:
for extern in Config.EXTERNS_FILES.split(','):
extern = os.path.join(Config.DEPLOY_PATH, extern)
if os.path.exists(extern):
os.remove(extern)
# Remove empty directories, deepest first
for path, dirs, files in os.walk(Config.DEPLOY_PATH, topdown=False):
# If there are no files or directories
if len(dirs) < 1 and len(files) < 1:
# Remove it
os.rmdir(path)
console.info('\nBuild complete!\n')
# --------------------------------------------------
#
# Define API
#
# --------------------------------------------------
install = staticmethod(install)
config = staticmethod(config)
build = staticmethod(build)
init = staticmethod(init)
help = staticmethod(help)
# --------------------------------------------------
#
# Start
#
# --------------------------------------------------
# Compile regex
RegEx.compile()
# Setup a console
console = logging.getLogger()
channel = logging.StreamHandler()
console.setLevel(logging.DEBUG)
console.addHandler(channel)
# Grab command line arguments
arguments = sys.argv[1:]
if len(arguments) >= 1:
# Method is first argument
task = arguments[0].lower()
# Configure with remaining args
Config.init(arguments[1:])
# Call Springboard task, if it exists
if not Utils.callMethod(Springboard, task):
# If call fails, show help
Springboard.help()
else:
# Show help if no arguments were provided
Springboard.help()