telegram-webapp-sdk 0.2.14

Telegram WebApp SDK for Rust
# Makefile.toml for cargo-make
# Usage:
#   cargo make                 # pretty help (default)
#   cargo make ci              # format, clippy, tests, package
#   cargo make tag             # create git tag from Cargo.toml version (no push)
#   TAG=v0.3.0 cargo make release
#   CARGO_REGISTRY_TOKEN=... TAG=v0.3.0 cargo make publish

[env]
ALL_FEATURES = "true" # toggle --all-features for clippy/test

# ------- Default task (no root 'default_task' key) -------

[tasks.default]
category = "Meta"
description = "Default task -> help"
run_task = "help"

# ------- Core checks -------

[tasks.format]
category = "Format"
clear = true
description = "Format code using rustfmt on nightly (optional)"
script_runner = "bash"
script = ['cargo +nightly fmt --']

[tasks.clippy]
category = "Lint"
clear = true
script_runner = "bash"
script = ['''
  set -euo pipefail
  if [ "${ALL_FEATURES}" = "true" ]; then
    cargo clippy --workspace --all-targets --all-features -- -D warnings
  else
    cargo clippy --workspace --all-targets -- -D warnings
  fi
''']
description = "Run clippy for all targets; fail on warnings"

[tasks.test]
category = "Test"
clear = true
script_runner = "bash"
script = ['''
  set -euo pipefail
  if [ "${ALL_FEATURES}" = "true" ]; then
    cargo test --workspace --all-features --no-fail-fast
  else
    cargo test --workspace --no-fail-fast
  fi
''']
description = "Run tests (optionally with all features)"

[tasks.package]
category = "Package"
clear = true
command = "cargo"
args = ["package", "--locked"]
description = "Verify that the crate can be packaged cleanly"

[tasks.ci]
category = "Meta"
dependencies = ["format", "clippy", "test", "package"]
description = "Run format, clippy, tests, and packaging checks"

# ------- Release gatekeeping -------

[tasks.check_tag_env]
clear = true
script_runner = "bash"
script = ['''
  set -euo pipefail
  # Ensure TAG is provided, e.g., TAG=v1.2.3 or refs/tags/v1.2.3
  if [ -z "${TAG:-}" ]; then
    echo "TAG env var is required, e.g. TAG=v1.2.3"
    exit 1
  fi
''']
description = "Ensure TAG env var is provided (e.g. TAG=v1.2.3)"

[tasks.ensure_tag_matches_version]
clear = true
script_runner = "bash"
script = [
  '''
  set -euo pipefail
  # Normalize TAG from refs/tags/vX.Y.Z -> vX.Y.Z
  TAG="${TAG#refs/tags/}"

  # Prefer jq; fallback to sed. Select the root package of the workspace.
  if command -v jq >/dev/null 2>&1; then
    FILE_VER="$(cargo metadata --no-deps --format-version=1 \
      | jq -r '.workspace_root as $root
               | .packages[]
               | select(.manifest_path == ($root + "/Cargo.toml"))
               | .version')"
  else
    META="$(cargo metadata --no-deps --format-version=1)"
    # Best-effort sed fallback: first "version" occurrence
    FILE_VER="$(printf "%s" "$META" | sed -n 's/.*\"version\":\"\\([^\"]*\\)\".*/\\1/p' | head -n1)"
  fi

  if [ -z "${FILE_VER}" ]; then
    echo "Unable to parse version from cargo metadata."
    exit 1
  fi

  if [ "${TAG}" != "v${FILE_VER}" ]; then
    echo "Tag ${TAG} != Cargo.toml version v${FILE_VER}"
    exit 1
  fi

  echo "Tag ${TAG} matches Cargo.toml version v${FILE_VER}"
''',
]
dependencies = ["check_tag_env"]
description = "Ensure git tag matches Cargo.toml version (workspace root package)"

[tasks.ensure_git_tag_exists]
clear = true
script_runner = "bash"
script = ['''
  set -euo pipefail
  # Verify tag exists in the local repo
  T="${TAG#refs/tags/}"
  if ! git rev-parse -q --verify "refs/tags/${T}" >/dev/null; then
    echo "Git tag '${T}' not found locally. Create and push it first:"
    echo "  cargo make tag && git push origin ${T}"
    exit 1
  fi
''']
dependencies = ["check_tag_env"]
description = "Ensure the git tag exists locally"

[tasks.check_token]
clear = true
script_runner = "bash"
script = ['''
  set -euo pipefail
  if [ -z "${CARGO_REGISTRY_TOKEN:-}" ]; then
    echo "CARGO_REGISTRY_TOKEN is required to publish."
    exit 1
  fi
''']
description = "Ensure crates.io token is present in env"

# ------- Publish & Release -------

[tasks.publish]
category = "Release"
clear = true
script_runner = "bash"
script = ['''
  set -euo pipefail
  cargo publish --locked --token "${CARGO_REGISTRY_TOKEN}"
''']
dependencies = [
  "check_token",
  "ensure_tag_matches_version",
  "ensure_git_tag_exists",
  "ci",
]
description = "Publish crate to crates.io after checks and tag/version verification"

[tasks.tag]
category = "Release"
clear = true
script_runner = "bash"
script = [
  '''
  set -euo pipefail

  # Prefer jq; fallback to sed. Select the root package of the workspace.
  if command -v jq >/dev/null 2>&1; then
    VER="$(cargo metadata --no-deps --format-version=1 \
      | jq -r '.workspace_root as $root
               | .packages[]
               | select(.manifest_path == ($root + "/Cargo.toml"))
               | .version')"
    NAME="$(cargo metadata --no-deps --format-version=1 \
      | jq -r '.workspace_root as $root
               | .packages[]
               | select(.manifest_path == ($root + "/Cargo.toml"))
               | .name')"
  else
    META="$(cargo metadata --no-deps --format-version=1)"
    # Fallback: first name/version occurrences
    VER="$(printf "%s" "$META" | sed -n 's/.*\"version\":\"\\([^\"]*\\)\".*/\\1/p' | head -n1)"
    NAME="$(printf "%s" "$META" | sed -n 's/.*\"name\":\"\\([^\"]*\\)\".*/\\1/p' | head -n1)"
  fi

  if [ -z "${VER}" ]; then
    echo "Unable to extract version from Cargo metadata."
    exit 1
  fi

  TAG="v${VER}"

  if git rev-parse -q --verify "refs/tags/${TAG}" >/dev/null; then
    echo "Tag ${TAG} already exists."
    exit 1
  fi

  # Lightweight tag with message for traceability
  git tag -a "${TAG}" -m "${NAME:-crate} ${TAG}"
  echo "Created tag ${TAG}. Push it with:"
  echo "  git push origin ${TAG}"
''',
]
description = "Create annotated git tag vX.Y.Z from Cargo.toml version (does not push)"

[tasks.release]
category = "Release"
clear = true
dependencies = ["publish"]
description = "Run checks and publish the crate (requires TAG and token)"

# ------- Hooks -------

[tasks.install-hooks]
clear = true
workspace = false
description = "Install git pre-commit hook from .hooks/"
script_runner = "bash"
script = [
  'set -euo pipefail',
  'if [ ! -f .hooks/pre-commit ]; then echo "❌ .hooks/pre-commit not found!"; exit 1; fi',
  'echo "🔗 Linking .hooks/pre-commit to .git/hooks/pre-commit..."',
  'mkdir -p .git/hooks',
  'ln -sf ../../.hooks/pre-commit .git/hooks/pre-commit',
  'chmod +x .hooks/pre-commit',
  'echo "✅ pre-commit hook installed."',
]

# ------- Pretty Help -------

[tasks.help]
clear = true
category = "Meta"
description = "Pretty, colored help with examples"
script_runner = "bash"
script = [
  '''
  set -euo pipefail

  if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then
    BOLD="\033[1m"; DIM="\033[2m"; RESET="\033[0m"
    BLUE="\033[34m"; CYAN="\033[36m"; GREEN="\033[32m"; YELLOW="\033[33m"; MAGENTA="\033[35m"
  else
    BOLD=""; DIM=""; RESET=""; BLUE=""; CYAN=""; GREEN=""; YELLOW=""; MAGENTA=""
  fi

  hr() { printf "%s\n" "──────────────────────────────────────────────────────────────────────────────"; }
  row() { printf "  %b%-28s%b %b%s%b\n" "$GREEN" "$1" "$RESET" "$DIM" "$2" "$RESET"; }

  # Prefer jq; fallback to sed. Root package only.
  if command -v jq >/dev/null 2>&1; then
    META="$(cargo metadata --no-deps --format-version=1)"
    VER="$(printf "%s" "$META" | jq -r '.workspace_root as $root
                                          | .packages[]
                                          | select(.manifest_path == ($root + "/Cargo.toml"))
                                          | .version')"
    MSRV="$(printf "%s" "$META" | jq -r '.workspace_root as $root
                                          | .packages[]
                                          | select(.manifest_path == ($root + "/Cargo.toml"))
                                          | .rust_version // "unknown"')"
    NAME="$(printf "%s" "$META" | jq -r '.workspace_root as $root
                                          | .packages[]
                                          | select(.manifest_path == ($root + "/Cargo.toml"))
                                          | .name')"
  else
    META="$(cargo metadata --no-deps --format-version=1)"
    VER="$(printf "%s" "$META" | sed -n 's/.*\"version\":\"\\([^\"]*\\)\".*/\\1/p' | head -n1)"
    MSRV="$(printf "%s" "$META" | sed -n 's/.*\"rust_version\":\"\\([^\"]*\\)\".*/\\1/p' | head -n1)"; [ -z "$MSRV" ] && MSRV="unknown"
    NAME="$(printf "%s" "$META" | sed -n 's/.*\"name\":\"\\([^\"]*\\)\".*/\\1/p' | head -n1)"
  fi

  BRANCH="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo 'unknown')"
  LAST_TAG="$(git describe --tags --abbrev=0 2>/dev/null || echo 'none')"

  printf "%b%s%b  %b(%s, MSRV %s, branch %s, last tag %s)%b\n" \
    "$BOLD$BLUE" "${NAME:-crate} development tasks" "$RESET" \
    "$DIM" "version ${VER:-unknown}" "${MSRV:-unknown}" "${BRANCH}" "${LAST_TAG}" "$RESET"
  hr

  printf "%b%s%b\n" "$BOLD$MAGENTA" "Common commands" "$RESET"
  row "cargo make ci"                    "Run format, clippy, tests, and packaging"
  row "cargo make tag"                   "Create git tag v${VER} from Cargo.toml (no push)"
  row "TAG=v${VER} cargo make release"   "Run checks and publish to crates.io"
  row "ALL_FEATURES=false cargo make ci" "Run CI without --all-features"
  echo

  printf "%b%s%b\n" "$BOLD$MAGENTA" "Formatting" "$RESET"
  row "cargo make format"                "Format with nightly rustfmt (unstable features)"
  echo

  printf "%b%s%b\n" "$BOLD$MAGENTA" "Lint & Tests" "$RESET"
  row "cargo make clippy"                "Run clippy --workspace --all-targets -D warnings"
  row "cargo make test"                  "Run tests --workspace"
  echo

  printf "%b%s%b\n" "$BOLD$MAGENTA" "Packaging & Release" "$RESET"
  row "cargo make package"               "Dry-run packaging (--locked)"
  row "cargo make publish"               "Publish to crates.io (requires TAG and token)"
  echo

  printf "%b%s%b\n" "$BOLD$DIM" "Environment variables:" "$RESET"
  printf "  %b%-22s%b %s\n" "$CYAN" "ALL_FEATURES=true|false" "$RESET" "Enable/disable --all-features for clippy/test"
  printf "  %b%-22s%b %s\n" "$CYAN" "TAG=vX.Y.Z"             "$RESET" "Git tag matching Cargo.toml version"
  printf "  %b%-22s%b %s\n" "$CYAN" "CARGO_REGISTRY_TOKEN"   "$RESET" "crates.io token for publish"
  echo

  printf "%b%s%b\n" "$BOLD$DIM" "Tips:" "$RESET"
  printf "  • Keep %bCargo.lock%b committed\n" "$BOLD" "$RESET"
  printf "  • Tag must exactly match version: %bv%s%b\n" "$BOLD" "${VER}" "$RESET"
  echo
''',
]

# Alias
[tasks.h]
clear = true
run_task = "help"
category = "Meta"
description = "Alias: help"