summaryrefslogtreecommitdiffstats
path: root/contrib/gen-meta.sh
blob: 2f51e80a9180b642b82bc371c1d05ce89d755388 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/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