# -*- coding: utf-8 -*-
import os
import sys
import webbrowser

from invoke import task, run

docs_dir = 'docs'
build_dir = os.path.join(docs_dir, '_build')

@task
def test(watch=False, last_failing=False):
    """Run the tests.

    Note: --watch requires pytest-xdist to be installed.
    """
    import pytest
    flake()
    args = []
    if watch:
        args.append('-f')
    if last_failing:
        args.append('--lf')
    retcode = pytest.main(args)
    sys.exit(retcode)

@task
def flake():
    """Run flake8 on codebase."""
    run('flake8 .', echo=True)

@task
def clean():
    run("rm -rf build")
    run("rm -rf dist")
    run("rm -rf aiohttp_utils.egg-info")
    clean_docs()
    print("Cleaned up.")

@task
def clean_docs():
    run("rm -rf %s" % build_dir, echo=True)

@task
def browse_docs():
    path = os.path.join(build_dir, 'index.html')
    webbrowser.open_new_tab(path)

@task
def docs(clean=False, browse=False, watch=False):
    """Build the docs."""
    if clean:
        clean_docs()
    run("sphinx-build %s %s" % (docs_dir, build_dir), echo=True)
    if browse:
        browse_docs()
    if watch:
        watch_docs()

@task
def watch_docs():
    """Run build the docs when a file changes."""
    try:
        import sphinx_autobuild  # noqa
    except ImportError:
        print('ERROR: watch task requires the sphinx_autobuild package.')
        print('Install it with:')
        print('    pip install sphinx-autobuild')
        sys.exit(1)
    run('sphinx-autobuild {0} {1} --watch {2}'.format(
        docs_dir, build_dir, 'aiohttp_utils'), echo=True, pty=True)

@task
def readme(browse=False):
    run('rst2html.py README.rst > README.html')
    if browse:
        webbrowser.open_new_tab('README.html')

@task
def publish(test=False):
    """Publish to the cheeseshop."""
    clean()
    if test:
        run('python setup.py register -r test sdist bdist_wheel', echo=True)
        run('twine upload dist/* -r test', echo=True)
    else:
        run('python setup.py register sdist bdist_wheel', echo=True)
        run('twine upload dist/*', echo=True)

@@ -84,13 +84,8 @@
 task
-, run
 %0A%0Ado
@@ -161,16 +161,21 @@
 ef test(
+ctx, 
 watch=Fa
@@ -316,16 +316,19 @@
   flake(
+ctx
 )%0A    ar
@@ -495,16 +495,19 @@
 f flake(
+ctx
 ):%0A    %22
@@ -535,24 +535,28 @@
 ase.%22%22%22%0A    
+ctx.
 run('flake8 
@@ -587,23 +587,30 @@
 f clean(
+ctx
 ):%0A    
+ctx.
 run(%22rm 
@@ -621,24 +621,28 @@
 build%22)%0A    
+ctx.
 run(%22rm -rf 
@@ -648,24 +648,28 @@
  dist%22)%0A    
+ctx.
 run(%22rm -rf 
@@ -704,24 +704,27 @@
  clean_docs(
+ctx
 )%0A    print(
@@ -760,23 +760,30 @@
 an_docs(
+ctx
 ):%0A    
+ctx.
 run(%22rm 
@@ -801,27 +801,16 @@
 uild_dir
-, echo=True
 )%0A%0A@task
@@ -822,24 +822,27 @@
 browse_docs(
+ctx
 ):%0A    path 
@@ -916,16 +916,155 @@
 (path)%0A%0A
+def build_docs(ctx, browse):%0A    ctx.run(%22sphinx-build %25s %25s%22 %25 (docs_dir, build_dir), echo=True)%0A    if browse:%0A        browse_docs(ctx)%0A%0A
 @task%0Ade
@@ -1070,16 +1070,21 @@
 ef docs(
+ctx, 
 clean=Fa
@@ -1179,86 +1179,72 @@
 ocs(
+ctx
 )%0A    
-run(%22sphinx-build %25s %25s%22 %25 (docs_dir, build_dir), echo=True)%0A    if brow
+if watch:%0A        watch_docs(ctx, browse=browse)%0A    el
 se:%0A
@@ -1256,54 +1256,36 @@
    b
-rowse
+uild
 _docs(
-)%0A    if watch:%0A        watch_docs(
+ctx, browse=browse
 )%0A%0A@
@@ -1304,16 +1304,33 @@
 ch_docs(
+ctx, browse=False
 ):%0A    %22
@@ -1625,24 +1625,28 @@
 exit(1)%0A    
+ctx.
 run('sphinx-
@@ -1667,36 +1667,81 @@
 %7B1%7D 
---watch %7B2%7D'.format(%0A       
+%7B2%7D -z aiohttp_utils'.format(%0A        '--open-browser' if browse else '',
  doc
@@ -1760,25 +1760,8 @@
 _dir
-, 'aiohttp_utils'
 ), e
@@ -1798,16 +1798,21 @@
  readme(
+ctx, 
 browse=F
@@ -1818,24 +1818,28 @@
 False):%0A    
+ctx.
 run('rst2htm
@@ -1951,16 +1951,21 @@
 publish(
+ctx, 
 test=Fal
@@ -2016,16 +2016,19 @@
   clean(
+ctx
 )%0A    if
@@ -2034,32 +2034,36 @@
 f test:%0A        
+ctx.
 run('python setu
@@ -2115,32 +2115,36 @@
 o=True)%0A        
+ctx.
 run('twine uploa
@@ -2187,24 +2187,28 @@
 se:%0A        
+ctx.
 run('python 
@@ -2264,16 +2264,20 @@
         
+ctx.
 run('twi

Update invoke tasks for compat with newer versions