d9e45746c5cbf478f72eb28461769d0c88dc7426
[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.NuGetParameters;\r
23 import jetbrains.buildServer.nuget.agent.parameters.PackagesParametersFactory;\r
24 import jetbrains.buildServer.nuget.agent.parameters.PackagesInstallParameters;\r
25 import jetbrains.buildServer.nuget.agent.parameters.PackagesUpdateParameters;\r
26 import jetbrains.buildServer.nuget.agent.util.DelegatingBuildProcess;\r
27 import jetbrains.buildServer.nuget.agent.util.impl.CompositeBuildProcessImpl;\r
28 import jetbrains.buildServer.nuget.common.DotNetConstants;\r
29 import jetbrains.buildServer.nuget.common.PackagesConstants;\r
30 import jetbrains.buildServer.util.FileUtil;\r
31 import org.jetbrains.annotations.NotNull;\r
32 import org.jetbrains.annotations.Nullable;\r
33 \r
34 import java.io.File;\r
35 \r
36 import static jetbrains.buildServer.nuget.agent.install.LocateNuGetConfigBuildProcess.Callback;\r
37 \r
38 /**\r
39  * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
40  * Date: 07.07.11 13:55\r
41  */\r
42 public class PackagesInstallerRunner implements AgentBuildRunner, AgentBuildRunnerInfo {\r
43   private static final Logger LOG = Logger.getInstance(PackagesInstallerRunner.class.getName());\r
44 \r
45   private final NuGetActionFactory myInstallActionFactory;\r
46   private final PackagesParametersFactory myParametersFactory;\r
47 \r
48   public PackagesInstallerRunner(@NotNull final NuGetActionFactory installActionFactory,\r
49                                  @NotNull final PackagesParametersFactory parametersFactory) {\r
50     myInstallActionFactory = installActionFactory;\r
51     myParametersFactory = parametersFactory;\r
52   }\r
53 \r
54   @NotNull\r
55   public BuildProcess createBuildProcess(@NotNull AgentRunningBuild runningBuild,\r
56                                          @NotNull final BuildRunnerContext context) throws RunBuildException {\r
57     final CompositeBuildProcessImpl process = new CompositeBuildProcessImpl();\r
58     final NuGetParameters parameters = myParametersFactory.loadNuGetParameters(context);\r
59     final PackagesInstallParameters installParameters = myParametersFactory.loadInstallPackagesParameters(context, parameters);\r
60     final PackagesUpdateParameters updateParameters = myParametersFactory.loadUpdatePackagesParameters(context, parameters);\r
61 \r
62     if (installParameters == null) {\r
63       throw new RunBuildException("NuGet install packages must be enabled");\r
64     }\r
65 \r
66     final LocateNuGetConfigBuildProcess locate = new LocateNuGetConfigBuildProcess(\r
67             parameters,\r
68             context.getBuild().getBuildLogger(),\r
69             createLocateCallback(context, process, installParameters, updateParameters)\r
70     );\r
71     process.pushBuildProcess(locate);\r
72 \r
73     return process;\r
74   }\r
75 \r
76   @NotNull\r
77   private Callback createLocateCallback(@NotNull final BuildRunnerContext context,\r
78                                         @NotNull final CompositeBuildProcessImpl process,\r
79                                         @NotNull final PackagesInstallParameters installParameters,\r
80                                         @Nullable final PackagesUpdateParameters updateParameters) {\r
81     return new Callback() {\r
82       public void onPackagesConfigFound(@NotNull final File config,\r
83                                         @NotNull final File targetFolder) {\r
84         process.pushBuildProcess(\r
85                 new DelegatingBuildProcess(\r
86                         new DelegatingBuildProcess.Action() {\r
87                           private final BuildProgressLogger logger = context.getBuild().getBuildLogger();\r
88 \r
89                           @NotNull\r
90                           public BuildProcess startImpl() throws RunBuildException {\r
91                             if (updateParameters != null) {\r
92                               process.pushBuildProcess(\r
93                                       createUpdateCallback(context, updateParameters, config, targetFolder));\r
94                             }\r
95 \r
96                             String pathToLog = FileUtil.getRelativePath(context.getBuild().getCheckoutDirectory(), config);\r
97                             if (pathToLog == null) pathToLog = config.getPath();\r
98                             logger.activityStarted("install", "Installing NuGet packages for " + pathToLog, "nuget");\r
99 \r
100 \r
101                             return myInstallActionFactory.createInstall(context,\r
102                                     installParameters,\r
103                                     config,\r
104                                     targetFolder)\r
105                                     ;\r
106                           }\r
107 \r
108                           public void finishedImpl() {\r
109                             logger.activityFinished("install", "nuget");\r
110                           }\r
111                         }\r
112                 )\r
113         );\r
114       }\r
115     };\r
116   }\r
117 \r
118   @NotNull\r
119   private BuildProcess createUpdateCallback(@NotNull final BuildRunnerContext context,\r
120                                             @NotNull final PackagesUpdateParameters parameters,\r
121                                             @NotNull final File config,\r
122                                             @NotNull final File targetFolder) {\r
123     return\r
124             new DelegatingBuildProcess(\r
125                     new DelegatingBuildProcess.Action() {\r
126                       private final BuildProgressLogger logger = context.getBuild().getBuildLogger();\r
127 \r
128                       @NotNull\r
129                       public BuildProcess startImpl() throws RunBuildException {\r
130                         String pathToLog = FileUtil.getRelativePath(context.getBuild().getCheckoutDirectory(), config);\r
131                         if (pathToLog == null) pathToLog = config.getPath();\r
132                         logger.activityStarted("update", "Updating NuGet packages for " + pathToLog, "nuget");\r
133 \r
134                         return myInstallActionFactory.createUpdate(context,\r
135                                 parameters,\r
136                                 config,\r
137                                 targetFolder)\r
138                                 ;\r
139                       }\r
140 \r
141                       public void finishedImpl() {\r
142                         logger.activityFinished("update", "nuget");\r
143                       }\r
144                     }\r
145             );\r
146   }\r
147 \r
148   @NotNull\r
149   public AgentBuildRunnerInfo getRunnerInfo() {\r
150     return this;\r
151   }\r
152 \r
153   @NotNull\r
154   public String getType() {\r
155     return PackagesConstants.RUN_TYPE;\r
156   }\r
157 \r
158   public boolean canRun(@NotNull BuildAgentConfiguration agentConfiguration) {\r
159     if (!agentConfiguration.getSystemInfo().isWindows()) {\r
160       LOG.warn("NuGet packages installer available only under windows");\r
161       return false;\r
162     }\r
163 \r
164     if (!agentConfiguration.getConfigurationParameters().containsKey(DotNetConstants.DOT_NET_FRAMEWORK_4_x86)) {\r
165       LOG.warn("NuGet requires .NET Framework 4.0 x86 installed");\r
166       return false;\r
167     }\r
168 \r
169     return true;\r
170   }\r
171 }\r