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