import os as _os import argparse as _argparse import lib.string as _string import lib.command as _command import lib.execute as _execute def main(): ## args argument_parser = _argparse.ArgumentParser( prog = "laravel-steer", ) argument_parser.add_argument( "action", type = str, choices = [ "selftest", "clear", "build", "init", "run", "logs", ], metavar = "", ) argument_parser.add_argument( "-v", "--verbose", action = "store_true", default = False, ) argument_parser.add_argument( "-q", "--dry-run", action = "store_true", default = False, ) argument_parser.add_argument( "-s", "--source-directory", type = str, default = None, metavar = "", ) argument_parser.add_argument( "-b", "--build-directory", type = str, default = None, metavar = "", ) argument_parser.add_argument( "-u", "--unlock", action = "store_true", default = False, help = "do not copy composer.lock", ) argument_parser.add_argument( "-x", "--exclude", type = str, action = "append", default = None, metavar = "", dest = "excludes", ) argument_parser.add_argument( "-e", "--env-path", type = str, default = None, metavar = "", ) argument_parser.add_argument( "-p", "--port", type = int, default = 8000, metavar = "", ) args = argument_parser.parse_args() ## exec if (args.action == "selftest"): print( _command.directory_create( "/tmp/foo" ) ) elif (args.action == "clear"): if (args.build_directory is None): raise ValueError("build directory required") else: _execute.execute( _command.remove(args.build_directory), { "show": args.verbose, "run": (not args.dry_run), } ) elif (args.action == "build"): if (args.source_directory is None): raise ValueError("source directory required") else: if (args.build_directory is None): raise ValueError("build directory required") else: ## create build directory if True: _execute.execute( _command.directory_create(args.build_directory), { "show": args.verbose, "run": (not args.dry_run), } ) ## unlock if True: if (not args.unlock): pass else: _execute.execute( _command.remove( _os.path.join(args.build_directory, "composer.lock") ) ) ## copy files if True: _execute.execute( _command.rsync( args.source_directory, args.build_directory, { "verbose": args.verbose, "excludes": ( [ ".editorconfig", ".env.example", ".env", "vendor/", "storage/", "_ide_helper.php", ] + ( ["composer.lock"] if args.unlock else [] ) + (args.excludes or []) ), } ), { "show": args.verbose, "run": (not args.dry_run), } ) ## set up storage if True: for name in ["sessions", "cache", "views"]: _execute.execute( # TODO: {"name": "group", "value": "www-data"}, _command.generic( "install", [ {"name": "directory"}, {"name": "mode", "value": "777"}, {"value": _os.path.join(args.build_directory, "storage", "framework", name)}, ] ), { "directory": args.build_directory, "show": args.verbose, "run": (not args.dry_run), } ) ## place env file if True: if (args.env_path is None): pass else: _execute.execute( _command.copy( args.env_path, _os.path.join(args.build_directory, ".env"), ), { "show": args.verbose, "run": (not args.dry_run), } ) elif (args.action == "init"): if (args.build_directory is None): raise ValueError("build directory required") else: ## install composer packages if True: _execute.execute( _command.generic( "composer", [ {"value": "install"}, ] ), { "directory": args.build_directory, "show": args.verbose, "run": (not args.dry_run), } ) ## run migrations if True: _execute.execute( _command.generic( "php", [ {"value": "artisan"}, {"value": "migrate"}, ] ), { "directory": args.build_directory, "show": args.verbose, "run": (not args.dry_run), } ) elif (args.action == "run"): if (args.port is None): raise ValueError("port required") else: _execute.execute( _command.generic( "php", [ {"value": "artisan"}, {"value": "serve"}, {"name": "host", "value": "0.0.0.0"}, {"name": "port", "value": ("%u" % args.port)}, ] ), { "directory": args.build_directory, "show": args.verbose, "run": (not args.dry_run), } ) elif (args.action == "logs"): if (args.build_directory is None): raise ValueError("build directory required") else: _execute.execute( _command.generic( "tail", [ {"name": "follow"}, {"value": "storage/logs/laravel.log"}, ] ), { "directory": args.build_directory, "show": args.verbose, "run": (not args.dry_run), } ) else: raise ValueError( _string.coin( "invalid action: {{action}}", { "action": args.action, } ) )