Skip to content

Commit

Permalink
refactor: split files (#2097)
Browse files Browse the repository at this point in the history
* refactor: split files

* refactor: refactor windows_ext

* refactor: refactor
  • Loading branch information
suzuki-shunsuke authored Jul 16, 2023
1 parent 7e858cb commit f8bf5a5
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 87 deletions.
96 changes: 9 additions & 87 deletions pkg/config/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import (
"github.com/aquaproj/aqua/v2/pkg/runtime"
"github.com/aquaproj/aqua/v2/pkg/template"
"github.com/aquaproj/aqua/v2/pkg/unarchive"
"github.com/aquaproj/aqua/v2/pkg/util"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
)

type Package struct {
Expand All @@ -24,38 +21,6 @@ type Package struct {
Registry *aqua.Registry
}

func (cpkg *Package) RenameFile(logE *logrus.Entry, fs afero.Fs, pkgPath string, file *registry.File, rt *runtime.Runtime) (string, error) {
s, err := cpkg.getFileSrc(file, rt)
if err != nil {
return "", err
}
if !(isWindows(rt.GOOS) && util.Ext(s, cpkg.Package.Version) == "") {
return s, nil
}
newName := s + cpkg.windowsExt()
newPath := filepath.Join(pkgPath, newName)
if s == newName {
return newName, nil
}
if _, err := fs.Stat(newPath); err == nil {
return newName, nil
}
old := filepath.Join(pkgPath, s)
if _, err := fs.Stat(old); err != nil {
return "", &FileNotFoundError{
Err: err,
}
}
logE.WithFields(logrus.Fields{
"new": newPath,
"old": old,
}).Info("rename a file")
if err := fs.Rename(old, newPath); err != nil {
return "", fmt.Errorf("rename a file: %w", err)
}
return newName, nil
}

func (cpkg *Package) GetExePath(rootDir string, file *registry.File, rt *runtime.Runtime) (string, error) {
pkgPath, err := cpkg.GetPkgPath(rootDir, rt)
if err != nil {
Expand All @@ -76,18 +41,10 @@ func (cpkg *Package) RenderAsset(rt *runtime.Runtime) (string, error) {
if asset == "" {
return "", nil
}
if isWindows(rt.GOOS) && !strings.HasSuffix(asset, ".exe") {
if cpkg.PackageInfo.Format == "raw" {
return cpkg.completeWindowsExt(asset), nil
}
if cpkg.PackageInfo.Format != "" {
return asset, nil
}
if util.Ext(asset, cpkg.Package.Version) == "" {
return cpkg.completeWindowsExt(asset), nil
}
if !isWindows(rt.GOOS) {
return asset, nil
}
return asset, nil
return cpkg.completeWindowsExtToAsset(asset), nil
}

func (cpkg *Package) GetTemplateArtifact(rt *runtime.Runtime, asset string) *template.Artifact {
Expand Down Expand Up @@ -160,18 +117,10 @@ func (cpkg *Package) RenderURL(rt *runtime.Runtime) (string, error) {
if err != nil {
return "", err
}
if isWindows(rt.GOOS) && !strings.HasSuffix(s, ".exe") {
if cpkg.PackageInfo.Format == "raw" {
return cpkg.completeWindowsExt(s), nil
}
if cpkg.PackageInfo.Format != "" {
return s, nil
}
if util.Ext(s, cpkg.Package.Version) == "" {
return cpkg.completeWindowsExt(s), nil
}
if !isWindows(rt.GOOS) {
return s, nil
}
return s, nil
return cpkg.completeWindowsExtToURL(s), nil
}

type FileNotFoundError struct {
Expand All @@ -186,10 +135,6 @@ func (errorFileNotFound *FileNotFoundError) Unwrap() error {
return errorFileNotFound.Err
}

func isWindows(goos string) bool {
return goos == "windows"
}

func (cpkg *Package) renderSrc(file *registry.File, rt *runtime.Runtime) (string, error) {
pkg := cpkg.Package
pkgInfo := cpkg.PackageInfo
Expand Down Expand Up @@ -230,10 +175,10 @@ func (cpkg *Package) getFileSrc(file *registry.File, rt *runtime.Runtime) (strin
if err != nil {
return "", err
}
if isWindows(rt.GOOS) && util.Ext(s, cpkg.Package.Version) == "" {
return s + cpkg.windowsExt(), nil
if !isWindows(rt.GOOS) {
return s, nil
}
return s, nil
return cpkg.completeWindowsExtToFileSrc(s), nil
}

func (cpkg *Package) getFileSrcWithoutWindowsExt(file *registry.File, rt *runtime.Runtime) (string, error) {
Expand Down Expand Up @@ -374,29 +319,6 @@ func (cpkg *Package) renderTemplate(tpl *texttemplate.Template, rt *runtime.Runt
return uS, nil
}

func (cpkg *Package) windowsExt() string {
if cpkg.PackageInfo.WindowsExt == "" {
if cpkg.PackageInfo.Type == registry.PkgInfoTypeGitHubContent || cpkg.PackageInfo.Type == registry.PkgInfoTypeGitHubArchive {
return ".sh"
}
return ".exe"
}
return cpkg.PackageInfo.WindowsExt
}

func (cpkg *Package) completeWindowsExt(s string) string {
if cpkg.PackageInfo.CompleteWindowsExt != nil {
if *cpkg.PackageInfo.CompleteWindowsExt {
return s + cpkg.windowsExt()
}
return s
}
if cpkg.PackageInfo.Type == registry.PkgInfoTypeGitHubContent || cpkg.PackageInfo.Type == registry.PkgInfoTypeGitHubArchive {
return s
}
return s + cpkg.windowsExt()
}

func (cpkg *Package) semVer() string {
v := cpkg.Package.Version
prefix := cpkg.PackageInfo.GetVersionPrefix()
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package config

func isWindows(goos string) bool {
return goos == "windows"
}
95 changes: 95 additions & 0 deletions pkg/config/windows_ext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package config

import (
"fmt"
"path/filepath"
"strings"

"github.com/aquaproj/aqua/v2/pkg/config/registry"
"github.com/aquaproj/aqua/v2/pkg/runtime"
"github.com/aquaproj/aqua/v2/pkg/util"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
)

func (cpkg *Package) RenameFile(logE *logrus.Entry, fs afero.Fs, pkgPath string, file *registry.File, rt *runtime.Runtime) (string, error) {
s, err := cpkg.getFileSrc(file, rt)
if err != nil {
return "", err
}
if !(isWindows(rt.GOOS) && util.Ext(s, cpkg.Package.Version) == "") {
return s, nil
}
newName := s + cpkg.windowsExt()
newPath := filepath.Join(pkgPath, newName)
if s == newName {
return newName, nil
}
if _, err := fs.Stat(newPath); err == nil {
return newName, nil
}
old := filepath.Join(pkgPath, s)
if _, err := fs.Stat(old); err != nil {
return "", &FileNotFoundError{
Err: err,
}
}
logE.WithFields(logrus.Fields{
"new": newPath,
"old": old,
}).Info("rename a file")
if err := fs.Rename(old, newPath); err != nil {
return "", fmt.Errorf("rename a file: %w", err)
}
return newName, nil
}

func (cpkg *Package) windowsExt() string {
if cpkg.PackageInfo.WindowsExt == "" {
if cpkg.PackageInfo.Type == registry.PkgInfoTypeGitHubContent || cpkg.PackageInfo.Type == registry.PkgInfoTypeGitHubArchive {
return ".sh"
}
return ".exe"
}
return cpkg.PackageInfo.WindowsExt
}

func (cpkg *Package) completeWindowsExt(s string) string {
if cpkg.PackageInfo.CompleteWindowsExt != nil {
if *cpkg.PackageInfo.CompleteWindowsExt {
return s + cpkg.windowsExt()
}
return s
}
if cpkg.PackageInfo.Type == registry.PkgInfoTypeGitHubContent || cpkg.PackageInfo.Type == registry.PkgInfoTypeGitHubArchive {
return s
}
return s + cpkg.windowsExt()
}

func (cpkg *Package) completeWindowsExtToAsset(asset string) string {
if strings.HasSuffix(asset, ".exe") {
return asset
}
if cpkg.PackageInfo.Format == "raw" {
return cpkg.completeWindowsExt(asset)
}
if cpkg.PackageInfo.Format != "" {
return asset
}
if util.Ext(asset, cpkg.Package.Version) == "" {
return cpkg.completeWindowsExt(asset)
}
return asset
}

func (cpkg *Package) completeWindowsExtToURL(url string) string {
return cpkg.completeWindowsExtToAsset(url)
}

func (cpkg *Package) completeWindowsExtToFileSrc(src string) string {
if util.Ext(src, cpkg.Package.Version) == "" {
return src + cpkg.windowsExt()
}
return src
}

0 comments on commit f8bf5a5

Please sign in to comment.