diff --git a/Makefile b/Makefile index 3db1f7342..5acae2ae2 100644 --- a/Makefile +++ b/Makefile @@ -41,6 +41,6 @@ install-ui-packages: @corepack prepare pnpm@latest --activate ui: - @cd ui && pnpm install && pnpm build && cd - + @cd ui && pnpm pre-install && pnpm build && cd - all: clean build diff --git a/internal/cli/build.go b/internal/cli/build.go index 1c11d922c..14c86b5c8 100644 --- a/internal/cli/build.go +++ b/internal/cli/build.go @@ -205,12 +205,23 @@ func copyUIFiles(b *buildingMaterial) (err error) { return fmt.Errorf("failed to run go list: %w", err) } - goModUIDir := filepath.Join(strings.TrimSpace(buf.String()), "ui") + answerDir := strings.TrimSpace(buf.String()) + goModUIDir := filepath.Join(answerDir, "ui") localUIBuildDir := filepath.Join(b.tmpDir, "vendor/github.com/answerdev/answer/ui/") // The node_modules folder generated during development will interfere packaging, so it needs to be ignored. if err = copyDirEntries(os.DirFS(goModUIDir), ".", localUIBuildDir, "node_modules"); err != nil { return fmt.Errorf("failed to copy ui files: %w", err) } + + pluginsDir := filepath.Join(b.tmpDir, "vendor/github.com/answerdev/plugins/") + localUIPluginDir := filepath.Join(localUIBuildDir, "src/plugins/") + + // copy plugins dir + fmt.Printf("try to copy dir from %s to %s\n", pluginsDir, localUIPluginDir) + if err = copyDirEntries(os.DirFS(pluginsDir), ".", localUIPluginDir); err != nil { + return fmt.Errorf("failed to copy ui files: %w", err) + } + formatUIPluginsDirName(localUIPluginDir) return nil } @@ -249,7 +260,7 @@ func generateIndexTsContent(folders []string) string { builder := &strings.Builder{} builder.WriteString("export default null;\n\n") for _, folder := range folders { - builder.WriteString(fmt.Sprintf("export { default as %s } from './%s';\n", folder, folder)) + builder.WriteString(fmt.Sprintf("export { default as %s } from '%s';\n", folder, folder)) } return builder.String() } @@ -258,7 +269,7 @@ func generateIndexTsContent(folders []string) string { func buildUI(b *buildingMaterial) (err error) { localUIBuildDir := filepath.Join(b.tmpDir, "vendor/github.com/answerdev/answer/ui") - pnpmInstallCmd := b.newExecCmd("pnpm", "install") + pnpmInstallCmd := b.newExecCmd("pnpm", "pre-install") pnpmInstallCmd.Dir = localUIBuildDir if err = pnpmInstallCmd.Run(); err != nil { return err @@ -432,6 +443,26 @@ func copyDirEntries(sourceFs fs.FS, sourceDir, targetDir string, ignoreDir ...st return err } +// format plugins dir name from dash to underline +func formatUIPluginsDirName(dirPath string) { + entries, err := os.ReadDir(dirPath) + if err != nil { + fmt.Printf("read ui plugins dir failed: [%s] %s\n", dirPath, err) + return + } + for _, entry := range entries { + if !entry.IsDir() || !strings.Contains(entry.Name(), "-") { + continue + } + newName := strings.ReplaceAll(entry.Name(), "-", "_") + if err := os.Rename(filepath.Join(dirPath, entry.Name()), filepath.Join(dirPath, newName)); err != nil { + fmt.Printf("rename ui plugins dir failed: [%s] %s\n", dirPath, err) + } else { + fmt.Printf("rename ui plugins dir success: [%s] -> [%s]\n", entry.Name(), newName) + } + } +} + // buildBinary build binary file func buildBinary(b *buildingMaterial) (err error) { versionInfo := b.originalAnswerInfo diff --git a/ui/src/plugins/Demo/demo.go b/ui/src/plugins/Demo/demo.go deleted file mode 100644 index 2f1795f13..000000000 --- a/ui/src/plugins/Demo/demo.go +++ /dev/null @@ -1,20 +0,0 @@ -package demo - -import "github.com/answerdev/answer/plugin" - -type DemoPlugin struct { -} - -func init() { - plugin.Register(&DemoPlugin{}) -} - -func (d DemoPlugin) Info() plugin.Info { - return plugin.Info{ - Name: plugin.MakeTranslator("i18n.demo.name"), - SlugName: "demo_plugin", - Description: plugin.MakeTranslator("i18n.demo.description"), - Author: "answerdev", - Version: "0.0.1", - } -} diff --git a/ui/src/plugins/Demo/i18n/en_US.yaml b/ui/src/plugins/Demo/i18n/en_US.yaml deleted file mode 100644 index b4ee42863..000000000 --- a/ui/src/plugins/Demo/i18n/en_US.yaml +++ /dev/null @@ -1,4 +0,0 @@ -plugin: - ui_plugin_demo: - ui: - msg: UI Plugin Demo diff --git a/ui/src/plugins/Demo/i18n/index.ts b/ui/src/plugins/Demo/i18n/index.ts deleted file mode 100644 index 68eb45908..000000000 --- a/ui/src/plugins/Demo/i18n/index.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { initI18nResource } from '@/utils/pluginKit/utils'; - -import en_US from './en_US.yaml'; -import zh_CN from './zh_CN.yaml'; - -initI18nResource({ - en_US, - zh_CN, -}); diff --git a/ui/src/plugins/Demo/i18n/zh_CN.yaml b/ui/src/plugins/Demo/i18n/zh_CN.yaml deleted file mode 100644 index 099e3bd96..000000000 --- a/ui/src/plugins/Demo/i18n/zh_CN.yaml +++ /dev/null @@ -1,4 +0,0 @@ -plugin: - ui_plugin_demo: - ui: - msg: UI 插件示例 diff --git a/ui/src/plugins/Demo/index.tsx b/ui/src/plugins/Demo/index.tsx deleted file mode 100644 index 016d9421e..000000000 --- a/ui/src/plugins/Demo/index.tsx +++ /dev/null @@ -1,27 +0,0 @@ -import { memo, FC } from 'react'; -import { useTranslation } from 'react-i18next'; -import { Alert } from 'react-bootstrap'; - -import { PluginInfo } from '@/utils/pluginKit'; -import { getTransNs, getTransKeyPrefix } from '@/utils/pluginKit/utils'; - -import './i18n'; - -import info from './info.yaml'; - -const pluginInfo: PluginInfo = { - slug_name: info.slug_name, - type: info.type, -}; - -const Index: FC = () => { - const { t } = useTranslation(getTransNs(), { - keyPrefix: getTransKeyPrefix(pluginInfo), - }); - - return {t('msg')}; -}; -export default { - info: pluginInfo, - component: memo(Index), -}; diff --git a/ui/src/plugins/Demo/info.yaml b/ui/src/plugins/Demo/info.yaml deleted file mode 100644 index 17d31a9e6..000000000 --- a/ui/src/plugins/Demo/info.yaml +++ /dev/null @@ -1,4 +0,0 @@ -slug_name: ui_plugin_demo -version: 0.0.1 -author: Answer.dev -