summarylogtreecommitdiffstats
path: root/PKGBUILD
blob: 05815aae4836684e937fb36855f7712059e89b86 (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# Maintainer: everyx <lunt.luo#gmail.com>

_pkgbase=rime-ice
pkgbase=rime-ice-git
pkgname=(
  ${_pkgbase}-git
  ${_pkgbase}-pinyin-git
  ${_pkgbase}-double-pinyin-git
  ${_pkgbase}-double-pinyin-abc-git
  ${_pkgbase}-double-pinyin-mspy-git
  ${_pkgbase}-double-pinyin-sogou-git
  ${_pkgbase}-double-pinyin-flypy-git
  ${_pkgbase}-double-pinyin-ziguang-git
)

pkgver=r633.87a7de4
pkgrel=1
pkgdesc="Rime 配置:雾凇拼音 | 长期维护的简体词库"
arch=("any")
url="https://github.com/iDvel/rime-ice"
license=("GPL-3.0-or-later")

depends=("lua" "librime")
makedepends=("git" "rime-prelude" "sed")

source=("${_pkgbase}::git+${url}.git")
sha512sums=("SKIP")

pkgver() {
  cd "${_pkgbase}" &&
    printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "${_pkgbase}" &&
    mv default.yaml _default.yaml &&
    # Link essentials.
    for _f in $(pacman -Qql rime-prelude | grep -v "/$"); do ln -sf "$_f" .; done
}

_schemas=(
  rime_ice
  double_pinyin
  double_pinyin_abc
  double_pinyin_mspy
  double_pinyin_sogou
  double_pinyin_flypy
  double_pinyin_ziguang
)

build() {
  cd "${_pkgbase}"

  # 
  # 生成各方案的默认配置文件,注释掉所有非当前方案
  # 
  _suggestion_schemas=$(sed -n '/^schema_list:/,/^$/ {/^schema_list:/d; /^\s*#.*$/d; /^$/d; s/.*schema:\s*//g; s/\s*#.*//g; p }' _default.yaml)

  for _suggestion_schema_name in $_suggestion_schemas; do
    if [[ ! ${_schemas[*]} =~ (^|[[:space:]])"$_suggestion_schema_name"($|[[:space:]]) ]]; then
      sed -i "s/^\s*- schema: $_suggestion_schema_name .*\$/#&/" _default.yaml;
    fi
  done

  for _schema_name in "${_schemas[@]}"; do
    cp -f _default.yaml "${_schema_name}.default.yaml"

    for _suggestion_schema_name in $_suggestion_schemas; do
      if [[ ! $_schema_name =~ (^|[[:space:]])"$_suggestion_schema_name"($|[[:space:]]) ]]; then
        sed -i "s/^\s*- schema: $_suggestion_schema_name .*\$/#&/" "${_schema_name}.default.yaml";
      fi
    done
  done

  # 
  # 编译语料库
  # 
  _schemas_deps=()
  for _schema_name in "${_schemas[@]}"; do
    _deps=()
    mapfile -t _deps <<< "$(sed -n '/dependencies:/,/^$/ {/dependencies:/d; /^$/d; s/.*- *//g; s/ *#.*//g; p }' "$_schema_name.schema.yaml")"
    _schemas_deps=("${_schemas_deps[@]}" "${_deps[@]}")
  done

  mapfile -t _schemas_deps <<< "$(printf "%s\n" "${_schemas_deps[@]}" | sort -u)"

  # 仅编译 _schemas 中列出方案所依赖的语料库,按方案名字符长度排序
  _compile_schemas=("${_schemas_deps[@]}" "${_schemas[@]}")
  for _schema_name in "${_compile_schemas[@]}";
    do rime_deployer --compile "$_schema_name.schema.yaml";
  done

  find . -type l -delete

  rm -f custom_phrase.txt
}

_install_schema_dicts() {
  _name=$1
  _install_base=$2

    # 遍历所有依赖方案,获取所有所有依赖词库
  _dicts=()
  mapfile -t _dicts <<< "$(
    sed -n '/dictionary:/ {
      s/.*: *//;          # 去除 dictionary: 后的内容
      s/ *#.*//;          # 去除注释部分
      s/""//;             # 去除空引号
      /^[[:space:]]*$/d;  # 去除空行
      p;                  # 打印处理后的结果
    }' "$_name.schema.yaml"
  )"

  for _dict in "${_dicts[@]}"; do
    # 处理 import_tables
    _import_tables=()
    mapfile -t _import_tables <<< "$(
      sed -n '/import_tables:/,/^$/ {
        /^[^ ]/d;         # 删除不以空格开头的行
        s/.*: *//;        # 去除: 前内容和后随空格
        s/.*- *//g;       # 去除列表标记
        s/ *#.*//g;       # 去除注释部分
        p;                # 打印处理后的结果
        }' "${_dict}.dict.yaml"
    )"
    install -Dm644 "${_dict}.dict.yaml" -t "$_install_base/";
    
    # 处理 user_dict
    _user_dicts=()
    mapfile -t _user_dicts <<< "$(
      sed -n '/user_dict:/ {
        s/.*: *//;          # 去除 user_dict: 后的内容
        s/ *#.*//;          # 去除注释部分
        s/""//;             # 去除空引号
        /^[[:space:]]*$/d;  # 去除空行
        p;                  # 打印处理后的结果
      }' "$_name.schema.yaml"
    )"

    _dict_src_files=("${_import_tables[@]}" "${_user_dicts[@]}")

    for _src_file in "${_dict_src_files[@]}"; do
      # 遍历词库文件,获取所有依赖词库文件
      for _f in "${_src_file}".*{yaml,txt}; do
        if [ -f "$_f" ]; then
          install -Dm644 "$_f" -t "$_install_base/$(dirname "${_src_file}")"
        fi
      done
    done

  done
}

_install_schema() {
  _name=$1
  _install_base=$2

  # schema 文件
  install -Dm644 "${_name}.schema.yaml" -t "$_install_base/";

  # 预编译的二进制语料库
  install -Dm644 ./build/"${_name}".*.{bin,yaml} -t "$_install_base/build";

  # emoji
  grep -q "opencc_config: emoji.json" "${_name}.schema.yaml" &&
    install -Dm644 ./opencc/* -t "$_install_base/opencc/"

  # __include 文件,例如:symbols.yaml
  _include_files=()
  mapfile -t _include_files <<< "$(
      sed -n '/__include:/ {
        s/ *__include: *//;        # 去除 dictionary: 后的内容
        s/:.*//;                   # 去除 dictionary: 后的内容
        s/ *#.*//g;                # 去除注释部分
        p;                         # 打印处理后的结果
      }' "$_name.schema.yaml"
  )"
  for _f in "${_include_files[@]}"; do
    if [ -f "${_f}.yaml" ]; then
      install -Dm644 "${_f}.yaml" -t "$_install_base/"
    fi

  done

  _install_schema_dicts "${_name}" "${_install_base}"
}

_patch_double_pinyin() {
  _schema=$1
  _install_base=$2
  _algebra="algebra_${_schema//double_pinyin_/}"
  _schemas_to_patch=(
    radical_pinyin
    melt_eng
  )

  # patch radical_pinyin
  for _f in "${_schemas_to_patch[@]}"; do
    _patch_f="${_install_base}/${_f}.schema.yaml"
    if [ -f "${_patch_f}" ]; then
      sed -i "{
          s/^\(\s*\)\(__include.*\)$/\1# \2/;          # 注释掉未注释的行
          s/^\(\s*\)#\s*\([^#]*${_algebra}.*\)$/\1\2/; # 启用当前的方案
        }" "${_patch_f}";
    fi
  done
}

_package_schema() {
  _schema=$1                                 # 方案名
  _suggestion=${2:-${_schema}.default.yaml}  # 默认配置源文件名
  _install_base="${pkgdir}/usr/share/rime-data"

  cd "${srcdir}/${_pkgbase}"

  # 方案默认配置
  install -Dm644 "$_suggestion" "$_install_base/rime_ice_suggestion.yaml"

  # lua
  [ -f "./rime.lua" ] && install -Dm644 ./rime.lua -t "$_install_base/"
  find lua -type f -exec sh -c 'install -Dm644 "$1" -t '"$_install_base"'/$(dirname $1)' shell {} \;

  # 
  # 处理词库
  # 
  # 获取当前方案依赖的所有方案
  _schema_deps=()
  mapfile -t _schema_deps <<< "$(
    sed -n '/dependencies:/,/^$/ {
      /dependencies:/d; # 去除 dependencies:
      /^$/d;            # 去除空行
      s/.*- *//g;       # 去除列表标记
      s/ *#.*//g;       # 去除注释部分
      p;                # 打印处理后的结果
    }' "$_schema.schema.yaml"
  )"
  mapfile -t _schema_deps <<< "$(printf "%s\n" "${_schema_deps[@]}" | sort -u)"

  _schemas_to_install=("${_schema_deps[@]}" "${_schema}")
  for _s in "${_schemas_to_install[@]}"; do
    _install_schema "$_s" "$_install_base"
  done

  # 对于双拼输入法,patch radical_pinyin 和 melt_eng 方案
  if [[ $_schema == "double_pinyin"* ]]; then
    _patch_double_pinyin "$_schema" "$_install_base"
  fi
}

package_rime-ice-git() {
  pkgdesc='Rime 配置:雾凇拼音 | 长期维护的简体词库'
  provides=(rime-ice)
  conflicts=(
    rime-emoji
    # ${_pkgbase}
    ${_pkgbase}-pinyin
    ${_pkgbase}-double-pinyin
    ${_pkgbase}-double-pinyin-abc
    ${_pkgbase}-double-pinyin-mspy
    ${_pkgbase}-double-pinyin-sogou
    ${_pkgbase}-double-pinyin-flypy
    ${_pkgbase}-double-pinyin-ziguang
  )

  for _schema_name in "${_schemas[@]}"; do
    _package_schema "${_schema_name}" _default.yaml
  done
}

package_rime-ice-pinyin-git() {
  pkgdesc='Rime 配置:雾凇拼音 | 长期维护的简体词库 - 拼音方案'
  provides=(rime-ice-pinyin)
  conflicts=(
    rime-emoji
    ${_pkgbase}
    # ${_pkgbase}-pinyin
    ${_pkgbase}-double-pinyin
    ${_pkgbase}-double-pinyin-abc
    ${_pkgbase}-double-pinyin-mspy
    ${_pkgbase}-double-pinyin-sogou
    ${_pkgbase}-double-pinyin-flypy
    ${_pkgbase}-double-pinyin-ziguang
  )

  _package_schema rime_ice
}

package_rime-ice-double-pinyin-git() {
  pkgdesc='Rime 配置:雾凇拼音 | 长期维护的简体词库 - 自然码双拼'
  provides=(rime-ice-double-pinyin)
  conflicts=(
    rime-emoji
    ${_pkgbase}
    ${_pkgbase}-pinyin
    # ${_pkgbase}-double-pinyin
    ${_pkgbase}-double-pinyin-abc
    ${_pkgbase}-double-pinyin-mspy
    ${_pkgbase}-double-pinyin-sogou
    ${_pkgbase}-double-pinyin-flypy
    ${_pkgbase}-double-pinyin-ziguang
  )
 
  _package_schema double_pinyin
}

package_rime-ice-double-pinyin-abc-git() {
  pkgdesc='Rime 配置:雾凇拼音 | 长期维护的简体词库 - 智能ABC双拼'
  provides=(rime-ice-double-pinyin-abc)
  conflicts=(
    rime-emoji
    ${_pkgbase}
    ${_pkgbase}-pinyin
    ${_pkgbase}-double-pinyin
    # ${_pkgbase}-double-pinyin-abc
    ${_pkgbase}-double-pinyin-mspy
    ${_pkgbase}-double-pinyin-sogou
    ${_pkgbase}-double-pinyin-flypy
    ${_pkgbase}-double-pinyin-ziguang
  )

  _package_schema double_pinyin_abc
}

package_rime-ice-double-pinyin-mspy-git() {
  pkgdesc='Rime 配置:雾凇拼音 | 长期维护的简体词库 - 微软双拼'
  provides=(rime-ice-double-pinyin-mspy)
  conflicts=(
    rime-emoji
    ${_pkgbase}
    ${_pkgbase}-pinyin
    ${_pkgbase}-double-pinyin
    ${_pkgbase}-double-pinyin-abc
    # ${_pkgbase}-double-pinyin-mspy
    ${_pkgbase}-double-pinyin-sogou
    ${_pkgbase}-double-pinyin-flypy
    ${_pkgbase}-double-pinyin-ziguang
  )

  _package_schema double_pinyin_mspy
}

package_rime-ice-double-pinyin-sogou-git() {
  pkgdesc='Rime 配置:雾凇拼音 | 长期维护的简体词库 - 搜狗双拼'
  provides=(rime-ice-double-pinyin-sogou)
  conflicts=(
    rime-emoji
    ${_pkgbase}
    ${_pkgbase}-pinyin
    ${_pkgbase}-double-pinyin
    ${_pkgbase}-double-pinyin-abc
    ${_pkgbase}-double-pinyin-mspy
    # ${_pkgbase}-double-pinyin-sogou
    ${_pkgbase}-double-pinyin-flypy
    ${_pkgbase}-double-pinyin-ziguang
  )

  _package_schema double_pinyin_sogou
}

package_rime-ice-double-pinyin-flypy-git() {
  pkgdesc='Rime 配置:雾凇拼音 | 长期维护的简体词库 - 小鹤双拼'
  provides=(rime-ice-double-pinyin-flypy)
  conflicts=(
    rime-emoji
    ${_pkgbase}
    ${_pkgbase}-pinyin
    ${_pkgbase}-double-pinyin
    ${_pkgbase}-double-pinyin-abc
    ${_pkgbase}-double-pinyin-mspy
    ${_pkgbase}-double-pinyin-sogou
    # ${_pkgbase}-double-pinyin-flypy
    ${_pkgbase}-double-pinyin-ziguang
  )

  _package_schema double_pinyin_flypy
}

package_rime-ice-double-pinyin-ziguang-git() {
  pkgdesc='Rime 配置:雾凇拼音 | 长期维护的简体词库 - 紫光双拼'
  provides=(rime-ice-double-pinyin-ziguang)
  conflicts=(
    rime-emoji
    ${_pkgbase}
    ${_pkgbase}-pinyin
    ${_pkgbase}-double-pinyin
    ${_pkgbase}-double-pinyin-abc
    ${_pkgbase}-double-pinyin-mspy
    ${_pkgbase}-double-pinyin-sogou
    ${_pkgbase}-double-pinyin-flypy
    # ${_pkgbase}-double-pinyin-ziguang
  )

  _package_schema double_pinyin_ziguang
}