-
Notifications
You must be signed in to change notification settings - Fork 5
/
version_shimmer.go
59 lines (45 loc) · 1.61 KB
/
version_shimmer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package bundler
import (
"fmt"
"os"
"path/filepath"
"github.com/paketo-buildpacks/packit/v2/fs"
)
// Bundler has an "auto-upgrade" feature that means that when simply invoking
// `bundle` from the command-line, you may receive a version that is not what
// was installed by this buildpack:
// https://bundler.io/guides/bundler_2_upgrade.html#version-autoswitch.
// In order to override this behavior, we need to invoke the `bundle`
// executable specifying a version number as is outlined here:
// https://stackoverflow.com/questions/4373128/how-do-i-activate-a-different-version-of-a-particular-gem#answer-4373478
const VersionShimTemplate = "#!/usr/bin/env sh\nexec %s _%s_ ${@:-}"
type VersionShimmer struct{}
func NewVersionShimmer() VersionShimmer {
return VersionShimmer{}
}
func (s VersionShimmer) Shim(dir, version string) error {
files, err := filepath.Glob(filepath.Join(dir, "*"))
if err != nil {
return fmt.Errorf("failed to shim bundler executables: %w", err)
}
for _, file := range files {
info, err := os.Stat(file)
if err != nil {
return fmt.Errorf("failed to shim bundler executables: %w", err)
}
if info.Mode()&0111 == 0 || info.IsDir() {
continue
}
original := filepath.Join(filepath.Dir(file), fmt.Sprintf("_%s", filepath.Base(file)))
err = fs.Move(file, original)
if err != nil {
return fmt.Errorf("failed to move bundler executables: %w", err)
}
content := fmt.Sprintf(VersionShimTemplate, original, version)
err = os.WriteFile(file, []byte(content), 0755)
if err != nil {
return fmt.Errorf("failed to rewrite bundler executables: %w", err)
}
}
return nil
}