66 lines
3.3 KiB
Python
66 lines
3.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Install/update dependencies and validate the local Companion module."""
|
|
import argparse
|
|
import datetime
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--node-bin', type=Path, help='Directory containing Node.js 22.20+ and npm')
|
|
parser.add_argument('--companion-config', type=Path, help='Enable developer modules in this launcher config.json; restart Companion afterward')
|
|
parser.add_argument('--package', action='store_true', help='Also build an importable .tgz')
|
|
args = parser.parse_args()
|
|
root = Path(__file__).resolve().parent
|
|
env = os.environ.copy()
|
|
if args.node_bin:
|
|
env['PATH'] = str(args.node_bin.resolve()) + os.pathsep + env.get('PATH', '')
|
|
node = shutil.which('node', path=env.get('PATH'))
|
|
npm = shutil.which('npm', path=env.get('PATH'))
|
|
if not node or not npm:
|
|
parser.error('Install Node.js 22.20+ (22.x) with npm, or pass --node-bin.')
|
|
version = subprocess.check_output([node, '--version'], text=True, env=env).strip()
|
|
parts = version.lstrip('v').split('.')
|
|
if int(parts[0]) != 22 or int(parts[1]) < 20:
|
|
parser.error(f'Found {version}; use Node.js 22.20+ (22.x) for the pinned module build tools.')
|
|
for command in [['ci', '--no-audit', '--no-fund', '--cache', str(root / '.npm-cache')], ['test'], ['run', 'check']]:
|
|
subprocess.run([npm, *command], cwd=root, env=env, check=True)
|
|
if args.package:
|
|
subprocess.run([npm, 'run', 'package'], cwd=root, env=env, check=True)
|
|
dev_path = root.parent
|
|
if args.companion_config:
|
|
config_path = args.companion_config.expanduser().resolve(strict=True)
|
|
config = json.loads(config_path.read_text())
|
|
current = config.get('dev_modules_path')
|
|
if current and Path(current).resolve() != dev_path:
|
|
parser.error(f'Existing developer folder is {current}; refusing to replace it. Add this module there manually.')
|
|
if not config.get('enable_developer') or current != str(dev_path):
|
|
stamp = datetime.datetime.now().strftime('%Y%m%d-%H%M%S-%f')
|
|
backup = config_path.with_name(config_path.name + '.before-mpc-dev-' + stamp)
|
|
shutil.copy2(config_path, backup)
|
|
config.update(enable_developer=True, dev_modules_path=str(dev_path))
|
|
temporary = None
|
|
try:
|
|
with tempfile.NamedTemporaryFile(mode='w', dir=config_path.parent, delete=False) as file:
|
|
temporary = Path(file.name)
|
|
json.dump(config, file, indent=2)
|
|
file.write('\n')
|
|
shutil.copymode(config_path, temporary)
|
|
os.replace(temporary, config_path)
|
|
finally:
|
|
if temporary and temporary.exists():
|
|
temporary.unlink()
|
|
print(f'Launcher configuration updated; backup: {backup}')
|
|
print('Restart Companion to load the developer settings. Existing connections retain their selected version.')
|
|
print(f'Developer modules folder: {dev_path}')
|
|
print('Enable developer modules in the Companion launcher and select the dev version of MPC-HC in Connections.')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|