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
31 * Created by Eugene Petrenko (eugene.petrenko@gmail.com)
\r
32 * Date: 07.07.11 15:13
\r
34 public class CommandlineBuildProcessFactoryImpl implements CommandlineBuildProcessFactory {
\r
35 private final BuildProcessFacade myFacade;
\r
37 public CommandlineBuildProcessFactoryImpl(@NotNull final BuildProcessFacade facade) {
\r
42 public BuildProcess executeCommandLine(@NotNull BuildRunnerContext hostContext,
\r
43 @NotNull File program,
\r
44 @NotNull Collection<String> argz,
\r
45 @NotNull File workingDir) throws RunBuildException {
\r
46 BuildRunnerContext context = myFacade.createBuildRunnerContext(
\r
47 hostContext.getBuild(),
\r
48 SimpleRunnerConstants.TYPE,
\r
49 workingDir.getPath(),
\r
53 context.addRunnerParameter(SimpleRunnerConstants.COMMAND_EXECUTABLE, program.getPath());
\r
54 context.addRunnerParameter(SimpleRunnerConstants.COMMAND_PARAMETERS, joinCommandLineArguments(argz));
\r
56 return myFacade.createExecutable(hostContext.getBuild(), context);
\r
59 //This is a same code as system uses in ProcessImpl class
\r
60 private String joinCommandLineArguments(@NotNull final Collection<String> cmd) {
\r
61 StringBuilder cmdbuf = new StringBuilder(80);
\r
62 boolean isFirst = true;
\r
63 for (String aCmd : cmd) {
\r
69 if (aCmd.indexOf(' ') < 0 && aCmd.indexOf('\t') < 0) {
\r
70 cmdbuf.append(aCmd);
\r
74 if (aCmd.charAt(0) != '"') {
\r
76 cmdbuf.append(aCmd);
\r
78 if (aCmd.endsWith("\\")) {
\r
79 cmdbuf.append("\\");
\r
83 } else if (aCmd.endsWith("\"")) {
\r
84 /* The argument has already been quoted. */
\r
85 cmdbuf.append(aCmd);
\r
87 /* Unmatched quote for the argument. */
\r
88 throw new IllegalArgumentException();
\r
91 return cmdbuf.toString();
\r