Skip to content

Commit

Permalink
feat(plugin): copy ui plugins to target when build
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkinStars committed Oct 18, 2023
1 parent d85091b commit caf634b
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 72 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 34 additions & 3 deletions internal/cli/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
20 changes: 0 additions & 20 deletions ui/src/plugins/Demo/demo.go

This file was deleted.

4 changes: 0 additions & 4 deletions ui/src/plugins/Demo/i18n/en_US.yaml

This file was deleted.

9 changes: 0 additions & 9 deletions ui/src/plugins/Demo/i18n/index.ts

This file was deleted.

4 changes: 0 additions & 4 deletions ui/src/plugins/Demo/i18n/zh_CN.yaml

This file was deleted.

27 changes: 0 additions & 27 deletions ui/src/plugins/Demo/index.tsx

This file was deleted.

4 changes: 0 additions & 4 deletions ui/src/plugins/Demo/info.yaml

This file was deleted.

0 comments on commit caf634b

Please sign in to comment.