Compare commits

...

7 Commits
1.0.0 ... main

@ -10,7 +10,7 @@
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"args": ["use" ,"18" ]
"args": ["ls-remote" ]
}
]
}

@ -1,21 +1,26 @@
# j
Java Version Manager
![d34a2d8bb5d1f088687399f1cfe3468](https://user-images.githubusercontent.com/56473277/228175418-e709a300-8448-4b01-a11b-cbdfa2bb83a7.png)
![image](https://user-images.githubusercontent.com/56473277/228444767-6cc15881-69a6-4ed1-8b30-f7c19413fde0.png)
## 安装
默认文件下载安装在用户目录下 ```.j```
[下载](https://github.com/forget-the-bright/j/releases) 下载自己需要的版本, 到自己自定义的目录 修改可执行文件名称为j
默认文件下载安装在用户目录下 ```.j```目录,目录下 ```versions```, ```downloads```, ```java``` 分别是本地安装目录安装包下载目录当前使用的java版本目录
将 JAVA_HOME 配置为 ```USER_HOME\.j\java```
指定安装目录需要 添加环境变量 ```J_HOME```
## 命令
### 列出
列出所有可按照版本
列出所有可安装版本
```
j ls-all
j ls-remote
```
![image](https://user-images.githubusercontent.com/56473277/228176289-d5037369-8c39-4596-8274-b62c51f1c44f.png)
![image](https://user-images.githubusercontent.com/56473277/228444893-1ae5779e-74a2-4884-9c7d-09aa533d644e.png)
列出本地安装版本
```

@ -100,13 +100,14 @@ const (
// ghome 返回g根目录
func ghome() (dir string) {
//fmt.Println(os.Getenv(homeEnv))
/* path, _ := os.Getwd()
return path */
if dir = os.Getenv(homeEnv); dir != "" {
return dir
}
homeDir, _ := os.UserHomeDir()
return filepath.Join(homeDir, ".j")
/* path, _ := os.Getwd()
return path */
}
// inuse 返回当前的go版本号

@ -10,11 +10,17 @@ var (
UsageText: "j ls",
Action: list,
},
{
/* {
Name: "ls-all",
Usage: "List All versions",
UsageText: "j ls-all",
Action: listAll,
}, */
{
Name: "ls-remote",
Usage: "List Remote versions",
UsageText: "j ls-remote",
Action: listRemote,
},
{
Name: "install",

@ -8,6 +8,7 @@ import (
"github.com/forget-the-bright/j/internal/pkg/archiver"
"github.com/forget-the-bright/j/internal/pkg/check"
"github.com/forget-the-bright/j/internal/pkg/collector"
"github.com/forget-the-bright/j/internal/pkg/config"
"github.com/forget-the-bright/j/internal/pkg/download"
@ -16,8 +17,12 @@ import (
)
func fundVersion(version string) *config.UrlItem {
config.Url_Items = collector.ConvertCollectorToUrlItem(collector.GetOpenJDKArchiveReleasesInfo(), false)
for _, v := range config.Url_Items {
if v.SimpleName == version { //strings.Contains(v.SimpleName, version)
if version != "8" {
v.In.Sha256 = collector.GetSha256ByUrl(v.In.Sha256, true)
}
return v
}
}

@ -0,0 +1,33 @@
package cli
import (
"fmt"
"github.com/fatih/color"
"github.com/forget-the-bright/j/internal/pkg/collector"
"github.com/k0kubun/go-ansi"
"github.com/urfave/cli/v2"
)
func remoteVersionLength(version string) string {
yu := 8 - len(version)
for i := 0; i < yu; i++ {
version += " "
}
return version
}
func listRemote(*cli.Context) (err error) {
use_version := inuse(goroot)
out := ansi.NewAnsiStdout()
rs := collector.ConvertCollectorToUrlItem(collector.GetOpenJDKArchiveReleasesInfo(), false)
color.New(color.FgGreen).Fprintf(out, " %s\n", " version info")
for _, v := range rs {
if v.SimpleName == use_version { //strings.Contains(v.SimpleName, version)
color.New(color.FgGreen).Fprintf(out, "* %s\n", remoteVersionLength(v.SimpleName)+" "+v.Expected)
} else {
fmt.Fprintf(out, " %s\n", remoteVersionLength(v.SimpleName)+" "+v.Expected)
}
}
return nil
}

@ -9,12 +9,6 @@ import (
"github.com/urfave/cli/v2"
)
func reverseArray(arr []*config.UrlItem) []*config.UrlItem {
for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {
arr[i], arr[j] = arr[j], arr[i]
}
return arr
}
func mathVersionLength(version string) string {
if len(version) <= 1 {
return version + " "
@ -25,7 +19,7 @@ func listAll(*cli.Context) (err error) {
use_version := inuse(goroot)
out := ansi.NewAnsiStdout()
color.New(color.FgGreen).Fprintf(out, " %s\n", "version info")
for _, v := range reverseArray(config.Url_Items) {
for _, v := range config.ReverseArray(config.Url_Items) {
if v.SimpleName == use_version { //strings.Contains(v.SimpleName, version)
color.New(color.FgGreen).Fprintf(out, "* %s\n", mathVersionLength(v.SimpleName)+" "+v.Expected)
} else {

@ -4,7 +4,7 @@ import "strings"
const (
// ShortVersion 短版本号
ShortVersion = "1.0.0"
ShortVersion = "1.0.1"
)
// The value of variables come form `gb build -ldflags '-X "build.Build=xxxxx" -X "build.CommitID=xxxx"' `

@ -1,6 +1,10 @@
package collector
import "strings"
import (
"io/ioutil"
"net/http"
"strings"
)
type Collector struct {
Version string
@ -19,6 +23,7 @@ type Op_Item struct {
FileName string
}
var Archive_Releases_Collectors []*Collector
var Collectors []*Collector
var Collector_Archive_Url string = Collector_Url + "/archive/"
@ -27,7 +32,7 @@ var Collector_Url string = "https://jdk.java.net"
func build_Op_Item(file_type, arch, download_url, sha256_url, file_name string) *Op_Item {
return &Op_Item{
FileType: file_type,
Arch: "x64",
Arch: arch,
Url: download_url,
Sha256Url: sha256_url,
FileName: file_name,
@ -39,6 +44,21 @@ func getFileNameByDownLoadUrl(url string) string {
file_name := downloads[len(downloads)-1]
return file_name
}
func getFileNameNoSuffix(file_name string) string {
return strings.ReplaceAll(file_name, "."+getFileTypeByFileName(file_name), "")
}
func GetSha256ByUrl(url string, isGetSha256 bool) string {
if isGetSha256 {
resp, _ := http.Get(url)
defer resp.Body.Close()
bytes, _ := ioutil.ReadAll(resp.Body)
return string(bytes)
} else {
return url
}
}
func getFileTypeByFileName(filename string) string {
filenames := strings.Split(filename, ".")
switch filenames[len(filenames)-1] {

@ -1,5 +1,215 @@
package collector
func GetOpenJDKReleasesInfo() {
import (
"fmt"
"net/http"
"runtime"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/forget-the-bright/j/internal/pkg/config"
)
func ConvertCollectorToUrlItem(colls []*Collector, isGetSha256 bool) []*config.UrlItem {
var rs = make([]*config.UrlItem, 0)
for _, coll := range colls {
var item *Op_Item
switch runtime.GOOS {
case "linux":
if runtime.GOARCH == "aarch64" {
item = coll.Linux_AArch64
} else {
item = coll.Linux_X64
}
case "windows":
item = coll.Windows_X64
case "darwin":
if runtime.GOARCH == "aarch64" {
item = coll.Mac_AArch64
} else {
item = coll.Mac_X64
}
default:
item = nil
}
if item != nil {
rs = append(rs, &config.UrlItem{
In: &config.JavaFileItem{
FileName: item.FileName,
URL: item.Url,
Sha256: GetSha256ByUrl(item.Sha256Url, isGetSha256),
},
SimpleName: coll.Version,
Expected: getFileNameNoSuffix(item.FileName),
})
}
}
switch runtime.GOOS {
case "windows":
rs = append(rs, &config.UrlItem{
In: &config.JavaFileItem{
FileName: "openjdk-8u42-b03-windows-i586-14_jul_2022.zip",
URL: "https://download.java.net/openjdk/jdk8u42/ri/openjdk-8u42-b03-windows-i586-14_jul_2022.zip",
Sha256: "0314134bd981db63c7ca68d262ef896383b5694307a14bac81af88b5ad926279",
},
Expected: "openjdk-8u42-b03-windows-i586-14_jul_2022",
SimpleName: "8",
})
case "linux":
rs = append(rs, &config.UrlItem{
In: &config.JavaFileItem{
FileName: "openjdk-8u42-b03-linux-x64-14_jul_2022.tar.gz",
URL: "https://download.java.net/openjdk/jdk8u42/ri/openjdk-8u42-b03-linux-x64-14_jul_2022.tar.gz",
Sha256: "dd5fc6ef5ebffb88cd66af5258226c31f6c719fdcd855d95464fdb2cab051baa",
},
Expected: "openjdk-8u42-b03-linux-x64-14_jul_2022",
SimpleName: "8",
})
}
return config.ReverseArray(rs)
}
func GetOpenJDKVesionUrlInfo() []*Collector {
resp, _ := http.Get(Collector_Archive_Url)
Collectors = make([]*Collector, 0)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Println("false")
}
doc_selector, _ := goquery.NewDocumentFromReader(resp.Body)
divs := doc_selector.Find("#sidebar").Find(".links")
divs.Each(func(j int, div *goquery.Selection) {
link_about := div.Find(".about").Text()
if link_about == "Reference Implementations" {
a_docs := div.Find("a")
a_docs.Each(func(j int, a_doc *goquery.Selection) {
info_url := a_doc.AttrOr("href", "")
versions := strings.Split(a_doc.Text(), " ")
version := versions[len(versions)-1]
version_url := Collector_Url + strings.ReplaceAll(info_url, ".", "")
Collectors = append(Collectors, getVersionByUrl(version, version_url))
//fmt.Println(version)
//fmt.Println(version_url)
})
}
//fmt.Printf("link_about: %v\n", link_about)
})
return Collectors
}
func GetOpenJDKArchiveReleasesInfo() []*Collector {
Archive_Releases_Collectors = make([]*Collector, 0)
resp, _ := http.Get(Collector_Archive_Url)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Println("false")
}
docs, _ := goquery.NewDocumentFromReader(resp.Body)
tbody_docs := docs.Find(".builds").Find("tbody").Children()
var collector_Item *Collector
tbody_docs.Each(func(j int, tr *goquery.Selection) {
th := tr.Find("th")
if th.Length() == 1 {
val := th.Text()
if val != "Source" {
s2 := strings.Split(val, "(build ")
version_str := strings.Trim(strings.Trim(s2[0], " "), " GA")
//fmt.Println("\n" + version_str)
collector_Item = &Collector{
Version: version_str,
}
Archive_Releases_Collectors = append(Archive_Releases_Collectors, collector_Item)
}
}
if th.Length() == 2 {
a_item := tr.Find("td").Find("a")
file_type := strings.Trim(a_item.First().Text(), "\n")
download_url := a_item.First().AttrOr("href", "")
file_name := getFileNameByDownLoadUrl(download_url)
sha256_url := a_item.Last().AttrOr("href", "")
releases := strings.Split(th.First().Text(), "/")
switch releases[0] {
case "Windows":
collector_Item.Windows_X64 = build_Op_Item(file_type, "x64", download_url, sha256_url, file_name)
case "Mac":
if len(releases) == 1 || releases[1] == "x64" {
collector_Item.Mac_X64 = build_Op_Item(file_type, "x64", download_url, sha256_url, file_name)
} else {
collector_Item.Mac_AArch64 = build_Op_Item(file_type, releases[1], download_url, sha256_url, file_name)
}
case "Linux":
if len(releases) == 1 || releases[1] == "x64" {
collector_Item.Linux_X64 = build_Op_Item(file_type, "x64", download_url, sha256_url, file_name)
} else {
collector_Item.Linux_X64 = build_Op_Item(file_type, releases[1], download_url, sha256_url, file_name)
}
}
}
})
return Archive_Releases_Collectors
}
func getVersionByUrl(version, url string) *Collector {
var coll = Collector{
Version: version,
}
resp, _ := http.Get(url)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Println("false")
}
doc_selector, _ := goquery.NewDocumentFromReader(resp.Body)
divs := doc_selector.Find("#main").Find("ul")
if divs.Length() <= 1 || version == "8" {
li_docs := divs.Find("li")
linux_x64_a := li_docs.Eq(0).Find("a")
windows_x64_a := li_docs.Eq(1).Find("a")
linux_url := linux_x64_a.Eq(0).AttrOr("href", "")
linux_file_name := getFileNameByDownLoadUrl(linux_url)
linux_file_type := getFileTypeByFileName(linux_file_name)
linux_sha256_url := linux_x64_a.Eq(1).AttrOr("href", "")
windows_url := windows_x64_a.Eq(0).AttrOr("href", "")
windows_file_name := getFileNameByDownLoadUrl(windows_url)
windows_file_type := getFileTypeByFileName(windows_file_name)
windows_sha256_url := windows_x64_a.Eq(1).AttrOr("href", "")
coll.Linux_X64 = &Op_Item{
Arch: "x64",
Url: linux_url,
Sha256Url: linux_sha256_url,
FileName: linux_file_name,
FileType: linux_file_type,
}
coll.Windows_X64 = &Op_Item{
Arch: "x64",
Url: windows_url,
Sha256Url: windows_sha256_url,
FileName: windows_file_name,
FileType: windows_file_type,
}
} else {
divs_eq0 := divs.Eq(0)
li_docs := divs_eq0.Find("li")
linux_x64_a := li_docs.Eq(0).Find("a")
linux_url := linux_x64_a.Eq(0).AttrOr("href", "")
linux_file_name := getFileNameByDownLoadUrl(linux_url)
linux_file_type := getFileTypeByFileName(linux_file_name)
linux_sha256_url := linux_x64_a.Eq(1).AttrOr("href", "")
coll.Linux_X64 = &Op_Item{
Arch: "x64",
Url: linux_url,
Sha256Url: linux_sha256_url,
FileName: linux_file_name,
FileType: linux_file_type,
}
}
return &coll
}

@ -2,6 +2,7 @@ package collector
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
@ -9,6 +10,16 @@ import (
"github.com/PuerkitoBio/goquery"
)
func Test_GetSha256ByUrl(t *testing.T) {
resp, _ := http.Get("https://download.java.net/java/GA/jdk19.0.1/afdd2e245b014143b62ccb916125e3ce/10/GPL/openjdk-19.0.1_windows-x64_bin.zip.sha256")
defer resp.Body.Close()
bytes, _ := ioutil.ReadAll(resp.Body)
t.Run("", func(t *testing.T) {
fmt.Println(string(bytes))
})
}
func Test_getOpenJdkVersion(t *testing.T) {
resp, _ := http.Get(Collector_Archive_Url)
colls := make([]*Collector, 0)
@ -105,7 +116,7 @@ func test_getVersionByUrl(version, url string) *Collector {
}
func Test_getArchiveVersion(t *testing.T) {
Collectors = make([]*Collector, 0)
Archive_Releases_Collectors = make([]*Collector, 0)
resp, _ := http.Get(Collector_Archive_Url)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
@ -137,7 +148,7 @@ func Test_getArchiveVersion(t *testing.T) {
collector_Item = &Collector{
Version: version_str,
}
Collectors = append(Collectors, collector_Item)
Archive_Releases_Collectors = append(Archive_Releases_Collectors, collector_Item)
}
}
if th.Length() == 2 {
@ -171,7 +182,7 @@ func Test_getArchiveVersion(t *testing.T) {
}
})
})
c2 := Collectors
c2 := Archive_Releases_Collectors
fmt.Printf("len(c2): %v\n", len(c2))
fmt.Println(c2)
}

@ -20,6 +20,13 @@ type UrlItem struct {
var Url_Items []*UrlItem
func ReverseArray(arr []*UrlItem) []*UrlItem {
for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {
arr[i], arr[j] = arr[j], arr[i]
}
return arr
}
func init() {
switch runtime.GOOS {
case "linux":

@ -19,7 +19,7 @@ build-linux: build-linux-386 build-linux-amd64 build-linux-arm build-linux-arm64
build-linux-386:
GOOS=linux GOARCH=386 $(GO) build $(GO_FLAGS) -o bin/linux-386/j
build-linux-amd64:
GOOS=linux GOARCH=amd64 $(GO) build $(GO_FLAGS) -o bin/linux-amd64/j
GOOS=linux GOARCH=amd64 $(GO) build $(GO_FLAGS) -o bin/linux-amd64/linux_amd_x86_64_j
build-linux-arm:
GOOS=linux GOARCH=arm $(GO) build $(GO_FLAGS) -o bin/linux-arm/j
build-linux-arm64:
@ -39,7 +39,7 @@ build-windows: build-windows-386 build-windows-amd64 build-windows-arm build-win
build-windows-386:
GOOS=windows GOARCH=386 $(GO) build $(GO_FLAGS) -o bin/windows-386/j.exe
build-windows-amd64:
GOOS=windows GOARCH=amd64 $(GO) build $(GO_FLAGS) -o bin/windows-amd64/j.exe
GOOS=windows GOARCH=amd64 $(GO) build $(GO_FLAGS) -o bin/windows-amd64/windows_amd_x86_64_j.exe
build-windows-arm:
GOOS=windows GOARCH=arm $(GO) build $(GO_FLAGS) -o bin/windows-arm/j.exe
build-windows-arm64:

Loading…
Cancel
Save