#!/bin/bash # Generates usable metadata for the cursors, allowing them to be more easily processed # TODO: Rewite in Amber - https://amber-lang.com set -e source contrib/util.sh CANCER_PATH="$(realpath cancer-meta.toml)" CANCER="$(yq e -p toml -o json . "$CANCER_PATH")" get_meta_from_cancer() { jq -Mc '.cursor[]' <<< "$CANCER" | while read cursor_json; do if [[ "$(jq -Mc .id <<< "$cursor_json")" == "$1" ]]; then echo -n "$cursor_json" return 0 fi done } cd cursors # Process metadata into usable files for type in ./*; do type="$(sed 's|./||' <<< "$type")" start "Processing CursorType($type)" cd "$type" for cursor in ./*; do cursor="$(sed 's|./||' <<< "$cursor")" if [[ -d "$cursor" ]]; then cd "$cursor" cursor_meta="./meta.json" cursor_assets="./assets" get_from_orig() { jq -Mc "$1" "$cursor_meta" } get_from_orig_raw() { jq -Mcr "$1" "$cursor_meta" } is_animated="$(get_from_orig .isAnimated)" start "Processing Cursor(CursorType($type), name=$cursor, animated=$is_animated)" if [[ "$is_animated" == "true" ]]; then # Animated files tend to have lots of SVGs, resulting in us being unable to store the entire JSON object in memory # This results in us getting an "Argument list too long" error - even though its not the arg list, but rather the env # We solve this by placing the temporary storage on a real file on the filesystem jsettempstorage "/tmp/_$(date)" else # Otherwise, we (dangerously!) assume the file will fit in env, and clear the temp storage. jsettempstorage fi additional_meta="$(get_meta_from_cancer "$(get_from_orig .name)")" jnew "./cursor.json" jtransaction jrun ".animated=$is_animated" jrun ".id=$(get_from_orig .id)" jrun ".name=$(get_from_orig .name)" jrun ".aliases=$(jq -Mc 'if has("links") then .links else [] end' <<< "$additional_meta")" jrun ".hot={}" jrun ".hot.x=$(jq -Mc 'if has("x") then .x else 128 end' <<< "$additional_meta")" jrun ".hot.y=$(jq -Mc 'if has("y") then .y else 128 end' <<< "$additional_meta")" jrun ".platform_names={}" jrun ".platform_names.windows=$(jq -Mc 'if has("winname") then .winname else null end' <<< "$additional_meta")" jrun ".platform_names.xorg=$(jq -Mc 'if has("xname") then .xname else .id end' <<< "$additional_meta")" jrun ".frames=[]" idx=0 trx_ctr=1 while read node_id; do url="$(jq -Mc ".urls[${idx}]" < "$cursor_meta")" raw_node_id="$(jq -Mcr . <<< "$node_id")" frame="$cursor_assets/$raw_node_id.svg" [[ "$is_animated" == "true" ]] && start "Processing CursorFrame(Cursor(CursorType($type), name=$cursor, animated=$is_animated), frame=$raw_node_id)" if [[ "$trx_ctr" == "1" ]]; then jcommit jtransaction trx_ctr=32 else ((trx_ctr=trx_ctr-1)) fi jrun ".frames[$idx]={}" jrun ".frames[$idx].idx=$idx" jrun ".frames[$idx].id=$node_id" jrun ".frames[$idx].url=$url" jrun ".frames[$idx].delay=(if $node_id | contains(\":\") then $node_id | split(\":\") | .[0] | tonumber else 60 end)" jrun ".frames[$idx].sha512=$(escape_str "$(sha512sum "$frame" | awk '{print $1}')")" jsetasset "$(cat "$frame")" && jrun ".frames[$idx].svg=$(jgetassetref)" [[ "$is_animated" == "true" ]] && complete "Finished $(_last | sed s/Pr/pr/)" ((idx=idx+1)) done <<< "$(jq -Mc '.node_ids[]' "$cursor_meta")" jrun ".frame_count=${idx}" jcommit jsave # We're done with the file, let's empty the storage jnew # If we started using tmpfs, let's go back to in-memory storage if [[ "$is_animated" == "true" ]]; then jsettempstorage fi complete "Finished $(_last | sed s/Pr/pr/)" cd .. fi done complete "Finished $(_last | sed s/Pr/pr/)" cd .. done # Process outputs into single file jsettempstorage "/tmp/_$(date)" jnew ../.cursors jtransaction jrun '.=[]' typeidx=0 for type in ./*; do type="$(sed 's|./||' <<< "$type")" start "Bundle all CursorType($type)" jrun ".[${typeidx}]={}" jrun ".[${typeidx}].kind=$(escape_str "$type")" jrun ".[${typeidx}].cursors=[]" jcommit jsave trx_ctr=1 cd "$type" for cursor in ./*; do cursor="$(sed 's|./||' <<< "$cursor")" if [[ -d "$cursor" ]]; then start "Push Cursor(CursorType($type), name=$cursor, animated=null)" cd "$cursor" _jrun_file ".[${typeidx}].cursors += [$(jq -M . ./cursor.json)]" cd .. complete fi done complete jtransaction cd .. ((typeidx=typeidx+1)) done jcommit jsave cd .. jq -Mc . .cursors > .cursors.min