Changeset - 47a1b46db452
[Not reviewed]
0 0 1
Branko Majic (branko) - 21 months ago 2022-08-16 00:42:10
branko@majic.rs
[factorio_development.sh] Added initial implementation of Factorio mod development tool:

- Include basic script structure.
- Implement initialisation command for a new mod.
1 file changed with 398 insertions and 0 deletions:
0 comments (0 inline, 0 general)
games/factorio_development.sh
Show inline comments
 
new file 100755
 
#!/bin/bash
 
#
 
# factorio_development.sh
 
#
 
# Copyright (C) 2022, Branko Majic <branko@majic.rs>
 
#
 
# This program is free software: you can redistribute it and/or modify
 
# it under the terms of the GNU General Public License as published by
 
# the Free Software Foundation, either version 3 of the License, or
 
# (at your option) any later version.
 
#
 
# This program is distributed in the hope that it will be useful,
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
# GNU General Public License for more details.
 
#
 
# You should have received a copy of the GNU General Public License
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
#
 

	
 
# Treat unset variables as errors.
 
set -u
 

	
 
PROGRAM="factorio_development.sh"
 
VERSION="0.0.1"
 

	
 
function usage() {
 
    cat <<EOF
 
$PROGRAM $VERSION, helper tool for development of Factorio mods
 

	
 
Usage:
 

	
 
  $PROGRAM [OPTIONS] init MOD_DIRECTORY_PATH
 

	
 
EOF
 
}
 

	
 
function short_help() {
 
    cat <<EOF
 
$(usage)
 

	
 
For more details see $PROGRAM -h.
 
EOF
 
}
 

	
 
function long_help() {
 
    cat <<EOF
 
$(usage)
 

	
 
$PROGRAM is a helper tool for development of Factorio mods.
 

	
 
The tool enforces certain conventions to make the mod development
 
fairly consistent across the board. The following conventions are
 
used:
 

	
 
- Development version is always 999.999.999 in order to guarantee that
 
  mod upgrade events get triggered properly and processed by event
 
  handlers. It also provides clear indicator to both developer and
 
  player that a development version is in use.
 

	
 
- Similar to version, development release date is set to 9999-99-99,
 
  thus guaranteeing that the changelog clearly indicates that the
 
  development version is currently in use.
 

	
 
Multiple commands are provided for covering the entire lifecycle of
 
Factorio mod development. Each command accepts its own set of
 
positional argument.
 

	
 

	
 
init MOD_DIRECTORY_PATH
 

	
 
  Arguments:
 

	
 
    MOD_DIRECTORY_PATH (path to base directory)
 

	
 
  Initialises directory for new mod development. Passed-in directory
 
  must be empty. Mod name is derived from the directory name. If the
 
  passed-in mod directory path does not point to an existing
 
  directory, it will be created.
 

	
 
  During initialisation it is possible to select if mod sources should
 
  be kept in a separate directory or within the base directory. This
 
  can be helpful for separating non-Factorio assets and files (such as
 
  license, README file etc).
 

	
 
  The following files are created as part of the process ([src/]
 
  indicates optional placement under separate source directory):
 

	
 
    - README.md, provides detailed mod description and information.
 
    - LICENSE, contains license information for the mod.
 
    - .gitignore, for ignoring files and paths when working with git.
 
    - [src/]info.json, mod metadata.
 
    - [src/]changelog.txt, with changelog information for the mod.
 

	
 
$PROGRAM accepts the following options:
 

	
 
    -q
 
        Quiet mode.
 
    -d
 
        Enable debug mode.
 
    -v
 
        Show script version and licensing information.
 
    -h
 
        Show full help.
 

	
 
Please report bugs and send feature requests to <branko@majic.rs>.
 
EOF
 
}
 

	
 
function version() {
 
    cat <<EOF
 
$PROGRAM, version $VERSION
 

	
 
+-----------------------------------------------------------------------+
 
| Copyright (C) 2022, Branko Majic <branko@majic.rs>                    |
 
|                                                                       |
 
| This program is free software: you can redistribute it and/or modify  |
 
| it under the terms of the GNU General Public License as published by  |
 
| the Free Software Foundation, either version 3 of the License, or     |
 
| (at your option) any later version.                                   |
 
|                                                                       |
 
| This program is distributed in the hope that it will be useful,       |
 
| but WITHOUT ANY WARRANTY; without even the implied warranty of        |
 
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
 
| GNU General Public License for more details.                          |
 
|                                                                       |
 
| You should have received a copy of the GNU General Public License     |
 
| along with this program.  If not, see <http://www.gnu.org/licenses/>. |
 
+-----------------------------------------------------------------------+
 

	
 
EOF
 
}
 

	
 

	
 
# Commands
 
# ========
 

	
 
#
 
# 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 -n1 -p "Separate mod source under \"src/\" sub-directory? (y/n) " separate_source
 
        echo
 
    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/.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
 
}
 

	
 

	
 
# 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
 

	
 
else
 

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

	
 
fi
0 comments (0 inline, 0 general)