Skip to content

Commit 8ebb02a

Browse files
committed
Fetch latest patch version from nuget
1 parent 055a4dc commit 8ebb02a

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

new_client.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
package main
22

33
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
47
"os"
58
"path/filepath"
9+
"sort"
610
"strings"
711

812
_ "embed"
913

1014
"github.com/Bananenpro/cli"
1115
"github.com/code-game-project/go-utils/cggenevents"
1216
"github.com/code-game-project/go-utils/exec"
17+
"github.com/code-game-project/go-utils/external"
1318
"github.com/code-game-project/go-utils/modules"
19+
"github.com/code-game-project/go-utils/semver"
1420
"github.com/code-game-project/go-utils/server"
1521
)
1622

@@ -67,6 +73,10 @@ func CreateNewClient(projectName string) error {
6773
cli.BeginLoading("Installing csharp-client...")
6874
installLibArgs := []string{"add", "package", "CodeGame.Client"}
6975
if data.LibraryVersion != "latest" {
76+
data.LibraryVersion, err = nugetVersion("CodeGame.Client", data.LibraryVersion)
77+
if err != nil {
78+
return err
79+
}
7080
installLibArgs = append(installLibArgs, "--version", data.LibraryVersion)
7181
}
7282
_, err = exec.Execute(true, "dotnet", installLibArgs...)
@@ -169,3 +179,55 @@ func toPascal(text string) string {
169179
text = strings.ReplaceAll(text, " ", "")
170180
return text
171181
}
182+
183+
func nugetVersion(pkg, version string) (string, error) {
184+
res, err := http.Get(fmt.Sprintf("https://api.nuget.org/v3/registration5-gz-semver2/%s/index.json", strings.ToLower(pkg)))
185+
if err != nil || res.StatusCode != http.StatusOK || !external.HasContentType(res.Header, "application/json") {
186+
return "", fmt.Errorf("Couldn't access version information from 'https://api.nuget.org/v3/registration5-gz-semver2/%s/index.json'.", strings.ToLower(pkg))
187+
}
188+
defer res.Body.Close()
189+
type response struct {
190+
Items []struct {
191+
Items []struct {
192+
Entry struct {
193+
Version string `json:"version"`
194+
} `json:"catalogEntry"`
195+
} `json:"items"`
196+
} `json:"items"`
197+
}
198+
var data response
199+
err = json.NewDecoder(res.Body).Decode(&data)
200+
if err != nil {
201+
return "", fmt.Errorf("Couldn't decode nuget version data: %s", err)
202+
}
203+
204+
versions := make([]string, 0, len(data.Items[0].Items))
205+
206+
for _, item := range data.Items[0].Items {
207+
if strings.HasPrefix(item.Entry.Version, version) {
208+
versions = append(versions, item.Entry.Version)
209+
}
210+
}
211+
212+
sort.Slice(versions, func(i, j int) bool {
213+
a := versions[i]
214+
b := versions[j]
215+
216+
a1, a2, a3, err := semver.ParseVersion(a)
217+
if err != nil {
218+
return false
219+
}
220+
b1, b2, b3, err := semver.ParseVersion(b)
221+
if err != nil {
222+
return false
223+
}
224+
225+
return a1 > b1 || (a1 == b1 && a2 > b2) || (a1 == b1 && a2 == b2 && a3 > b3)
226+
})
227+
228+
if len(versions) > 0 {
229+
return versions[0], nil
230+
}
231+
232+
return "", fmt.Errorf("Couldn't fetch the correct library package version to use.")
233+
}

update.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,11 @@ func updateClient(projectName, libraryVersion string, config *cgfile.CodeGameFil
6565
cli.BeginLoading("Updating csharp-client...")
6666
installLibArgs := []string{"add", "package", "CodeGame.Client"}
6767
if libraryVersion != "latest" {
68-
installLibArgs = append(installLibArgs, "--version")
69-
installLibArgs = append(installLibArgs, libraryVersion)
68+
libraryVersion, err = nugetVersion("CodeGame.Client", libraryVersion)
69+
if err != nil {
70+
return err
71+
}
72+
installLibArgs = append(installLibArgs, "--version", libraryVersion)
7073
}
7174
_, err = exec.Execute(true, "dotnet", installLibArgs...)
7275
if err != nil {

0 commit comments

Comments
 (0)