htmgo/cli/tasks/copyassets/bundle.go

120 lines
2.8 KiB
Go
Raw Normal View History

package copyassets
2024-09-13 19:54:19 +00:00
import (
"fmt"
"golang.org/x/mod/modfile"
"io"
"log"
"os"
"path/filepath"
)
func getModuleVersion(modulePath string) (string, error) {
// Read the go.mod file
data, err := os.ReadFile("go.mod")
if err != nil {
return "", fmt.Errorf("error reading go.mod: %v", err)
}
// Parse the go.mod file
modFile, err := modfile.Parse("go.mod", data, nil)
if err != nil {
return "", fmt.Errorf("error parsing go.mod: %v", err)
}
// Find the module version
for _, req := range modFile.Require {
if req.Mod.Path == modulePath {
return req.Mod.Version, nil
}
}
return "", fmt.Errorf("module %s not found in go.mod", modulePath)
}
func copyFile(src, dst string) error {
// Open the source file for reading.
srcFile, err := os.Open(src)
if err != nil {
return fmt.Errorf("failed to open source file: %v", err)
}
defer srcFile.Close()
// Create the destination file.
dstFile, err := os.Create(dst)
if err != nil {
return fmt.Errorf("failed to create destination file: %v", err)
}
defer dstFile.Close()
// Copy the content from srcFile to dstFile.
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return fmt.Errorf("failed to copy file contents: %v", err)
}
return nil
}
// copyDir copies a directory recursively from src to dst.
func copyDir(srcDir, dstDir string) error {
// Walk the source directory tree.
return filepath.Walk(srcDir, func(srcPath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Construct the corresponding destination path.
relPath, err := filepath.Rel(srcDir, srcPath)
if err != nil {
return err
}
dstPath := filepath.Join(dstDir, relPath)
if info.IsDir() {
// If it's a directory, create the corresponding directory in the destination.
err := os.MkdirAll(dstPath, 0700)
if err != nil {
return fmt.Errorf("failed to create directory: %v", err)
}
} else {
// If it's a file, copy the file.
err := copyFile(srcPath, dstPath)
if err != nil {
return err
}
}
return nil
})
}
func CopyAssets() {
2024-09-14 00:05:55 +00:00
modulePath := "github.com/maddalax/htmgo/framework"
2024-09-13 19:54:19 +00:00
version, err := getModuleVersion(modulePath)
if err != nil {
log.Fatalf("Error: %v", err)
}
dirname, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
2024-09-14 01:50:24 +00:00
assetDir := fmt.Sprintf("%s/go/pkg/mod/%s@%s/assets", dirname, modulePath, version)
assetDistDir := fmt.Sprintf("%s/dist", assetDir)
assetCssDir := fmt.Sprintf("%s/css", assetDir)
2024-09-13 19:54:19 +00:00
cwd, err := os.Getwd()
if err != nil {
log.Fatal("failed to get cwd")
}
2024-09-14 01:50:24 +00:00
destDir := fmt.Sprintf("%s/assets", cwd)
destDirDist := fmt.Sprintf("%s/dist", destDir)
destDirCss := fmt.Sprintf("%s/css", destDir)
2024-09-13 19:54:19 +00:00
2024-09-14 01:50:24 +00:00
err = copyDir(assetDistDir, destDirDist)
err = copyDir(assetCssDir, destDirCss)
2024-09-13 19:54:19 +00:00
if err != nil {
2024-09-14 01:50:24 +00:00
log.Fatalf("Error: %v", err)
2024-09-13 19:54:19 +00:00
}
fmt.Printf("successfully copied assets to %s\n", destDir)
}