Last active
April 1, 2018 16:49
-
-
Save fungusakafungus/eb2bdf39da73c03dcb2325b3350ff24b to your computer and use it in GitHub Desktop.
gen-mod-from-diff.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
test -z "$1" && { | |
cat >/dev/stderr <<-HELP | |
Usage: $0 mod-name | |
Use this script from top level of Cataclysm checkout, with your changes to data/json not added to git. | |
It will try to make a mod out of those changes instead. | |
The script needs: | |
- python (2?) | |
- jsondiff from jsonpatch python package (https://python-json-patch.readthedocs.io) | |
- jq (https://stedolan.github.io/jq/) | |
- git | |
HELP | |
exit 2 | |
} | |
set -x | |
MODDIR=data/mods/$1 | |
git diff --name-only data/json | | |
grep '\.json$' | | |
while read f | |
do | |
DIR=$(dirname $f) | |
DIR=${DIR#data/json/} | |
BASENAME=$(basename $f .json) | |
mkdir -p $MODDIR/$DIR | |
JQ_EXPR='[.[] | {key:.id, value: .}] | from_entries' | |
# make a fake json patch that retains item id information | |
jq "$JQ_EXPR" $f > $MODDIR/$DIR/$BASENAME.by-id.json | |
jsondiff --indent=2 \ | |
<(git show HEAD:$f | jq "$JQ_EXPR") \ | |
$MODDIR/$DIR/$BASENAME.by-id.json \ | |
> $MODDIR/$DIR/$BASENAME.patch.json | |
python - $MODDIR/$DIR/$BASENAME.by-id.json $MODDIR/$DIR/$BASENAME.patch.json $MODDIR/$DIR/$BASENAME.json <<'PYTHON' | |
import json, sys | |
fn_by_id, fn_patch, fn_out = sys.argv[1:] | |
by_id = json.load(open(fn_by_id)) | |
patch = json.load(open(fn_patch)) | |
items = {} | |
for chunk in patch: | |
_, id, key = chunk['path'].split('/') | |
key = key.replace('~1~1', '//') | |
if id not in items: | |
items[id] = {} | |
item = items[id] | |
item['id'] = id | |
item['copy-from'] = id | |
item['type'] = by_id[id]['type'] | |
item['name'] = by_id[id]['name'] | |
item[key] = chunk['value'] | |
with open(fn_out, 'w') as out: | |
json.dump(items.values(), out, indent=2) | |
out.write('\n') | |
PYTHON | |
test -z "$KEEP_FILES" && rm $MODDIR/$DIR/$BASENAME.by-id.json $MODDIR/$DIR/$BASENAME.patch.json | |
done | |
set +x | |
echo Written mod data to $MODDIR | |
test -f $MODDIR/modinfo.json || echo Warning: The mod still needs $MODDIR/modinfo.json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment