From 744662e257214d9ed8bece457cfe368dd1b9a981 Mon Sep 17 00:00:00 2001 From: fekir Date: Thu, 2 Feb 2017 18:05:10 +0100 Subject: [PATCH 1/5] Initial support for new parameter (cache dir) --- src/ChocolateStore/Arguments.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ChocolateStore/Arguments.cs b/src/ChocolateStore/Arguments.cs index 8c515b4..cb51edd 100644 --- a/src/ChocolateStore/Arguments.cs +++ b/src/ChocolateStore/Arguments.cs @@ -2,7 +2,11 @@ { class Arguments { + // where the packages are saved and referenced public string Directory { get; set; } + //where to download the package from public string Url { get; set; } + // where to save the packages + public string CacheDir { get; set; } } } From badefb80215837b3bc1bd9b6d6f76ef13cdcf7e3 Mon Sep 17 00:00:00 2001 From: fekir Date: Thu, 2 Feb 2017 18:07:37 +0100 Subject: [PATCH 2/5] update command line interface and error message --- src/ChocolateStore/Program.cs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/ChocolateStore/Program.cs b/src/ChocolateStore/Program.cs index 50a2c94..86340bf 100644 --- a/src/ChocolateStore/Program.cs +++ b/src/ChocolateStore/Program.cs @@ -37,17 +37,26 @@ private static Arguments ParseArguments(string[] args) Arguments arguments = new Arguments(); - if (args.Length != 2) - { - WriteError("USAGE: ChocolateStore "); + if (args.Length != 2 && args.Length != 3 ) + { + WriteError("USAGE: ChocolateStore "); return null; } arguments.Directory = args[0]; - if (!Directory.Exists(arguments.Directory)) + if (args.Length == 3) + { + arguments.CacheDir = args[2]; + } + else + { + arguments.CacheDir = arguments.Directory; + } + + if (!Directory.Exists(arguments.CacheDir)) { - WriteError("Directory '{0}' does not exist.", arguments.Directory); + WriteError("Directory '{0}' does not exist.", arguments.CacheDir); return null; } From fd67bd194c8389b5ee5f36df54d5245310b7df76 Mon Sep 17 00:00:00 2001 From: fekir Date: Thu, 2 Feb 2017 18:25:42 +0100 Subject: [PATCH 3/5] completed support for cache directory as third parameter --- src/ChocolateStore/PackageCacher.cs | 22 ++++++++++++---------- src/ChocolateStore/Program.cs | 2 +- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/ChocolateStore/PackageCacher.cs b/src/ChocolateStore/PackageCacher.cs index e5d9f47..f94be1a 100644 --- a/src/ChocolateStore/PackageCacher.cs +++ b/src/ChocolateStore/PackageCacher.cs @@ -17,12 +17,12 @@ class PackageCacher public delegate void DownloadFailedHandler(string url, Exception ex); public event FileHandler SkippingFile = delegate { }; - public event FileHandler DownloadingFile = delegate { }; + public event FileHandler DownloadingFile = delegate { }; public event DownloadFailedHandler DownloadFailed = delegate { }; - public void CachePackage(string dir, string url) + public void CachePackage(string dir, string url, string cachedir) { - var packagePath = DownloadFile(url, dir); + var packagePath = DownloadFile(url, cachedir, cachedir); using (var zip = ZipFile.Read(packagePath)) { @@ -41,7 +41,7 @@ public void CachePackage(string dir, string url) } } - content = CacheUrlFiles(Path.Combine(dir, packageName), content); + content = CacheUrlFiles(Path.Combine(dir, packageName), content, Path.Combine(cachedir, packageName)); zip.UpdateEntry(INSTALL_FILE, content); zip.Save(); @@ -51,20 +51,22 @@ public void CachePackage(string dir, string url) } - private string CacheUrlFiles(string folder, string content) + private string CacheUrlFiles(string folder, string content, string cachedir) { const string pattern = "(?<=['\"])http[\\S ]*(?=['\"])"; - if (!Directory.Exists(folder)) { - Directory.CreateDirectory(folder); + if (!Directory.Exists(cachedir)) { + Directory.CreateDirectory(cachedir); } - return Regex.Replace(content, pattern, new MatchEvaluator(m => DownloadFile(m.Value, folder))); + return Regex.Replace(content, pattern, new MatchEvaluator(m => DownloadFile(m.Value, cachedir, folder))); } - private string DownloadFile(string url, string destination) + // the third parameter is used as return value for the matcher, + // the file is downloaded at destination + private string DownloadFile(string url, string destination, string store) { try @@ -87,7 +89,7 @@ private string DownloadFile(string url, string destination) } } - return filePath; + return Path.Combine(store, fileName); } catch (Exception ex) { diff --git a/src/ChocolateStore/Program.cs b/src/ChocolateStore/Program.cs index 86340bf..6aef417 100644 --- a/src/ChocolateStore/Program.cs +++ b/src/ChocolateStore/Program.cs @@ -21,7 +21,7 @@ static void Main(string[] args) if (arguments != null) { - cacher.CachePackage(arguments.Directory, arguments.Url); + cacher.CachePackage(arguments.Directory, arguments.Url, arguments.CacheDir); } } From e3775ae93f49b5643e6073f4e5b656e164d373e2 Mon Sep 17 00:00:00 2001 From: fekir Date: Thu, 2 Feb 2017 18:28:00 +0100 Subject: [PATCH 4/5] made line endings consistent --- src/ChocolateStore/PackageCacher.cs | 195 ++++++++++++++-------------- 1 file changed, 98 insertions(+), 97 deletions(-) diff --git a/src/ChocolateStore/PackageCacher.cs b/src/ChocolateStore/PackageCacher.cs index f94be1a..30dc010 100644 --- a/src/ChocolateStore/PackageCacher.cs +++ b/src/ChocolateStore/PackageCacher.cs @@ -1,103 +1,104 @@ -using System; -using System.IO; -using System.Linq; -using System.Net; -using System.Text; -using System.Text.RegularExpressions; -using Ionic.Zip; - -namespace ChocolateStore -{ - class PackageCacher - { - - private const string INSTALL_FILE = "tools/chocolateyInstall.ps1"; - - public delegate void FileHandler(string fileName); - public delegate void DownloadFailedHandler(string url, Exception ex); - - public event FileHandler SkippingFile = delegate { }; - public event FileHandler DownloadingFile = delegate { }; - public event DownloadFailedHandler DownloadFailed = delegate { }; - - public void CachePackage(string dir, string url, string cachedir) - { - var packagePath = DownloadFile(url, cachedir, cachedir); - - using (var zip = ZipFile.Read(packagePath)) - { - var entry = zip.FirstOrDefault(x => string.Equals(x.FileName, INSTALL_FILE, StringComparison.OrdinalIgnoreCase)); - - if (entry != null) { - string content = null; - var packageName = Path.GetFileNameWithoutExtension(packagePath); - - using (MemoryStream ms = new MemoryStream()) { +using System; +using System.IO; +using System.Linq; +using System.Net; +using System.Text; +using System.Text.RegularExpressions; +using Ionic.Zip; + +namespace ChocolateStore +{ + class PackageCacher + { + + private const string INSTALL_FILE = "tools/chocolateyInstall.ps1"; + + public delegate void FileHandler(string fileName); + public delegate void DownloadFailedHandler(string url, Exception ex); + + public event FileHandler SkippingFile = delegate { }; + public event FileHandler DownloadingFile = delegate { }; + public event DownloadFailedHandler DownloadFailed = delegate { }; + + public void CachePackage(string dir, string url, string cachedir) + { + var packagePath = DownloadFile(url, cachedir, cachedir); + + using (var zip = ZipFile.Read(packagePath)) + { + var entry = zip.FirstOrDefault(x => string.Equals(x.FileName, INSTALL_FILE, StringComparison.OrdinalIgnoreCase)); + + if (entry != null) { + string content = null; + var packageName = Path.GetFileNameWithoutExtension(packagePath); + + using (MemoryStream ms = new MemoryStream()) { entry.Extract(ms); ms.Position = 0; using (StreamReader reader = new StreamReader(ms, true)) { content = reader.ReadToEnd(); } - } - - content = CacheUrlFiles(Path.Combine(dir, packageName), content, Path.Combine(cachedir, packageName)); - zip.UpdateEntry(INSTALL_FILE, content); - zip.Save(); - - } - - } - - } - - private string CacheUrlFiles(string folder, string content, string cachedir) - { - - const string pattern = "(?<=['\"])http[\\S ]*(?=['\"])"; - - if (!Directory.Exists(cachedir)) { - Directory.CreateDirectory(cachedir); - } - - return Regex.Replace(content, pattern, new MatchEvaluator(m => DownloadFile(m.Value, cachedir, folder))); - - } - - // the third parameter is used as return value for the matcher, - // the file is downloaded at destination - private string DownloadFile(string url, string destination, string store) - { - - try - { - var request = WebRequest.Create(url); - var response = request.GetResponse(); - var fileName = Path.GetFileName(response.ResponseUri.LocalPath); - var filePath = Path.Combine(destination, fileName); - - if (File.Exists(filePath)) - { - SkippingFile(fileName); - } - else - { - DownloadingFile(fileName); - using (var fs = File.Create(filePath)) - { - response.GetResponseStream().CopyTo(fs); - } - } - - return Path.Combine(store, fileName); - } - catch (Exception ex) - { - DownloadFailed(url, ex); - return url; - } - - } - - } -} \ No newline at end of file + } + + content = CacheUrlFiles(Path.Combine(dir, packageName), content, Path.Combine(cachedir, packageName)); + zip.UpdateEntry(INSTALL_FILE, content); + zip.Save(); + + } + + } + + } + + private string CacheUrlFiles(string folder, string content, string cachedir) + { + + const string pattern = "(?<=['\"])http[\\S ]*(?=['\"])"; + + if (!Directory.Exists(cachedir)) { + Directory.CreateDirectory(cachedir); + } + + return Regex.Replace(content, pattern, new MatchEvaluator(m => DownloadFile(m.Value, cachedir, folder))); + + } + + // the third parameter is used as return value for the matcher, + // the file is downloaded at destination + private string DownloadFile(string url, string destination, string store) + { + + try + { + var request = WebRequest.Create(url); + var response = request.GetResponse(); + var fileName = Path.GetFileName(response.ResponseUri.LocalPath); + var filePath = Path.Combine(destination, fileName); + + if (File.Exists(filePath)) + { + SkippingFile(fileName); + } + else + { + DownloadingFile(fileName); + using (var fs = File.Create(filePath)) + { + response.GetResponseStream().CopyTo(fs); + } + } + + return Path.Combine(store, fileName); + } + catch (Exception ex) + { + DownloadFailed(url, ex); + return url; + } + + } + + } +} + From 042ab6739d20960bdd180748ec71c9581ef25b39 Mon Sep 17 00:00:00 2001 From: PGP 2F93D5AF Date: Fri, 3 Nov 2017 22:39:26 +0100 Subject: [PATCH 5/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8f2a205..b1d4ab8 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Apache 2.0 - see LICENSE ### COMPILATION REQUIREMENTS * Visual Studio 2010 -* .NET Framewrok 4.0 +* .NET Framework 4.0 * NuGet Package Manager with "Allow NuGet to download missing packages" setting enabled ### SYNTAX