#!/usr/bin/env python3
"""Launch a disposable standalone or isolated IntelliJ fixture host (macOS development)."""
import argparse
import os
import shutil
from pathlib import Path
import subprocess
import sys
import tempfile
import zipfile
from xml.sax.saxutils import escape
ROOT = Path(__file__).resolve().parent.parent
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("host", choices=["standalone","intellij"])
p.add_argument("--java-home", type=Path, required=True)
p.add_argument("--target-java-home", type=Path, required=True)
p.add_argument("--idea-home", type=Path)
p.add_argument("--check", action="store_true", help="Install the disposable IDE qualification companion")
p.add_argument("--mode", choices=["classpath","modulepath"], default="classpath")
args = p.parse_args()
base = Path(tempfile.mkdtemp(prefix="neitzel-designer-host-")).resolve()
project = base / "project"
subprocess.run([sys.executable, ROOT / "scripts/prepare-fixture-host.py", "--destination", project,
"--target-java-home", args.target_java_home, "--mode", args.mode], check=True)
settings = project / ".neitzel-fx-designer/host.properties"
file = project / "app/src/main/resources/fixture/components/Person.fxml"
env = dict(os.environ)
if args.host == "standalone":
cp = os.pathsep.join([str(ROOT / "designer-standalone/target/classes"),
(ROOT / "designer-standalone/target/smoke-classpath.txt").read_text().strip()])
command = [str(args.java_home / "bin/java"), "--enable-native-access=ALL-UNNAMED", "-cp", cp,
"de.neitzel.fxdesigner.standalone.Launcher", str(settings), str(file)]
else:
if args.idea_home is None:
p.error("--idea-home is required for IntelliJ")
plugin = next((ROOT / "designer-intellij/build/distributions").glob("*.zip"))
with zipfile.ZipFile(plugin) as archive:
archive.extractall(base / "plugins")
if args.check:
classes = base / "check-classes"
classes.mkdir()
checks = base / "plugins/qualification/lib/projection-checks.jar"
checks.parent.mkdir(parents=True)
shutil.copyfile(ROOT / "designer-kit-ui/target/designer-kit-ui-0.1.0-SNAPSHOT-tests.jar", checks)
jars = [*(args.idea_home / "lib").glob("*.jar"), *(base / "plugins").rglob("*.jar")]
subprocess.run([str(args.java_home / "bin/javac"), "-cp", os.pathsep.join(map(str,jars)), "-d", str(classes),
str(ROOT / "integration-tests/idea/WorkflowProbe.java")], check=True)
meta = classes / "META-INF"
meta.mkdir()
(meta / "plugin.xml").write_text('de.neitzel.fxdesigner.qualificationDesigner workflow qualification1Local testscom.intellij.modules.platformde.neitzel.fxdesigner')
jar = base / "plugins/qualification/lib/qualification.jar"
jar.parent.mkdir(parents=True, exist_ok=True)
subprocess.run([str(args.java_home / "bin/jar"), "cf", str(jar), "-C", str(classes), "."], check=True)
options = base / "config/options"
options.mkdir(parents=True)
(options / "ide.general.xml").write_text('')
(options / "trusted-paths.xml").write_text('')
props = base / "idea.properties"
props.write_text("\n".join("idea."+name+".path="+str(base/name) for name in ["config","system","plugins","log"])+"\nidea.initially.ask.config=false\n")
vm = base / "idea.vmoptions"
vm.write_text((args.idea_home / "bin/idea.vmoptions").read_text()+"\n--enable-native-access=ALL-UNNAMED\n-Dneitzel.designer.launch="+str(settings)+"\n-Ddesigner.check.output="+str(base)+"\n")
env.update(IDEA_PROPERTIES=str(props), IDEA_VM_OPTIONS=str(vm), IDEA_JDK=str(args.java_home))
command = [str(args.idea_home / "MacOS/idea"), str(project), str(file)]
with (base / "launcher.log").open("w") as log:
process = subprocess.Popen(command, env=env, stdout=log, stderr=subprocess.STDOUT, start_new_session=True)
(base / "host.pid").write_text(str(process.pid))
print("Host PID:", process.pid)
print("Disposable host:", base)
print("FXML:", file)