Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 103 lines (83 sloc) 3.194 kb
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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
from optparse import OptionParser

""" Usage: python build.py [-acemnrRstu] [-AgGMj] [-h|--help] """

src_dir = 'src'
build_dir = 'build'
dist_dir = 'dist'

douban_js = '%s/jquery.douban.js' % dist_dir
min_js = '%s/jquery.douban.min.js' % dist_dir
version = 'VERSION'

core = ['core', 'utils', 'parser', 'client']

services = ['collection', 'event', 'miniblog', 'note', 'recommendation', \
            'review', 'subject', 'tag', 'user']

handlers = ['jquery', 'gears', 'gadget', 'greasemonkey']

options = (
    ('-a', '--all', 'include all services'),
    ('-c', '--collection', 'include collection service'),
    ('-e', '--event', 'include event service'),
    ('-m', '--miniblog', 'include miniblog service'),
    ('-n', '--note', 'include note service'),
    ('-r', '--recommendation', 'include recommendation service'),
    ('-R', '--review', 'include review service'),
    ('-s', '--subject', 'include subject service'),
    ('-t', '--tag', 'include tag service'),
    ('-u', '--user', 'include user service'),

    ('-A', '--all-handlers', 'include all handlers'),
    ('-g', '--gadget', 'include gadget handler'),
    ('-G', '--gears', 'include gears handler'),
    ('-M', '--greasemonkey', 'include greasemonkey handler'),
    ('-j', '--jquery', 'include jquery handler'),
)

def main():
    my_services = []
    my_handlers = []
    my_files = []

    parser = OptionParser()
    for short, long, help in options:
        parser.add_option(long, short, action='store_true',
            default=False, help=help)
    opts, args = parser.parse_args()

    if opts.all:
        my_services = services
    else:
        for service in services:
            if getattr(opts, service):
                my_services.append(service)
        if not my_services:
            my_services = services

    if opts.all_handlers:
        my_handlers = handlers
    else:
        for handler in handlers:
            if getattr(opts, handler):
                my_handlers.append(handler)
        if not my_handlers:
            my_handlers = handlers
    my_handlers = ['%s_handler' % handler for handler in my_handlers]
    
    my_files = ['intro'] + core + my_services + my_handlers + ['outro']

    print "Files need to build...\n%s" % ", ".join(my_files)

    my_path = ['%s/%s.js' % (src_dir, file) for file in my_files]

    try:
        os.mkdir(dist_dir)
    except OSError:
        # Directory exists
        pass

    # Build js
    print "\nBuilding..."
    try:
        os.system("cat %s | sed s/@VERSION/`cat %s`/ > %s" % \
                  (" ".join(my_path), version, douban_js))
        print "Built: %s" % douban_js

        print "\nMinifying..."
        try:
            os.system("java -jar %s/js.jar %s/build/min.js %s %s" % \
                      (build_dir, build_dir, douban_js, min_js))
            print "Minified: %s" % min_js
        except:
            print "Failed to minifiy: %s" % min_js
    except:
        print "Failed to build: %s" % douban_js

if __name__ == '__main__':
    main()
Something went wrong with that request. Please try again.