def test_it_includes_client_config(self, jinja_env): assets_env = mock.Mock(spec_set=['urls']) template = mock.Mock(spec_set=['render']) jinja_env.get_template.return_value = template render_app_html(assets_env=assets_env, service_url='https://hypothes.is/', api_url='https://hypothes.is/api', sentry_public_dsn='test-sentry-dsn', ga_client_tracking_id='UA-4567', websocket_url='wss://hypothes.is/') expected_config = { 'apiUrl': 'https://hypothes.is/api', 'websocketUrl': 'wss://hypothes.is/', 'serviceUrl': 'https://hypothes.is/', 'release': __version__, 'raven': { 'dsn': 'test-sentry-dsn', 'release': __version__ }, 'googleAnalytics': 'UA-4567' } ((context, ), kwargs) = template.render.call_args actual_config = json.loads(context['app_config']) assert actual_config == expected_config
def _render_app(request, extra={}): request.response.text = client.render_app_html( api_url=request.route_url('api'), service_url=request.route_url('index'), extra=extra, ga_tracking_id=request.registry.settings.get('ga_tracking_id'), sentry_public_dsn=request.sentry.get_public_dsn(), webassets_env=request.webassets_env, websocket_url=request.registry.settings.get('h.websocket_url')) return request.response
def _render_app(request, extra={}): request.response.text = client.render_app_html( api_url=request.route_url("api"), service_url=request.route_url("index"), extra=extra, ga_tracking_id=request.registry.settings.get("ga_tracking_id"), sentry_public_dsn=request.sentry.get_public_dsn(), webassets_env=request.webassets_env, websocket_url=client.websocketize(request.route_url("ws")), ) return request.response
def render_app(request, extra=None): """Render a page that serves a preconfigured annotation client.""" html = client.render_app_html( assets_env=request.assets_env, # FIXME: The '' here is to ensure this has a trailing slash. This seems # rather messy, and is inconsistent with the rest of the application's # URLs. api_url=request.route_url('api.index'), service_url=request.route_url('index'), ga_tracking_id=request.registry.settings.get('ga_tracking_id'), sentry_public_dsn=request.sentry.get_public_dsn(), websocket_url=request.registry.settings.get('h.websocket_url'), extra=extra) request.response.text = html return request.response
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))
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))
def build_chrome(args): """ Build the Chrome extension. 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) webassets_env = get_webassets_env(base_dir='./build/chrome/public', assets_url='/public', debug=args.debug) # Prepare a fresh build. clean('build/chrome') os.makedirs('build/chrome') content_dir = webassets_env.directory os.makedirs(content_dir) # Bundle the extension assets. copytree('h/browser/chrome/content', 'build/chrome/content') copytree('h/browser/chrome/help', 'build/chrome/help') copytree('h/browser/chrome/images', 'build/chrome/images') copytree('h/static/images', 'build/chrome/public/images') os.makedirs('build/chrome/lib') subprocess_args = ['node_modules/.bin/browserify', 'h/browser/chrome/lib/extension.js', '--outfile', 'build/chrome/lib/extension-bundle.js'] if args.debug: subprocess_args.append('--debug') subprocess.call(subprocess_args) # Render the sidebar html api_url = '{}api/'.format(service_url) if args.bundle_sidebar: build_extension_common(webassets_env, service_url, bundle_app=True) with codecs.open(content_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, webassets_env=webassets_env, websocket_url=args.websocket_url, sentry_public_dsn=args.sentry_public_dsn) fp.write(data) else: build_extension_common(webassets_env, service_url) # Render the manifest. with codecs.open('build/chrome/manifest.json', 'w', 'utf-8') as fp: script_url = urlparse.urlparse(webassets_env.url) if script_url.scheme and script_url.netloc: script_host_url = '{}://{}'.format(script_url.scheme, script_url.netloc) else: script_host_url = None data = chrome_manifest(script_host_url) fp.write(data) # Write build settings to a JSON file with codecs.open('build/chrome/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))