Skip to content

Commit

Permalink
プラグインの簡易的なログを表示する機能 (#13564)
Browse files Browse the repository at this point in the history
* add plugin logging

* change variable name

* Update plugin.ts

* Update CHANGELOG.md
  • Loading branch information
FineArchs authored Mar 13, 2024
1 parent 29f6ba6 commit 88d47ab
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
- Enhance: 広告がMisskeyと同一ドメインの場合はRouterで遷移するように
- Enhance: リアクション・いいねの総数を表示するように
- Enhance: リアクション受け入れが「いいねのみ」の場合はリアクション絵文字一覧を表示しないように
- Enhance: 設定>プラグインのページからプラグインの簡易的なログやエラーを見られるように
- 実装の都合により、プラグインは1つエラーを起こした時に即時停止するようになりました
- Fix: 一部のページ内リンクが正しく動作しない問題を修正
- Fix: 周年の実績が閏年を考慮しない問題を修正
- Fix: ローカルURLのプレビューポップアップが左上に表示される
Expand Down
1 change: 1 addition & 0 deletions locales/ja-JP.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1772,6 +1772,7 @@ _plugin:
installWarn: "信頼できないプラグインはインストールしないでください。"
manage: "プラグインの管理"
viewSource: "ソースを表示"
viewLog: "ログを表示"

_preferencesBackups:
list: "作成したバックアップ"
Expand Down
20 changes: 17 additions & 3 deletions packages/frontend/src/pages/settings/plugin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,26 @@ SPDX-License-Identifier: AGPL-3.0-only
<MkButton inline danger @click="uninstall(plugin)"><i class="ti ti-trash"></i> {{ i18n.ts.uninstall }}</MkButton>
</div>

<MkFolder>
<template #icon><i class="ti ti-terminal-2"></i></template>
<template #label>{{ i18n.ts._plugin.viewLog }}</template>

<div class="_gaps_s">
<div class="_buttons">
<MkButton inline @click="copy(pluginLogs.get(plugin.id)?.join('\n'))"><i class="ti ti-copy"></i> {{ i18n.ts.copy }}</MkButton>
</div>

<MkCode :code="pluginLogs.get(plugin.id)?.join('\n') ?? ''"/>
</div>
</MkFolder>

<MkFolder>
<template #icon><i class="ti ti-code"></i></template>
<template #label>{{ i18n.ts._plugin.viewSource }}</template>

<div class="_gaps_s">
<div class="_buttons">
<MkButton inline @click="copy(plugin)"><i class="ti ti-copy"></i> {{ i18n.ts.copy }}</MkButton>
<MkButton inline @click="copy(plugin.src)"><i class="ti ti-copy"></i> {{ i18n.ts.copy }}</MkButton>
</div>

<MkCode :code="plugin.src ?? ''" lang="is"/>
Expand All @@ -74,6 +87,7 @@ import { ColdDeviceStorage } from '@/store.js';
import { unisonReload } from '@/scripts/unison-reload.js';
import { i18n } from '@/i18n.js';
import { definePageMetadata } from '@/scripts/page-metadata.js';
import { pluginLogs } from '@/plugin.js';

const plugins = ref(ColdDeviceStorage.get('plugins'));

Expand All @@ -87,8 +101,8 @@ async function uninstall(plugin) {
});
}

function copy(plugin) {
copyToClipboard(plugin.src ?? '');
function copy(text) {
copyToClipboard(text ?? '');
os.success();
}

Expand Down
25 changes: 17 additions & 8 deletions packages/frontend/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/

import { ref } from 'vue';
import { Interpreter, Parser, utils, values } from '@syuilo/aiscript';
import { aiScriptReadline, createAiScriptEnv } from '@/scripts/aiscript/api.js';
import { inputText } from '@/os.js';
import { Plugin, noteActions, notePostInterruptors, noteViewInterruptors, postFormActions, userActions, pageViewInterruptors } from '@/store.js';

const parser = new Parser();
const pluginContexts = new Map<string, Interpreter>();
export const pluginLogs = ref(new Map<string, string[]>());

export async function install(plugin: Plugin): Promise<void> {
// 後方互換性のため
Expand All @@ -22,21 +24,27 @@ export async function install(plugin: Plugin): Promise<void> {
in: aiScriptReadline,
out: (value): void => {
console.log(value);
pluginLogs.value.get(plugin.id).push(utils.reprValue(value));
},
log: (): void => {
},
err: (err): void => {
pluginLogs.value.get(plugin.id).push(`${err}`);
throw err; // install時のtry-catchに反応させる
},
});

initPlugin({ plugin, aiscript });

try {
await aiscript.exec(parser.parse(plugin.src));
} catch (err) {
console.error('Plugin install failed:', plugin.name, 'v' + plugin.version);
return;
}

console.info('Plugin installed:', plugin.name, 'v' + plugin.version);
aiscript.exec(parser.parse(plugin.src)).then(
() => {
console.info('Plugin installed:', plugin.name, 'v' + plugin.version);
},
(err) => {
console.error('Plugin install failed:', plugin.name, 'v' + plugin.version);
throw err;
},
);
}

function createPluginEnv(opts: { plugin: Plugin; storageKey: string }): Record<string, values.Value> {
Expand Down Expand Up @@ -92,6 +100,7 @@ function createPluginEnv(opts: { plugin: Plugin; storageKey: string }): Record<s

function initPlugin({ plugin, aiscript }): void {
pluginContexts.set(plugin.id, aiscript);
pluginLogs.value.set(plugin.id, []);
}

function registerPostFormAction({ pluginId, title, handler }): void {
Expand Down

1 comment on commit 88d47ab

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chromatic detects changes. Please review the changes on Chromatic.

Please sign in to comment.