Skip to content
Snippets Groups Projects
Commit 55fb4225 authored by Bensong Liu's avatar Bensong Liu
Browse files

write a new nuget download

parent beedcbf7
No related branches found
No related tags found
No related merge requests found
......@@ -2,5 +2,4 @@
.vscode
openxt
csproj-to-5
*.tar.gz
......@@ -15,7 +15,7 @@ function build () {
rm -rf bin/
GOOS="$os" GOARCH="$arch" go build -o "bin/openxt$bin_tail" . &&
GOOS="$os" GOARCH="$arch" go build -o "bin/csproj-to-5$bin_tail" tools/csproj-to-5.go &&
GOOS="$os" GOARCH="$arch" go build -o "bin/csproj-to-5$bin_tail" tools/csproj-to-5/main.go &&
tar cvzf "openxt-$version-$os-$arch.tar.gz" bin
return $?
......
File moved
package main
import (
"github.com/antchfx/xmlquery"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"sync"
)
/*
`dotnet add` has a bug:
If my csproj is using net462, and I run
dotnet add package Microsoft.Azure.Kusto.Cloud.Platform --package-directory ~/tmp/ttt --framework net462
It would say: Package Microsoft.Azure.Kusto.Cloud.Platform 9.1.0 is not compatible with net462
However, Microsoft.Azure.Kusto.Cloud.Platform 8.1.5 IS compatible with net462. It doesn't download at all!
dotnet add also requires a valid csproj. It's heavy, complex, and slow. It sucks.
Let's write a simple one!
*/
const nugetConfigPath = "~/.nuget/NuGet/NuGet.Config"
func panicErrorIfAny(err error, title string) {
if err != nil {
panic("[Error] on " + title + ": " + err.Error())
}
}
func logErrorIfAny(err error, title string) {
if err != nil {
log.Println("[Error] on " + title + ": " + err.Error())
}
}
func pkgVersionLeftIsGreater(left, right string) bool {
// return if left > right
return true
}
var nugetIndexJsonCache []string
func initNugetIndexJsonCache(pkgName string) {
// download index.json from every source, and cache them in global variable.
nugetIndexJsonCache = []string{}
f, err := os.Open(nugetConfigPath)
panicErrorIfAny(err, "Open " + nugetConfigPath)
parsed, err := xmlquery.Parse(f)
panicErrorIfAny(err, "Parse XML " + nugetConfigPath)
sources := xmlquery.Find(parsed, "//configuration/packageSources/add")
var wg sync.WaitGroup
nugetIndexJsonCache_wlock := sync.RWMutex{}
for _, source := range sources {
// Parse xml
sname, surl, sauth_username, sauth_password := "", "", "", ""
for _, attr := range source.Attr {
if attr.Name.Local == "key" {
sname = attr.Value
} else if attr.Name.Local == "value" {
surl = attr.Value
}
}
auth_entries := xmlquery.Find(parsed, "//configuration/packageSourceCredentials/" + sname + "/add")
for _, auth_entry := range auth_entries {
for _, attr := range auth_entry.Attr {
if attr.Name.Local == "Username" {
sauth_username = attr.Value
} else if attr.Name.Local == "ClearTextPassword" {
sauth_password = attr.Value
}
}
}
// Send the HTTP request and download index.json!
wg.Add(1)
nugetIndexJsonCache = append(nugetIndexJsonCache, "")
go func(url, authu, authp string) {
defer wg.Done()
req, err := http.NewRequest("GET", url, nil)
panicErrorIfAny(err, "http.NewRequest")
if authu + authp != "" {
req.SetBasicAuth(authu, authp)
}
resp, err := http.DefaultClient.Do(req)
logErrorIfAny(err, "download json")
defer resp.Body.Close()
resp_txt, err := ioutil.ReadAll(resp.Body)
logErrorIfAny(err, "download json")
if len(resp_txt) > 0 {
nugetIndexJsonCache_wlock.Lock()
nugetIndexJsonCache = append(nugetIndexJsonCache, string(resp_txt))
nugetIndexJsonCache_wlock.Unlock()
}
}(surl, sauth_username, sauth_password)
}
wg.Wait()
}
// recursive. If package exists, it exit.
// One of [pkgVerBegin, pkgVerEnd] would be downloaded.
func downloadPackageAndDeps(packageName, pkgVerBegin, pkgVerEnd, localRepoDir string) {
}
// [pkgVerBegin, pkgVerEnd] is good for frameworkVersion.
func decidePackageVersion(packageName, frameworkVersion string) (pkgVerBegin, pkgVerEnd string) {
// Generated by curl-to-Go: https://mholt.github.io/curl-to-go
// curl -u bensl:xbwejuparq4ighqs4pjq6bqjqmxpzghhtqshql2uy3woi73ew6iq https://o365exchange.pkgs.visualstudio.com/959adb23-f323-4d52-8203-ff34e5cbeefa/_packaging/aacaa93f-61ac-420c-8b20-c17c5d2b52f1/nuget/v3/registrations2-semver2/microsoft.azure.kusto.cloud.platform/index.json
return "1.1.1", "8.8.8"
}
func main() {
if(len(os.Args) < 4) {
log.Println("Usage: nuget-download-package <packageName> <packageVersion/frameworkVersion> <localRepoDir>")
log.Println("frameworkVersion maybe 'net472', 'netstandard2.0', 'netcoreapp3.1' ...")
os.Exit(1)
}
pkgName, pkgVer, localRepoDir := os.Args[1], os.Args[2], os.Args[3]
if strings.HasPrefix(pkgVer, "net") {
_, pkgVer = decidePackageVersion(pkgName, pkgVer)
}
downloadPackageAndDeps(pkgName, pkgVer, pkgVer, localRepoDir)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment