Skip to content

Commit ba147be

Browse files
authored
Merge pull request #19 from Lotti/hotfix/version-rework
Version rework
2 parents 22a8747 + 312a1d3 commit ba147be

File tree

7 files changed

+82
-233
lines changed

7 files changed

+82
-233
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,10 @@ pvm install 8.2
4141
> The install command will automatically determine the newest minor/patch versions if they are not specified
4242
4343
Will install PHP 8.2 at the latest patch.
44+
45+
## Build
46+
47+
To compile the program use:
48+
```shell
49+
GOOS=windows GOARCH=amd64 go build -o pvm.exe
50+
```

commands/help.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
func Help(notFoundError bool) {
99
theme.Title("pvm: PHP Version Manager")
10-
theme.Info("Version 1.1")
10+
theme.Info("Version 1.1.1")
1111

1212
if notFoundError {
1313
theme.Error("Command not found")

commands/install.go

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@ import (
1414
"strings"
1515
)
1616

17-
type Version struct {
18-
Major string
19-
Minor string
20-
Patch string
21-
Url string
22-
ThreadSafe bool
23-
}
24-
2517
func Install(args []string) {
2618
if len(args) < 2 {
2719
theme.Error("You must specify a version to install.")
@@ -48,7 +40,7 @@ func Install(args []string) {
4840
theme.Warning("Non-thread safe version will be installed")
4941
}
5042

51-
desiredVersionNumbers := common.GetVersion(args[1])
43+
desiredVersionNumbers := common.GetVersion(args[1], desireThreadSafe, "")
5244

5345
if desiredVersionNumbers == (common.Version{}) {
5446
theme.Error("Invalid version specified")
@@ -77,7 +69,7 @@ func Install(args []string) {
7769
re := regexp.MustCompile(`<A HREF="([a-zA-Z0-9./-]+)">([a-zA-Z0-9./-]+)</A>`)
7870
matches := re.FindAllStringSubmatch(sb, -1)
7971

80-
versions := make([]Version, 0)
72+
versions := make([]common.Version, 0)
8173

8274
for _, match := range matches {
8375
url := match[1]
@@ -118,44 +110,31 @@ func Install(args []string) {
118110
continue
119111
}
120112

121-
// regex match name
122-
versionNumbers := common.GetVersion(name)
123-
124-
major := versionNumbers.Major
125-
minor := versionNumbers.Minor
126-
patch := versionNumbers.Patch
127-
128-
// push to versions
129-
versions = append(versions, Version{
130-
Major: major,
131-
Minor: minor,
132-
Patch: patch,
133-
Url: url,
134-
ThreadSafe: threadSafe,
135-
})
113+
// regex match name and push to versions
114+
versions = append(versions, common.GetVersion(name, threadSafe, url))
136115
}
137116

138117
// find desired version
139-
var desiredVersion Version
118+
var desiredVersion common.Version
140119

141-
if desiredMajorVersion != "" && desiredMinorVersion != "" && desiredPatchVersion != "" {
120+
if desiredMajorVersion > -1 && desiredMinorVersion > -1 && desiredPatchVersion > -1 {
142121
desiredVersion = FindExactVersion(versions, desiredMajorVersion, desiredMinorVersion, desiredPatchVersion, desireThreadSafe)
143122
}
144123

145-
if desiredMajorVersion != "" && desiredMinorVersion != "" && desiredPatchVersion == "" {
124+
if desiredMajorVersion > -1 && desiredMinorVersion > -1 && desiredPatchVersion == -1 {
146125
desiredVersion = FindLatestPatch(versions, desiredMajorVersion, desiredMinorVersion, desireThreadSafe)
147126
}
148127

149-
if desiredMajorVersion != "" && desiredMinorVersion == "" && desiredPatchVersion == "" {
128+
if desiredMajorVersion > -1 && desiredMinorVersion == -1 && desiredPatchVersion == -1 {
150129
desiredVersion = FindLatestMinor(versions, desiredMajorVersion, desireThreadSafe)
151130
}
152131

153-
if desiredVersion == (Version{}) {
154-
theme.Error("Could not find the desired version: " + args[1] + " " + threadSafeString)
132+
if desiredVersion == (common.Version{}) {
133+
theme.Error(fmt.Sprintf("Could not find the desired version: %s %s", args[1], threadSafeString))
155134
return
156135
}
157136

158-
fmt.Println("Installing PHP " + desiredVersion.Major + "." + desiredVersion.Minor + "." + desiredVersion.Patch + " " + threadSafeString)
137+
fmt.Printf("Installing PHP %s\n", desiredVersion)
159138

160139
homeDir, err := os.UserHomeDir()
161140

@@ -190,7 +169,7 @@ func Install(args []string) {
190169

191170
// check if zip already exists
192171
if _, err := os.Stat(homeDir + "/.pvm/versions/" + zipFileName); err == nil {
193-
theme.Error("PHP " + desiredVersion.Major + "." + desiredVersion.Minor + "." + desiredVersion.Patch + " " + threadSafeString + " already exists")
172+
theme.Error(fmt.Sprintf("PHP %s already exists", desiredVersion))
194173
return
195174
}
196175

@@ -222,7 +201,7 @@ func Install(args []string) {
222201
log.Fatalln(err)
223202
}
224203

225-
theme.Success("Finished installing PHP " + desiredVersion.Major + "." + desiredVersion.Minor + "." + desiredVersion.Patch + " " + threadSafeString)
204+
theme.Success(fmt.Sprintf("Finished installing PHP %s", desiredVersion))
226205
}
227206

228207
func Unzip(src, dest string) error {
@@ -289,7 +268,7 @@ func Unzip(src, dest string) error {
289268
return nil
290269
}
291270

292-
func FindExactVersion(versions []Version, major string, minor string, patch string, threadSafe bool) Version {
271+
func FindExactVersion(versions []common.Version, major int, minor int, patch int, threadSafe bool) common.Version {
293272
for _, version := range versions {
294273
if version.ThreadSafe != threadSafe {
295274
continue
@@ -299,18 +278,18 @@ func FindExactVersion(versions []Version, major string, minor string, patch stri
299278
}
300279
}
301280

302-
return Version{}
281+
return common.Version{}
303282
}
304283

305-
func FindLatestPatch(versions []Version, major string, minor string, threadSafe bool) Version {
306-
latestPatch := Version{}
284+
func FindLatestPatch(versions []common.Version, major int, minor int, threadSafe bool) common.Version {
285+
latestPatch := common.Version{}
307286

308287
for _, version := range versions {
309288
if version.ThreadSafe != threadSafe {
310289
continue
311290
}
312291
if version.Major == major && version.Minor == minor {
313-
if latestPatch.Patch == "" || version.Patch > latestPatch.Patch {
292+
if latestPatch.Patch == -1 || version.Patch > latestPatch.Patch {
314293
latestPatch = version
315294
}
316295
}
@@ -319,16 +298,16 @@ func FindLatestPatch(versions []Version, major string, minor string, threadSafe
319298
return latestPatch
320299
}
321300

322-
func FindLatestMinor(versions []Version, major string, threadSafe bool) Version {
323-
latestMinor := Version{}
301+
func FindLatestMinor(versions []common.Version, major int, threadSafe bool) common.Version {
302+
latestMinor := common.Version{}
324303

325304
for _, version := range versions {
326305
if version.ThreadSafe != threadSafe {
327306
continue
328307
}
329308
if version.Major == major {
330-
if latestMinor.Minor == "" || version.Minor > latestMinor.Minor {
331-
if latestMinor.Patch == "" || version.Patch > latestMinor.Patch {
309+
if latestMinor.Minor == -1 || version.Minor > latestMinor.Minor {
310+
if latestMinor.Patch == -1 || version.Patch > latestMinor.Patch {
332311
latestMinor = version
333312
}
334313
}

commands/list.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"hjbdev/pvm/theme"
55
"log"
66
"os"
7-
87
"github.com/fatih/color"
98
)
109

commands/use.go

Lines changed: 20 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"os"
99
"os/exec"
1010
"path/filepath"
11-
"sort"
1211
"strings"
1312
)
1413

@@ -57,61 +56,32 @@ func Use(args []string) {
5756
log.Fatalln(err)
5857
}
5958

60-
// transform to easily sortable slice
61-
var availableVersions []versionMeta
62-
for i, version := range versions {
63-
availableVersions = append(availableVersions, versionMeta{
64-
number: common.GetVersion(version.Name()),
65-
folder: versions[i],
66-
})
67-
}
68-
69-
// check if version exists
7059
var selectedVersion *versionMeta
71-
for _, version := range availableVersions {
72-
if version.number.Major+"."+version.number.Minor+"."+version.number.Patch == args[0] {
73-
if threadSafe && !strings.Contains(version.folder.Name(), "nts") {
74-
selectedVersion = &versionMeta{
75-
number: version.number,
76-
folder: version.folder,
77-
}
78-
} else if !threadSafe && strings.Contains(version.folder.Name(), "nts") {
79-
selectedVersion = &versionMeta{
80-
number: version.number,
81-
folder: version.folder,
82-
}
60+
// loop over all found installed versions
61+
for i, version := range versions {
62+
safe := true
63+
if strings.Contains(version.Name(), "nts") || strings.Contains(version.Name(), "NTS") {
64+
safe = false
65+
}
66+
foundVersion := common.GetVersion(version.Name(), safe, "")
67+
if threadSafe == foundVersion.ThreadSafe && strings.HasPrefix(foundVersion.String(), args[0]) {
68+
selectedVersion = &versionMeta{
69+
number: foundVersion,
70+
folder: versions[i],
8371
}
8472
}
8573
}
8674

87-
// if patch version is not specified, use the newest matching major.minor
8875
if selectedVersion == nil {
89-
// Sort by newest patch first
90-
availableVersions = sortVersions(availableVersions)
91-
92-
for _, version := range availableVersions {
93-
if version.number.Major+"."+version.number.Minor == args[0] {
94-
if threadSafe && !strings.Contains(version.folder.Name(), "nts") {
95-
selectedVersion = &versionMeta{
96-
number: version.number,
97-
folder: version.folder,
98-
}
99-
} else if !threadSafe && strings.Contains(version.folder.Name(), "nts") {
100-
selectedVersion = &versionMeta{
101-
number: version.number,
102-
folder: version.folder,
103-
}
104-
}
105-
break
106-
}
107-
}
76+
theme.Error("The specified version is not installed.")
77+
return
78+
}
10879

109-
if selectedVersion == nil {
110-
theme.Error("The specified version is not installed.")
111-
return
112-
} else {
113-
theme.Warning(fmt.Sprintf("No patch version specified, assumed newest patch version %s.", selectedVersion.number.String()))
114-
}
80+
requestedVersion := common.GetVersion(args[0], threadSafe, "")
81+
if requestedVersion.Minor == -1 {
82+
theme.Warning(fmt.Sprintf("No minor version specified, assumed newest minor version %s.", selectedVersion.number.String()))
83+
} else if requestedVersion.Patch == -1 {
84+
theme.Warning(fmt.Sprintf("No patch version specified, assumed newest patch version %s.", selectedVersion.number.String()))
11585
}
11686

11787
// remove old php bat script
@@ -214,28 +184,7 @@ func Use(args []string) {
214184
}
215185
// end of ext directory link creation
216186

217-
var threadSafeString string
218-
if threadSafe {
219-
threadSafeString = "thread safe"
220-
} else {
221-
threadSafeString = "non-thread safe"
222-
}
223-
224-
theme.Success("Using PHP " + selectedVersion.number.String() + " " + threadSafeString)
225-
}
226-
227-
func sortVersions(in []versionMeta) []versionMeta {
228-
sort.Slice(in, func(i, j int) bool {
229-
if in[i].number.Major != in[j].number.Major {
230-
return in[i].number.Major > in[j].number.Major
231-
}
232-
if in[i].number.Minor != in[j].number.Minor {
233-
return in[i].number.Minor > in[j].number.Minor
234-
}
235-
return in[i].number.Patch > in[j].number.Patch
236-
})
237-
238-
return in
187+
theme.Success(fmt.Sprintf("Using PHP %s", selectedVersion.number))
239188
}
240189

241190
type versionMeta struct {

0 commit comments

Comments
 (0)