diff --git a/.build b/.build new file mode 100644 index 0000000..2934735 --- /dev/null +++ b/.build @@ -0,0 +1,17 @@ + + + The Hello World of build files. + + + + + + + + + + FreeSpeech FreeSpeech.Droid FreeSpeech.Gtk FreeSpeech.iOS FreeSpeech.UWP + + + + diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 030a2f7..e395bab 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -16,7 +16,7 @@ nonreg: echo "setting : $ISND_TESTING_SETTINGS" cd test/isnd.tests/ dotnet build - cat $ISND_TESTING_SETTINGS > bin/Debug/netcoreapp2.1/appsettings.Testing.json + cat $ISND_TESTING_SETTINGS > appsettings.Testing.json ASPNETCORE_ENVIRONMENT=Testing dotnet test publish: tags: diff --git a/src/isn.abst/isn.abst.csproj b/src/isn.abst/isn.abst.csproj index ae0f314..0b3a272 100644 --- a/src/isn.abst/isn.abst.csproj +++ b/src/isn.abst/isn.abst.csproj @@ -1,7 +1,7 @@ - net45 + netcoreapp2.1 diff --git a/src/isn/Program.cs b/src/isn/Program.cs index 37b623d..616ced9 100644 --- a/src/isn/Program.cs +++ b/src/isn/Program.cs @@ -19,6 +19,7 @@ namespace isn { var json = File.ReadAllText(cfgSettingIf.FullName); settings = JsonConvert.DeserializeObject(json); + source = settings.DefaultSource; } } static OptionSet storeoptions = new OptionSet { @@ -165,7 +166,7 @@ namespace isn pushoptions.WriteOptionDescriptions(Console.Out); return; } - List reports = await PushPkgAsync(pargs); + List reports = PushPkg(pargs); Console.WriteLine(JsonConvert.SerializeObject(reports)); pushKO = reports.Count(r => !r.OK && !r.AlreadyPresent); } @@ -205,9 +206,7 @@ namespace isn } int runCode = commandSet.Run(args); - if (runCode == 0) if (pushKO > 0) return 3; - - return runCode; + return (runCode == 0 && pushKO > 0) ? 500 : runCode; } } diff --git a/src/isn/SourceHelpers.cs b/src/isn/SourceHelpers.cs index 5b568fa..c643c1e 100644 --- a/src/isn/SourceHelpers.cs +++ b/src/isn/SourceHelpers.cs @@ -10,21 +10,24 @@ namespace isn { public static class SourceHelpers { - public static async Task GetServerResourcesAsync(string url) + public static ApiIndexViewModel GetServerResources(string url) { HttpClient client = new HttpClient(); + ApiIndexViewModel result = null; // var json = await client.GetStringAsync(new System.Uri(url)); try { - var response = await client.GetStringAsync(url); - // var json = await response.Content.ReadAsStringAsync(); - return JsonConvert.DeserializeObject(response); - + Task.Run(async () => + { + var response = await client.GetStringAsync(url); + result = JsonConvert.DeserializeObject(response); + }).Wait(); } catch(Exception ex) { throw; } + return result; } public static async Task GetServerResourcesUsingWebRequestAsync(string url) diff --git a/src/isn/UploadFilesToServerUsingHttpClient.cs b/src/isn/UploadFilesToServerUsingHttpClient.cs new file mode 100644 index 0000000..c68a302 --- /dev/null +++ b/src/isn/UploadFilesToServerUsingHttpClient.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; + +namespace isn +{ + public static class UploadFilesToServerUsingHttpClient + { + public static PushReport UploadFilesToServer(Uri uri, FileInfo fi, + string apikey) + { + using (var client = new HttpClient()) + { + client.DefaultRequestHeaders.Add("X-NuGet-Client-Version", Constants.ClientVersion); + client.DefaultRequestHeaders.Add("X-NuGet-ApiKey", apikey); + using (var multipartFormDataContent = new MultipartFormDataContent()) + { + /* var values = new[] + { + new KeyValuePair("Id", Guid.NewGuid().ToString()), + new KeyValuePair("Key", "awesome"), + new KeyValuePair("From", "khalid@home.com") + //other values + };foreach (var keyValuePair in values) + { + multipartFormDataContent.Add(new StringContent(keyValuePair.Value), + String.Format("\"{0}\"", keyValuePair.Key)); + } */ + multipartFormDataContent.Add(new ByteArrayContent(File.ReadAllBytes(fi.FullName)), + '"' + "File" + '"', + '"' + fi.Name + '"'); + + var result = client.PutAsync(uri, multipartFormDataContent).Result; + result.EnsureSuccessStatusCode(); + if (result.IsSuccessStatusCode) + { + Task.Run(async ()=> + { + string report = await result.Content.ReadAsStringAsync(); + Console.WriteLine(report); + }).Wait(); + + } + return new PushReport(); + } + } + } + } +} \ No newline at end of file diff --git a/src/isn/UploadFilesToServerUsingWebRequest.cs b/src/isn/UploadFilesToServerUsingWebRequest.cs index 7720390..21ead1a 100644 --- a/src/isn/UploadFilesToServerUsingWebRequest.cs +++ b/src/isn/UploadFilesToServerUsingWebRequest.cs @@ -10,7 +10,7 @@ namespace isn public class UploadFilesToServerUsingWebRequest { - internal void UploadFilesToServer(PushReport report, Uri uri, FileInfo fi, + public void UploadFilesToServer(PushReport report, Uri uri, FileInfo fi, string apikey) { // string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; @@ -26,7 +26,7 @@ namespace isn byte[] fileheaderbytes = Encoding.ASCII.GetBytes(fileheader); var boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); var endBoundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--"); - HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri); + HttpWebRequest httpWebRequest = (HttpWebRequest) WebRequest.Create(uri); httpWebRequest.Method = "PUT"; httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary; diff --git a/src/isn/commands/PushCommand.cs b/src/isn/commands/PushCommand.cs index 794db2a..ed3c23e 100644 --- a/src/isn/commands/PushCommand.cs +++ b/src/isn/commands/PushCommand.cs @@ -9,63 +9,81 @@ namespace isn { public class PushCommand { - static public async Task RunAsync(string pkg, string source, string apikey) + static public PushReport Run(string pkg, string source) { + if (source == null) source = Program.Settings.DefaultSource; + if (source == null) throw new InvalidOperationException("source is null"); + string apikey = Program.Protector.UnProtect(Program.Settings.Sources[source].ApiKey); + var resources = SourceHelpers.GetServerResources(source); + if (resources.Resources == null) + throw new InvalidOperationException("source gave no resource"); + if (!resources.Resources.Any(res => res.Type == "PackagePublish/2.0.0")) + throw new InvalidOperationException("Source won't serve the expected push command"); + var pubRes = resources.Resources.First(res => res.Type == "PackagePublish/2.0.0"); FileInfo fi = new FileInfo(pkg); - var report = new PushReport - { - PkgName = fi.Name - }; if (!fi.Exists) { - report.Message = "Le fichier n'existe pas"; + var report = new PushReport + { + PkgName = fi.Name, + Message = "Le fichier n'existe pas" + }; return report; } try { - var wrqueryHandler = new UploadFilesToServerUsingWebRequest(); - // await wrqueryHandler.UploadFilesToServerAsync(report, new Uri(source), fi, apikey); - if (source == null) - { - source = Program.Settings.DefaultSource; - if (apikey is null) if (source!=null) - apikey = Program.Protector.UnProtect(Program.Settings.Sources[source].ApiKey); - } - if (apikey is null) if (source!=null) - if (Program.Settings.Sources.ContainsKey(source)) - apikey = Program.Protector.UnProtect(Program.Settings.Sources[source].ApiKey); - if (source == null) throw new InvalidOperationException("source is null"); - var resources = await SourceHelpers.GetServerResourcesAsync(source); - if (resources.Resources==null || resources.Resources.Any(res => res.Id == "" )) - throw new InvalidOperationException("Source won't serve the expected push command"); - Console.WriteLine(JsonConvert.SerializeObject(resources)); - wrqueryHandler.UploadFilesToServer(report, new Uri(resources.Resources[0].Id), fi, apikey); + Console.WriteLine("Connecting to "+pubRes.Id); + return UploadFilesToServerUsingHttpClient.UploadFilesToServer(new Uri(pubRes.Id), fi, apikey); } catch (WebException ex) { - await Console.Error.WriteLineAsync(ex.Message); + Console.Error.WriteLine(ex.Message); + var report = new PushReport + { + PkgName = fi.Name + }; report.StatusCode = ex.Status.ToString(); report.OK = false; - using (Stream respStream = ex.Response.GetResponseStream()) + if (ex.Response != null) { - StreamReader sr = new StreamReader(respStream); - string json = sr.ReadToEnd(); - var res = JsonConvert.DeserializeObject(json); - // ecode == 1 => package already present server side. - report.AlreadyPresent = res.ecode == 1; - report.Message = res.msg; + try + { + using (Stream respStream = ex.Response.GetResponseStream()) + { + StreamReader sr = new StreamReader(respStream); + string json = sr.ReadToEnd(); + var res = JsonConvert.DeserializeObject(json); + report.Message = res.msg; + + // ecode == 1 => package already present server side. + report.AlreadyPresent = res.ecode == 1; + } + } + catch (Exception iex) + { + report.Message = iex.Message; + } + } + + else + { + report.Message = ex.Message; + } + return report; } catch (Exception ex) { - report.Message = ex.Message; - report.StackTrace = ex.StackTrace; - await Console.Error.WriteLineAsync(ex.Message); + var report = new PushReport + { + PkgName = fi.Name, + Message = ex.Message, + StackTrace = ex.StackTrace + }; + Console.Error.WriteLine(ex.Message); throw; } - report.Executed = true; - return report; } } } \ No newline at end of file diff --git a/src/isn/commands/push.cs b/src/isn/commands/push.cs index abe021f..429c207 100644 --- a/src/isn/commands/push.cs +++ b/src/isn/commands/push.cs @@ -9,13 +9,13 @@ namespace isn partial class Program { - public static async Task> PushPkgAsync(IEnumerable pkgs) + public static List PushPkg(IEnumerable pkgs) { List pushReports = new List(); foreach (string pkg in pkgs) { - var report = await PushCommand.RunAsync(pkg, source, apiKey); + var report = PushCommand.Run(pkg, source); pushReports.Add(report); } diff --git a/src/isn/isn.csproj b/src/isn/isn.csproj index 48cbcb6..911b692 100644 --- a/src/isn/isn.csproj +++ b/src/isn/isn.csproj @@ -2,7 +2,7 @@ Exe - net45;netcoreapp2.1;net6 + netcoreapp2.1 nuget_cli 45b74c62-05bc-4603-95b4-3e80ae2fdf50 1.0.1 @@ -13,8 +13,9 @@ - + + diff --git a/test/isn.tests/UnitTest1.cs b/test/isn.tests/PushTest.cs similarity index 91% rename from test/isn.tests/UnitTest1.cs rename to test/isn.tests/PushTest.cs index 5ebd0cd..3226c22 100644 --- a/test/isn.tests/UnitTest1.cs +++ b/test/isn.tests/PushTest.cs @@ -57,10 +57,10 @@ dataTable.Rows.Add(dataRow); Assert.NotNull(vm.Resources); } [Fact] - public async Task TestPush() + public void TestPush() { Program.LoadConfig(); - var report = await Program.PushPkgAsync(new string[] { "bin/Debug/isn.1.0.1.nupkg" }); + var report = Program.PushPkg(new string[] { "/home/paul/Nupkgs/Yavsc.Abstract.1.0.8.nupkg" }); } [Fact] diff --git a/test/isn.tests/isn.tests.csproj b/test/isn.tests/isn.tests.csproj index 735af58..1b8f237 100644 --- a/test/isn.tests/isn.tests.csproj +++ b/test/isn.tests/isn.tests.csproj @@ -1,7 +1,7 @@ - net6 + netcoreapp2.1 false diff --git a/test/isnd.tests/UnitTestWebHost.cs b/test/isnd.tests/UnitTestWebHost.cs index 575153c..66dbe9e 100644 --- a/test/isnd.tests/UnitTestWebHost.cs +++ b/test/isnd.tests/UnitTestWebHost.cs @@ -14,6 +14,7 @@ namespace isnd.host.tests public class UnitTestWebHost { const string testingUrl = "http://localhost:5000"; + [Fact] public void TestHaveTestDbContextAndMigrate() diff --git a/test/isnd.tests/isnd.tests.csproj b/test/isnd.tests/isnd.tests.csproj index 9d3e076..fff32bf 100644 --- a/test/isnd.tests/isnd.tests.csproj +++ b/test/isnd.tests/isnd.tests.csproj @@ -1,7 +1,7 @@  - net6 + netcoreapp2.1 false d7144e46-4e63-4391-ba86-64b61f6e7be4