1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
#include "GenericFormatter.hpp"
#include "Platform.hpp"
#include "StringUtils.h"
#include "asyncprocess.h"
#include "clCodeLiteRemoteProcess.hpp"
#include "clDirChanger.hpp"
#include "clTempFile.hpp"
#include "clWorkspaceManager.h"
#include "environmentconfig.h"
#include "file_logger.h"
#include "fileutils.h"
#include "globals.h"
#include "imanager.h"
#include "macromanager.h"
#include "procutils.h"
#include "workspace.h"
#include <sstream>
#include <thread>
#include <wx/msgdlg.h>
namespace
{
wxString join_array(const wxArrayString& arr)
{
wxString cmd;
for(auto c : arr) {
cmd << WrapWithQuotes(c) << " ";
}
if(!cmd.IsEmpty()) {
cmd.RemoveLast();
}
return cmd;
}
wxArrayString to_wx_array(const std::vector<wxString>& arr)
{
wxArrayString wxarr;
wxarr.reserve(arr.size());
for(const auto& s : arr) {
wxarr.push_back(s);
}
return wxarr;
}
wxString replace_macros(const wxString& expression, const wxString& filepath)
{
wxString expand_stage1 = MacroManager::Instance()->ExpandFileMacros(expression, filepath);
wxString resolved = MacroManager::Instance()->Expand(expand_stage1, clGetManager(), wxEmptyString, wxEmptyString);
return resolved;
}
wxString get_command_with_desc(const wxArrayString& command_arr, const wxString& desc)
{
wxString command = StringUtils::BuildCommandStringFromArray(command_arr, StringUtils::WITH_COMMENT_PREFIX);
command.Prepend(wxString() << "# " << desc << "\n");
return command;
}
bool sync_format(const wxString& cmd, const wxString& wd, bool inplace_formatter, wxString* output)
{
// save the current directory
clDirChanger cd{ wd };
EnvSetter es(NULL, NULL, wxEmptyString, wxEmptyString);
bool res = ProcUtils::ShellExecSync(cmd, output) == 0;
if(inplace_formatter) {
output->clear();
}
return res;
}
} // namespace
GenericFormatter::GenericFormatter()
{
SetWorkingDirectory("$(WorkspacePath)");
Bind(wxEVT_SHELL_ASYNC_PROCESS_TERMINATED, &GenericFormatter::OnAsyncShellProcessTerminated, this);
}
GenericFormatter::~GenericFormatter()
{
Unbind(wxEVT_SHELL_ASYNC_PROCESS_TERMINATED, &GenericFormatter::OnAsyncShellProcessTerminated, this);
}
wxString GenericFormatter::GetCommandAsString() const { return join_array(m_command); }
/**
* @brief format source file using background thread, when the formatting is done, fire an event to the sink object
*/
void GenericFormatter::async_format(const wxString& cmd, const wxString& wd, const wxString& filepath,
bool inplace_formatter, wxEvtHandler* sink)
{
clDirChanger cd{ wd };
long pid = wxNOT_FOUND;
if(ProcUtils::ShellExecAsync(cmd, &pid, this)) {
m_pid_commands.insert({ pid, CommandMetadata{ cmd, filepath, sink } });
}
}
bool GenericFormatter::DoFormatFile(const wxString& filepath, wxEvtHandler* sink, wxString* output)
{
// Create a copy
wxString cmd = GetCommandAsString();
cmd = replace_macros(cmd, filepath);
wxString wd = replace_macros(GetWorkingDirectory(), filepath);
clDEBUG() << "Working dir:" << wd << endl;
clDEBUG() << "Calling:" << cmd << endl;
wxBusyCursor bc;
if(sink) {
async_format(cmd, wd, filepath, IsInplaceFormatter(), sink);
return true;
} else {
return sync_format(cmd, wd, IsInplaceFormatter(), output);
}
}
bool GenericFormatter::FormatFile(const wxFileName& filepath, wxEvtHandler* sink)
{
return FormatFile(filepath.GetFullPath(), sink);
}
bool GenericFormatter::FormatRemoteFile(const wxString& filepath, wxEvtHandler* sink)
{
if(!CanHandleRemoteFile()) {
return false;
}
// Create a copy
wxString cmd = GetRemoteCommand();
cmd = replace_macros(cmd, filepath);
wxString wd = replace_macros(GetWorkingDirectory(), filepath);
clDEBUG() << "Working dir:" << wd << endl;
clDEBUG() << "Calling:" << cmd << endl;
async_format(cmd, wd, filepath, IsInplaceFormatter(), sink);
return true;
}
bool GenericFormatter::FormatFile(const wxString& filepath, wxEvtHandler* sink)
{
return DoFormatFile(filepath, sink, nullptr);
}
bool GenericFormatter::FormatString(const wxString& content, const wxString& fullpath, wxString* output)
{
auto file_type = FileExtManager::GetType(fullpath);
if(!CanHandle(file_type)) {
return false;
}
wxString fullpath_linux_style = fullpath;
fullpath_linux_style.Replace("\\", "/");
wxString dirpart = fullpath_linux_style.BeforeLast('/');
clTempFile tmpfile{ dirpart, "txt" };
if(!tmpfile.Write(content)) {
clWARNING() << "failed to write content to temp file:" << tmpfile.GetFullPath() << endl;
return false;
}
if(!DoFormatFile(tmpfile.GetFullPath(), nullptr, output)) {
return false;
}
if(IsInplaceFormatter()) {
// read the content of the temp file and return it
return FileUtils::ReadFileContent(tmpfile.GetFullPath(), *output);
}
return true;
}
void GenericFormatter::FromJSON(const JSONItem& json)
{
SourceFormatterBase::FromJSON(json);
m_command = json["command"].toArrayString();
m_workingDirectory = json["working_directory"].toString();
}
JSONItem GenericFormatter::ToJSON() const
{
auto json = SourceFormatterBase::ToJSON();
json.addProperty("command", m_command);
json.addProperty("working_directory", m_workingDirectory);
return json;
}
void GenericFormatter::SetCommand(const std::vector<wxString>& command) { SetCommand(to_wx_array(command)); }
void GenericFormatter::SetCommandFromString(const wxString& command)
{
SetCommand(StringUtils::BuildCommandArrayFromString(command));
}
wxString GenericFormatter::GetCommandWithComments() const { return get_command_with_desc(m_command, m_description); }
void GenericFormatter::SetRemoteCommand(const wxString& cmd) { m_remote_command = cmd; }
void GenericFormatter::OnAsyncShellProcessTerminated(clShellProcessEvent& event)
{
event.Skip();
if(m_pid_commands.count(event.GetPid()) == 0) {
clWARNING() << "Could not find command for process:" << event.GetPid() << endl;
return;
}
// remove the entry from the map
auto command_data = m_pid_commands[event.GetPid()];
m_pid_commands.erase(event.GetPid());
if(event.GetExitCode() != 0) {
wxString errmsg;
errmsg << wxT("\u26A0") << _(" format error. Process exit code: ") << event.GetExitCode();
clGetManager()->SetStatusMessage(errmsg, 3);
return;
}
// notify the sink that formatting is done
if(command_data.m_sink) {
clSourceFormatEvent format_completed_event{ IsInplaceFormatter() ? wxEVT_FORMAT_INPLACE_COMPELTED
: wxEVT_FORMAT_COMPELTED };
format_completed_event.SetFormattedString(IsInplaceFormatter() ? "" : event.GetOutput());
format_completed_event.SetFileName(command_data.m_filepath);
command_data.m_sink->QueueEvent(format_completed_event.Clone());
}
}
|