package jetbrains.buildServer.nuget.agent;

import com.intellij.openapi.diagnostic.Logger;
import jetbrains.buildServer.agent.DirectoryCleanersProvider;
import jetbrains.buildServer.agent.DirectoryCleanersProviderContext;
import jetbrains.buildServer.agent.DirectoryCleanersRegistry;
import jetbrains.buildServer.util.StringUtil;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.Date;

/**
 * Cleans up nuget package and cache directories.
 */
public class NuGetCacheCleaner implements DirectoryCleanersProvider {

  private static final String CLEANER_FORMAT = "Registering directory %s for cleaning";
  private static final Logger LOG = Logger.getInstance(NuGetCacheCleaner.class.getName());

  @NotNull
  @Override
  public String getCleanerName() {
    return "NuGet Cache Cleaner";
  }

  @Override
  public void registerDirectoryCleaners(@NotNull final DirectoryCleanersProviderContext context,
                                        @NotNull final DirectoryCleanersRegistry registry) {
    final String nugetPackages = System.getenv("NUGET_PACKAGES");
    if (!StringUtil.isEmptyOrSpaces(nugetPackages)) {
      final File globalPackages = new File(nugetPackages);
      if (globalPackages.isAbsolute()) {
        LOG.info(String.format(CLEANER_FORMAT, globalPackages));
        registry.addCleaner(globalPackages, new Date());
      }
    }

    final String localAppData = System.getenv("LOCALAPPDATA");
    if (!StringUtil.isEmptyOrSpaces(localAppData)) {
      final File nugetCache = new File(localAppData, "NuGet/Cache");
      LOG.info(String.format(CLEANER_FORMAT, nugetCache));
      registry.addCleaner(nugetCache, new Date());

      final File nugetV3Cache = new File(localAppData, "NuGet/v3-cache");
      LOG.info(String.format(CLEANER_FORMAT, nugetV3Cache));
      registry.addCleaner(nugetV3Cache, new Date());
    }

    final String userHome = System.getenv("user.home");
    if (!StringUtil.isEmptyOrSpaces(userHome)) {
      final File homeCache = new File(userHome, ".nuget/packages");
      LOG.info(String.format(CLEANER_FORMAT, homeCache));
      registry.addCleaner(homeCache, new Date());
    }
  }
}

@@ -1235,16 +1235,43 @@
 solute()
+ && globalPackages.exists()
 ) %7B%0A    
@@ -1580,32 +1580,67 @@
 %22NuGet/Cache%22);%0A
+      if (nugetCache.exists()) %7B%0A  
       LOG.info(S
@@ -1674,32 +1674,34 @@
 , nugetCache));%0A
+  
       registry.a
@@ -1730,24 +1730,32 @@
 new Date());
+%0A      %7D
 %0A%0A      fina
@@ -1814,24 +1814,61 @@
 v3-cache%22);%0A
+      if (nugetV3Cache.exists()) %7B%0A  
       LOG.in
@@ -1914,32 +1914,34 @@
 3Cache));%0A      
+  
 registry.addClea
@@ -1967,24 +1967,32 @@
 ew Date());%0A
+      %7D%0A
     %7D%0A%0A    f
@@ -2024,19 +2024,24 @@
 stem.get
-env
+Property
 (%22user.h
@@ -2160,24 +2160,58 @@
 packages%22);%0A
+      if (homeCache.exists()) %7B%0A  
       LOG.in
@@ -2258,24 +2258,26 @@
 he));%0A      
+ 
+ 
 registry.add
@@ -2304,24 +2304,32 @@
 ew Date());%0A
+      %7D%0A
     %7D%0A  %7D%0A%7D%0A

Fix user home directory detection while cleaning NuGet caches