Changeset - b14d3faa45c4
[Not reviewed]
0 1 0
Branko Majic (branko) - 2 years ago 2022-08-21 15:41:25
branko@majic.rs
[factorio_development.sh] Added (personal) workaround for tag signing:

- Older versions of git (<2.23) do not have the tag.gpgSign
option. Try to emulate it by appending the --sign option if it is
set in git configuration.
1 file changed with 8 insertions and 1 deletions:
0 comments (0 inline, 0 general) First comment
games/factorio_development.sh
Show inline comments
 
@@ -469,649 +469,656 @@ function get_mod_name() {
 
# Initialises mod directory structure.
 
#
 
# Arguments:
 
#
 
#   $1 (base_dir)
 
#     Base directory under which the structure should be created.
 
#
 
# Returns:
 
#   0 on success, 1 otherwise.
 
#
 
function command_init() {
 
    local base_dir="$1"
 

	
 
    local mod_name separate_source source_dir
 

	
 
    # Normalise the path.
 
    base_dir=$(readlink -f "$base_dir")
 

	
 
    # Determine the mod name.
 
    mod_name=$(basename "$base_dir")
 

	
 
    # Read list of all files in destination directory.
 
    shopt -s nullglob
 
    local base_dir_files=("$base_dir"/* "$base_dir"/.*)
 
    shopt -u nullglob
 

	
 
    if [[ -e $base_dir && ! -d $base_dir ]]; then
 
        error "Specified path is not a directory."
 
        return 1
 
    fi
 

	
 
    # Only . and .. entries are allowed in the listing (directory must be empty or non-existant).
 
    if (( ${#base_dir_files[@]} > 2 )); then
 
        error "Directory must be empty."
 
        return 1
 
    fi
 

	
 
    # Set-up the base directory (if it does not exist).
 
    if ! mkdir -p "$base_dir"; then
 
        error "Failed to create the base mod directory."
 
        return 1
 
    fi
 

	
 
    # Create separate source directory if the user requests it.
 
    while [[ ${separate_source-} == "" || ${separate_source,,} != y && ${separate_source,,} != n ]]; do
 
        read -r -p "Separate mod source under \"src/\" sub-directory? (y/n) " separate_source
 
    done
 

	
 
    [[ ${separate_source,,} == y ]] && source_dir="$base_dir/src" || source_dir="$base_dir"
 
    mkdir -p "$source_dir"
 

	
 
    # Create initial set of files.
 
    cat <<EOF > "$base_dir/README.md"
 
MOD_TITLE
 
=========
 

	
 

	
 
About
 
-----
 

	
 

	
 
Features
 
--------
 

	
 

	
 
Known issues
 
------------
 

	
 

	
 
Contributions
 
-------------
 

	
 

	
 
Credits
 
-------
 

	
 

	
 
License
 
-------
 

	
 
All code, documentation, and assets implemented as part of this mod are released under the terms of MIT license (see the accompanying \`LICENSE\` file).
 
EOF
 

	
 
    cat <<EOF > "$base_dir/LICENSE"
 
Copyright (c) $(date +%Y) YOUR_NAME
 

	
 
Permission is hereby granted, free of charge, to any person obtaining a copy
 
of this software and associated documentation files (the "Software"), to deal
 
in the Software without restriction, including without limitation the rights
 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
copies of the Software, and to permit persons to whom the Software is
 
furnished to do so, subject to the following conditions:
 

	
 
The above copyright notice and this permission notice shall be included in all
 
copies or substantial portions of the Software.
 

	
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 
SOFTWARE.
 
EOF
 

	
 
    cat <<EOF > "$base_dir/build.cfg"
 
# -*- mode: sh-mode; sh-shell: bash -*-
 

	
 
# Specify list of paths to exclude from the built release archives.
 
IGNORE_PATHS=(
 
    ".gitignore"
 
    "build.cfg"
 
)
 

	
 
# Specify prefix to use in front of versions when tagging releases
 
# (for example if tags should be of format vX.Y.Z as opposed to
 
# X.Y.Z).
 
GIT_VERSION_TAG_PREFIX=""
 
EOF
 

	
 
    cat <<EOF > "$base_dir/.gitignore"
 
# Ignore IDE and backup files.
 
*~
 
.#*
 

	
 
# Ignore build artefacts.
 
dist/
 
build/
 

	
 
# Ignore project temporary directory.
 
tmp/
 
EOF
 

	
 
    cat <<EOF > "$source_dir/info.json"
 
{
 
    "name": "$mod_name",
 
    "version": "999.999.999",
 
    "title": "MOD_TITLE",
 
    "author": "YOUR_NAME",
 
    "homepage": "HOMEPAGE",
 
    "description": "DESCRIPTION",
 
    "factorio_version": "1.1",
 
    "dependencies": [
 
        "? base >= 1.1.0"
 
     ]
 
}
 
EOF
 

	
 
    cat <<EOF > "$source_dir/changelog.txt"
 
---------------------------------------------------------------------------------------------------
 
Version: 999.999.999
 
Date: 9999-99-99
 
  Changes:
 
  Features:
 
  Bugfixes:
 
EOF
 

	
 
    git init "$base_dir"
 
    git -C "$base_dir" add .
 

	
 
    success "Mod structure initialised."
 

	
 
    return 0
 
}
 

	
 

	
 
#
 
# Builds mod relase archive.
 
#
 
# Arguments:
 
#
 
#   $1 (base_dir)
 
#     Base (top-level) directory with the mod files.
 
#
 
# Returns:
 
#   0 on success, 1 otherwise.
 
#
 
function command_build() {
 
    local base_dir="$1"
 

	
 
    local error_count=0
 

	
 
    declare -a mod_files
 

	
 
    local source_dir dist_dir build_dir info_file target_dir archive_file build_config
 
    local mod_name mod_version
 
    local path mod_file mod_file_path
 

	
 
    # Calculate absolute paths to various directories and artifacts.
 
    base_dir=$(readlink -f "$base_dir")
 
    build_dir="$base_dir/build"
 
    dist_dir="$base_dir/dist"
 

	
 
    load_build_configuration "$base_dir" || return 1
 

	
 
    source_dir=$(get_source_directory "$base_dir") || return 1
 
    info_file=$(get_info_file "$base_dir") || return 1
 

	
 
    # Extract modpack name and version
 
    if ! jq . "$info_file" > /dev/null; then
 
        error "Could not parse mod info file: $info_file"
 
        return 1
 
    fi
 
    mod_name=$(get_mod_name "$base_dir") || return 1
 
    mod_version=$(jq -r ".version" "$info_file")
 

	
 
    # Validate version string format.
 
    if [[ ! $mod_version =~ ^[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+$ ]]; then
 
        error "Invalid mod version read from info file: $mod_version"
 
        return 1
 
    fi
 

	
 
    target_dir="${build_dir}/${mod_name}_${mod_version}"
 
    archive_file="${dist_dir}/${mod_name}_${mod_version}.zip"
 

	
 
    # Create list of files to package. Exclude development files from the
 
    # build.
 
    readarray -t mod_files < <(git -C "$base_dir" ls-files)
 

	
 
    # IGNORE_PATHS comes from build configuration.
 
    for path in "${IGNORE_PATHS[@]}"; do
 
        for i in "${!mod_files[@]}"; do
 
            if [[ -d $path && ${mod_files[i]} == $path/* ]] || [[ ${mod_files[i]} == "$path" ]]; then
 
                unset "mod_files[i]"
 
            fi
 
        done
 
    done
 

	
 
    # Fix the index gaps in file list array.
 
    mod_files=("${mod_files[@]}")
 

	
 
    # Validate the files.
 
    error_count=0
 

	
 
    for mod_file in "${mod_files[@]}"; do
 
        mod_file_path="${base_dir}/${mod_file}"
 

	
 
        if [[ $mod_file_path =~ .*\.lua ]] && ! luac -o /dev/null "$mod_file_path"; then
 
            (( error_count += 1 ))
 
            error "Validation failed for file: $mod_file"
 
        fi
 

	
 
        if [[ $mod_file_path =~ .*\.json ]] && ! python -m json.tool "$mod_file_path" > /dev/null; then
 
            (( error_count += 1 ))
 
            error "Validation failed for file: $mod_file"
 
        fi
 
    done
 

	
 
    if [[ $error_count != 0 ]]; then
 
        return 1
 
    fi
 

	
 
    # Do some basic validation so we do not overwrite things by mistake.
 
    if [[ -e $archive_file ]]; then
 
        error "Output archive already exists: $archive_file"
 
        return 1
 
    fi
 

	
 
    info "Building release: $mod_version"
 

	
 
    # Set-up the necessary directories.
 
    rm -rf "$target_dir"
 
    mkdir -p "$target_dir"
 
    mkdir -p "$dist_dir"
 

	
 
    # Copy the files.
 
    for mod_file in "${mod_files[@]}"; do
 
        if ! (cd "$base_dir" && install -m 0644 -D "$mod_file" "${target_dir}/${mod_file%%${source_dir}/}"); then
 
            error "Failed to copy the file: $mod_file"
 
            return 1
 
        fi
 
    done
 

	
 
    # Move sources to base directory.
 
    if [[ $source_dir != . ]]; then
 
        mv -n "${target_dir}/${source_dir}"/* "${target_dir}"
 
        if ! rmdir "${target_dir}/${source_dir}"; then
 
            error "Failed to remove directory: ${target_dir}/${source_dir}"
 
            return 1
 
        fi
 
    fi
 

	
 
    # Zip the files.
 
    if ! (cd "$build_dir/" && zip -q -r "$archive_file" "$(basename "$target_dir")"); then
 
        error "Could not prepare the release archive."
 
        return 1
 
    fi
 

	
 
    rm -rf "$target_dir"
 

	
 
    success "Release built and placed under: $archive_file"
 
}
 

	
 

	
 
#
 
# Releases a mod version. Takes care of creating separate branch,
 
# making versioning changes/updates to mod information file and
 
# changelog, creating the tags, and even switching back the mod to
 
# development version.
 
#
 
# Arguments:
 
#
 
#   $1 (version)
 
#     Version of mod to release.
 
#
 
#   $2 (base_dir)
 
#     Base (top-level) directory with the mod files.
 
#
 
# Returns:
 
#
 
#   0 on success, 1 otherwise.
 
#
 
function command_release() {
 
    local version="$1"
 
    local base_dir="$2"
 

	
 
    local info_file changelog_file build_config changelog
 
    local main_branch current_branch release_branch
 

	
 
    build_config="$base_dir/build.cfg"
 

	
 
    # Read build configuration.
 
    # shellcheck disable=SC1090 # build configuration file is create per-mod directory
 
    if [[ -f $build_config ]] && ! source "$build_config"; then
 
        error "Failed to load build configuration from: $build_config"
 
        return 1
 
    fi
 

	
 
    current_branch=$(git -C "$base_dir" rev-parse --abbrev-ref HEAD)
 
    release_branch="release-${version}"
 

	
 
    source_dir=$(get_source_directory "$base_dir") || return 1
 
    info_file=$(get_info_file "$base_dir") || return 1
 
    changelog_file=$(get_changelog_file "$base_dir") || return 1
 
    current_branch=$(get_current_branch "$base_dir") || return 1
 
    main_branch=$(get_main_branch "$base_dir") || return 1
 

	
 
    if [[ $current_branch != "$main_branch" ]]; then
 
        error "Releases must be based off of the main branch."
 
        return 1
 
    fi
 

	
 
    if [[ $(git -C "$base_dir" status --short) != "" ]]; then
 
        error "Releases must be based off of a clean git working tree."
 
        return 1
 
    fi
 

	
 
    if ! git -C "$base_dir" checkout -b "$release_branch"; then
 
        error "Failed to create release branch: $release_branch"
 
        return 1
 
    fi
 

	
 
    # Update versioning information in info file and changelog.
 
    sed -i -e "s/999.999.999/$version/" "$info_file" "$changelog_file"
 

	
 
    # Update release date.
 
    sed -i -e "s/9999-99-99/$(date +%Y-%m-%d)/" "$changelog_file"
 

	
 
    # Drop empty changelog sections. Changelog sections begin with two
 
    # whitespaces at line beginning, followed by a non-whitespace
 
    # character.
 
    #
 
    # First sed expression:
 
    #
 
    # 1. Finds a section line.
 
    # 2. Reads the next line and adds it to pattern space. If the next
 
    #    line is not a section line, outputs the section it found
 
    #    (P). Otherwise it deletes the matched section line (D).
 
    # 3. Finally, it checks if the next line read (in step 2) is also
 
    #    a section line, and if it is, repeats the process for it (has
 
    #    to be done via goto directive since sed cannot be told to
 
    #    re-read the lines once they have been read with the N
 
    #    directive).
 
    #
 
    # Second sed expression deals with empty sections at the end of
 
    # the file (special case).
 
    sed -i -E -e '/^  [^ ]/{: checknext; N; /\n   /P; D; /^  [^ ]/b checknext}' "$changelog_file"
 
    sed -i -z -E -e 's/\n  [^ \n]+\n$/\n/' "$changelog_file"
 

	
 
    # Build the release.
 
    if ! command_build "$base_dir"; then
 
        return 1
 
    fi
 

	
 
    # @WORKAROUND: For using git tag signing on older versions of git.
 
    if [[ $(git -C "$base_dir" config --get tag.gpgSign) == true ]]; then
 
        local git_tag_gpgsign=("--sign")
 
    else
 
        local git_tag_gpgsign=()
 
    fi
 

	
 
    # Commit the changes and create a tag. GIT_VERSION_TAG_PREFIX
 
    # comes from build configuration.
 
    if ! git -C "$base_dir" add "$changelog_file" "$info_file" || \
 
       ! git -C "$base_dir" commit --edit -m "Prepared release $version." || \
 
       ! git -C "$base_dir" tag -a -m "Release $version." "${GIT_VERSION_TAG_PREFIX}${version}"; then
 
       ! git -C "$base_dir" tag "${git_tag_gpgsign[@]}" -a -m "Release $version." "${GIT_VERSION_TAG_PREFIX}${version}"; then
 

	
 
        error "Failed to create release commit and tag."
 
        return 1
 
    fi
 

	
 
    # Switch back to development version.
 
    sed -i -e "s/$version/999.999.999/" "$info_file"
 
    changelog=$(cat <<EOF
 
---------------------------------------------------------------------------------------------------
 
Version: 999.999.999
 
Date: 9999-99-99
 
  Changes:
 
  Features:
 
  Bugfixes:
 
$(cat "$changelog_file")
 
EOF
 
                )
 
    echo "$changelog" > "$changelog_file"
 

	
 
    # Commit the changes.
 
    if ! git -C "$base_dir" add "$changelog_file" "$info_file" || \
 
       ! git -C "$base_dir" commit -m "Switched back to development version."; then
 
        error "Failed to create developement commit."
 
        return 1
 
    fi
 

	
 
    return 0
 
}
 

	
 

	
 
#
 
# Aborts the current release process.
 
#
 
# Arguments:
 
#
 
#   $1 (base_dir)
 
#     Base (top-level) directory with the mod files.
 
#
 
# Returns:
 
#
 
#   0 on success, 1 otherwise.
 
#
 
function command_abort_release() {
 
    local base_dir="$1"
 

	
 
    local info_file changelog_file build_config mod_name release_archive
 
    local current_branch release_branch version
 

	
 
    load_build_configuration "$base_dir" || return 1
 
    main_branch=$(get_main_branch "$base_dir") || return 1
 
    release_branch=$(get_current_branch "$base_dir") || return 1
 
    mod_name=$(get_mod_name "$base_dir") || return 1
 
    version="${release_branch##release-}"
 
    release_archive="${base_dir}/dist/${mod_name}_${version}.zip"
 

	
 
    if [[ ! $release_branch =~ ^release-[[:digit:]]+\.[[:digit:]]+\.[[:digit:]] ]]; then
 
        error "Current branch is not a release branch."
 
        return 1
 
    fi
 

	
 
    # Drop the release archive.
 
    if [[ -f "${base_dir}/dist/${mod_name}_${version}.zip" ]] &&
 
       ! rm "$release_archive"; then
 

	
 
        error "Failed to remove the release file: $release_archive"
 
        return 1
 
    fi
 

	
 
    # Drop the tag.
 
    if [[ -n $(git -C "$base_dir" tag --list "${GIT_VERSION_TAG_PREFIX}${version}") ]] &&
 
       ! git tag --delete "${GIT_VERSION_TAG_PREFIX}${version}"; then
 

	
 
        error "Failed to drop the git tag: ${GIT_VERSION_TAG_PREFIX}${version}"
 
        return 1
 
    fi
 

	
 
    # Reset working directory, dropping eventual local changes.
 
    if ! git -C "$base_dir" reset --hard HEAD; then
 
        error "Failed to reset working directory."
 
        return 1
 
    fi
 

	
 
    # Switch back to main branch.
 
    if ! git -C "$base_dir" checkout "$main_branch"; then
 
        error "Failed to switch to main branch."
 
        return 1
 
    fi
 

	
 
    # Drop the release branch.
 
    if ! git -C "$base_dir" branch --delete --force "$release_branch"; then
 
        error "Failed to remove the release branch."
 
        return 1
 
    fi
 

	
 
    return 0
 
}
 

	
 

	
 
# Set-up colours for message printing if we're not piping and terminal is
 
# capable of outputting the colors.
 
_COLOR_TERMINAL=$(tput colors 2>&1)
 
if [[ -t 1 ]] && (( _COLOR_TERMINAL > 0 )); then
 
    _TEXT_BOLD=$(tput bold)
 
    _TEXT_WHITE=$(tput setaf 7)
 
    _TEXT_BLUE=$(tput setaf 6)
 
    _TEXT_GREEN=$(tput setaf 2)
 
    _TEXT_YELLOW=$(tput setaf 3)
 
    _TEXT_RED=$(tput setaf 1)
 
    _TEXT_RESET=$(tput sgr0)
 
else
 
    _TEXT_BOLD=""
 
    _TEXT_WHITE=""
 
    _TEXT_BLUE=""
 
    _TEXT_GREEN=""
 
    _TEXT_YELLOW=""
 
    _TEXT_RED=""
 
    _TEXT_RESET=""
 
fi
 

	
 
# Set-up functions for printing coloured messages.
 
function debug() {
 
    if [[ $DEBUG != 0 ]]; then
 
        echo "${_TEXT_BOLD}${_TEXT_BLUE}[DEBUG]${_TEXT_RESET}" "$@"
 
    fi
 
}
 

	
 
function info() {
 
    if [[ $QUIET == 0 ]]; then
 
        echo "${_TEXT_BOLD}${_TEXT_WHITE}[INFO] ${_TEXT_RESET}" "$@"
 
    fi
 
}
 

	
 
function success() {
 
    if [[ $QUIET == 0 ]]; then
 
        echo "${_TEXT_BOLD}${_TEXT_GREEN}[OK]   ${_TEXT_RESET}" "$@"
 
    fi
 
}
 

	
 
function warning() {
 
    echo "${_TEXT_BOLD}${_TEXT_YELLOW}[WARN] ${_TEXT_RESET}" "$@" >&2
 
}
 

	
 
function error() {
 
    echo "${_TEXT_BOLD}${_TEXT_RED}[ERROR]${_TEXT_RESET}" "$@" >&2
 
}
 

	
 
# Define error codes.
 
SUCCESS=0
 
ERROR_ARGUMENTS=1
 
ERROR_GENERAL=2
 

	
 
# Disable debug and quiet modes by default.
 
DEBUG=0
 
QUIET=0
 

	
 
# If no arguments were given, just show usage help.
 
if [[ -z ${1-} ]]; then
 
    short_help
 
    exit "$SUCCESS"
 
fi
 

	
 
# Parse the arguments
 
while getopts "qdvh" opt; do
 
    case "$opt" in
 
	q) QUIET=1;;
 
	d) DEBUG=1;;
 
        v) version
 
           exit "$SUCCESS";;
 
        h) long_help
 
           exit "$SUCCESS";;
 
        *) short_help
 
           exit "$ERROR_ARGUMENTS";;
 
    esac
 
done
 
i=$OPTIND
 
shift $(( i-1 ))
 

	
 
# Quiet and debug are mutually exclusive.
 
if [[ $QUIET != 0 && $DEBUG != 0 ]]; then
 
    error "Quiet and debug options are mutually exclusive."
 
    exit "$ERROR_ARGUMENTS"
 
fi
 

	
 
COMMAND="${1-}"
 
shift
 

	
 
if [[ $COMMAND == init ]]; then
 

	
 
    MOD_DIRECTORY_PATH="${1-}"
 
    shift
 

	
 
    if [[ -z $MOD_DIRECTORY_PATH ]]; then
 
        error "Mod directory path must be specified."
 
        exit "$ERROR_ARGUMENTS"
 
    fi
 

	
 
    if ! command_init "$MOD_DIRECTORY_PATH"; then
 
        exit "$ERROR_GENERAL"
 
    fi
 

	
 
elif [[ $COMMAND == build ]]; then
 

	
 
    MOD_DIRECTORY_PATH="${1:-.}"
 
    shift
 

	
 
    # Ensure that passed-in base directory is the repository root.
 
    if [[ ! -d $MOD_DIRECTORY_PATH/.git ]]; then
 
        error "Passed-in path does not point to base directory of the mod (must contain the .git sub-directory)."
 
        exit "$ERROR_ARGUMENTS"
 
    fi
 

	
 
    if ! command_build "$MOD_DIRECTORY_PATH"; then
 
        exit "$ERROR_GENERAL"
 
    fi
 

	
 
elif [[ $COMMAND == release ]]; then
 

	
 
    MOD_VERSION="${1-}"
 
    MOD_DIRECTORY_PATH="${2:-.}"
 
    shift 2
 

	
 
    if [[ -z $MOD_VERSION ]]; then
 
        error "Mod version must be specified."
 
        exit "$ERROR_ARGUMENTS"
 
    elif [[ ! $MOD_VERSION =~ ^[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+$ ]]; then
 
        error "Mod version must be specified in x.y.z format (where x, y, and z are non-negative integers)."
 
        exit "$ERROR_ARGUMENTS"
 
    fi
 

	
 
    # Ensure that passed-in base directory is the repository root.
 
    if [[ ! -d $MOD_DIRECTORY_PATH/.git ]]; then
 
        error "Passed-in path does not point to base directory of the mod (must contain the .git sub-directory)."
 
        exit "$ERROR_ARGUMENTS"
 
    fi
 

	
 
    if ! command_release "$MOD_VERSION" "$MOD_DIRECTORY_PATH"; then
 
        exit "$ERROR_GENERAL"
 
    fi
 

	
 
elif [[ $COMMAND == abort-release ]]; then
 

	
 
    MOD_DIRECTORY_PATH="${1:-.}"
 
    shift
 

	
 
    # Ensure that passed-in base directory is the repository root.
 
    if [[ ! -d $MOD_DIRECTORY_PATH/.git ]]; then
 
        error "Passed-in path does not point to base directory of the mod (must contain the .git sub-directory)."
 
        exit "$ERROR_ARGUMENTS"
 
    fi
 

	
 
    if ! command_abort_release "$MOD_DIRECTORY_PATH"; then
 
        exit "$ERROR_GENERAL"
 
    fi
 

	
 
else
 

	
 
    error "Unsupported command: $COMMAND"
 
    exit "$ERROR_ARGUMENTS"
 

	
 
fi
0 comments (0 inline, 0 general) First comment
You need to be logged in to comment. Login now