vcs: extract method

GitOrigin-RevId: 254dbd05f43d740c7c4038b25ac7a04f6a47f6f4
This commit is contained in:
Aleksey Pivovarov
2022-02-09 10:59:50 +00:00
committed by intellij-monorepo-bot
parent 2224111cfc
commit 20f44eb972
4 changed files with 48 additions and 51 deletions
@@ -1,9 +1,7 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.externalProcessAuthHelper;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.PathUtil;
import com.intellij.util.containers.ContainerUtil;
@@ -15,7 +13,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
/**
@@ -23,10 +20,6 @@ import java.util.ArrayList;
* are removed after application ends.
*/
public class ScriptGenerator {
/**
* The script prefix
*/
private final String myPrefix;
/**
* The scripts may class
*/
@@ -41,13 +34,9 @@ public class ScriptGenerator {
private final ArrayList<String> myInternalParameters = new ArrayList<>();
/**
* A constructor
*
* @param prefix the script prefix
* @param mainClass the script main class
*/
public ScriptGenerator(@NotNull @NonNls String prefix, @NotNull Class mainClass) {
myPrefix = prefix;
public ScriptGenerator(@NotNull Class mainClass) {
myMainClass = mainClass;
addClasses(myMainClass);
addClasses(ExternalApp.class);
@@ -80,37 +69,6 @@ public class ScriptGenerator {
return this;
}
@NotNull
private static File generateBatch(@NotNull @NonNls String fileName, @NotNull @NonNls String commandLine) throws IOException {
@NonNls StringBuilder sb = new StringBuilder();
sb.append("@echo off").append("\n");
sb.append(commandLine).append(" %*").append("\n");
return createTempExecutable(fileName + ".bat", sb.toString());
}
@NotNull
private static File generateShell(@NotNull @NonNls String fileName, @NotNull @NonNls String commandLine) throws IOException {
@NonNls StringBuilder sb = new StringBuilder();
sb.append("#!/bin/sh").append("\n");
sb.append(commandLine).append(" \"$@\"").append("\n");
return createTempExecutable(fileName + ".sh", sb.toString());
}
@NotNull
private static File createTempExecutable(@NotNull @NonNls String fileName, @NotNull @NonNls String content) throws IOException {
File file = new File(PathManager.getTempPath(), fileName);
FileUtil.writeToFile(file, content);
FileUtil.setExecutable(file);
return file;
}
@NotNull
public File generate(boolean useBatchFile, @Nullable CustomScriptCommandLineBuilder customBuilder) throws IOException {
String commandLine = commandLine(customBuilder);
return useBatchFile ? generateBatch(myPrefix, commandLine)
: generateShell(myPrefix, commandLine);
}
/**
* @return a command line for the customCmdBuilder program
*/
@@ -0,0 +1,43 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.externalProcessAuthHelper;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
public class ScriptGeneratorUtil {
@NotNull
private static File createBatchScript(@NotNull @NonNls String fileName, @NotNull @NonNls String commandLine) throws IOException {
@NonNls StringBuilder sb = new StringBuilder();
sb.append("@echo off").append("\n");
sb.append(commandLine).append(" %*").append("\n");
return createTempExecutable(fileName + ".bat", sb.toString());
}
@NotNull
private static File createShellScript(@NotNull @NonNls String fileName, @NotNull @NonNls String commandLine) throws IOException {
@NonNls StringBuilder sb = new StringBuilder();
sb.append("#!/bin/sh").append("\n");
sb.append(commandLine).append(" \"$@\"").append("\n");
return createTempExecutable(fileName + ".sh", sb.toString());
}
@NotNull
private static File createTempExecutable(@NotNull @NonNls String fileName, @NotNull @NonNls String content) throws IOException {
File file = new File(PathManager.getTempPath(), fileName);
FileUtil.writeToFile(file, content);
FileUtil.setExecutable(file);
return file;
}
@NotNull
public static File createTempScript(@NotNull String commandLine, @NotNull String fileNamePrefix, boolean useBatchFile)
throws IOException {
return useBatchFile ? createBatchScript(fileNamePrefix, commandLine)
: createShellScript(fileNamePrefix, commandLine);
}
}
@@ -79,8 +79,9 @@ public abstract class XmlRpcHandlerService<T> implements Disposable {
String id = scriptId + (useBatchFile ? "-bat" : ""); //NON-NLS
File scriptPath = myScriptPaths.get(id);
if (scriptPath == null || !scriptPath.exists()) {
ScriptGenerator generator = new ScriptGenerator(myScriptTempFilePrefix + "-" + scriptId, myScriptMainClass);
scriptPath = generator.generate(useBatchFile, customCmdBuilder);
ScriptGenerator generator = new ScriptGenerator(myScriptMainClass);
String commandLine = generator.commandLine(customCmdBuilder);
scriptPath = ScriptGeneratorUtil.createTempScript(commandLine, myScriptTempFilePrefix + "-" + scriptId, useBatchFile);
myScriptPaths.put(id, scriptPath);
}
return scriptPath;
@@ -11,7 +11,6 @@ import git4idea.commands.GitHandler;
import git4idea.config.GitExecutable;
import git4idea.editor.GitRebaseEditorApp;
import git4idea.editor.GitRebaseEditorXmlRpcHandler;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.ide.BuiltInServerManager;
@@ -37,10 +36,6 @@ public final class GitRebaseEditorService implements Disposable {
* The lock for the handlers
*/
private final Object myHandlersLock = new Object();
/**
* The prefix for rebase editors
*/
@NonNls private static final String GIT_REBASE_EDITOR_PREFIX = "git-rebase-editor-";
/**
* @return an instance of the server
@@ -77,7 +72,7 @@ public final class GitRebaseEditorService implements Disposable {
@NotNull
public synchronized String getEditorCommand(@NotNull GitExecutable executable) {
synchronized (myScriptLock) {
ScriptGenerator generator = new ScriptGenerator(GIT_REBASE_EDITOR_PREFIX, GitRebaseEditorApp.class);
ScriptGenerator generator = new ScriptGenerator(GitRebaseEditorApp.class);
generator.addInternal(Integer.toString(BuiltInServerManager.getInstance().waitForStart().getPort()));
return generator.commandLine(executable instanceof ScriptGenerator.CustomScriptCommandLineBuilder
? (ScriptGenerator.CustomScriptCommandLineBuilder)executable : null);