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