/*
 * Copyright 2000-2011 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jetbrains.buildServer.nuget.server.toolRegistry.impl;

import jetbrains.buildServer.nuget.server.ToolPaths;
import jetbrains.buildServer.util.StringUtil;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Eugene Petrenko (eugene.petrenko@gmail.com)
 * Date: 16.08.11 1:00
 */
public class PluginNaming {
  private final ToolPaths myPaths;

  public PluginNaming(@NotNull final ToolPaths paths) {
    myPaths = paths;
  }

  @NotNull
  public File getUnpackedFolder(@NotNull final File packageFile) {
    //here we could take a look into .nuspec to fetch version and name
    return new File(myPaths.getNuGetToolsPath(), packageFile.getName());
  }

  @NotNull
  public File getAgentFile(@NotNull final File packageFile) {
    //here we could take a look into .nuspec to fetch version and name
    return new File(myPaths.getNuGetToolsAgentPluginsPath(), packageFile.getName() + ".zip");
  }

  @NotNull
  public String getVersion(@NotNull final File plugin) {
    Pattern pt = Pattern.compile("\\d+(\\.\\d+)+");
    final Matcher matcher = pt.matcher(plugin.getName());
    String match = "N/A";
    while (matcher.find()) {
      match = matcher.group();
    }
    return StringUtil.isEmptyOrSpaces(match) ? "N/A" : match;
  }
}

@@ -1744,16 +1744,17 @@
 ompile(%22
+(
 %5C%5Cd+(%5C%5C.
@@ -1758,17 +1758,44 @@
 %5C%5C.%5C%5Cd+)
-+
+*.*)%5C%5C.%5BNn%5D%5BUu%5D%5BPp%5D%5BKk%5D%5BGg%5D$
 %22);%0D%0A   
@@ -1935,16 +1935,17 @@
 r.group(
+1
 );%0D%0A    

support prerelease nuget versions in UI