Files
plagin_tokens/plagin_tokens/release/package.sh
T
Andrew Emelyaneko 9f10b4aab3
Release Candidates / linux-package (push) Failing after 0s
Release Candidates / macos-package (push) Canceled after 0s
feat: port plagin-tokens to the OpenCode 2 plugin API
- Export a V2 plugin definition (Plugin.define shape) via a root index.ts
  entrypoint and switch the devDependency to @opencode/plugin 2.2.5.
- Convert the V1 hook object to setup(ctx) with ctx.event.subscribe() and
  return a cleanup function instead of dispose().
- Track OpenCode 2 streaming events (session.text.delta,
  session.reasoning.delta, session.step.started/ended, session.execution.*)
  alongside the legacy message.* events, accumulating multi-step output per
  assistant message and de-duplicating replayed durable events.
- Drop client.app.log in favour of console logging; keep the shared HTTP
  server and macOS HUD refcounting intact.
- Ship installers (tar.gz, deb, rpm, dmg) as a plugin directory instead of a
  single .ts symlink, which OpenCode 2 ignores.
- Add v2 routeEvent tests; bump to 0.2.0.
2026-11-02 13:51:21 +03:00

165 lines
5.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
REPO_ROOT="$(git -C "$ROOT" rev-parse --show-toplevel)"
PLUGIN_DIR="$REPO_ROOT/plagin_tokens"
VERSION="${VERSION:-}"
if [[ -z "${VERSION}" ]]; then
VERSION="$(sed -n 's/.*"version":[[:space:]]*"\([^"]*\)".*/\1/p' "$PLUGIN_DIR/package.json" | head -n 1)"
fi
if [[ -z "${VERSION}" ]]; then
echo "Could not determine package version" >&2
exit 1
fi
OS_NAME="$(uname -s)"
RELEASE_DIR="$PLUGIN_DIR/_release/$VERSION"
BASE_NAME="plagin-tokens-${VERSION}"
DEB_NAME="plagin-tokens_${VERSION}_all.deb"
ARCHIVE_NAME="${BASE_NAME}.tar.gz"
DMG_NAME="${BASE_NAME}.dmg"
rm -rf "$RELEASE_DIR"
mkdir -p "$RELEASE_DIR"
echo "Verifying TypeScript/Bun..."
(cd "$PLUGIN_DIR" && bun install --frozen-lockfile >/dev/null 2>&1 || bun install)
(cd "$PLUGIN_DIR" && bun test && bun run typecheck)
echo "Preparing source staging directory..."
SOURCE_STAGE="$RELEASE_DIR/${BASE_NAME}"
SOURCE_TMP="$RELEASE_DIR/.source-tree"
rm -rf "$SOURCE_TMP" "$SOURCE_STAGE"
mkdir -p "$SOURCE_TMP" "$SOURCE_STAGE"
git -C "$REPO_ROOT" archive --format=tar HEAD -- plagin_tokens \
| tar -C "$SOURCE_TMP" -xf -
if [[ ! -d "$SOURCE_TMP/plagin_tokens" ]]; then
echo "Expected plagin_tokens directory not found in git archive" >&2
exit 1
fi
mv "$SOURCE_TMP/plagin_tokens"/* "$SOURCE_STAGE"/
rm -rf "$SOURCE_TMP"
mkdir -p "$SOURCE_STAGE/release"
install -m 0755 "$ROOT/release/install-opencode-plugin.sh" "$SOURCE_STAGE/release/"
echo "Building ${ARCHIVE_NAME}..."
tar -C "$RELEASE_DIR" -czf "$RELEASE_DIR/$ARCHIVE_NAME" "$(basename "$SOURCE_STAGE")"
# Create a Debian-compatible all-architecture package wrapper.
echo "Building ${DEB_NAME}..."
DEB_ROOT="$RELEASE_DIR/deb/plagin-tokens_${VERSION}_all"
rm -rf "$DEB_ROOT"
mkdir -p "$DEB_ROOT/DEBIAN"
mkdir -p "$DEB_ROOT/opt/plagin-tokens"
mkdir -p "$DEB_ROOT/usr/local/bin"
mkdir -p "$DEB_ROOT/usr/share/doc/plagin-tokens"
mkdir -p "$DEB_ROOT/etc/skel/.config/opencode/plugins"
cp -R "$SOURCE_STAGE"/. "$DEB_ROOT/opt/plagin-tokens/"
install -m 0755 "$ROOT/release/install-opencode-plugin.sh" "$DEB_ROOT/usr/local/bin/plagin-tokens-install"
install -m 0644 "$PLUGIN_DIR/README.md" "$DEB_ROOT/usr/share/doc/plagin-tokens/README.md"
ln -sf /opt/plagin-tokens \
"$DEB_ROOT/etc/skel/.config/opencode/plugins/plagin-tokens"
INSTALLED_SIZE_KB="$(du -sk "$DEB_ROOT" | cut -f1)"
cat > "$DEB_ROOT/DEBIAN/control" <<EOF
Package: plagin-tokens
Version: ${VERSION}
Section: utils
Priority: optional
Architecture: all
Maintainer: Andrew Emelyanenko <andrew@gitea.andre-y-ru.ru>
Installed-Size: ${INSTALLED_SIZE_KB}
Homepage: https://gitea.andre-y-ru.ru/andrew/plagin_tokens
Description: Live token speed indicator for opencode web
This plugin tracks opencode's streaming responses, exposes token throughput
via a local HTTP server, and provides a floating macOS HUD and static
overlay. Run plagin-tokens-install after package installation.
EOF
cat > "$DEB_ROOT/DEBIAN/postinst" <<'EOF'
#!/bin/sh
set -e
chmod 0755 /usr/local/bin/plagin-tokens-install
echo "plagin-tokens is installed; run plagin-tokens-install as a normal user."
EOF
chmod 0755 "$DEB_ROOT/DEBIAN/postinst"
printf '%s\n' '2.0' > "$RELEASE_DIR/debian-binary"
cat > "$RELEASE_DIR/control-file-list.txt" <<'EOF'
control
postinst
EOF
# Build control archive.
tar -C "$DEB_ROOT/DEBIAN" -czf "$RELEASE_DIR/control.tar.gz" control postinst
# Build a deterministic-ish data archive excluding DEBIAN.
(cd "$DEB_ROOT" && find . -path './DEBIAN' -prune -o -type f -print -o -type l -print | sort -u > "$RELEASE_DIR/data-file-list.txt")
tar -C "$DEB_ROOT" -czf "$RELEASE_DIR/data.tar.gz" -T "$RELEASE_DIR/data-file-list.txt"
ar rc "$RELEASE_DIR/$DEB_NAME" \
"$RELEASE_DIR/debian-binary" \
"$RELEASE_DIR/control.tar.gz" \
"$RELEASE_DIR/data.tar.gz"
# macOS-specific package imaging.
if [[ "$OS_NAME" == "Darwin" ]]; then
echo "Building ${DMG_NAME}..."
DMG_ROOT="$RELEASE_DIR/dmg"
rm -rf "$DMG_ROOT"
mkdir -p "$DMG_ROOT/plagin-tokens"
cp -R "$SOURCE_STAGE"/. "$DMG_ROOT/plagin-tokens/"
cat > "$DMG_ROOT/INSTALL.command" <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
ROOT="$(cd "$(dirname "$0")" && pwd)"
SOURCE_DIR="$ROOT/plagin-tokens"
OPT_TARGET="/opt/plagin-tokens"
USER_CONFIG="$HOME/.config/opencode/plugins"
TARGET_DIR="$USER_CONFIG/plagin-tokens"
LEGACY_FILE="$USER_CONFIG/plagin-tokens.ts"
if [[ ! -d "$SOURCE_DIR" ]]; then
echo "plagin-tokens source directory is not found in the mounted image" >&2
exit 1
fi
sudo rm -rf "$OPT_TARGET"
sudo mkdir -p "/opt"
sudo cp -R "$SOURCE_DIR" "$OPT_TARGET"
mkdir -p "$TARGET_DIR"
cat > "$TARGET_DIR/index.ts" <<PLUGIN_ENTRY
// Generated by plagin-tokens INSTALL.command. Do not edit.
export { default } from "$OPT_TARGET/index.ts"
PLUGIN_ENTRY
# OpenCode 1 installs linked a single .ts file; OpenCode 2 skips those.
rm -f "$LEGACY_FILE"
echo "Installed OpenCode plugin directory at:"
echo " $TARGET_DIR -> $OPT_TARGET"
echo "Restart opencode to load the plugin."
EOF
chmod 0755 "$DMG_ROOT/INSTALL.command"
rm -f "$RELEASE_DIR/$DMG_NAME"
hdiutil create \
-srcfolder "$DMG_ROOT" \
-volname "plagin-tokens-${VERSION}" \
-format UDZO \
-fs HFS+ \
"$RELEASE_DIR/$DMG_NAME" >/dev/null
fi
echo "Products:"
find "$RELEASE_DIR" -maxdepth 1 -type f -name 'plagin-tokens*' -print