|
| 1 | +#!/usr/bin/env bash |
| 2 | +(set -o igncr) 2>/dev/null && set -o igncr; # This comment is required. |
| 3 | +### The above line ensures that the script can be run on Cygwin/Linux even with Windows CRNL. |
| 4 | +### Run this script on GitHub after zipping the mod to upload the mod on mods.factorio.com |
| 5 | +### This a modified version of https://github.com/Penguin-Spy/factorio-mod-portal-publish/blob/5c669dc60672e6293afd5b1913a0c8475c5490c0/entrypoint.sh |
| 6 | +### You can generate your FACTORIO_MOD_API_KEY on https://factorio.com/create-api-key |
| 7 | + |
| 8 | + |
| 9 | +main() { |
| 10 | +### Check commands |
| 11 | +local has_errors=false |
| 12 | +if ! command -v jq &> /dev/null; then |
| 13 | + echo "Please install jq https://stedolan.github.io/jq/" |
| 14 | + local has_errors=true |
| 15 | +fi |
| 16 | +if [ $has_errors = true ] ; then |
| 17 | + exit 1 |
| 18 | +fi |
| 19 | + |
| 20 | + |
| 21 | +### Get mod name and version from info.json |
| 22 | +### https://stedolan.github.io/jq/ |
| 23 | +local MOD_VERSION=$(jq -r '.version' info.json) |
| 24 | +local MOD_NAME=$(jq -r '.name' info.json) |
| 25 | + |
| 26 | + |
| 27 | +# Validate the version string we're building |
| 28 | +if ! echo "${MOD_VERSION}" | grep -P --quiet '^\d+\.\d+\.\d+$'; then |
| 29 | + echo "Incorrect version pattern, needs to be %u.%u.%u (e.q., 0.1.0)" |
| 30 | + exit 1 |
| 31 | +fi |
| 32 | + |
| 33 | + |
| 34 | +# Get an upload url for the mod |
| 35 | +local URL_RESULT=$(curl -s -d "mod=${MOD_NAME}" -H "Authorization: Bearer ${FACTORIO_MOD_API_KEY}" https://mods.factorio.com/api/v2/mods/releases/init_upload) |
| 36 | +local UPLOAD_URL=$(echo "${URL_RESULT}" | jq -r '.upload_url') |
| 37 | +if [[ "${UPLOAD_URL}" == "null" ]] || [[ -z "${UPLOAD_URL}" ]]; then |
| 38 | + echo "Couldn't get an upload url, failed" |
| 39 | + local ERROR=$(echo "${URL_RESULT}" | jq -r '.error') |
| 40 | + local MESSAGE=$(echo "${URL_RESULT}" | jq -r '.message // empty') |
| 41 | + echo "${ERROR}: ${MESSAGE}" |
| 42 | + exit 1 |
| 43 | +fi |
| 44 | + |
| 45 | + |
| 46 | +# Upload the file |
| 47 | +local UPLOAD_RESULT=$(curl -s -F "file=@${MOD_NAME}_${MOD_VERSION}.zip" "${UPLOAD_URL}") |
| 48 | + |
| 49 | +# The success attribute only appears on successful uploads |
| 50 | +local SUCCESS=$(echo "${UPLOAD_RESULT}" | jq -r '.success') |
| 51 | +if [[ "${SUCCESS}" == "null" ]] || [[ -z "${SUCCESS}" ]]; then |
| 52 | + echo "Upload failed" |
| 53 | + local ERROR=$(echo "${UPLOAD_RESULT}" | jq -r '.error') |
| 54 | + local MESSAGE=$(echo "${UPLOAD_RESULT}" | jq -r '.message // empty') |
| 55 | + echo "${ERROR}: ${MESSAGE}" |
| 56 | + exit 1 |
| 57 | +fi |
| 58 | + |
| 59 | +echo "Upload of ${MOD_NAME}_${MOD_VERSION}.zip completed" |
| 60 | +} |
| 61 | +main |
0 commit comments