137 lines
3.8 KiB
Bash
137 lines
3.8 KiB
Bash
#!/bin/bash
|
|
set -euo pipefail
|
|
|
|
ENTRYPOINT="$1"
|
|
FLAGS_STRING="${2:-}"
|
|
IFS=' ' read -r -a DENOCOMPILE_FLAGS <<< "$FLAGS_STRING"
|
|
|
|
TAG_NAME=$(git describe --tags --abbrev=0)
|
|
REPO_NAME=$(basename -s .git "$(git config --get remote.origin.url)")
|
|
OWNER_NAME=$(basename "$(dirname "$(git config --get remote.origin.url)")")
|
|
TMPDIR=$(mktemp -d)
|
|
|
|
echo "Building for tag: $TAG_NAME"
|
|
echo "Using entrypoint: $ENTRYPOINT"
|
|
echo "With flags: ${DENOCOMPILE_FLAGS[*]}"
|
|
|
|
platforms=(
|
|
"x86_64-unknown-linux-gnu"
|
|
"x86_64-pc-windows-msvc"
|
|
"x86_64-apple-darwin"
|
|
)
|
|
|
|
for target in "${platforms[@]}"; do
|
|
echo "Compiling for $target"
|
|
outfile="${TMPDIR}/${REPO_NAME}-${target}"
|
|
[[ "$target" == *"windows"* ]] && outfile="${outfile}.exe"
|
|
|
|
deno compile \
|
|
--target="$target" \
|
|
--output="$outfile" \
|
|
"${DENOCOMPILE_FLAGS[@]}" \
|
|
"$ENTRYPOINT"
|
|
done
|
|
|
|
API_URL="${CI_SERVER_URL:-https://gitea.example.com}/api/v1"
|
|
RELEASE_ENDPOINT="$API_URL/repos/$OWNER_NAME/$REPO_NAME/releases"
|
|
|
|
# Get commits since the last tag
|
|
PREV_TAG=$(git describe --tags --abbrev=0 "$TAG_NAME"^ 2>/dev/null || echo "")
|
|
if [[ -n "$PREV_TAG" ]]; then
|
|
echo "Generating changelog from $PREV_TAG to $TAG_NAME"
|
|
COMMITS=$(git log "$PREV_TAG..$TAG_NAME" --pretty=format:"%s (%an)" --no-merges)
|
|
else
|
|
echo "No previous tag found, generating changelog from initial commit"
|
|
COMMITS=$(git log --pretty=format:"%s (%an)" --no-merges)
|
|
fi
|
|
|
|
# Group commits by type
|
|
CHANGELOG=""
|
|
append_section() {
|
|
local TYPE="$1"
|
|
local TITLE="$2"
|
|
local EMOJI="$3"
|
|
local MATCHED=$(echo "$COMMITS" | grep -E "^${TYPE}: " || true)
|
|
if [[ -n "$MATCHED" ]]; then
|
|
CHANGELOG+="## $EMOJI $TITLE\\n"
|
|
while IFS= read -r line; do
|
|
# Remove prefix (e.g., "feat: ")
|
|
local CLEANED=$(echo "$line" | sed -E "s/^${TYPE}: //")
|
|
CHANGELOG+="- $CLEANED\\n"
|
|
done <<< "$MATCHED"
|
|
CHANGELOG+="\\n"
|
|
fi
|
|
}
|
|
|
|
append_section "feat" "Features" "✨"
|
|
append_section "fix" "Fixes" "🐛"
|
|
append_section "chore" "Chores" "🧹"
|
|
append_section "refactor" "Refactors" "🔧"
|
|
append_section "docs" "Documentation" "📝"
|
|
append_section "test" "Tests" "🧪"
|
|
append_section "style" "Style" "🎨"
|
|
append_section "ci" "CI/CD" "⚙️"
|
|
|
|
# Escape quotes for JSON
|
|
CHANGELOG_ESCAPED=$(printf "%s" "$CHANGELOG" | sed 's/"/\\"/g')
|
|
|
|
RELEASE_BODY=""
|
|
|
|
if [[ -f "CHANGELOG.md" ]]; then
|
|
echo "Looking for manual changelog entry for $TAG_NAME..."
|
|
MANUAL_SECTION=$(awk "/^## \\[?$TAG_NAME\\]? /{flag=1; next} /^## /{flag=0} flag" CHANGELOG.md)
|
|
|
|
if [[ -n "$MANUAL_SECTION" ]]; then
|
|
echo "Manual entry found for $TAG_NAME"
|
|
|
|
if grep -q '<!-- auto-changelog -->' <<< "$MANUAL_SECTION"; then
|
|
echo "Injecting auto-changelog into manual entry"
|
|
FINAL_SECTION=$(sed "s|<!-- auto-changelog -->|$CHANGELOG|" <<< "$MANUAL_SECTION")
|
|
else
|
|
FINAL_SECTION="$MANUAL_SECTION"
|
|
fi
|
|
|
|
# Escape quotes and newlines
|
|
RELEASE_BODY=$(printf "%s" "$FINAL_SECTION" | sed ':a;N;$!ba;s/\n/\\n/g; s/"/\\"/g')
|
|
fi
|
|
fi
|
|
|
|
# Fallback to full auto-changelog
|
|
if [[ -z "$RELEASE_BODY" ]]; then
|
|
echo "Using full auto-generated changelog"
|
|
RELEASE_BODY="$CHANGELOG_ESCAPED"
|
|
fi
|
|
|
|
|
|
IS_PRERELEASE="false"
|
|
if [[ "$TAG_NAME" =~ -[0-9A-Za-z] ]]; then
|
|
IS_PRERELEASE="true"
|
|
fi
|
|
|
|
echo "Creating release $TAG_NAME (prerelease: $IS_PRERELEASE)..."
|
|
|
|
RELEASE_JSON=$(curl -sS -X POST "$RELEASE_ENDPOINT" \
|
|
-H "Authorization: token $GITEA_TOKEN" \
|
|
-H "Content-Type: application/json" \
|
|
-d @- <<EOF
|
|
{
|
|
"tag_name": "$TAG_NAME",
|
|
"name": "$TAG_NAME",
|
|
"draft": false,
|
|
"prerelease": $IS_PRERELEASE,
|
|
"body": "$RELEASE_BODY"
|
|
}
|
|
EOF
|
|
)
|
|
|
|
UPLOAD_URL=$(echo "$RELEASE_JSON" | jq -r '.upload_url // .assets_url')
|
|
|
|
for file in "$TMPDIR"/*; do
|
|
FILENAME=$(basename "$file")
|
|
echo "Uploading $FILENAME..."
|
|
curl -sS -X POST "$UPLOAD_URL" \
|
|
-H "Authorization: token $GITEA_TOKEN" \
|
|
-F name="$FILENAME" \
|
|
-F attachment=@"$file"
|
|
done
|