1
1
Fork 0
mirror of https://github.com/goreleaser/nfpm synced 2024-05-07 01:26:08 +02:00

fix: windows issues (#690)

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
Carlos Alexandro Becker 2023-07-12 22:09:28 -03:00 committed by GitHub
parent d710da8c9c
commit d309eb6015
Signed by: GitHub
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 39 additions and 46 deletions

View File

@ -401,26 +401,24 @@ func createBuilderData(info *nfpm.Info, sizep *int64) func(tw *tar.Writer) error
func createFilesInsideTarGz(info *nfpm.Info, tw *tar.Writer, sizep *int64) (err error) {
for _, file := range info.Contents {
file.Destination = strings.TrimPrefix(file.Destination, "./")
file.Destination = files.AsRelativePath(file.Destination)
switch file.Type {
case files.TypeDir, files.TypeImplicitDir:
err = tw.WriteHeader(&tar.Header{
Name: files.AsRelativePath(file.Destination),
Name: file.Destination,
Mode: int64(file.FileInfo.Mode),
Typeflag: tar.TypeDir,
Format: tar.FormatGNU,
Uname: file.FileInfo.Owner,
Gname: file.FileInfo.Group,
ModTime: file.FileInfo.MTime,
})
case files.TypeSymlink:
err = newItemInsideTarGz(tw, []byte{}, &tar.Header{
Name: files.AsRelativePath(file.Destination),
Name: file.Destination,
Linkname: file.Source,
Typeflag: tar.TypeSymlink,
ModTime: file.FileInfo.MTime,
Format: tar.FormatGNU,
})
default:
err = copyToTarAndDigest(file, tw, sizep)

View File

@ -586,7 +586,7 @@ func writeScripts(w io.Writer, scripts map[string]string) error {
return err
}
fl.Close()
_ = fl.Close()
_, err = io.WriteString(w, "\n}\n\n")
if err != nil {

View File

@ -657,7 +657,7 @@ func conffiles(info *nfpm.Info) []byte {
for _, file := range info.Contents {
switch file.Type {
case files.TypeConfig, files.TypeConfigNoReplace:
confs = append(confs, filepath.Join("/", file.Destination))
confs = append(confs, files.NormalizeAbsoluteFilePath(file.Destination))
}
}
return []byte(strings.Join(confs, "\n") + "\n")

View File

@ -283,7 +283,11 @@ func PrepareForPackager(rawContents Contents, umask fs.FileMode, packager string
return nil, fmt.Errorf("add tree: %w", err)
}
case TypeConfig, TypeConfigNoReplace, TypeFile, "":
globbed, err := glob.Glob(content.Source, content.Destination, disableGlobbing)
globbed, err := glob.Glob(
filepath.ToSlash(content.Source),
filepath.ToSlash(content.Destination),
disableGlobbing,
)
if err != nil {
return nil, err
}
@ -474,7 +478,7 @@ func addTree(all map[string]*Content, tree *Content, umask os.FileMode) error {
return err
}
c.Source = linkDestination
c.Source = filepath.ToSlash(strings.TrimPrefix(linkDestination, filepath.VolumeName(linkDestination)))
c.Destination = NormalizeAbsoluteFilePath(destination)
c.Type = TypeSymlink
default:
@ -515,42 +519,20 @@ func ToNixPath(path string) string {
}
// As relative path converts a path to an explicitly relative path starting with
// a dot (e.g. it converts /foo -> ./foo).
// a dot (e.g. it converts /foo -> ./foo and foo -> ./foo).
func AsExplicitRelativePath(path string) string {
if path == "/" {
return "./"
}
cleanedPath := filepath.Clean(path)
end := ""
if len(cleanedPath) > 1 && strings.HasSuffix(path, "/") {
end = "/"
}
if !filepath.IsAbs(cleanedPath) {
cleanedPath = filepath.Join("/", cleanedPath)
}
return "." + cleanedPath + end
return "./" + AsRelativePath(path)
}
// AsRelativePath converts a path to a relative path without a "./" prefix. This
// function leaves trailing slashes to indicate that the path refers to a
// directory.
// directory, and converts the path to Unix path.
func AsRelativePath(path string) string {
cleanedPath := filepath.Clean(path)
end := ""
cleanedPath := strings.TrimLeft(ToNixPath(path), "/")
if len(cleanedPath) > 1 && strings.HasSuffix(path, "/") {
end = "/"
return cleanedPath + "/"
}
if filepath.IsAbs(cleanedPath) {
return strings.TrimLeft(cleanedPath, "/") + end
}
return cleanedPath + end
return cleanedPath
}
// NormalizeAbsoluteFilePath returns an absolute cleaned path separated by

View File

@ -1,8 +1,10 @@
package files_test
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
@ -353,6 +355,9 @@ func withoutImplicitDirs(contents files.Contents) files.Contents {
}
func TestGlobbingWhenFilesHaveBrackets(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("doesn't work on windows")
}
result, err := files.PrepareForPackager(files.Contents{
{
Source: "./testdata/\\{test\\}/",
@ -633,7 +638,7 @@ func TestRelevantFiles(t *testing.T) {
func TestTree(t *testing.T) {
results, err := files.PrepareForPackager(files.Contents{
{
Source: "testdata/tree",
Source: filepath.Join("testdata", "tree"),
Destination: "/base",
Type: files.TypeTree,
},
@ -652,7 +657,7 @@ func TestTree(t *testing.T) {
Type: files.TypeDir,
},
{
Source: "testdata/tree/files/a",
Source: filepath.Join("testdata", "tree", "files", "a"),
Destination: "/base/files/a",
Type: files.TypeFile,
},
@ -662,7 +667,7 @@ func TestTree(t *testing.T) {
Type: files.TypeDir,
},
{
Source: "testdata/tree/files/b/c",
Source: filepath.Join("testdata", "tree", "files", "b", "c"),
Destination: "/base/files/b/c",
Type: files.TypeFile,
},
@ -697,11 +702,14 @@ func withoutFileInfo(contents files.Contents) files.Contents {
}
func TestAsRelativePath(t *testing.T) {
sep := fmt.Sprintf("%c", filepath.Separator)
testCases := map[string]string{
"/etc/foo/": "etc/foo/",
"./etc/foo": "etc/foo",
"./././foo/../bar/": "bar/",
"/": "",
sep: "",
sep + sep: "",
sep + strings.Join([]string{"foo", "bar", "zazz"}, sep): "foo/bar/zazz",
}
for input, expected := range testCases {
@ -710,11 +718,14 @@ func TestAsRelativePath(t *testing.T) {
}
func TestAsExplicitRelativePath(t *testing.T) {
sep := fmt.Sprintf("%c", filepath.Separator)
testCases := map[string]string{
"/etc/foo/": "./etc/foo/",
"./etc/foo": "./etc/foo",
"./././foo/../bar/": "./bar/",
"/": "./",
sep: "./",
sep: "./",
sep + strings.Join([]string{"foo", "bar", "zazz"}, sep): "./foo/bar/zazz",
}
for input, expected := range testCases {

View File

@ -20,7 +20,10 @@ func longestCommonPrefix(strs []string) string {
}
lcp := strs[0]
for _, str := range strs {
lcp = strlcp(lcp, str)
lcp = strlcp(
filepath.ToSlash(lcp),
filepath.ToSlash(str),
)
}
return lcp
}

View File

@ -51,7 +51,7 @@ func TestGlob(t *testing.T) {
files, err := Glob("./testdata/dir_a/dir_b/test_b.txt", "/foo/bar/", false)
require.NoError(t, err)
require.Len(t, files, 1)
require.Equal(t, "/foo/bar/test_b.txt", files["testdata/dir_a/dir_b/test_b.txt"])
require.Equal(t, "/foo/bar/test_b.txt", filepath.ToSlash(files["testdata/dir_a/dir_b/test_b.txt"]))
})
t.Run("to parent", func(t *testing.T) {

View File

@ -7,7 +7,6 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"time"
@ -378,7 +377,7 @@ func createFilesInsideRPM(info *nfpm.Info, rpm *rpmpack.RPM) (err error) {
}
// clean assures that even folders do not have a trailing slash
file.Name = filepath.Clean(file.Name)
file.Name = files.ToNixPath(file.Name)
rpm.AddFile(*file)
}