#!/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")
p.add_argument("--archiver", type=Path, help="Use a disposable copy of the Archiver project")
p.add_argument("--connection-form", action="store_true", help="Open and qualify the Archiver connection form")
args = p.parse_args()
if args.connection_form and not args.archiver:
p.error("--connection-form requires --archiver")
base = Path(tempfile.mkdtemp(prefix="neitzel-designer-host-")).resolve()
project = base / "project"
if args.archiver:
if args.host != "intellij" or args.mode != "classpath":
p.error("Archiver requires the IntelliJ classpath configuration")
shutil.copytree(args.archiver, project, ignore=shutil.ignore_patterns(".git", ".idea", "target", ".neitzel-fx-designer"))
subprocess.run([sys.executable, ROOT / "scripts/configure-library-example.py", "--project", project,
"--target-java-home", args.target_java_home], check=True)
view = "ConnectionForm" if args.connection_form else "ArchiveWindow"
file = project / f"src/main/resources/de/neitzel/archiver/ui/{view}.fxml"
else:
subprocess.run([sys.executable, ROOT / "scripts/prepare-fixture-host.py", "--destination", project,
"--target-java-home", args.target_java_home, "--mode", args.mode], check=True)
file = project / "app/src/main/resources/fixture/components/Person.fxml"
settings = project / ".neitzel-fx-designer/host.properties"
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"), *(args.idea_home / "plugins/platform-problemView-plugin").rglob("*.jar")]
sources = ROOT / "integration-tests/idea"
probe = "ArchiveWorkflowProbe" if args.archiver else "WorkflowProbe"
if args.connection_form:
probe = "ConnectionWorkflowProbe"
files = [sources / "WorkflowProbe.java"]
if args.archiver:
files.append(sources / f"{probe}.java")
target_probe = "ConnectionTargetProbe" if args.connection_form else "ArchiveTargetProbe"
agent_classes = base / "agent-classes"
agent_classes.mkdir()
subprocess.run([args.java_home / "bin/javac", "-cp", os.pathsep.join(map(str,jars)), "-d", agent_classes,
sources / f"{target_probe}.java"], check=True)
agent = base / "archiver-ui-check.jar"
with zipfile.ZipFile(agent, "w") as archive:
archive.writestr("META-INF/MANIFEST.MF", f"Manifest-Version: 1.0\nPremain-Class: qualification.{target_probe}\n\n")
for item in agent_classes.rglob("*.class"):
archive.write(item, item.relative_to(agent_classes))
env["JAVA_TOOL_OPTIONS"] = env.get("JAVA_TOOL_OPTIONS", "") + f' "-javaagent:{agent}={base}"'
subprocess.run([str(args.java_home / "bin/javac"), "-cp", os.pathsep.join(map(str,jars)), "-d", str(classes),
*files], check=True)
meta = classes / "META-INF"
meta.mkdir()
(meta / "plugin.xml").write_text('de.neitzel.fxdesigner.qualificationDesigner workflow qualification1Local testscom.intellij.modules.platformde.neitzel.fxdesignercom.intellij.problemsView')
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)