pfirsich / makelove

A build tool for löve games
MIT License
140 stars 12 forks source link

Open project after exporting -- improve iteration on lovejs builds #12

Open idbrii opened 3 years ago

idbrii commented 3 years ago

Since lovejs seems to be tricky to get working, it would be nice to have a better flow for testing it.

Currently:

  1. modify project to try to fix
  2. makelove
  3. unzip output zip file
  4. python -m http.server
  5. open served page, test
  6. repeat

lovejs doesn't have an "artifacts" option for creating unzipped packages.

In theory, you could do something like this:

[hooks]
postbuild = [
    "unzip {build_directory}/lovejs/projectname-lovejs.zip -d {build_directory}/lovejs",
    "python -m http.server 8000 --bind 127.0.0.1 --directory {build_directory}/lovejs/projectname",
]

However, I've found that when makelove executes commands it doesn't seem to have the same PATH as where makelove is executed from, so unzip.exe and python.exe are not in the PATH (I'm on Win10 and both are installed via scoop). Using absolute paths fixes unzip, but I cannot get python to execute -- it always fails with "Fatal Python error: _Py_HashRandomization_Init: failed to get random numbers to initialize Python".

However, since makelove is python, these steps should be doable entirely internally. An --open command that would launch the executable for desktop platforms and unzip and open an http.server for lovejs.

idbrii commented 3 years ago

14 fixes that Fatal Python error, and it allows me to use this config to improve testing:

postbuild = [
    "unzip {build_directory}/lovejs/projectname-lovejs.zip -d {build_directory}/lovejs",
    "start http://127.0.0.1:8000", # use open or xdg-open on macos/linux
    "python -m http.server 8000 --bind 127.0.0.1 --directory {build_directory}/lovejs/projectname",
]

Iteration is now much quicker:

  1. modify project to try to fix
  2. makelove
  3. test in opened page
  4. repeat

An --open flag would be nice to make this more accessible, but this workaround works well.

pfirsich commented 3 years ago

What exactly would that --open flag do? Maybe there should be a switch like --run that simply executes commands in a new kind of optional postbuild action. Did you have something like that in mind? If makelove was Linux only, I would absolutely just suggest to use a Makefile/shell wrapper for that, but the fact that most people probably don't have make installed on their Windows machines and don't like to mess around with batch or powershell files, I think that might be a good addition.

idbrii commented 3 years ago

Passing --open would do different steps depending on the requested config. Not sure how it should interact with multiple targets. This is something that could be done from postbuild, but testing makelove's output seems relevant enough to include in the project.

desktop

unzip the package and do subprocess.run(path_to_executable). I think this should work for all platforms?

lovejs

unzip the package and do:

import http.server
import socketserver
import webbrowser
import os

PORT = 8000

# open first because serve_forever blocks
webbrowser.open(f'http://127.0.0.1:{PORT}')

# SimpleHTTPRequestHandler hosts cwd
os.chdir("{build_directory}/lovejs/{projectname}".format(**get_makelove_params())) # TODO

Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("serving at port", PORT)
    try:
        httpd.serve_forever()
    except Exception:
        httpd.shutdown()