Skip to content

Commit

Permalink
use prebuilt opencc dict; shrink path
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj committed Jul 15, 2023
1 parent bf64783 commit b810e8f
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ jobs:
run: |
npm i -g pnpm
pnpm i
npx playwright install
npx playwright install ${{ matrix.browser }}
npx playwright install-deps ${{ matrix.browser }}
- name: Download artifact
uses: actions/download-artifact@v3
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ build
public/worker.js
.vercel
dependency-map.json
opencc-configs.json
schema-files.json
schema-name.json
schema-target.json
Expand Down
4 changes: 2 additions & 2 deletions checksum
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
185429368a2ee8a18ef1437e88de4b56 public/rime.data
b8e95b105aa7234f4957cc3e8cb84d23 public/rime.js
8d6f1a6aa95573beeffe3978beb8a5d3 public/rime.wasm
9eb1e7b57b665f9cdb82d0aa5b866407 public/rime.js
f44908380a0f747edc0f20693393af94 public/rime.wasm
4 changes: 2 additions & 2 deletions doc/customize.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ It's highly recommended to be done BEFORE your customization, in order to locate
An IME corresponds to a schema id.

* IMEs in different plum-compatible repositories should be placed in different objects, e.g. `luna_pinyin` (in [luna-pinyin](https://github.com/rime/rime-luna-pinyin)) and `jyut6ping3` (in [cantonese](https://github.com/rime/rime-cantonese)), although there's a reverse-lookup dependency between them.
* IMEs in one repository should be placed in one object if they share dictionary, e.g. `luna_pinyin` and `luna_pinyin_fluency` both use `luna_pinyin.dict.yaml`.
* IMEs in one repository should be placed in different objects if they don't share dictionary, e.g. you define your own dictionary `fancy.dict.yaml`, but also copy `luna_pinyin.dict.yaml` into your repository for reverse-lookup.
* IMEs in one repository should be placed in one object if they share variant configurations, e.g. `luna_pinyin` and `luna_pinyin_fluency` both have 简 and 繁.
* IMEs in one repository should be placed in different objects if they don't share variant configurations, e.g. you have both `luna_pinyin` and `melt_eng`.

For each object, here are key and value definitions:
* `id: string`, the schema id that you place in `default.yaml` for desktop RIME.
Expand Down
2 changes: 1 addition & 1 deletion doc/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ After making changes, click `Deploy`.
Some conventions:
* The rime directory is `/rime`.
* OpenCC files are at `/usr/local/share/opencc`.
* OpenCC files are at `/usr/share/opencc`.
* All files in [rime-config](../rime-config/) will be available at `/rime` after you execute `pnpm run wasm`.
4 changes: 2 additions & 2 deletions opencc_patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ee08591..84e8f47 100644
index ee08591..c347bb8 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -127,12 +127,21 @@ add_definitions(
Expand All @@ -10,7 +10,7 @@ index ee08591..84e8f47 100644
+add_definitions(-ffile-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}=.)
+
+if (EMSCRIPTEN)
+ add_definitions(-I"${CMAKE_CURRENT_SOURCE_DIR}/../../../build/sysroot/usr/local/include")
+ add_definitions(-I"${CMAKE_CURRENT_SOURCE_DIR}/../../../build/sysroot/usr/include")
+endif()
+
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
Expand Down
10 changes: 5 additions & 5 deletions scripts/build_lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ popd
--with-system \
--with-regex \
--disable-icu \
--prefix=$root/build/sysroot/usr/local install -j $n
--prefix=$root/build/sysroot/usr install -j $n
popd

[[ -L librime/plugins/lua ]] || ln -s ../../librime-lua librime/plugins/lua
mkdir -p librime-lua/thirdparty
[[ -L librime-lua/thirdparty/lua5.4 ]] || ln -s ../../lua librime-lua/thirdparty/lua5.4
rm -f lua/onelua.c

PREFIX=/usr/local
PREFIX=/usr
CMAKE_DEF="""
-DCMAKE_INSTALL_PREFIX:PATH=$PREFIX
-DCMAKE_BUILD_TYPE:STRING=Release
Expand Down Expand Up @@ -70,11 +70,11 @@ if [[ -z `git status --porcelain` ]]; then
git apply $root/opencc_patch
fi
popd
opencc_blddir=build/opencc_wasm
opencc_blddir=build/opencc
rm -rf $opencc_blddir
emcmake cmake librime/deps/opencc -B $opencc_blddir -G Ninja \
$CMAKE_DEF \
-DCMAKE_FIND_ROOT_PATH:PATH=$root/build/sysroot/usr/local \
-DCMAKE_FIND_ROOT_PATH:PATH=$root/build/sysroot/usr \
-DUSE_SYSTEM_MARISA:BOOL=ON
cmake --build $opencc_blddir
DESTDIR=$root/build/sysroot cmake --install $opencc_blddir
Expand All @@ -100,7 +100,7 @@ librime_blddir=build/librime_wasm
rm -rf $librime_blddir
emcmake cmake librime -B $librime_blddir -G Ninja \
$CMAKE_DEF \
-DCMAKE_FIND_ROOT_PATH:PATH=$root/build/sysroot/usr/local \
-DCMAKE_FIND_ROOT_PATH:PATH=$root/build/sysroot/usr \
-DBUILD_TEST:BOOL=OFF \
-DBUILD_STATIC:BOOL=ON \
-DENABLE_LOGGING:BOOL=$ENABLE_LOGGING
Expand Down
8 changes: 0 additions & 8 deletions scripts/build_native.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@ set -e

root=$PWD

opencc_blddir=build/opencc_native
rm -rf $opencc_blddir
cmake librime/deps/opencc -B $opencc_blddir -G Ninja \
-DCMAKE_INSTALL_PREFIX:PATH=/usr/local \
-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON
cmake --build $opencc_blddir
DESTDIR=$root/build/sysroot cmake --install $opencc_blddir

pushd librime
if [[ -z `git status --porcelain -uno --ignore-submodules` ]]; then
git apply $root/librime_patch
Expand Down
41 changes: 6 additions & 35 deletions scripts/build_wasm.mjs
Original file line number Diff line number Diff line change
@@ -1,54 +1,25 @@
import { spawnSync } from 'child_process'
import { exit } from 'process'
import { existsSync, readFileSync } from 'fs'
import { existsSync } from 'fs'
import { ensure } from './util.mjs'

const OPENCC_TARGET = '/usr/local/share/opencc'
const OPENCC_HOST = `build/sysroot/${OPENCC_TARGET}`
const LIB_PATH = 'build/sysroot/usr/local/lib'
const OPENCC_TARGET = '/usr/share/opencc'
const LIB_PATH = 'build/sysroot/usr/lib'
const RIME_PATH = 'build/librime_native/bin'

const preloadFiles = []
function preload (file) {
if (!preloadFiles.includes(file)) {
preloadFiles.push(file)
}
}

function collectPreload (config) {
const content = JSON.parse(readFileSync(`${OPENCC_HOST}/${config}`, {
encoding: 'utf-8'
}))
preload(config)
preload(content.segmentation.dict.file)
for (const { dict } of content.conversion_chain) {
const { file, dicts } = dict
file && preload(file)
if (dicts) {
for (const { file } of dicts) {
preload(file)
}
}
}
}

JSON.parse(readFileSync('opencc-configs.json', { encoding: 'utf-8' })).map(collectPreload)
const OPENCC_HOST = `${RIME_PATH}/opencc`

const compileArgs = [
'-std=c++14',
'-O2',
'-s', 'ALLOW_MEMORY_GROWTH=1',
'-s', 'EXPORTED_FUNCTIONS=_init,_set_schema_name,_set_option,_set_ime,_process,_select_candidate_on_current_page,_deploy',
'-s', 'EXPORTED_RUNTIME_METHODS=["ccall","FS"]',
'--preload-file', `${OPENCC_HOST}@${OPENCC_TARGET}`,
'--preload-file', `${RIME_PATH}/build/default.yaml@rime/build/default.yaml`,
'-I', 'build/sysroot/usr/local/include',
'-I', 'build/sysroot/usr/include',
'-o', 'public/rime.js'
]

for (const file of preloadFiles) {
compileArgs.push('--preload-file', `${OPENCC_HOST}/${file}@${OPENCC_TARGET}/${file}`)
}

for (const file of ['rime.lua', 'lua']) {
const path = `${RIME_PATH}/${file}`
if (existsSync(path)) {
Expand Down
26 changes: 8 additions & 18 deletions scripts/install_schemas.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { spawnSync } from 'child_process'
import { readFileSync, writeFileSync, mkdirSync, copyFileSync, readdirSync, cpSync } from 'fs'
import { readFileSync, writeFileSync, mkdirSync, copyFileSync, readdirSync, cpSync, rmSync } from 'fs'
import { cwd, chdir, exit } from 'process'
import yaml from 'js-yaml'
import { Recipe } from '@libreservice/micro-plum'
import { utf8, ensure, md5sum } from './util.mjs'
import { rf, utf8, ensure, md5sum } from './util.mjs'

const root = cwd()
const { version } = JSON.parse(readFileSync('package.json'))
Expand All @@ -20,7 +20,6 @@ const schemaTarget = {} // maps schema to target
const targetFiles = {} // maps target to files with hash
const targetVersion = {} // maps target to npm package version
const dependencyMap = {} // maps schema to dependent schemas
const openccConfigs = ['t2s.json']

// temp data structures
const targetManifest = {} // maps target to files downloaded from it
Expand All @@ -43,12 +42,6 @@ async function install (recipe, target) {
}
}

function loadOpenCC (config) {
if (!openccConfigs.includes(config)) {
openccConfigs.push(config)
}
}

function parseYaml (schemaId) {
const content = yaml.load(readFileSync(`${RIME_DIR}/build/${schemaId}.schema.yaml`, utf8))
for (const [key, value] of Object.entries(content)) {
Expand All @@ -63,9 +56,6 @@ function parseYaml (schemaId) {
schemaFiles[schemaId].prism = prism
}
}
if (value && typeof value === 'object' && 'opencc_config' in value) {
loadOpenCC(value.opencc_config)
}
}
}

Expand All @@ -91,6 +81,10 @@ function bumpVersion (oldVersion) {

// Main

for (const fileName of ['rime.lua', 'lua', 'opencc']) {
rmSync(`${RIME_DIR}/${fileName}`, rf)
}

mkdirSync(`${RIME_DIR}/opencc`, { recursive: true })
for (const fileName of readdirSync('rime-config')) {
cpSync(`rime-config/${fileName}`, `${RIME_DIR}/${fileName}`, { recursive: true })
Expand All @@ -99,10 +93,12 @@ await Promise.all(['prelude', 'essay', 'emoji'].map(target => install(new Recipe

// remove emoji_category as I don't want to visit a zoo when I type 东吴
const emojiJson = `${RIME_DIR}/opencc/emoji.json`
const emojiCategory = `${RIME_DIR}/opencc/emoji_category.txt`
const emojiContent = JSON.parse(readFileSync(emojiJson, utf8))
const emojiDict = emojiContent.conversion_chain[0].dict
emojiDict.dicts = emojiDict.dicts.filter(({ file }) => file !== 'emoji_category.txt')
writeFileSync(emojiJson, JSON.stringify(emojiContent))
rmSync(emojiCategory, rf)

for (const schema of schemas) {
const recipe = new Recipe(schema.target, { schemaIds: [schema.id] })
Expand Down Expand Up @@ -137,7 +133,6 @@ for (const schema of schemas) {
}
await install(recipe, target)
if (schema.emoji) {
loadOpenCC('emoji.json')
writeFileSync(`${RIME_DIR}/${schema.id}.custom.yaml`,
`__patch:
- patch/+:
Expand Down Expand Up @@ -249,10 +244,5 @@ writeFileSync('schema-files.json', JSON.stringify(schemaFiles))
writeFileSync('schema-target.json', JSON.stringify(schemaTarget))
writeFileSync('dependency-map.json', JSON.stringify(dependencyMap))
writeFileSync('target-version.json', JSON.stringify(targetVersion))
writeFileSync('opencc-configs.json', JSON.stringify(openccConfigs.sort()))

for (const fileName of readdirSync(`${RIME_DIR}/opencc`)) {
copyFileSync(`${RIME_DIR}/opencc/${fileName}`, `build/sysroot/usr/local/share/opencc/${fileName}`)
}

console.log("Run 'pnpm run wasm' before 'pnpm run dev' to update rime.data.")
5 changes: 2 additions & 3 deletions src/components/MyEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ onMounted(() => {
'/',
'/rime/',
'/usr/',
'/usr/local/',
'/usr/local/share/',
'/usr/local/share/opencc/'
'/usr/share/',
'/usr/share/opencc/'
].forEach(wc.value!.expandFolder)
})
</script>
Expand Down

0 comments on commit b810e8f

Please sign in to comment.