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 234 235 236 237 238 239 240
|
#include "clNodeJS.h"
#include "NodeJSLocator.h"
#include "asyncprocess.h"
#include "clConsoleBase.h"
#include "file_logger.h"
#include "globals.h"
#include "ieditor.h"
#include "imanager.h"
#include "processreaderthread.h"
#include <algorithm>
wxDEFINE_EVENT(wxEVT_NODE_COMMAND_TERMINATED, clProcessEvent);
clNodeJS::clNodeJS() {}
clNodeJS::~clNodeJS() {}
bool clNodeJS::Initialise(const wxArrayString& hints)
{
if(!IsInitialised()) {
// bind the events once
BindEvents();
}
NodeJSLocator locator;
locator.Locate(hints);
m_node = locator.GetNodejs();
m_npm = locator.GetNpm();
m_initialised = m_node.FileExists() && m_npm.FileExists();
return m_initialised;
}
bool clNodeJS::NpmInstall(const wxString& package, const wxString& workingDirectory, const wxString& args,
wxEvtHandler* sink, const wxString& uid)
{
if(!IsInitialised()) {
return false;
}
// Create the working directory if not exists
wxFileName wd(workingDirectory, "");
wd.Mkdir(wxS_DEFAULT, wxPATH_MKDIR_FULL);
wxString _args;
_args.Prepend("install ");
_args << package;
if(!args.IsEmpty()) {
_args << " " << args; // --save
}
clConsoleBase::Ptr_t console = clConsoleBase::GetTerminal();
console->SetWorkingDirectory(wd.GetPath());
console->SetCommand(GetNpm().GetFullPath(), _args);
console->SetWaitWhenDone(true);
console->SetTerminalNeeded(true);
console->SetSink(sink, uid);
return console->Start();
}
void clNodeJS::OnProcessOutput(clProcessEvent& event)
{
IProcess* process = event.GetProcess();
if(m_processes.count(process)) {
ProcessData& d = m_processes[process];
d.GetOutput() << event.GetOutput();
}
}
void clNodeJS::OnProcessTerminated(clProcessEvent& event)
{
IProcess* process = event.GetProcess();
if(m_processes.count(process)) {
const ProcessData& d = m_processes[process];
// Handle lint
if(!d.GetOutput().IsEmpty() && d.GetUid() == "lint") {
ProcessLintOuput(d.GetFilename(), d.GetOutput());
}
if(d.GetSink()) {
clProcessEvent evt(wxEVT_NODE_COMMAND_TERMINATED);
evt.SetOutput(d.GetOutput());
evt.SetString(d.GetUid()); // pass the unique ID
d.GetSink()->AddPendingEvent(evt);
}
m_processes.erase(process);
}
wxDELETE(process);
}
clNodeJS& clNodeJS::Get()
{
static clNodeJS nodejs;
return nodejs;
}
void clNodeJS::Shutdown()
{
m_initialised = false;
m_node.Clear();
m_npm.Clear();
// Unbind before we kill the process
UnBindEvents();
std::for_each(m_processes.begin(), m_processes.end(),
[&](const std::unordered_map<IProcess*, ProcessData>::value_type& vt) {
IProcess* p = vt.first;
// Terminate the process
p->Terminate();
wxDELETE(p);
});
m_processes.clear();
}
void clNodeJS::BindEvents()
{
Bind(wxEVT_ASYNC_PROCESS_OUTPUT, &clNodeJS::OnProcessOutput, this);
Bind(wxEVT_ASYNC_PROCESS_TERMINATED, &clNodeJS::OnProcessTerminated, this);
}
void clNodeJS::UnBindEvents()
{
Unbind(wxEVT_ASYNC_PROCESS_OUTPUT, &clNodeJS::OnProcessOutput, this);
Unbind(wxEVT_ASYNC_PROCESS_TERMINATED, &clNodeJS::OnProcessTerminated, this);
}
bool clNodeJS::NpmInit(const wxString& workingDirectory, wxEvtHandler* sink)
{
if(!IsInitialised()) {
return false;
}
// Create the working directory if not exists
wxFileName wd(workingDirectory, "");
wd.Mkdir(wxS_DEFAULT, wxPATH_MKDIR_FULL);
clConsoleBase::Ptr_t console = clConsoleBase::GetTerminal();
console->SetWorkingDirectory(wd.GetPath());
console->SetCommand(GetNpm().GetFullPath(), "init");
console->SetWaitWhenDone(true);
console->SetTerminalNeeded(true);
console->SetSink(sink);
return console->Start();
}
void clNodeJS::LintFile(const wxFileName& filename)
{
if(!IsInitialised()) {
return;
}
wxString wd = filename.GetPath();
wxString command;
command << GetNode().GetFullPath();
::WrapWithQuotes(command);
command << " -c " << filename.GetFullName();
IProcess* process = ::CreateAsyncProcess(this, command, IProcessCreateDefault, wd);
if(process) {
ProcessData d;
d.SetFilename(filename);
d.SetUid("lint");
m_processes.insert({ process, d });
}
}
void clNodeJS::ProcessLintOuput(const wxFileName& fn, const wxString& output)
{
// the first line in the output is "file:line\n"
wxString where = output.BeforeFirst('\n');
wxString line = where.AfterLast(':');
wxString file = where.BeforeLast(':');
wxString errorMessage = output.AfterFirst('\n');
line.Trim().Trim(false);
file.Trim().Trim(false);
// Use the code font for this error message
errorMessage.Prepend("```").Append("```");
long nLineNumber = -1;
line.ToCLong(&nLineNumber);
// Mark the editor with error marker
IEditor* editor = clGetManager()->FindEditor(fn.GetFullPath());
if(editor && (nLineNumber != wxNOT_FOUND)) {
editor->SetErrorMarker(nLineNumber - 1, errorMessage);
}
}
bool clNodeJS::NpmSilentInstall(const wxString& package, const wxString& workingDirectory, const wxString& args,
wxEvtHandler* sink, const wxString& uid)
{
if(!IsInitialised()) {
return false;
}
wxString command;
command << GetNpm().GetFullPath();
::WrapWithQuotes(command);
command << " install " << package << " --silent --quiet " << args;
IProcess* process = ::CreateAsyncProcess(this, command, IProcessCreateDefault, workingDirectory);
if(process) {
ProcessData d;
d.SetUid(uid);
d.SetSink(sink);
m_processes.insert({ process, d });
}
return (process != nullptr);
}
wxProcess* clNodeJS::RunScript(const wxArrayString& argv, const wxString& workingDirectory, size_t execFlags)
{
if(!IsInitialised() || argv.IsEmpty()) {
return nullptr;
}
wxFileName scriptPath(argv.Item(0));
if(!scriptPath.FileExists()) {
clERROR() << "NodeJS: cant run script:" << scriptPath << ". No such file";
return nullptr;
}
wxString command;
for(const wxString& arg : argv) {
// Build the command line
wxString tmp = std::move(arg);
::WrapWithQuotes(tmp);
command << tmp << " ";
}
wxProcess* process = new wxProcess();
process->Redirect();
if(::wxExecute(command, execFlags, process) <= 0) {
wxDELETE(process);
return nullptr;
}
return process;
}
|