2 * Copyright 2000-2011 JetBrains s.r.o.
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
17 package jetbrains.buildServer.nuget.agent.util.impl;
\r
19 import jetbrains.buildServer.RunBuildException;
\r
20 import jetbrains.buildServer.agent.BuildProcess;
\r
21 import jetbrains.buildServer.agent.BuildProcessFacade;
\r
22 import jetbrains.buildServer.agent.BuildRunnerContext;
\r
23 import jetbrains.buildServer.nuget.agent.util.CommandlineBuildProcessFactory;
\r
24 import jetbrains.buildServer.runner.SimpleRunnerConstants;
\r
25 import org.jetbrains.annotations.NotNull;
\r
27 import java.io.File;
\r
28 import java.util.Collection;
\r
29 import java.util.Map;
\r
32 * Created by Eugene Petrenko (eugene.petrenko@gmail.com)
\r
33 * Date: 07.07.11 15:13
\r
35 public class CommandlineBuildProcessFactoryImpl implements CommandlineBuildProcessFactory {
\r
36 private final BuildProcessFacade myFacade;
\r
38 public CommandlineBuildProcessFactoryImpl(@NotNull final BuildProcessFacade facade) {
\r
43 public BuildProcess executeCommandLine(@NotNull BuildRunnerContext hostContext,
\r
44 @NotNull File program,
\r
45 @NotNull Collection<String> argz,
\r
46 @NotNull File workingDir,
\r
47 @NotNull final Map<String, String> additionalEnvironment) throws RunBuildException {
\r
48 BuildRunnerContext context = myFacade.createBuildRunnerContext(
\r
49 hostContext.getBuild(),
\r
50 SimpleRunnerConstants.TYPE,
\r
51 workingDir.getPath(),
\r
55 for (Map.Entry<String, String> entry : additionalEnvironment.entrySet()) {
\r
56 context.addEnvironmentVariable(entry.getKey(), entry.getValue());
\r
59 context.addRunnerParameter(SimpleRunnerConstants.COMMAND_EXECUTABLE, program.getPath());
\r
60 context.addRunnerParameter(SimpleRunnerConstants.COMMAND_PARAMETERS, joinCommandLineArguments(argz));
\r
62 return myFacade.createExecutable(hostContext.getBuild(), context);
\r
65 //This is a same code as system uses in ProcessImpl class
\r
66 private String joinCommandLineArguments(@NotNull final Collection<String> cmd) {
\r
67 StringBuilder cmdbuf = new StringBuilder(80);
\r
68 boolean isFirst = true;
\r
69 for (String aCmd : cmd) {
\r
75 if (aCmd.indexOf(' ') < 0 && aCmd.indexOf('\t') < 0) {
\r
76 cmdbuf.append(aCmd);
\r
80 if (aCmd.charAt(0) != '"') {
\r
82 cmdbuf.append(aCmd);
\r
84 if (aCmd.endsWith("\\")) {
\r
85 cmdbuf.append("\\");
\r
89 } else if (aCmd.endsWith("\"")) {
\r
90 /* The argument has already been quoted. */
\r
91 cmdbuf.append(aCmd);
\r
93 /* Unmatched quote for the argument. */
\r
94 throw new IllegalArgumentException();
\r
97 return cmdbuf.toString();
\r