From 34afda9dfce5b09252bc159a4f644af9e52eb14d Mon Sep 17 00:00:00 2001 From: Pranav K Date: Mon, 17 Mar 2014 22:19:27 -0700 Subject: [PATCH] Adding tooling for mirroring project k packages --- tools/ProjectKClone/App.config | 6 + tools/ProjectKClone/MyService.cs | 108 ++++++++++++++++++ tools/ProjectKClone/MyServiceInstaller.cs | 45 ++++++++ tools/ProjectKClone/Program.cs | 27 +++++ tools/ProjectKClone/ProjectKClone.csproj | 79 +++++++++++++ tools/ProjectKClone/ProjectKClone.sln | 22 ++++ .../ProjectKClone/Properties/AssemblyInfo.cs | 36 ++++++ tools/ProjectKClone/packages.config | 5 + 8 files changed, 328 insertions(+) create mode 100644 tools/ProjectKClone/App.config create mode 100644 tools/ProjectKClone/MyService.cs create mode 100644 tools/ProjectKClone/MyServiceInstaller.cs create mode 100644 tools/ProjectKClone/Program.cs create mode 100644 tools/ProjectKClone/ProjectKClone.csproj create mode 100644 tools/ProjectKClone/ProjectKClone.sln create mode 100644 tools/ProjectKClone/Properties/AssemblyInfo.cs create mode 100644 tools/ProjectKClone/packages.config diff --git a/tools/ProjectKClone/App.config b/tools/ProjectKClone/App.config new file mode 100644 index 0000000000..9c05822ff5 --- /dev/null +++ b/tools/ProjectKClone/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/tools/ProjectKClone/MyService.cs b/tools/ProjectKClone/MyService.cs new file mode 100644 index 0000000000..02c66b4564 --- /dev/null +++ b/tools/ProjectKClone/MyService.cs @@ -0,0 +1,108 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Net; +using System.ServiceProcess; +using System.Threading; +using System.Threading.Tasks; +using NuGet; + +namespace NuGetClone +{ + public class MyService : ServiceBase + { + private static readonly Uri DeveloperFeed = new Uri("https://www.myget.org/F/aspnetvnext/api/v2"); + private static readonly ICredentials _credentials = new NetworkCredential("aspnetreadonly", "4d8a2d9c-7b80-4162-9978-47e918c9658c"); + + private Timer _timer; + private string _targetDirectory; + + protected override void OnStart(string[] args) + { + base.OnStart(args); + Init(); + _timer = new Timer(Run, null, TimeSpan.FromSeconds(1), TimeSpan.FromMinutes(2)); + } + + public void Init() + { + _targetDirectory = Environment.GetEnvironmentVariable("PROJECTK_PACKAGE_CACHE"); + if (string.IsNullOrEmpty(_targetDirectory)) + { + _targetDirectory = @"c:\projectk-cache"; + } + } + + public void Run(object state) + { + try + { + RunFromGallery(); + } + catch (Exception ex) + { + Trace.WriteLine(ex.Message); + } + } + + public void RunFromGallery() + { + Directory.CreateDirectory(_targetDirectory); + + var client = new HttpClient(DeveloperFeed); + client.SendingRequest += (sender, e) => + { + e.Request.Credentials = _credentials; + e.Request.PreAuthenticate = true; + }; + var remoteRepo = new DataServicePackageRepository(client); + var targetRepo = new LocalPackageRepository(_targetDirectory); + var packages = remoteRepo.GetPackages() + .Where(p => p.IsAbsoluteLatestVersion) + .ToList(); + Parallel.ForEach(packages, package => + { + if (!targetRepo.Exists(package)) + { + PurgeOldVersions(targetRepo, package); + + var packagePath = GetPackagePath(package); + + using (var input = package.GetStream()) + using (var output = File.Create(packagePath)) + { + input.CopyTo(output); + } + } + }); + } + + private string GetPackagePath(IPackage package) + { + return Path.Combine(_targetDirectory, package.Id + "." + package.Version + ".nupkg"); + } + + private void PurgeOldVersions(LocalPackageRepository targetRepo, IPackage package) + { + foreach (var oldPackage in targetRepo.FindPackagesById(package.Id).Where(p => p.Version < package.Version)) + { + try + { + var path = GetPackagePath(oldPackage); + File.Delete(path); + } + catch + { + // Ignore + } + } + } + + + protected override void OnStop() + { + base.OnStop(); + } + } +} diff --git a/tools/ProjectKClone/MyServiceInstaller.cs b/tools/ProjectKClone/MyServiceInstaller.cs new file mode 100644 index 0000000000..1e61e38cec --- /dev/null +++ b/tools/ProjectKClone/MyServiceInstaller.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Configuration.Install; +using System.Diagnostics; +using System.Linq; +using System.ServiceProcess; +using System.Text; +using System.Threading.Tasks; + +namespace NuGetClone +{ + [RunInstaller(true)] + public class MyServiceInstaller : Installer + { + internal const string ServiceName = "ProjectKClone"; + + public MyServiceInstaller() + { + var processInstaller = new ServiceProcessInstaller(); + var serviceInstaller = new ServiceInstaller(); + + processInstaller.Account = ServiceAccount.LocalSystem; + processInstaller.Username = null; + processInstaller.Password = null; + + serviceInstaller.ServiceName = ServiceName; + serviceInstaller.StartType = ServiceStartMode.Automatic; + + serviceInstaller.ServiceName = ServiceName; + + this.Installers.Add(processInstaller); + this.Installers.Add(serviceInstaller); + + this.Committed += new InstallEventHandler(ServiceInstaller_Committed); + } + + void ServiceInstaller_Committed(object sender, InstallEventArgs e) + { + // Auto Start the Service Once Installation is Finished. + var controller = new ServiceController(ServiceName); + controller.Start(); + } + } +} diff --git a/tools/ProjectKClone/Program.cs b/tools/ProjectKClone/Program.cs new file mode 100644 index 0000000000..c5e6e39818 --- /dev/null +++ b/tools/ProjectKClone/Program.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Configuration.Install; +using System.Diagnostics; +using System.Linq; +using System.ServiceProcess; +using System.Text; +using System.Threading.Tasks; + +namespace NuGetClone +{ + static class Program + { + /// + /// The main entry point for the application. + /// + public static void Main(string[] args) + { + ServiceBase[] ServicesToRun; + ServicesToRun = new ServiceBase[] + { + new MyService() + }; + ServiceBase.Run(ServicesToRun); + } + } +} diff --git a/tools/ProjectKClone/ProjectKClone.csproj b/tools/ProjectKClone/ProjectKClone.csproj new file mode 100644 index 0000000000..9851829e33 --- /dev/null +++ b/tools/ProjectKClone/ProjectKClone.csproj @@ -0,0 +1,79 @@ + + + + + Debug + AnyCPU + {7F7F6AD7-FF5E-48A2-9EE8-274E742787E1} + WinExe + Properties + ProjectKClone + ProjectKClone + v4.5.1 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + packages\Microsoft.Web.Xdt.1.0.0\lib\net40\Microsoft.Web.XmlTransform.dll + + + False + packages\Nuget.Core.2.8.0\lib\net40-Client\NuGet.Core.dll + + + + + + + + + + + + + + + + + + Component + + + + + Component + + + + + + + + + \ No newline at end of file diff --git a/tools/ProjectKClone/ProjectKClone.sln b/tools/ProjectKClone/ProjectKClone.sln new file mode 100644 index 0000000000..878a832060 --- /dev/null +++ b/tools/ProjectKClone/ProjectKClone.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.21126.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectKClone", "ProjectKClone.csproj", "{7F7F6AD7-FF5E-48A2-9EE8-274E742787E1}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7F7F6AD7-FF5E-48A2-9EE8-274E742787E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7F7F6AD7-FF5E-48A2-9EE8-274E742787E1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7F7F6AD7-FF5E-48A2-9EE8-274E742787E1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7F7F6AD7-FF5E-48A2-9EE8-274E742787E1}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/tools/ProjectKClone/Properties/AssemblyInfo.cs b/tools/ProjectKClone/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000..32f8346039 --- /dev/null +++ b/tools/ProjectKClone/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 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("NuGetClone")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("NuGetClone")] +[assembly: AssemblyCopyright("Copyright © 2014")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("cfb7b99d-4f04-4c77-ab79-b4d05aa5a1f5")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/tools/ProjectKClone/packages.config b/tools/ProjectKClone/packages.config new file mode 100644 index 0000000000..01686de28b --- /dev/null +++ b/tools/ProjectKClone/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file