Skip to content

Instantly share code, notes, and snippets.

@serious-angel
Created December 22, 2024 23:30
Show Gist options
  • Save serious-angel/2bf771889196465d470c036bfac74eae to your computer and use it in GitHub Desktop.
Save serious-angel/2bf771889196465d470c036bfac74eae to your computer and use it in GitHub Desktop.
The Elder Scrolls Online addon package archive script
#! /usr/bin/env bash
# Tested on p7zip Version 16.02
_Main()
{
declare __manifestFilepath="$1";
shift;
if [[ ! "$__manifestFilepath" =~ \.txt$ ]];
then
printf -- $'\n [-] Manifest file extension is not ".txt": \'%s\'.\n\n' "$__manifestFilepath";
return 2;
fi
declare manifestFilepath;
if ! manifestFilepath="$( readlink -en -- "$__manifestFilepath"; )" || [[ ! -r "$manifestFilepath" ]];
then
printf -- $'\n [-] Manifest file is not found or available: \'%s\'.\n\n' "$__manifestFilepath";
return 2;
fi
# --------------------------------
# Manifest directory
# ----------------
declare manifestDirpath;
if ! manifestDirpath="$( dirname -- "$manifestFilepath"; )" || [[ ! -r "$manifestDirpath" ]];
then
printf -- $'\n [x] Manifest directory is not found or available: \'%s\'.\n\n' "$manifestFilepath";
return 2;
fi
# Addon name
# ----------------
declare addonName;
if ! addonName="$( basename -- "$manifestFilepath" '.txt'; )" || (( ! ${#addonName} ));
then
printf -- $'\n [x] Manifest filename is not found or available: \'%s\'.\n\n' "$manifestFilepath";
return 2;
fi
# Addon version
# ----------------
declare version;
if ! version="$( perl -ne 'print /^## Version:\s+([A-Za-z0-9\._-]+)$/' -- "$manifestFilepath"; )";
then
printf -- $'\n [-] Failed to find the version in manifest: \'%s\'.\n\n' "$manifestFilepath";
return 1;
fi
# Find addon files
# ----------------
if ! readarray -td $'\n' -- files < <( perl -ne 'print if /^[^#\s]+/../EOF/' -- "$manifestFilepath"; ) || (( ! ${#files[@]} ));
then
printf -- $'\n [-] Failed to determine package files of manifest: \'%s\'.\n\n' "$manifestFilepath";
return 1;
fi
# Add manifest file to be archived
files+=( "${addonName}.txt" );
# Create initial addon archive
# ----------------
declare archiveDirpath; archiveDirpath="$( pwd -P; )";
pushd -- "$manifestDirpath" > /dev/null || return $?;
declare archiveFilename="${addonName}_v${version}.zip";
declare archiveFilepath; archiveFilepath="${archiveDirpath}/${archiveFilename}";
if [[ -f "$archiveFilepath" ]];
then
printf -- $'\n [!] Addon archive already exists: \'%s\'.\n\n' "$archiveFilepath";
return 1;
fi
declare rCode=0;
if 7za a -r -t7z '-x!.git*' -bb0 -bso0 -- "$archiveFilepath" -- "${files[@]}"; (( rCode=$? ));
then
popd > /dev/null || return $?;
printf -- $'\n [-] Failed to create archive (code %s): \'%s\'.\n\n' "$rCode" "$archiveFilepath";
return "$rCode";
fi
popd > /dev/null || return $?;
# Move addon archive files into internal subdirectory
# ----------------
declare archiveFiles=();
readarray -t -- archiveFiles < <(
set -o pipefail;
7za l -t7z -slt -- "$archiveFilepath" | perl -ne 'print "$1\n" if /^Path = (.+)/';
) \
|| return $?;
archiveFiles=( "${archiveFiles[@]:1}" );
declare archiveFilesCount="${#archiveFiles[@]}";
if (( ! archiveFilesCount ));
then
printf -- $'\n [x] No files found in the created initial archive: \'%s\'.\n\n' "$archiveFilepath";
return 1;
fi
declare filepath;
for filepath in "${archiveFiles[@]}";
do
if 7za rn -t7z -bb0 -bso0 -- "$archiveFilepath" "$filepath" "${addonName}/${filepath}"; (( rCode=$? ));
then
return "$rCode";
fi
done
# Done
# ----------------
7za l -t7z -bb0 -bso0 -- "$archiveFilepath";
}
_Main "$@";
$ pwd -P;
/tmp
$ ll -A;
total 4
-rwxrwxrwx 1 user user 3658 Dec 23 01:02 pack.sh*
$ ./pack.sh /pathToAddonManifest/Addon.txt;

Listing archive: /tmp/Addon_v1.0.zip

--
Path = /tmp/Addon_v1.0.zip
Type = 7z
Physical Size = 10859
Headers Size = 192
Method = LZMA2:13
Solid = +
Blocks = 1

   Date      Time    Attr         Size   Compressed  Name
------------------- ----- ------------ ------------  ------------------------
2024-12-22 17:48:55 ....A        29124         4598  Addon/Lib/Magic.lua
2024-12-22 17:48:55 ....A         6843         1667  Addon/Addon.lua
2024-12-23 00:46:27 ....A          265               Addon/Addon.txt
2024-12-22 17:48:55 ....A         6611         3125  Addon/Core.lua
------------------- ----- ------------ ------------  ------------------------
2024-12-23 00:46:27              42843         9390  4 files
$ ll -A;
total 8
-rwxrwxrwx 1 user user 10859 Dec 23 01:03 Addon_v1.0.zip*
-rwxrwxrwx 1 user user  3658 Dec 23 01:02 pack.sh*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment