d27aee8f2a9de293e9acde79341db83611421d81
[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 jetbrains.buildServer.RunBuildException;\r
20 import jetbrains.buildServer.agent.BuildFinishedStatus;\r
21 import jetbrains.buildServer.agent.BuildProcess;\r
22 import jetbrains.buildServer.agent.BuildRunnerContext;\r
23 import jetbrains.buildServer.nuget.agent.commands.CommandFactory;\r
24 import jetbrains.buildServer.nuget.agent.commands.NuGetActionFactory;\r
25 import jetbrains.buildServer.nuget.agent.dependencies.PackageUsages;\r
26 import jetbrains.buildServer.nuget.agent.parameters.*;\r
27 import jetbrains.buildServer.nuget.agent.util.BuildProcessBase;\r
28 import jetbrains.buildServer.nuget.agent.util.CommandlineBuildProcessFactory;\r
29 import org.jetbrains.annotations.NotNull;\r
30 \r
31 import java.io.File;\r
32 import java.util.Collection;\r
33 import java.util.Map;\r
34 \r
35 /**\r
36  * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
37  * Date: 07.07.11 17:49\r
38  */\r
39 public class NuGetActionFactoryImpl implements NuGetActionFactory {\r
40   private final CommandFactory myCommandFactory;\r
41   private final CommandlineBuildProcessFactory myFactory;\r
42   private final PackageUsages myPackageUsages;\r
43 \r
44   public NuGetActionFactoryImpl(@NotNull final CommandlineBuildProcessFactory factory,\r
45                                 @NotNull final PackageUsages packageUsages,\r
46                                 @NotNull final CommandFactory commandFactory) {\r
47     myFactory = factory;\r
48     myPackageUsages = packageUsages;\r
49     myCommandFactory = commandFactory;\r
50   }\r
51 \r
52   private CommandFactory.Callback<BuildProcess> getCallback(@NotNull final BuildRunnerContext context) {\r
53     return new CommandFactory.Callback<BuildProcess>() {\r
54       @NotNull\r
55       public BuildProcess createCommand(@NotNull File program,\r
56                                         @NotNull File workingDir,\r
57                                         @NotNull Collection<String> argz,\r
58                                         @NotNull Map<String, String> additionalEnvironment) throws RunBuildException {\r
59         return myFactory.executeCommandLine(\r
60                 context,\r
61                 program,\r
62                 argz,\r
63                 workingDir,\r
64                 additionalEnvironment\r
65         );\r
66       }\r
67     };\r
68   }\r
69 \r
70   @NotNull\r
71   public BuildProcess createInstall(@NotNull final BuildRunnerContext context,\r
72                                     @NotNull final PackagesInstallParameters params,\r
73                                     @NotNull final File packagesConfig,\r
74                                     @NotNull final File targetFolder) throws RunBuildException {\r
75     return myCommandFactory.createInstall(params, packagesConfig, targetFolder, getCallback(context));\r
76   }\r
77 \r
78 \r
79   @NotNull\r
80   public BuildProcess createUpdate(@NotNull final BuildRunnerContext context,\r
81                                    @NotNull final PackagesUpdateParameters params,\r
82                                    @NotNull final File packagesConfig,\r
83                                    @NotNull final File targetFolder) throws RunBuildException {\r
84     return myCommandFactory.createUpdate(params, packagesConfig, targetFolder, getCallback(context));\r
85   }\r
86 \r
87   @NotNull\r
88   public BuildProcess createUsageReport(@NotNull final BuildRunnerContext context,\r
89                                         @NotNull final NuGetFetchParameters params,\r
90                                         @NotNull final File packagesConfig,\r
91                                         @NotNull final File targetFolder) throws RunBuildException {\r
92     return new BuildProcessBase() {\r
93       @NotNull\r
94       @Override\r
95       protected BuildFinishedStatus waitForImpl() throws RunBuildException {\r
96         myPackageUsages.createReport(packagesConfig);\r
97         return BuildFinishedStatus.FINISHED_SUCCESS;\r
98       }\r
99     };\r
100   }\r
101 \r
102   @NotNull\r
103   public BuildProcess createPush(@NotNull BuildRunnerContext context,\r
104                                  @NotNull NuGetPublishParameters params,\r
105                                  @NotNull File packagePath) throws RunBuildException {\r
106     return myCommandFactory.createPush(params, packagePath, getCallback(context));\r
107   }\r
108 \r
109   @NotNull\r
110   public BuildProcess createPack(@NotNull BuildRunnerContext context, @NotNull NuGetPackParameters params) throws RunBuildException {\r
111     return myCommandFactory.createPack(params, context.getBuild().getCheckoutDirectory(), getCallback(context));\r
112   }\r
113 }\r