Skip to content

Commit 54a67ce

Browse files
authored
Merge pull request #27 from Lotti/feature/ls-remote
reworked command "list" display format and added command "list remote"
2 parents 31544a5 + 9f0c6fc commit 54a67ce

File tree

9 files changed

+202
-120
lines changed

9 files changed

+202
-120
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,17 @@ pvm install 8.2
4242
4343
Will install PHP 8.2 at the latest patch.
4444

45-
## Build
45+
## Composer support
46+
`pvm` now installs also composer with each php version installed.
47+
It will install Composer latest stable release for PHP >= 7.2 and Composer latest 2.2.x LTS for PHP < 7.2.
48+
You'll be able to invoke composer from terminal as it is intended:
49+
```shell
50+
composer --version
51+
```
52+
53+
## Build this project
4654

47-
To compile the program use:
55+
To compile this project use:
4856
```shell
4957
GOOS=windows GOARCH=amd64 go build -o pvm.exe
5058
```

commands/help.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func Help(notFoundError bool) {
1717
fmt.Println(" help")
1818
fmt.Println(" install")
1919
fmt.Println(" list")
20+
fmt.Println(" list-remote")
2021
fmt.Println(" path")
2122
fmt.Println(" use")
2223
}

commands/install.go

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"net/http"
1111
"os"
1212
"path/filepath"
13-
"regexp"
1413
"strings"
1514
)
1615

@@ -40,7 +39,7 @@ func Install(args []string) {
4039
theme.Warning("Non-thread safe version will be installed")
4140
}
4241

43-
desiredVersionNumbers := common.GetVersion(args[1], desireThreadSafe, "")
42+
desiredVersionNumbers := common.ComputeVersion(args[1], desireThreadSafe, "")
4443

4544
if desiredVersionNumbers == (common.Version{}) {
4645
theme.Error("Invalid version specified")
@@ -52,67 +51,10 @@ func Install(args []string) {
5251
desiredMinorVersion := desiredVersionNumbers.Minor
5352
desiredPatchVersion := desiredVersionNumbers.Patch
5453

55-
// perform get request to https://windows.php.net/downloads/releases/archives/
56-
resp, err := http.Get("https://windows.php.net/downloads/releases/archives/")
54+
versions, err := common.RetrievePHPVersions()
5755
if err != nil {
5856
log.Fatalln(err)
5957
}
60-
// We Read the response body on the line below.
61-
body, err := io.ReadAll(resp.Body)
62-
if err != nil {
63-
log.Fatalln(err)
64-
}
65-
// Convert the body to type string
66-
sb := string(body)
67-
68-
// regex match
69-
re := regexp.MustCompile(`<A HREF="([a-zA-Z0-9./-]+)">([a-zA-Z0-9./-]+)</A>`)
70-
matches := re.FindAllStringSubmatch(sb, -1)
71-
72-
versions := make([]common.Version, 0)
73-
74-
for _, match := range matches {
75-
url := match[1]
76-
name := match[2]
77-
78-
// check if name starts with "php-devel-pack-"
79-
if name != "" && len(name) > 15 && name[:15] == "php-devel-pack-" {
80-
continue
81-
}
82-
// check if name starts with "php-debug-pack-"
83-
if name != "" && len(name) > 15 && name[:15] == "php-debug-pack-" {
84-
continue
85-
}
86-
// check if name starts with "php-test-pack-"
87-
if name != "" && len(name) > 15 && name[:14] == "php-test-pack-" {
88-
continue
89-
}
90-
91-
// check if name contains "src"
92-
if name != "" && strings.Contains(name, "src") {
93-
continue
94-
}
95-
96-
// check if name does not end in zip
97-
if name != "" && !strings.HasSuffix(name, ".zip") {
98-
continue
99-
}
100-
101-
threadSafe := true
102-
103-
// check if name contains "nts" or "NTS"
104-
if name != "" && (strings.Contains(name, "nts") || strings.Contains(name, "NTS")) {
105-
threadSafe = false
106-
}
107-
108-
// make sure we only get x64 versions
109-
if name != "" && !strings.Contains(name, "x64") {
110-
continue
111-
}
112-
113-
// regex match name and push to versions
114-
versions = append(versions, common.GetVersion(name, threadSafe, url))
115-
}
11658

11759
// find desired version
11860
var desiredVersion common.Version

commands/list-remote.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package commands
2+
3+
import (
4+
"hjbdev/pvm/common"
5+
"hjbdev/pvm/theme"
6+
"log"
7+
"slices"
8+
9+
"github.com/fatih/color"
10+
)
11+
12+
func ListRemote() {
13+
versions, err := common.RetrievePHPVersions()
14+
if err != nil {
15+
log.Fatalln(err)
16+
}
17+
18+
common.SortVersions(versions)
19+
20+
installedVersions, _ := common.RetrieveInstalledPHPVersions()
21+
22+
theme.Title("PHP versions available")
23+
for _, version := range versions {
24+
idx := slices.IndexFunc(installedVersions, func(v common.Version) bool { return v.Same(version) })
25+
found := " "
26+
if idx != -1 {
27+
found = "*"
28+
}
29+
color.White(found + " " + version.StringShort())
30+
}
31+
}

commands/list.go

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,21 @@
11
package commands
22

33
import (
4+
"hjbdev/pvm/common"
45
"hjbdev/pvm/theme"
5-
"log"
6-
"os"
6+
77
"github.com/fatih/color"
88
)
99

1010
func List() {
11-
// get users home dir
12-
homeDir, err := os.UserHomeDir()
13-
14-
if err != nil {
15-
log.Fatalln(err)
16-
}
17-
18-
// check if .pvm folder exists
19-
if _, err := os.Stat(homeDir + "/.pvm"); os.IsNotExist(err) {
20-
theme.Error("No PHP versions installed")
21-
return
22-
}
23-
24-
// check if .pvm/versions folder exists
25-
if _, err := os.Stat(homeDir + "/.pvm/versions"); os.IsNotExist(err) {
26-
theme.Error("No PHP versions installed")
27-
return
28-
}
29-
30-
// get all folders in .pvm/versions
31-
versions, err := os.ReadDir(homeDir + "/.pvm/versions")
11+
versions, err := common.RetrieveInstalledPHPVersions()
3212
if err != nil {
33-
log.Fatalln(err)
13+
theme.Error(err.Error())
3414
}
3515

3616
theme.Title("Installed PHP versions")
37-
3817
// print all folders
3918
for _, version := range versions {
40-
color.White(" " + version.Name())
19+
color.White(" " + version.StringShort())
4120
}
4221
}

commands/use.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func Use(args []string) {
6363
if strings.Contains(version.Name(), "nts") || strings.Contains(version.Name(), "NTS") {
6464
safe = false
6565
}
66-
foundVersion := common.GetVersion(version.Name(), safe, "")
66+
foundVersion := common.ComputeVersion(version.Name(), safe, "")
6767
if threadSafe == foundVersion.ThreadSafe && strings.HasPrefix(foundVersion.String(), args[0]) {
6868
selectedVersion = &versionMeta{
6969
number: foundVersion,
@@ -77,7 +77,7 @@ func Use(args []string) {
7777
return
7878
}
7979

80-
requestedVersion := common.GetVersion(args[0], threadSafe, "")
80+
requestedVersion := common.ComputeVersion(args[0], threadSafe, "")
8181
if requestedVersion.Minor == -1 {
8282
theme.Warning(fmt.Sprintf("No minor version specified, assumed newest minor version %s.", selectedVersion.number.String()))
8383
} else if requestedVersion.Patch == -1 {

0 commit comments

Comments
 (0)