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

nuget downloader done

parent 36f37e6c
No related branches found
No related tags found
No related merge requests found
......@@ -24,8 +24,8 @@ func print_help_and_exit() {
func main() {
log.Println("OpenXT version " + OPENXT_VERSION)
nugetConfigPath := "/home/recolic/tmp/test.nuget.config" // warning: not supported yet
projectDir := flag.String("project-dir", "", "Path to the CoreXT/OpenXT project. Could be a small subproject, instead of the whole larget CoreXT project. ")
nugetConfigPath := flag.String("nuget-config", "", "Path to nuget.config. It's ~/.nuget/NuGet/NuGet.Config by default. ")
localRepoDir := flag.String("local-repo-dir", "", "Path to local nuget repo. (known as CxCache in CoreXT)")
binDir := flag.String("bin-dir", "", "Path for output build. (TODO: remove in the future)")
shell := flag.String("shell", "bash", "Shell for the output env variables, supports bash/fish/powershell")
......@@ -43,7 +43,7 @@ func main() {
switch os.Args[0] {
case "sync":
SyncPackages(nugetConfigPath, *localRepoDir, deps)
SyncPackages(*nugetConfigPath, *localRepoDir, deps)
case "env":
assertStringNotEmpty(*binDir, "`--bin-dir` must be set")
......
......@@ -2,13 +2,11 @@ package main
import (
"errors"
"fmt"
"github.com/mcuadros/go-version"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
......@@ -31,25 +29,14 @@ func ScanForDependencies(scanPath string) (allDeps []dependencyItem) {
func SyncPackages(nugetConfigPath, localRepoPath string, allDeps []dependencyItem) {
log.Println("WARNING: We do not support custom nugetConfigPath yet. Please make sure your default nuget.config works. (usually ~/.nuget/NuGet/NuGet.Config)")
// We must create a fake environment to make dotnet happy. Let's do that!
// Create the fake environment directory.
fakeProjectTemplate := "<Project Sdk=\"Microsoft.NET.Sdk\"><PropertyGroup><TargetFramework>%v</TargetFramework></PropertyGroup></Project>"
fakeProjectDir, err := ioutil.TempDir("", "openxt-syncpkg-tmp")
panicErrorIfAny(err, "createTempDir for sync package")
defer os.RemoveAll(fakeProjectDir)
// Prepare nuget local repo
depsToSync := depsDeduplicate(allDeps)
for index, dep := range depsToSync {
// TODO: parallelize me!
log.Printf("[%v/%v] Downloading pkg: %v:%v as var %v", index, len(depsToSync), dep.pkgName, dep.targetNetVer, dep.envName)
// Create the fake project.
err = ioutil.WriteFile(filepath.Join(fakeProjectDir, "noob.csproj"), []byte(fmt.Sprintf(fakeProjectTemplate, dep.targetNetVer)), 0666)
logErrorIfAny(err, "write noob.csproj for " + dep.pkgName)
// dotnet add package Microsoft.Azure.Cis.Activities.Contracts --package-directory ~/tmp/your.repo # --framework is not working at all.
cmd := exec.Command("dotnet", "add", "package", dep.pkgName, "--package-directory", localRepoPath)
cmd.Dir = fakeProjectDir
stdout, err := cmd.Output()
cmd := exec.Command("nuget-download-package", dep.pkgName, dep.targetNetVer, localRepoPath, nugetConfigPath)
stdout, err := cmd.CombinedOutput()
log.Println(string(stdout))
// We can do nothing on failure. usually, if envName is NULL, we need not install it at all.
logErrorIfAny(err, "EXEC command")
......
......@@ -25,7 +25,6 @@ dotnet add also requires a valid csproj. It's heavy, complex, and slow. It sucks
Let's write a simple one!
*/
const nugetConfigPath = "/home/recolic/.nuget/NuGet/NuGet.Config"
func panicErrorIfAny(err error, title string) {
if err != nil {
......@@ -58,7 +57,7 @@ func netVersionNugetFormatToStandardFormat(netVersionNugetFormat string) string
func httpGet(url, authu, authp, outputFilepath string) string {
// This function would follow HTTP 30x
log.Println("HTTP GET " + url + " with username " + authu)
log.Println("GET " + url + " with username " + authu)
req, err := http.NewRequest("GET", url, nil)
panicErrorIfAny(err, "http.NewRequest")
if authu + authp != "" {
......@@ -78,7 +77,7 @@ func httpGet(url, authu, authp, outputFilepath string) string {
}
}
func initNugetIndexJsonCache(pkgName string) (nugetIndexJsonCache, nugetIndexJsonCache_Login []string) {
func initNugetIndexJsonCache(nugetConfigPath, pkgName string) (nugetIndexJsonCache, nugetIndexJsonCache_Login []string) {
// download index.json from every source, and cache them in array.
f, err := os.Open(nugetConfigPath)
panicErrorIfAny(err, "Open " + nugetConfigPath)
......@@ -240,13 +239,22 @@ func decidePackageVersion(frameworkVersion, packageVersion string, indexJsons, i
func main() {
if(len(os.Args) < 4) {
log.Println("Usage: nuget-download-package <packageName> <packageVersion/frameworkVersion> <localRepoDir>")
log.Println("Usage: nuget-download-package <packageName> <packageVersion/frameworkVersion> <localRepoDir> [nugetConfigPath]")
log.Println("frameworkVersion maybe 'net472', 'netstandard2.0', 'netcoreapp3.1' ...")
log.Println("default nugetConfigPath is UserHomeDir/.nuget/NuGet/NuGet.Config")
os.Exit(1)
}
pkgName, pkgVerOrNetVer, localRepoDir := strings.ToLower(os.Args[1]), os.Args[2], os.Args[3]
indexJsons, indexJsons_Login := initNugetIndexJsonCache(pkgName)
pkgName, pkgVerOrNetVer, localRepoDir, nugetConfigPath := strings.ToLower(os.Args[1]), os.Args[2], os.Args[3], ""
if len(os.Args) == 4 || os.Args[4] == "" {
homeDir, err := os.UserHomeDir()
panicErrorIfAny(err, "Get UserHomeDir")
nugetConfigPath = filepath.Join(homeDir, ".nuget", "NuGet", "NuGet.Config")
} else {
nugetConfigPath = os.Args[4]
}
indexJsons, indexJsons_Login := initNugetIndexJsonCache(nugetConfigPath, pkgName)
pkgVer, targetUrl, targetUrlLogin := "", "", ""
if strings.HasPrefix(pkgVerOrNetVer, "net") {
......
......@@ -11,7 +11,7 @@ import (
func Extract(src, dest string) error {
fmt.Println("Extraction of " + src + " started!")
fmt.Println("Extracting " + src + "...")
r, err := zip.OpenReader(src)
if err != nil {
......@@ -69,10 +69,10 @@ func Extract(src, dest string) error {
if err != nil {
return err
}
fmt.Println("Extracting file: " + f.Name)
// fmt.Println("Extracting file: " + f.Name)
}
fmt.Println("Extraction of " + src + " finished!")
// fmt.Println("Extraction of " + src + " finished!")
return nil
}
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