re-implement hide of nuget api key in build logs, add more tests for it
[teamcity/dotNetPackagesSupport.git] / nuget-agent / src / jetbrains / buildServer / nuget / agent / commands / impl / NuGetActionFactoryImpl.java
1 /*\r
2  * Copyright 2000-2011 JetBrains s.r.o.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  * http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package jetbrains.buildServer.nuget.agent.commands.impl;\r
18 \r
19 import com.intellij.openapi.diagnostic.Logger;\r
20 import jetbrains.buildServer.RunBuildException;\r
21 import jetbrains.buildServer.agent.BuildFinishedStatus;\r
22 import jetbrains.buildServer.agent.BuildProcess;\r
23 import jetbrains.buildServer.agent.BuildRunnerContext;\r
24 import jetbrains.buildServer.nuget.agent.commands.CommandFactory;\r
25 import jetbrains.buildServer.nuget.agent.commands.NuGetActionFactory;\r
26 import jetbrains.buildServer.nuget.agent.dependencies.PackageUsages;\r
27 import jetbrains.buildServer.nuget.agent.parameters.*;\r
28 import jetbrains.buildServer.nuget.agent.util.BuildProcessBase;\r
29 import jetbrains.buildServer.nuget.agent.util.CommandlineBuildProcessFactory;\r
30 import jetbrains.buildServer.util.StringUtil;\r
31 import org.jetbrains.annotations.NotNull;\r
32 \r
33 import java.io.File;\r
34 import java.util.ArrayList;\r
35 import java.util.Collection;\r
36 import java.util.List;\r
37 import java.util.Map;\r
38 \r
39 /**\r
40  * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
41  * Date: 07.07.11 17:49\r
42  */\r
43 public class NuGetActionFactoryImpl implements NuGetActionFactory {\r
44   private static final Logger LOG = Logger.getInstance(NuGetActionFactoryImpl.class.getName());\r
45 \r
46   private final CommandFactory myCommandFactory;\r
47   private final CommandlineBuildProcessFactory myFactory;\r
48   private final PackageUsages myPackageUsages;\r
49 \r
50   public NuGetActionFactoryImpl(@NotNull final CommandlineBuildProcessFactory factory,\r
51                                 @NotNull final PackageUsages packageUsages,\r
52                                 @NotNull final CommandFactory commandFactory) {\r
53     myFactory = factory;\r
54     myPackageUsages = packageUsages;\r
55     myCommandFactory = commandFactory;\r
56   }\r
57 \r
58   private CommandFactory.Callback<BuildProcess> getCallback(@NotNull final BuildRunnerContext context) {\r
59     return new CommandFactory.Callback<BuildProcess>() {\r
60       @NotNull\r
61       public BuildProcess createCommand(@NotNull File program,\r
62                                         @NotNull File workingDir,\r
63                                         @NotNull Collection<String> _argz,\r
64                                         @NotNull Map<String, String> additionalEnvironment) throws RunBuildException {\r
65         String cmd = context.getBuildParameters().getEnvironmentVariables().get("ComSpec");\r
66         if (StringUtil.isEmptyOrSpaces(cmd)) {\r
67           LOG.warn("Failed to find path to cmd.exe in %ComSpec% environment variable");\r
68           cmd = "cmd.exe";\r
69         }\r
70 \r
71         List<String> argz = new ArrayList<String>();\r
72         argz.add("/c");\r
73         argz.add(program.getPath());\r
74         argz.addAll(_argz);\r
75 \r
76         return myFactory.executeCommandLine(\r
77                 context,\r
78                 cmd,\r
79                 argz,\r
80                 workingDir,\r
81                 additionalEnvironment\r
82         );\r
83       }\r
84     };\r
85   }\r
86 \r
87   @NotNull\r
88   public BuildProcess createInstall(@NotNull final BuildRunnerContext context,\r
89                                     @NotNull final PackagesInstallParameters params,\r
90                                     @NotNull final File packagesConfig,\r
91                                     @NotNull final File targetFolder) throws RunBuildException {\r
92     return myCommandFactory.createInstall(params, packagesConfig, targetFolder, getCallback(context));\r
93   }\r
94 \r
95 \r
96   @NotNull\r
97   public BuildProcess createUpdate(@NotNull final BuildRunnerContext context,\r
98                                    @NotNull final PackagesUpdateParameters params,\r
99                                    @NotNull final File packagesConfig,\r
100                                    @NotNull final File targetFolder) throws RunBuildException {\r
101     return myCommandFactory.createUpdate(params, packagesConfig, targetFolder, getCallback(context));\r
102   }\r
103 \r
104   @NotNull\r
105   public BuildProcess createUsageReport(@NotNull final BuildRunnerContext context,\r
106                                         @NotNull final NuGetFetchParameters params,\r
107                                         @NotNull final File packagesConfig,\r
108                                         @NotNull final File targetFolder) throws RunBuildException {\r
109     return new BuildProcessBase() {\r
110       @NotNull\r
111       @Override\r
112       protected BuildFinishedStatus waitForImpl() throws RunBuildException {\r
113         myPackageUsages.createReport(packagesConfig);\r
114         return BuildFinishedStatus.FINISHED_SUCCESS;\r
115       }\r
116     };\r
117   }\r
118 \r
119   @NotNull\r
120   public BuildProcess createPush(@NotNull BuildRunnerContext context,\r
121                                  @NotNull NuGetPublishParameters params,\r
122                                  @NotNull File packagePath) throws RunBuildException {\r
123     return myCommandFactory.createPush(params, packagePath, getCallback(context));\r
124   }\r
125 \r
126   @NotNull\r
127   public BuildProcess createPack(@NotNull BuildRunnerContext context, @NotNull NuGetPackParameters params) throws RunBuildException {\r
128     return myCommandFactory.createPack(params, context.getBuild().getCheckoutDirectory(), getCallback(context));\r
129   }\r
130 }\r