Consolidate artifact declarations
This commit is contained in:
@@ -13,15 +13,13 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/ReleaseKit/internal/artifact"
|
||||
"gitea.maximumdirect.net/eric/ReleaseKit/internal/candidate"
|
||||
"gitea.maximumdirect.net/eric/ReleaseKit/internal/gotool"
|
||||
"gitea.maximumdirect.net/eric/ReleaseKit/internal/versioncontract"
|
||||
)
|
||||
|
||||
type Target struct {
|
||||
OS string
|
||||
Arch string
|
||||
}
|
||||
type Target = artifact.Target
|
||||
|
||||
type Spec struct {
|
||||
MainPackage string
|
||||
@@ -62,7 +60,7 @@ func Build(ctx context.Context, request Request) (Result, error) {
|
||||
Version: request.Version,
|
||||
MainPackage: request.Spec.MainPackage,
|
||||
BinaryName: request.Spec.BinaryName,
|
||||
Targets: normalizeTargets(request.Spec.Targets),
|
||||
Targets: artifact.NormalizeTargets(request.Spec.Targets),
|
||||
}
|
||||
if !versioncontract.ValidStableVersion(request.Version) {
|
||||
return result, fmt.Errorf("%w: %q", candidate.ErrInvalidVersion, request.Version)
|
||||
@@ -74,11 +72,11 @@ func Build(ctx context.Context, request Request) (Result, error) {
|
||||
if request.Spec.MainPackage == "" {
|
||||
return result, errors.New("build requires a main package when targets are selected")
|
||||
}
|
||||
if !safeComponent(request.Spec.BinaryName) {
|
||||
if !artifact.ValidComponent(request.Spec.BinaryName) {
|
||||
return result, fmt.Errorf("unsafe binary name %q", request.Spec.BinaryName)
|
||||
}
|
||||
for _, target := range result.Targets {
|
||||
if !safeComponent(target.OS) || !safeComponent(target.Arch) {
|
||||
if !artifact.ValidComponent(target.OS) || !artifact.ValidComponent(target.Arch) {
|
||||
return result, fmt.Errorf("unsafe target %q/%q", target.OS, target.Arch)
|
||||
}
|
||||
}
|
||||
@@ -107,9 +105,10 @@ func Build(ctx context.Context, request Request) (Result, error) {
|
||||
}
|
||||
|
||||
result.OutputDir = resolvedOutputDir(repositoryDir, request.OutputDir)
|
||||
filenames := make([]string, 0, len(result.Targets))
|
||||
for _, target := range result.Targets {
|
||||
filenames = append(filenames, artifactFilename(request.Spec.BinaryName, request.Version, target))
|
||||
declarations := artifact.ExpectedBuild(request.Spec.BinaryName, request.Version, result.Targets)
|
||||
filenames := make([]string, len(declarations))
|
||||
for i, declaration := range declarations {
|
||||
filenames[i] = declaration.Filename
|
||||
}
|
||||
if err := os.MkdirAll(result.OutputDir, 0o755); err != nil {
|
||||
return result, fmt.Errorf("create build output directory %q: %w", result.OutputDir, err)
|
||||
@@ -188,23 +187,6 @@ func Build(ctx context.Context, request Request) (Result, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func normalizeTargets(targets []Target) []Target {
|
||||
seen := make(map[Target]struct{}, len(targets))
|
||||
result := make([]Target, 0, len(targets))
|
||||
for _, target := range targets {
|
||||
if _, ok := seen[target]; ok {
|
||||
continue
|
||||
}
|
||||
seen[target] = struct{}{}
|
||||
result = append(result, target)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func safeComponent(value string) bool {
|
||||
return value != "" && !strings.ContainsAny(value, "/\\\r\n")
|
||||
}
|
||||
|
||||
func resolvedOutputDir(repositoryDir, outputDir string) string {
|
||||
if outputDir == "" {
|
||||
outputDir = filepath.Join(repositoryDir, "dist")
|
||||
@@ -218,14 +200,6 @@ func resolvedOutputDir(repositoryDir, outputDir string) string {
|
||||
return resolved
|
||||
}
|
||||
|
||||
func artifactFilename(binaryName, version string, target Target) string {
|
||||
filename := fmt.Sprintf("%s-%s-%s-%s", binaryName, version, target.OS, target.Arch)
|
||||
if target.OS == "windows" {
|
||||
filename += ".exe"
|
||||
}
|
||||
return filename
|
||||
}
|
||||
|
||||
func gitCommit(ctx context.Context, repositoryDir string) (string, error) {
|
||||
command := exec.CommandContext(ctx, "git", "-C", repositoryDir, "rev-parse", "--verify", "HEAD^{commit}")
|
||||
output, err := command.Output()
|
||||
|
||||
Reference in New Issue
Block a user