2020-04-12 11:47:46 -03:00
|
|
|
// Package sourcearchive archives the source of the project using git-archive.
|
|
|
|
package sourcearchive
|
|
|
|
|
|
|
|
import (
|
2022-08-25 02:15:37 -03:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-04-12 11:47:46 -03:00
|
|
|
"path/filepath"
|
|
|
|
|
2022-06-21 21:11:15 -03:00
|
|
|
"github.com/caarlos0/log"
|
2022-08-25 01:10:26 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/archivefiles"
|
2020-04-12 11:47:46 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
feat: better archives relative paths (#3656)
with this patch, a config like:
```yaml
archives:
- format: tar.gz
# this name template makes the OS and Arch compatible with the results of uname.
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
rlcp: true
files:
- src: "build/**/*"
dst: .
nfpms:
- package_name: foo
contents:
- src: "build/**/*"
dst: usr/share/foo
formats:
- apk
```
will eval this:
<img width="1384" alt="CleanShot 2022-12-21 at 22 21 00@2x"
src="https://user-images.githubusercontent.com/245435/209034244-7c31b5f7-cfcd-4825-bb2f-7dd463c5286a.png">
as much as I would like to make this the default, it would be a breaking
change, so we really can't do it.
If `dst` is empty, it'll have the same behavior as before (no rlcp), and
if `strip_parent` is set, it will also still have the same behavior.
Finally, if the format is binary, `rlcp` is ignored too (as it doesn't
make sense).
So, this only changes if:
- your format is not binary; and
- you have files with `src` and `dst` set
Then, goreleaser will warn you to set `rlcp: true`.
## todo
- [x] docs
- [x] more tests probably
- [x] any ideas for a better name for the new config option?
fixes #3655
Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
2022-12-27 17:42:55 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/deprecate"
|
2023-04-07 22:53:15 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/gio"
|
2020-04-12 11:47:46 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/git"
|
|
|
|
"github.com/goreleaser/goreleaser/internal/tmpl"
|
2022-08-25 02:15:37 -03:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/archive"
|
2020-04-12 11:47:46 -03:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
|
|
|
)
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// Pipe for source archive.
|
2020-04-12 11:47:46 -03:00
|
|
|
type Pipe struct{}
|
|
|
|
|
|
|
|
func (Pipe) String() string {
|
|
|
|
return "creating source archive"
|
|
|
|
}
|
|
|
|
|
2021-09-18 10:21:29 -03:00
|
|
|
func (Pipe) Skip(ctx *context.Context) bool {
|
|
|
|
return !ctx.Config.Source.Enabled
|
|
|
|
}
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// Run the pipe.
|
2023-04-07 22:53:15 -03:00
|
|
|
func (Pipe) Run(ctx *context.Context) error {
|
|
|
|
format := ctx.Config.Source.Format
|
|
|
|
if format != "zip" && format != "tar" && format != "tgz" && format != "tar.gz" {
|
|
|
|
return fmt.Errorf("invalid source archive format: %s", format)
|
|
|
|
}
|
2020-04-12 11:47:46 -03:00
|
|
|
name, err := tmpl.New(ctx).Apply(ctx.Config.Source.NameTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-07 22:53:15 -03:00
|
|
|
filename := name + "." + format
|
2021-04-19 09:31:57 -03:00
|
|
|
path := filepath.Join(ctx.Config.Dist, filename)
|
2020-04-12 11:47:46 -03:00
|
|
|
log.WithField("file", filename).Info("creating source archive")
|
2023-04-07 22:53:15 -03:00
|
|
|
args := []string{
|
|
|
|
"archive",
|
|
|
|
"-o", path,
|
|
|
|
}
|
2022-08-25 02:15:37 -03:00
|
|
|
|
2023-04-07 22:53:15 -03:00
|
|
|
prefix := ""
|
|
|
|
if ctx.Config.Source.PrefixTemplate != "" {
|
|
|
|
pt, err := tmpl.New(ctx).Apply(ctx.Config.Source.PrefixTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
prefix = pt
|
|
|
|
args = append(args, "--prefix", prefix)
|
2021-11-02 23:57:20 +01:00
|
|
|
}
|
2023-04-07 22:53:15 -03:00
|
|
|
args = append(args, ctx.Git.FullCommit)
|
2022-08-25 01:10:26 -03:00
|
|
|
|
2023-04-07 22:53:15 -03:00
|
|
|
if _, err := git.Clean(git.Run(ctx, args...)); err != nil {
|
2022-08-25 02:15:37 -03:00
|
|
|
return err
|
2021-11-02 23:57:20 +01:00
|
|
|
}
|
2022-08-25 01:10:26 -03:00
|
|
|
|
2023-04-13 11:44:02 -03:00
|
|
|
if len(ctx.Config.Source.Files) > 0 {
|
|
|
|
if err := appendExtraFilesToArchive(ctx, prefix, path, format); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-07 22:53:15 -03:00
|
|
|
}
|
|
|
|
|
2023-04-13 11:44:02 -03:00
|
|
|
ctx.Artifacts.Add(&artifact.Artifact{
|
|
|
|
Type: artifact.UploadableSourceArchive,
|
|
|
|
Name: filename,
|
|
|
|
Path: path,
|
|
|
|
Extra: map[string]interface{}{
|
|
|
|
artifact.ExtraFormat: format,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendExtraFilesToArchive(ctx *context.Context, prefix, path, format string) error {
|
2023-04-07 22:53:15 -03:00
|
|
|
oldPath := path + ".bkp"
|
|
|
|
if err := gio.Copy(path, oldPath); err != nil {
|
|
|
|
return fmt.Errorf("failed make a backup of %q: %w", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// i could spend a lot of time trying to figure out how to append to a tar,
|
|
|
|
// tgz and zip file... but... this seems easy enough :)
|
|
|
|
of, err := os.Open(oldPath)
|
2022-08-25 02:15:37 -03:00
|
|
|
if err != nil {
|
2023-04-07 22:53:15 -03:00
|
|
|
return fmt.Errorf("could not open %q: %w", oldPath, err)
|
|
|
|
}
|
|
|
|
defer of.Close()
|
|
|
|
|
|
|
|
af, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0o644)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not open archive: %w", err)
|
2022-08-25 02:15:37 -03:00
|
|
|
}
|
|
|
|
defer af.Close() //nolint:errcheck
|
|
|
|
|
2023-04-07 22:53:15 -03:00
|
|
|
arch, err := archive.Copying(of, af, format)
|
2022-08-25 02:15:37 -03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-06 13:54:43 -03:00
|
|
|
files, err := archivefiles.Eval(tmpl.New(ctx), ctx.Config.Source.Files)
|
2022-08-25 01:10:26 -03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
2022-08-25 02:15:37 -03:00
|
|
|
f.Destination = filepath.Join(prefix, f.Destination)
|
|
|
|
if err := arch.Add(f); err != nil {
|
|
|
|
return fmt.Errorf("could not add %q to archive: %w", f.Source, err)
|
|
|
|
}
|
2022-08-25 01:10:26 -03:00
|
|
|
}
|
|
|
|
|
2022-08-25 02:15:37 -03:00
|
|
|
if err := arch.Close(); err != nil {
|
|
|
|
return fmt.Errorf("could not close archive file: %w", err)
|
|
|
|
}
|
|
|
|
if err := af.Close(); err != nil {
|
|
|
|
return fmt.Errorf("could not close archive file: %w", err)
|
|
|
|
}
|
2023-04-13 11:44:02 -03:00
|
|
|
return nil
|
2020-04-12 11:47:46 -03:00
|
|
|
}
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// Default sets the pipe defaults.
|
2020-04-12 11:47:46 -03:00
|
|
|
func (Pipe) Default(ctx *context.Context) error {
|
2021-04-19 09:31:57 -03:00
|
|
|
archive := &ctx.Config.Source
|
2020-04-12 11:47:46 -03:00
|
|
|
if archive.Format == "" {
|
|
|
|
archive.Format = "tar.gz"
|
|
|
|
}
|
|
|
|
|
|
|
|
if archive.NameTemplate == "" {
|
|
|
|
archive.NameTemplate = "{{ .ProjectName }}-{{ .Version }}"
|
|
|
|
}
|
feat: better archives relative paths (#3656)
with this patch, a config like:
```yaml
archives:
- format: tar.gz
# this name template makes the OS and Arch compatible with the results of uname.
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
rlcp: true
files:
- src: "build/**/*"
dst: .
nfpms:
- package_name: foo
contents:
- src: "build/**/*"
dst: usr/share/foo
formats:
- apk
```
will eval this:
<img width="1384" alt="CleanShot 2022-12-21 at 22 21 00@2x"
src="https://user-images.githubusercontent.com/245435/209034244-7c31b5f7-cfcd-4825-bb2f-7dd463c5286a.png">
as much as I would like to make this the default, it would be a breaking
change, so we really can't do it.
If `dst` is empty, it'll have the same behavior as before (no rlcp), and
if `strip_parent` is set, it will also still have the same behavior.
Finally, if the format is binary, `rlcp` is ignored too (as it doesn't
make sense).
So, this only changes if:
- your format is not binary; and
- you have files with `src` and `dst` set
Then, goreleaser will warn you to set `rlcp: true`.
## todo
- [x] docs
- [x] more tests probably
- [x] any ideas for a better name for the new config option?
fixes #3655
Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
2022-12-27 17:42:55 -03:00
|
|
|
|
2023-06-06 13:54:43 -03:00
|
|
|
if archive.Enabled && archive.RLCP != "" {
|
|
|
|
deprecate.Notice(ctx, "source.rlcp")
|
feat: better archives relative paths (#3656)
with this patch, a config like:
```yaml
archives:
- format: tar.gz
# this name template makes the OS and Arch compatible with the results of uname.
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
rlcp: true
files:
- src: "build/**/*"
dst: .
nfpms:
- package_name: foo
contents:
- src: "build/**/*"
dst: usr/share/foo
formats:
- apk
```
will eval this:
<img width="1384" alt="CleanShot 2022-12-21 at 22 21 00@2x"
src="https://user-images.githubusercontent.com/245435/209034244-7c31b5f7-cfcd-4825-bb2f-7dd463c5286a.png">
as much as I would like to make this the default, it would be a breaking
change, so we really can't do it.
If `dst` is empty, it'll have the same behavior as before (no rlcp), and
if `strip_parent` is set, it will also still have the same behavior.
Finally, if the format is binary, `rlcp` is ignored too (as it doesn't
make sense).
So, this only changes if:
- your format is not binary; and
- you have files with `src` and `dst` set
Then, goreleaser will warn you to set `rlcp: true`.
## todo
- [x] docs
- [x] more tests probably
- [x] any ideas for a better name for the new config option?
fixes #3655
Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
2022-12-27 17:42:55 -03:00
|
|
|
}
|
2020-04-12 11:47:46 -03:00
|
|
|
return nil
|
|
|
|
}
|