fix nuget runner crash if non-local nuget.exe was used
[teamcity/dotNetPackagesSupport.git] / nuget-extensions / nuget-runner / src / NuGetInstallExtensions.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.IO;\r
4 \r
5 namespace JetBrains.TeamCity.NuGetRunner\r
6 {\r
7   public class NuGetInstallExtensions\r
8   {\r
9     public NuGetInstallExtensions(NuGetRunner runner, IEnumerable<string> extensions)\r
10     {\r
11       Func<string> computeHome = () => Path.Combine(runner.NuGetExtensionsPath.Value, "TeamCity.Extensions");\r
12       runner.BeforeNuGetStarted += (_, __) =>\r
13                                      {\r
14                                        string home = computeHome();\r
15                                        if (!Directory.Exists(home))\r
16                                          Directory.CreateDirectory(home);\r
17 \r
18                                        foreach (var ext in extensions)\r
19                                        {\r
20                                          var destFileName = Path.Combine(home, Path.GetFileName(ext));\r
21                                          File.Copy(ext, destFileName);\r
22                                        }\r
23                                      };\r
24 \r
25       runner.AfterNuGetFinished += (_, __) =>\r
26                                      {\r
27                                        string home = computeHome();\r
28                                        if (Directory.Exists(home))\r
29                                          Directory.Delete(home, true);\r
30                                      };\r
31     }\r
32   }\r
33 }