diff --git a/foundation/console/vendor_publish_command.go b/foundation/console/vendor_publish_command.go index 94be9eb8f..0fa0357c1 100644 --- a/foundation/console/vendor_publish_command.go +++ b/foundation/console/vendor_publish_command.go @@ -155,28 +155,28 @@ func (receiver *VendorPublishCommand) packageDir(packageName string) (string, er func (receiver *VendorPublishCommand) publish(sourcePath, targetPath string, existing, force bool) (map[string]string, error) { result := make(map[string]string) - sourcePathStat, err := os.Stat(sourcePath) + isTargetPathDir := filepath.Ext(targetPath) == "" + isSourcePathDir := filepath.Ext(sourcePath) == "" + + sourceFiles, err := receiver.getSourceFiles(sourcePath) if err != nil { return nil, err } - var sourceFiles []string - if sourcePathStat.IsDir() { - fileInfos, err := os.ReadDir(sourcePath) - if err != nil { - return nil, err - } - for _, fileInfo := range fileInfos { - sourceFiles = append(sourceFiles, filepath.Join(sourcePath, fileInfo.Name())) + for _, sourceFile := range sourceFiles { + relativePath := "" + if isSourcePathDir { + relativePath, err = filepath.Rel(sourcePath, sourceFile) + if err != nil { + return nil, err + } + } else { + relativePath = filepath.Base(sourcePath) } - } else { - sourceFiles = append(sourceFiles, sourcePath) - } - for _, sourceFile := range sourceFiles { targetFile := targetPath - if filepath.Ext(targetFile) == "" { - targetFile = filepath.Join(targetFile, filepath.Base(sourceFile)) + if isTargetPathDir { + targetFile = filepath.Join(targetPath, relativePath) } success, err := receiver.publishFile(sourceFile, targetFile, existing, force) @@ -191,6 +191,41 @@ func (receiver *VendorPublishCommand) publish(sourcePath, targetPath string, exi return result, nil } +func (receiver *VendorPublishCommand) getSourceFiles(sourcePath string) ([]string, error) { + sourcePathStat, err := os.Stat(sourcePath) + if err != nil { + return nil, err + } + + if sourcePathStat.IsDir() { + return receiver.getSourceFilesForDir(sourcePath) + } else { + return []string{sourcePath}, nil + } +} + +func (receiver *VendorPublishCommand) getSourceFilesForDir(sourcePath string) ([]string, error) { + dirEntries, err := os.ReadDir(sourcePath) + if err != nil { + return nil, err + } + + var sourceFiles []string + for _, dirEntry := range dirEntries { + if dirEntry.IsDir() { + sourcePaths, err := receiver.getSourceFilesForDir(filepath.Join(sourcePath, dirEntry.Name())) + if err != nil { + return nil, err + } + sourceFiles = append(sourceFiles, sourcePaths...) + } else { + sourceFiles = append(sourceFiles, filepath.Join(sourcePath, dirEntry.Name())) + } + } + + return sourceFiles, nil +} + func (receiver *VendorPublishCommand) publishFile(sourceFile, targetFile string, existing, force bool) (bool, error) { content, err := os.ReadFile(sourceFile) if err != nil { diff --git a/foundation/console/vendor_publish_command_test.go b/foundation/console/vendor_publish_command_test.go index e113d61da..a4b1f9495 100644 --- a/foundation/console/vendor_publish_command_test.go +++ b/foundation/console/vendor_publish_command_test.go @@ -6,6 +6,8 @@ import ( "testing" "github.com/stretchr/testify/suite" + + "github.com/goravel/framework/support/file" ) type VendorPublishCommandTestSuite struct { @@ -20,6 +22,69 @@ func (s *VendorPublishCommandTestSuite) SetupTest() { } +func (s *VendorPublishCommandTestSuite) TestGetSourceFiles() { + command := &VendorPublishCommand{} + + sourceDir, err := os.MkdirTemp("", "source") + s.Require().Nil(err) + defer func(path string) { + if err := file.Remove(path); err != nil { + panic(err) + } + }(sourceDir) + + sourceFile := filepath.Join(sourceDir, "test.txt") + s.Require().Nil(file.Create(sourceFile, "test")) + sourceFile = filepath.Join(sourceDir, "dir1/test.txt") + s.Require().Nil(file.Create(sourceFile, "test")) + + files, err := command.getSourceFiles(filepath.Join(sourceDir, "test.txt")) + s.Require().NoError(err) + s.ElementsMatch(files, []string{ + filepath.Join(sourceDir, "test.txt"), + }) + + files, err = command.getSourceFiles(sourceDir) + s.Require().NoError(err) + s.ElementsMatch(files, []string{ + filepath.Join(sourceDir, "test.txt"), + filepath.Join(sourceDir, "dir1/test.txt"), + }) +} + +func (s *VendorPublishCommandTestSuite) TestGetSourceFilesForDir() { + command := &VendorPublishCommand{} + + sourceDir, err := os.MkdirTemp("", "source") + s.Require().Nil(err) + defer func(path string) { + if err := file.Remove(path); err != nil { + panic(err) + } + }(sourceDir) + + sourceFile := filepath.Join(sourceDir, "test.txt") + s.Require().Nil(file.Create(sourceFile, "test")) + sourceFile = filepath.Join(sourceDir, "test1.txt") + s.Require().Nil(file.Create(sourceFile, "test")) + sourceFile = filepath.Join(sourceDir, "dir1/test.txt") + s.Require().Nil(file.Create(sourceFile, "test")) + sourceFile = filepath.Join(sourceDir, "dir1/dir11/test.txt") + s.Require().Nil(file.Create(sourceFile, "test")) + sourceFile = filepath.Join(sourceDir, "dir2/test.txt") + s.Require().Nil(file.Create(sourceFile, "test")) + + files, err := command.getSourceFiles(sourceDir) + s.Require().NoError(err) + s.ElementsMatch(files, []string{ + filepath.Join(sourceDir, "test.txt"), + filepath.Join(sourceDir, "test1.txt"), + filepath.Join(sourceDir, "dir1/test.txt"), + filepath.Join(sourceDir, "dir1/dir11/test.txt"), + filepath.Join(sourceDir, "dir2/test.txt"), + }) +} + func (s *VendorPublishCommandTestSuite) TestPathsForPackageOrGroup() { tests := []struct { name string @@ -197,64 +262,71 @@ func (s *VendorPublishCommandTestSuite) TestPublish() { command := &VendorPublishCommand{} // Create temporary source and target directories for testing - sourceData := "test" sourceDir, err := os.MkdirTemp("", "source") - s.Nil(err) + s.Require().Nil(err) defer func(path string) { - err = os.RemoveAll(path) - if err != nil { + if err := file.Remove(path); err != nil { panic(err) } }(sourceDir) targetDir, err := os.MkdirTemp("", "target") - s.Nil(err) - defer func(path string) { - err = os.RemoveAll(path) - if err != nil { - panic(err) - } - }(targetDir) + s.Require().Nil(err) - // source and target are directory sourceFile := filepath.Join(sourceDir, "test.txt") - s.Nil(os.WriteFile(sourceFile, []byte(sourceData), 0644)) - targetDir = filepath.Join(targetDir, "test") + s.Require().Nil(file.Create(sourceFile, "test")) + sourceFile = filepath.Join(sourceDir, "test1.txt") + s.Require().Nil(file.Create(sourceFile, "test")) + sourceFile = filepath.Join(sourceDir, "dir1/test.txt") + s.Require().Nil(file.Create(sourceFile, "test")) + sourceFile = filepath.Join(sourceDir, "dir2/test.txt") + s.Require().Nil(file.Create(sourceFile, "test")) + // source and target are directory result, err := command.publish(sourceDir, targetDir, false, false) - s.Nil(err) - s.Equal(1, len(result)) + s.Require().Nil(err) + s.Require().Equal(4, len(result)) - targetFile := filepath.Join(targetDir, "test.txt") - content, err := os.ReadFile(targetFile) - s.Nil(err) - s.Equal(sourceData, string(content)) + content, err := os.ReadFile(filepath.Join(targetDir, "test.txt")) + s.Require().Nil(err) + s.Equal("test", string(content)) + content, err = os.ReadFile(filepath.Join(targetDir, "test1.txt")) + s.Require().Nil(err) + s.Equal("test", string(content)) + content, err = os.ReadFile(filepath.Join(targetDir, "dir1/test.txt")) + s.Require().Nil(err) + s.Equal("test", string(content)) + content, err = os.ReadFile(filepath.Join(targetDir, "dir2/test.txt")) + s.Require().Nil(err) + s.Equal("test", string(content)) - // source is file and target is directory - sourceFile = filepath.Join(sourceDir, "test1.txt") - s.Nil(os.WriteFile(sourceFile, []byte(sourceData), 0644)) + s.Require().Nil(file.Remove(targetDir)) + // source is file and target is directory + sourceFile = filepath.Join(sourceDir, "test.txt") result, err = command.publish(sourceFile, targetDir, false, false) s.Nil(err) s.Equal(1, len(result)) - targetFile = filepath.Join(targetDir, "test1.txt") - content, err = os.ReadFile(targetFile) - s.Nil(err) + content, err = os.ReadFile(filepath.Join(targetDir, "test.txt")) + s.Require().Nil(err) s.Equal("test", string(content)) + s.Require().Nil(file.Remove(targetDir)) + // source and target are file - sourceFile = filepath.Join(sourceDir, "test2.txt") - s.Nil(os.WriteFile(sourceFile, []byte(sourceData), 0644)) - targetFile = filepath.Join(targetDir, "test3.txt") + sourceFile = filepath.Join(sourceDir, "test.txt") + targetFile := filepath.Join(targetDir, "test.txt") result, err = command.publish(sourceFile, targetFile, false, false) s.Nil(err) s.Equal(1, len(result)) content, err = os.ReadFile(targetFile) - s.Nil(err) + s.Require().Nil(err) s.Equal("test", string(content)) + + s.Require().Nil(file.Remove(targetDir)) } func (s *VendorPublishCommandTestSuite) TestPublishFile() { diff --git a/support/constant.go b/support/constant.go index 1a86c3225..10cc0939a 100644 --- a/support/constant.go +++ b/support/constant.go @@ -1,6 +1,6 @@ package support -const Version string = "v1.13.8" +const Version string = "v1.13.9" const ( EnvRuntime = "runtime"