diff --git a/README.md b/README.md index 4ceb1eb..3b3a078 100644 --- a/README.md +++ b/README.md @@ -2,5 +2,6 @@ [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/rhwy/cleancode-webget-tool?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +Cloned by Teybeo English readme not available at this time, it's only available in [french](readme.fr.md) diff --git a/Students/Teybeo/nget-v1/nget.sln b/Students/Teybeo/nget-v1/nget.sln new file mode 100644 index 0000000..0ef2ccc --- /dev/null +++ b/Students/Teybeo/nget-v1/nget.sln @@ -0,0 +1,18 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +# SharpDevelop 4.4 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "nget", "nget\nget.csproj", "{98625EA3-2BCD-4938-B748-7C5CD260A80B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {98625EA3-2BCD-4938-B748-7C5CD260A80B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {98625EA3-2BCD-4938-B748-7C5CD260A80B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {98625EA3-2BCD-4938-B748-7C5CD260A80B}.Release|Any CPU.Build.0 = Release|Any CPU + {98625EA3-2BCD-4938-B748-7C5CD260A80B}.Release|Any CPU.ActiveCfg = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Students/Teybeo/nget-v1/nget/Program.cs b/Students/Teybeo/nget-v1/nget/Program.cs new file mode 100644 index 0000000..a8782da --- /dev/null +++ b/Students/Teybeo/nget-v1/nget/Program.cs @@ -0,0 +1,120 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Net; + +namespace nget +{ + class Program + { + public static void Main(string[] args) + { + Console.WriteLine(args.Length); + + if (args.Length == 0) + return; + + var url = extractArg(args, "-url"); + + // The url is mandatory, so stop here if there is none + if (url == null) { + Console.WriteLine("The -url argument is missing"); + return; + } + + if (args[0].Equals("get")) { + + // save file is optional + var fileOutput = extractArg(args, "-save"); + + var result = getUrl(url); + + // if -save is valid, we save in file, else print on console + if (fileOutput != null) { + saveFile(fileOutput, result); + } else { + Console.WriteLine(result); + } + + } else if (args[0].Equals("test")) { + + // samples count is mandatory + string samplesCountString = extractArg(args, "-times"); + if (samplesCountString == null) { + Console.WriteLine("The -times argument is missing"); + return; + } + + int samplesCount = int.Parse(samplesCountString); + var samples = new long[samplesCount]; + + for (int i = 0; i < samplesCount; i++) { + getUrl(url, ref samples[i]); + } + + if (args.Contains("-avg")) { + Console.WriteLine("average: " + computeAvg(samples) + "ms"); + } else { + printTimes(samples); + } + } + } + + public static string extractArg(string[] args, string argName) { + + // For each string in the array + for (int i = 0; i < args.Length; i++) { + + // If we find the string we are looking for, return the next string (if it exists) + if (args[i].Equals(argName)) { + if (i + 1 < args.Length) + return args[i + 1]; + } + } + + return null; + } + + public static string getUrl(string url) { + long duration = 0; + return getUrl(url, ref duration); + } + + public static string getUrl(string url, ref long duration) { + + string response = null; + var webclient = new WebClient(); + + var chrono = new Stopwatch(); + chrono.Start(); + using (webclient) { + response = webclient.DownloadString(url); + } + chrono.Stop(); + duration = chrono.ElapsedMilliseconds; + return response; + } + + public static void saveFile(string fileName, string data) { + File.WriteAllText(fileName, data); + } + + public static void printTimes(long[] times) { + for (int i = 0; i < times.Length; i++) { + Console.WriteLine(times[i]); + } + } + + public static long computeAvg(long[] times) { + + long avg = 0; + + for (int i = 0; i < times.Length; i++) { + avg += times[i]; + } + + return avg / times.Length; + } + } +} \ No newline at end of file diff --git a/Students/Teybeo/nget-v1/nget/Properties/AssemblyInfo.cs b/Students/Teybeo/nget-v1/nget/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..1b0bef5 --- /dev/null +++ b/Students/Teybeo/nget-v1/nget/Properties/AssemblyInfo.cs @@ -0,0 +1,31 @@ +#region Using directives + +using System; +using System.Reflection; +using System.Runtime.InteropServices; + +#endregion + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("nget")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("nget")] +[assembly: AssemblyCopyright("Copyright 2015")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// This sets the default COM visibility of types in the assembly to invisible. +// If you need to expose a type to COM, use [ComVisible(true)] on that type. +[assembly: ComVisible(false)] + +// The assembly version has following format : +// +// Major.Minor.Build.Revision +// +// You can specify all the values or you can use the default the Revision and +// Build Numbers by using the '*' as shown below: +[assembly: AssemblyVersion("1.0.*")] diff --git a/Students/Teybeo/nget-v1/nget/nget.csproj b/Students/Teybeo/nget-v1/nget/nget.csproj new file mode 100644 index 0000000..ec96b35 --- /dev/null +++ b/Students/Teybeo/nget-v1/nget/nget.csproj @@ -0,0 +1,68 @@ + + + + {98625EA3-2BCD-4938-B748-7C5CD260A80B} + Debug + AnyCPU + Exe + nget + nget + v4.0 + Client + Properties + False + False + False + OnBuildSuccess + False + False + False + obj\$(Configuration)\ + 4 + + + x86 + 4194304 + False + Auto + 4096 + + + bin\Debug\ + True + Full + False + True + DEBUG;TRACE + obj\ + Project + get -url "http://api.openweathermap.org/data/2.5/weather?q=paris&units=metric" + + + bin\Release\ + False + None + True + False + TRACE + + + + + 3.5 + + + + 3.5 + + + + 3.5 + + + + + + + + \ No newline at end of file diff --git a/Students/Teybeo/nget-v2/nget-v2.sln b/Students/Teybeo/nget-v2/nget-v2.sln new file mode 100644 index 0000000..6709505 --- /dev/null +++ b/Students/Teybeo/nget-v2/nget-v2.sln @@ -0,0 +1,18 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +# SharpDevelop 4.4 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "nget-v2", "nget-v2\nget-v2.csproj", "{EAEEF979-34F9-4C1B-9507-0D4289892F85}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {EAEEF979-34F9-4C1B-9507-0D4289892F85}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EAEEF979-34F9-4C1B-9507-0D4289892F85}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EAEEF979-34F9-4C1B-9507-0D4289892F85}.Release|Any CPU.Build.0 = Release|Any CPU + {EAEEF979-34F9-4C1B-9507-0D4289892F85}.Release|Any CPU.ActiveCfg = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Students/Teybeo/nget-v2/nget-v2/Arg.cs b/Students/Teybeo/nget-v2/nget-v2/Arg.cs new file mode 100644 index 0000000..10f9454 --- /dev/null +++ b/Students/Teybeo/nget-v2/nget-v2/Arg.cs @@ -0,0 +1,28 @@ + +using System; + +namespace nget_v2 +{ + /// + /// Description of Arg. + /// + public class Arg + { + public string name { + get; set; + } + public bool isRequired { + get; set; + } + public bool hasValue { + get; set; + } + + public Arg(string _name, bool _isRequired, bool _hasValue) + { + name = _name; + isRequired = _isRequired; + hasValue = _hasValue; + } + } +} diff --git a/Students/Teybeo/nget-v2/nget-v2/Get.cs b/Students/Teybeo/nget-v2/nget-v2/Get.cs new file mode 100644 index 0000000..fc5243d --- /dev/null +++ b/Students/Teybeo/nget-v2/nget-v2/Get.cs @@ -0,0 +1,57 @@ + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Net; + +namespace nget_v2 +{ + /// + /// Description of Get. + /// + public class Get: ICommand + { + string fileOutput; + string url; + + public Get() + { + } + public string getName() + { + return "get"; + } + + public List getArgs() { + var list = new List(); + list.Add(new Arg("-save", false, true)); + list.Add(new Arg("-url", true, true)); + return list; + } + + public void setValues(Dictionary values) { + + // save file is optional + fileOutput = values["-save"]; + url = values["-url"]; + } + + public void execute() { + + var result = UrlDownloader.download(url); + + // if -save is valid, we save in file, else print on console + if (fileOutput != null) { + saveFile(fileOutput, result); + } else { + Console.WriteLine(result); + } + } + + private static void saveFile(string fileName, string data) { + File.WriteAllText(fileName, data); + } + + } +} diff --git a/Students/Teybeo/nget-v2/nget-v2/ICommand.cs b/Students/Teybeo/nget-v2/nget-v2/ICommand.cs new file mode 100644 index 0000000..7b37ff8 --- /dev/null +++ b/Students/Teybeo/nget-v2/nget-v2/ICommand.cs @@ -0,0 +1,17 @@ + +using System; +using System.Collections.Generic; + +namespace nget_v2 +{ + /// + /// Description of ICommand. + /// + public interface ICommand + { + string getName(); + List getArgs(); + void execute(); + void setValues(Dictionary values); + } +} diff --git a/Students/Teybeo/nget-v2/nget-v2/Program.cs b/Students/Teybeo/nget-v2/nget-v2/Program.cs new file mode 100644 index 0000000..179e8c5 --- /dev/null +++ b/Students/Teybeo/nget-v2/nget-v2/Program.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Net; +using nget_v2; + +namespace nget +{ + class Program + { + public static void Main(string[] args) + { + if (args.Length == 0) + return; + + foreach (var element in args) { + Console.Write(element + " "); + } + Console.WriteLine(); + + List list = new List(); + list.Add(new Get()); + list.Add(new Test()); + + foreach (var command in list) { + + if (match(command, args)) { + command.execute(); + break; + } + } + Console.ReadKey(); + } + + public static bool match(ICommand command, string[] args) { + + if (args[0].Equals(command.getName()) == false) + return false; + + List command_args = command.getArgs(); + + var values = new Dictionary(); + + foreach (var arg in command_args) { + + string value; + if (arg.hasValue) { + value = extractArg(args, arg.name); + } + else { + value = args.Contains(arg.name) ? "found" : null; + } + + if (arg.isRequired && value == null) { + Console.WriteLine("argument <" + arg.name + "> is missing"); + return false; + } + values.Add(arg.name, value); + + } + command.setValues(values); + return true; + } + + public static string extractArg(string[] args, string argName) { + + int index = Array.IndexOf(args, argName); + + if (index == -1 || (index + 1 == args.Length)) + return null; + + return args[index + 1]; + } + } +} \ No newline at end of file diff --git a/Students/Teybeo/nget-v2/nget-v2/Properties/AssemblyInfo.cs b/Students/Teybeo/nget-v2/nget-v2/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..b2291b5 --- /dev/null +++ b/Students/Teybeo/nget-v2/nget-v2/Properties/AssemblyInfo.cs @@ -0,0 +1,31 @@ +#region Using directives + +using System; +using System.Reflection; +using System.Runtime.InteropServices; + +#endregion + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("nget-v2")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("nget-v2")] +[assembly: AssemblyCopyright("Copyright 2015")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// This sets the default COM visibility of types in the assembly to invisible. +// If you need to expose a type to COM, use [ComVisible(true)] on that type. +[assembly: ComVisible(false)] + +// The assembly version has following format : +// +// Major.Minor.Build.Revision +// +// You can specify all the values or you can use the default the Revision and +// Build Numbers by using the '*' as shown below: +[assembly: AssemblyVersion("1.0.*")] diff --git a/Students/Teybeo/nget-v2/nget-v2/Test.cs b/Students/Teybeo/nget-v2/nget-v2/Test.cs new file mode 100644 index 0000000..88ba4ca --- /dev/null +++ b/Students/Teybeo/nget-v2/nget-v2/Test.cs @@ -0,0 +1,66 @@ + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Net; + +namespace nget_v2 +{ + /// + /// Description of Test. + /// + public class Test: ICommand + { + + int samplesCount; + string url; + bool average; + + public Test() + { + } + + public string getName() + { + return "test"; + } + + public List getArgs() + { + var list = new List(); + list.Add(new Arg("-times", true, true)); + list.Add(new Arg("-avg", false, false)); + list.Add(new Arg("-url", true, true)); + return list; + } + + public void setValues(Dictionary values) + { + samplesCount = int.Parse(values["-times"]); + url = values["-url"]; + average = values["-avg"] != null ? true : false; + } + + public void execute() { + + var samples = new long[samplesCount]; + + for (int i = 0; i < samplesCount; i++) { + UrlDownloader.download(url, ref samples[i]); + } + + if (average) { + Console.WriteLine("average: " + samples.Average() + "ms"); + } else { + printTimes(samples); + } + } + + private static void printTimes(long[] times) { + for (int i = 0; i < times.Length; i++) { + Console.WriteLine(times[i]); + } + } + } +} diff --git a/Students/Teybeo/nget-v2/nget-v2/UrlDownloader.cs b/Students/Teybeo/nget-v2/nget-v2/UrlDownloader.cs new file mode 100644 index 0000000..bc9a90d --- /dev/null +++ b/Students/Teybeo/nget-v2/nget-v2/UrlDownloader.cs @@ -0,0 +1,37 @@ + +using System; +using System.Diagnostics; +using System.Net; + +namespace nget_v2 +{ + /// + /// Description of UrlDownloader. + /// + public class UrlDownloader + { + public UrlDownloader() + { + } + + public static string download(string url) { + long duration = 0; + return download(url, ref duration); + } + + public static string download(string url, ref long duration) { + + string response = null; + var webclient = new WebClient(); + + var chrono = new Stopwatch(); + chrono.Start(); + using (webclient) { + response = webclient.DownloadString(url); + } + chrono.Stop(); + duration = chrono.ElapsedMilliseconds; + return response; + } + } +} diff --git a/Students/Teybeo/nget-v2/nget-v2/nget-v2.csproj b/Students/Teybeo/nget-v2/nget-v2/nget-v2.csproj new file mode 100644 index 0000000..4997d16 --- /dev/null +++ b/Students/Teybeo/nget-v2/nget-v2/nget-v2.csproj @@ -0,0 +1,55 @@ + + + + {EAEEF979-34F9-4C1B-9507-0D4289892F85} + Debug + AnyCPU + Exe + nget_v2 + nget-v2 + v4.0 + Client + Properties + + + x86 + + + bin\Debug\ + True + Full + False + True + DEBUG;TRACE + + + bin\Release\ + False + None + True + False + TRACE + + + + + 3.5 + + + + 3.5 + + + + 3.5 + + + + + + + + + + + \ No newline at end of file