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