Пример #1
0
Файл: app.py Проект: hashin/h
def configure_jinja2_assets(config):
    assets_env = assets.Environment('/assets', 'h/assets.ini',
                                    'build/manifest.json')
    jinja2_env = config.get_jinja2_environment()
    jinja2_env.globals['asset_urls'] = assets_env.urls
Пример #2
0
def build_extension(args):
    """
    Build the Chrome or Firefox extensions.

    You can supply the base URL of an h installation with which this extension
    will communicate, such as "http://localhost:5000" when developing locally or
    "https://hypothes.is" to talk to the production Hypothesis application.
    """
    service_url = args.service_url
    if not service_url.endswith('/'):
        service_url = '{}/'.format(service_url)

    build_dir = 'build/' + args.browser
    public_dir = os.path.join(build_dir, 'public')

    # Prepare a fresh build.
    clean(build_dir)
    os.makedirs(build_dir)
    os.makedirs(public_dir)

    # Bundle the extension assets.
    copytree('h/browser/chrome/content', os.path.join(build_dir, 'content'))
    copytree('h/browser/chrome/help', os.path.join(build_dir, 'help'))
    copytree('h/browser/chrome/images', os.path.join(build_dir, 'images'))
    copytree('h/static/images', os.path.join(public_dir, 'images'))
    copytree('h/static/styles/vendor/fonts', os.path.join(public_dir, 'fonts'))

    extension_sources = ['extension.bundle.js']
    if args.debug:
        extension_sources.extend([x + '.map' for x in extension_sources])

    client_sources = []
    env = assets.Environment('/public', 'h/assets.ini', 'build/manifest.json')
    for bundle in ['app_js', 'app_css', 'inject_js', 'inject_css']:
        client_sources.extend(env.files(bundle))
    if args.debug:
        client_sources.extend([x + '.map' for x in client_sources])

    try:
        copyfilelist(src='build/scripts',
                     dst=os.path.join(build_dir, 'lib'),
                     filelist=extension_sources)
        copyfilelist(src='h/browser/chrome/lib',
                     dst=os.path.join(build_dir, 'lib'),
                     filelist=['options.html', 'options.js'])
        copyfilelist(src='build', dst=public_dir, filelist=client_sources)
        copyfilelist(src='h/browser/chrome/lib',
                     dst=public_dir,
                     filelist=['destroy.js'])
    except MissingSourceFile as e:
        print(
            "Missing source file: {:s}! Have you run `gulp build`?".format(e))
        sys.exit(1)

    # Render the embed code.
    with codecs.open(os.path.join(public_dir, 'embed.js'), 'w', 'utf-8') as fp:
        data = client.render_embed_js(assets_env=env,
                                      app_html_url='/public/app.html')
        fp.write(data)

    # Render the sidebar html
    api_url = '{}api/'.format(service_url)
    with codecs.open(os.path.join(public_dir, 'app.html'), 'w', 'utf-8') as fp:
        data = client.render_app_html(
            api_url=api_url,
            service_url=service_url,
            # Google Analytics tracking is currently not enabled
            # for the extension
            ga_tracking_id=None,
            assets_env=env,
            websocket_url=args.websocket_url,
            sentry_public_dsn=args.sentry_public_dsn)
        fp.write(data)

    # Render the manifest.
    with codecs.open(os.path.join(build_dir, 'manifest.json'), 'w',
                     'utf-8') as fp:
        data = chrome_manifest(script_host_url=None,
                               bouncer_url=args.bouncer_url,
                               browser=args.browser)
        fp.write(data)

    # Write build settings to a JSON file
    with codecs.open(os.path.join(build_dir, 'settings-data.js'), 'w',
                     'utf-8') as fp:
        settings = settings_dict(service_url, api_url, args.sentry_public_dsn)
        fp.write('window.EXTENSION_CONFIG = ' + json.dumps(settings))