aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/publish-to-pypi.sh
blob: a6ac14e97385d9fdf2f0b4975892f5ad14091bc2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/bin/sh
set -e

PYPI_TOKEN=""
ANNOUNCE_MAILING_LIST="~eugenetriguba/taskipy2-announce@lists.sr.ht"

main() {
  parse_args "$@"
  uv build
  uv publish --token "$PYPI_TOKEN"
  send_announcement_email
}

usage() {
  echo "Usage: $0 [-h|--help] -t|--token TOKEN"
  echo ""
  echo "Options:"
  echo "  -h, --help    Show this help message and exit"
  echo "  -t, --token   Required PyPI token for publishing the package"
  echo ""
  echo "Environment Variables:"
  echo "  SMTP_HOST: The SMTP host to send an announcement email from."
  echo "  SMTP_FROM: The email address in the announcement email 'from' field."
  echo "  SMTP_USER: The username to login the SMTP host with."
  echo "  SMTP_PASSWORD: The password associated with the username in the SMTP host."
}

parse_args() {
  while [ $# -gt 0 ]; do
    case "$1" in
    -h | --help)
      usage
      exit 0
      ;;
    -t | --token)
      PYPI_TOKEN="$2"
      shift 2
      ;;
    *)
      usage
      exit 1
      ;;
    esac
  done
}

send_announcement_email() {
  _pyproject_version=$(grep '^version = ' pyproject.toml | sed 's/version = "\(.*\)"/\1/')
  _changelog_body=$(extract_changelog_section "$_pyproject_version")

  cat <<EOF >mail.txt
From: ${SMTP_FROM}
To: ${ANNOUNCE_MAILING_LIST}
Subject: [taskipy2] v${_pyproject_version} released
Message-ID: <$(date +%s).$$@${SMTP_HOST}>
Date: $(date -R)

Hi all,

taskipy2 v${_pyproject_version} has been released and is now available on PyPI.

  pip install --upgrade taskipy2

What's new in v${_pyproject_version}:

${_changelog_body}

Full changelog: https://git.sr.ht/~eugenetriguba/taskipy2/tree/main/item/CHANGELOG.md

Regards,
Eugene Triguba
EOF

  curl \
    --silent \
    --show-error \
    --ssl-reqd \
    --url "smtp://${SMTP_HOST}:587" \
    --user "${SMTP_USER}:${SMTP_PASSWORD}" \
    --mail-from "$SMTP_FROM" \
    --mail-rcpt "$ANNOUNCE_MAILING_LIST" \
    --upload-file mail.txt

  echo "Announcement email sent to $ANNOUNCE_MAILING_LIST"
}

extract_changelog_section() {
  _changelog_version="$1"
  awk "
    /^\#\# \[$_changelog_version\]/ { found=1; next }
    found && /^\#\# \[/  { exit }
    found                { print }
  " CHANGELOG.md |
    sed -e 's/^### \(.*\)/\1:/' \
      -e '/./,$!d'
}

main "$@"