Skip to content

Commit 7c32cc0

Browse files
committed
feat: automatically generate latest PHP version
1 parent fa3f5a5 commit 7c32cc0

File tree

3 files changed

+136
-1
lines changed

3 files changed

+136
-1
lines changed

commands/generator/main.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright (c) 2021-present Fabien Potencier <fabien@symfony.com>
3+
*
4+
* This file is part of Symfony CLI project
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as
8+
* published by the Free Software Foundation, either version 3 of the
9+
* License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
package main
21+
22+
import (
23+
"encoding/json"
24+
"fmt"
25+
"io"
26+
"net/http"
27+
"os"
28+
29+
"github.com/hashicorp/go-version"
30+
)
31+
32+
func main() {
33+
generateLatestPhpVersion()
34+
}
35+
36+
func generateLatestPhpVersion() {
37+
resp, err := http.Get("https://www.php.net/releases/active.php")
38+
if err != nil {
39+
panic(err)
40+
}
41+
defer resp.Body.Close()
42+
43+
body, err := io.ReadAll(resp.Body)
44+
if err != nil {
45+
panic(err)
46+
}
47+
48+
var result map[int]map[string]struct {
49+
Announcement bool
50+
LatestMinor string `json:"version"`
51+
}
52+
53+
if err := json.Unmarshal(body, &result); err != nil {
54+
panic(err)
55+
}
56+
57+
var latestVersion *version.Version
58+
59+
for _, versions := range result {
60+
for _, versionInfo := range versions {
61+
if !versionInfo.Announcement {
62+
continue
63+
}
64+
65+
ver, err := version.NewVersion(versionInfo.LatestMinor)
66+
if err != nil {
67+
panic(err)
68+
}
69+
70+
if latestVersion == nil || ver.GreaterThan(latestVersion) {
71+
latestVersion = ver
72+
}
73+
}
74+
}
75+
76+
f, err := os.Create("php_version.go")
77+
if err != nil {
78+
panic(err)
79+
}
80+
f.WriteString(`// Code generated by commands/generator/main.go
81+
// DO NOT EDIT
82+
83+
/*
84+
* Copyright (c) 2021-present Fabien Potencier <fabien@symfony.com>
85+
*
86+
* This file is part of Symfony CLI project
87+
*
88+
* This program is free software: you can redistribute it and/or modify
89+
* it under the terms of the GNU Affero General Public License as
90+
* published by the Free Software Foundation, either version 3 of the
91+
* License, or (at your option) any later version.
92+
*
93+
* This program is distributed in the hope that it will be useful,
94+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
95+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
96+
* GNU Affero General Public License for more details.
97+
*
98+
* You should have received a copy of the GNU Affero General Public License
99+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
100+
*/
101+
102+
package commands
103+
104+
const LatestPhpMajorVersion = "` + fmt.Sprintf("%d.%d", latestVersion.Segments()[0], latestVersion.Segments()[1]) + `"
105+
const LatestPhpMinorVersion = "` + latestVersion.Original() + `"
106+
`)
107+
}

commands/local_php_list.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
package commands
2121

22+
//go:generate go run generator/main.go
23+
2224
import (
2325
"os"
2426
"strings"
@@ -88,7 +90,7 @@ var localPhpListCmd = &console.Command{
8890
}
8991

9092
terminal.Println("")
91-
terminal.Println("To control the version used in a directory, create a <comment>.php-version</> file that contains the version number (e.g. 8.4 or 8.4.2),")
93+
terminal.Println("To control the version used in a directory, create a <comment>.php-version</> file that contains the version number (e.g. " + LatestPhpMajorVersion + " or " + LatestPhpMinorVersion + "),")
9294
terminal.Println("or define <href=https://getcomposer.org/doc/06-config.md#platform>config.platform.php</> inside <comment>composer.json</>.")
9395
terminal.Println("If you're using Platform.sh or Upsun, the version can also be specified in their configuration files.")
9496

commands/php_version.go

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy