Skip to content

Commit

Permalink
micro plum purge unused schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj committed Aug 26, 2023
1 parent f9c769a commit 5b81f8b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"@libreservice/micro-plum": "^0.2.0",
"@libreservice/my-widget": "^0.1.4",
"@libreservice/my-worker": "^0.4.2",
"@libreservice/wasm-code": "^0.1.2",
"@libreservice/wasm-code": "^0.1.3",
"@playwright/test": "^1.37.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.2.1",
Expand Down
75 changes: 65 additions & 10 deletions src/components/micro-plum/DeployPane.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ import {
NButton
} from 'naive-ui'
import yaml from 'js-yaml'
import { u2s } from '@libreservice/micro-plum'
import {
u2s,
Recipe,
FileLoader
} from '@libreservice/micro-plum'
import { traverseFS } from '@libreservice/wasm-code'
import { FS, deploy } from '../../workerAPI'
import {
RIME_PATH,
DEFAULT_CUSTOM,
prerequisites,
getAvailableSchemas,
customizeDefault
} from '../../micro-plum'
Expand All @@ -30,12 +36,23 @@ const options = ref<{
const selected = ref<string[]>([])
onMounted(async () => {
const _options: typeof options.value = []
let _selected: string[]
const available = await getAvailableSchemas()
class LocalLoader implements FileLoader {
repo: string
schemaIds: string[]
constructor (repo: string, schemaIds: string[]) {
this.repo = repo
this.schemaIds = schemaIds
}
for (const schema of available) {
loadFile (file: string): Promise<Uint8Array> {
return FS.readFile(`${RIME_PATH}/${file}`)
}
}
async function getOptions (schemas: string[]) {
const _options: typeof options.value = []
for (const schema of schemas) {
const content = u2s(await FS.readFile(`${RIME_PATH}/${schema}.schema.yaml`))
const obj = yaml.load(content) as {
schema?: {
Expand All @@ -48,6 +65,14 @@ onMounted(async () => {
value: schema
})
}
return _options
}
onMounted(async () => {
let _selected: string[]
const available = await getAvailableSchemas()
const _options = await getOptions(available)
try {
const defaultCustomYaml = u2s(await FS.readFile(DEFAULT_CUSTOM))
const schemaList: { schema: string }[] = (yaml.load(defaultCustomYaml) as any)?.patch?.schema_list || []
Expand All @@ -65,7 +90,29 @@ onMounted(async () => {
selected.value = _selected
})
async function onClick () {
async function onTrash () {
const keptFiles: string[] = []
const add = async (loader: LocalLoader) => {
const manifest = await new Recipe(loader).load()
for (const { file, content } of manifest) {
if (content && !keptFiles.includes(file)) {
keptFiles.push(file)
}
}
}
await Promise.all(prerequisites.map(prerequisite => add(new LocalLoader(prerequisite, []))))
await add(new LocalLoader('', selected.value))
await traverseFS(FS, undefined, async (path: string) => {
const file = path.slice(6)
const match = file.match(/^(\S+)\.userdb\/\S+$/)
if (!keptFiles.includes(file) && (!match || !keptFiles.includes(`${match[1]}.dict.yaml`))) {
await FS.unlink(path)
}
}, (path: string) => FS.rmdir(path).catch(() => {}))(RIME_PATH)
options.value = await getOptions(await getAvailableSchemas())
}
async function onDeploy () {
props.dialogInstance.destroy()
await customizeDefault(selected.value)
deploy()
Expand All @@ -78,15 +125,23 @@ async function onClick () {
v-model:value="selected"
:options="options"
/>
<div style="display: flex; justify-content: end">
<n-space style="justify-content: end">
<n-button
:disabled="selected.length === options.length"
secondary
type="error"
@click="onTrash"
>
Purge unused
</n-button>
<n-button
:disabled="selected.length === 0"
secondary
type="info"
@click="onClick"
@click="onDeploy"
>
Deploy
</n-button>
</div>
</n-space>
</n-space>
</template>
27 changes: 27 additions & 0 deletions test/test-micro-plum.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ function microPlum (page: Page) {
return page.locator('.n-dialog')
}

function expand (rppi: Locator, key: string) {
return rppi.locator(`[data-key='s-${key}'] > .n-tree-node-switcher`).click()
}

async function installAndDeploy (page: Page, mp: Locator, ime: string) {
await mp.getByRole('button').getByText('Install').click()
await mp.getByRole('button').getByText('Deploy').click()
Expand Down Expand Up @@ -127,3 +131,26 @@ test('Install from query string', async ({ page }) => {
await input(page, 'ul', 'pb ')
await expectValue(page, '雙拼')
})

test('RPPI', async ({ page }) => {
const ime = '小鶴雙拼'
await init(page)

await page.getByText('RPPI').click()
const rppi = microPlum(page)
await rppi.locator('.n-tree-select').click()
await expand(rppi, 'Chinese')
await expand(rppi, 'Chinese/Mandarin')
await rppi.getByText('双拼').click()
await rppi.getByText('Install').click()
await rppi.getByText(ime).click()
await rppi.getByText('Purge unused').click()
await rppi.getByRole('button').getByText('Deploy').click()
await expectSuccessfulDeployment(page)
await newIMEReady(page, ime)
await select(page).click()
await expect(page.locator('.n-base-select-option')).toHaveCount(1)
await textarea(page).click()
await input(page, 'ul', 'pb ')
await expectValue(page, '雙拼')
})

0 comments on commit 5b81f8b

Please sign in to comment.