rename to NuGetActionFactory, add Update packages getters
[teamcity/dotNetPackagesSupport.git] / nuget-agent / src / jetbrains / buildServer / nuget / agent / install / PackagesInstallerRunner.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;\r
18 \r
19 import com.intellij.openapi.diagnostic.Logger;\r
20 import jetbrains.buildServer.RunBuildException;\r
21 import jetbrains.buildServer.agent.*;\r
22 import jetbrains.buildServer.nuget.agent.util.DelegatingBuildProcess;\r
23 import jetbrains.buildServer.nuget.agent.util.impl.CompositeBuildProcessImpl;\r
24 import jetbrains.buildServer.nuget.common.DotNetConstants;\r
25 import jetbrains.buildServer.nuget.common.PackagesInstallerConstants;\r
26 import jetbrains.buildServer.util.FileUtil;\r
27 import org.jetbrains.annotations.NotNull;\r
28 \r
29 import java.io.File;\r
30 \r
31 import static jetbrains.buildServer.nuget.agent.install.LocateNuGetConfigBuildProcess.Callback;\r
32 \r
33 /**\r
34  * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
35  * Date: 07.07.11 13:55\r
36  */\r
37 public class PackagesInstallerRunner implements AgentBuildRunner, AgentBuildRunnerInfo {\r
38   private static final Logger LOG = Logger.getInstance(PackagesInstallerRunner.class.getName());\r
39 \r
40   private final NuGetActionFactory myInstallActionFactory;\r
41   private final PackageInstallParametersFactory myParametersFactory;\r
42 \r
43   public PackagesInstallerRunner(@NotNull final NuGetActionFactory installActionFactory,\r
44                                  @NotNull final PackageInstallParametersFactory parametersFactory) {\r
45     myInstallActionFactory = installActionFactory;\r
46     myParametersFactory = parametersFactory;\r
47   }\r
48 \r
49   @NotNull\r
50   public BuildProcess createBuildProcess(@NotNull AgentRunningBuild runningBuild,\r
51                                          @NotNull final BuildRunnerContext context) throws RunBuildException {\r
52     final CompositeBuildProcessImpl process = new CompositeBuildProcessImpl();\r
53     final PackagesInstallParameters parameters = myParametersFactory.loadParameters(context);\r
54     final LocateNuGetConfigBuildProcess locate = new LocateNuGetConfigBuildProcess(\r
55             parameters,\r
56             context.getBuild().getBuildLogger(),\r
57             createLocateCallback(context, process, parameters)\r
58     );\r
59     process.pushBuildProcess(locate);\r
60 \r
61     return process;\r
62   }\r
63 \r
64   @NotNull\r
65   private Callback createLocateCallback(@NotNull final BuildRunnerContext context,\r
66                                         @NotNull final CompositeBuildProcessImpl process,\r
67                                         @NotNull final PackagesInstallParameters parameters) {\r
68     return new Callback() {\r
69       public void onPackagesConfigFound(@NotNull final File config,\r
70                                         @NotNull final File targetFolder) {\r
71         process.pushBuildProcess(\r
72                 new DelegatingBuildProcess(\r
73                         new DelegatingBuildProcess.Action() {\r
74                           private final BuildProgressLogger logger = context.getBuild().getBuildLogger();\r
75 \r
76                           @NotNull\r
77                           public BuildProcess startImpl() throws RunBuildException {\r
78                             String pathToLog = FileUtil.getRelativePath(context.getBuild().getCheckoutDirectory(), config);\r
79                             if (pathToLog == null) pathToLog = config.getPath();\r
80                             logger.activityStarted("install", "Installing NuGet packages for " + pathToLog, "nuget");\r
81 \r
82                             return myInstallActionFactory.createInstall(context,\r
83                                     parameters,\r
84                                     config,\r
85                                     targetFolder)\r
86                                     ;\r
87                           }\r
88 \r
89                           public void finishedImpl() {\r
90                             logger.activityFinished("install", "nuget");\r
91                           }\r
92                         }\r
93                 )\r
94         );\r
95       }\r
96     };\r
97   }\r
98 \r
99   @NotNull\r
100   public AgentBuildRunnerInfo getRunnerInfo() {\r
101     return this;\r
102   }\r
103 \r
104   @NotNull\r
105   public String getType() {\r
106     return PackagesInstallerConstants.RUN_TYPE;\r
107   }\r
108 \r
109   public boolean canRun(@NotNull BuildAgentConfiguration agentConfiguration) {\r
110     if (!agentConfiguration.getSystemInfo().isWindows()) {\r
111       LOG.warn("NuGet packages installer available only under windows");\r
112       return false;\r
113     }\r
114 \r
115     if (!agentConfiguration.getConfigurationParameters().containsKey(DotNetConstants.DOT_NET_FRAMEWORK_4_x86)) {\r
116       LOG.warn("NuGet requires .NET Framework 4.0 x86 installed");\r
117       return false;\r
118     }\r
119 \r
120     return true;\r
121   }\r
122 }\r