File: clDockerWorkspace.cpp

package info (click to toggle)
codelite 17.0.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 136,204 kB
  • sloc: cpp: 491,547; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 804; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (322 lines) | stat: -rw-r--r-- 10,832 bytes parent folder | download | duplicates (3)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "clDockerWorkspace.h"

#include "NewDockerWorkspaceDlg.h"
#include "clDockerWorkspaceView.h"
#include "clWorkspaceManager.h"
#include "clWorkspaceView.h"
#include "codelite_events.h"
#include "ctags_manager.h"
#include "docker.h"
#include "event_notifier.h"
#include "globals.h"
#include "tags_options_data.h"

#include <imanager.h>
#include <wx/msgdlg.h>

#define CHECK_EVENT(e) \
    e.Skip();          \
    if(!IsOpen()) {    \
        return;        \
    }                  \
    e.Skip(false);

clDockerWorkspace::clDockerWorkspace(bool bindEvents, Docker* plugin, clDockerDriver::Ptr_t driver)
    : m_bindEvents(bindEvents)
    , m_driver(driver)
    , m_plugin(plugin)
{
    SetWorkspaceType("Docker");
    if(m_bindEvents) {
        EventNotifier::Get()->Bind(wxEVT_CMD_OPEN_WORKSPACE, &clDockerWorkspace::OnOpenWorkspace, this);
        EventNotifier::Get()->Bind(wxEVT_CMD_CLOSE_WORKSPACE, &clDockerWorkspace::OnCloseWorkspace, this);
        EventNotifier::Get()->Bind(wxEVT_CMD_CREATE_NEW_WORKSPACE, &clDockerWorkspace::OnNewWorkspace, this);
        EventNotifier::Get()->Bind(wxEVT_SAVE_SESSION_NEEDED, &clDockerWorkspace::OnSaveSession, this);
        EventNotifier::Get()->Bind(wxEVT_GET_IS_BUILD_IN_PROGRESS, &clDockerWorkspace::OnIsBuildInProgress, this);
        EventNotifier::Get()->Bind(wxEVT_BUILD_STARTING, &clDockerWorkspace::OnBuildStarting, this);
        EventNotifier::Get()->Bind(wxEVT_STOP_BUILD, &clDockerWorkspace::OnStopBuild, this);
        EventNotifier::Get()->Bind(wxEVT_CMD_EXECUTE_ACTIVE_PROJECT, &clDockerWorkspace::OnRun, this);
        EventNotifier::Get()->Bind(wxEVT_CMD_STOP_EXECUTED_PROGRAM, &clDockerWorkspace::OnStop, this);
        m_view = new clDockerWorkspaceView(clGetManager()->GetWorkspaceView()->GetBook());
        clGetManager()->GetWorkspaceView()->AddPage(m_view, GetWorkspaceType());
    }
}

clDockerWorkspace::~clDockerWorkspace()
{
    if(m_bindEvents) {
        EventNotifier::Get()->Unbind(wxEVT_CMD_OPEN_WORKSPACE, &clDockerWorkspace::OnOpenWorkspace, this);
        EventNotifier::Get()->Unbind(wxEVT_CMD_CLOSE_WORKSPACE, &clDockerWorkspace::OnCloseWorkspace, this);
        EventNotifier::Get()->Unbind(wxEVT_CMD_CREATE_NEW_WORKSPACE, &clDockerWorkspace::OnNewWorkspace, this);
        EventNotifier::Get()->Unbind(wxEVT_SAVE_SESSION_NEEDED, &clDockerWorkspace::OnSaveSession, this);
        EventNotifier::Get()->Unbind(wxEVT_GET_IS_BUILD_IN_PROGRESS, &clDockerWorkspace::OnIsBuildInProgress, this);
        EventNotifier::Get()->Unbind(wxEVT_BUILD_STARTING, &clDockerWorkspace::OnBuildStarting, this);
        EventNotifier::Get()->Unbind(wxEVT_STOP_BUILD, &clDockerWorkspace::OnStopBuild, this);
        EventNotifier::Get()->Unbind(wxEVT_CMD_EXECUTE_ACTIVE_PROJECT, &clDockerWorkspace::OnRun, this);
        EventNotifier::Get()->Unbind(wxEVT_CMD_STOP_EXECUTED_PROGRAM, &clDockerWorkspace::OnStop, this);
    }
}

wxString clDockerWorkspace::GetActiveProjectName() const { return ""; }

wxString clDockerWorkspace::GetFileName() const { return m_filename.GetFullPath(); }
wxString clDockerWorkspace::GetDir() const { return m_filename.GetPath(); }

wxString clDockerWorkspace::GetFilesMask() const { return "Dockerfile;docker-compose.yml;*.txt"; }

wxFileName clDockerWorkspace::GetProjectFileName(const wxString& projectName) const { return wxFileName(); }

void clDockerWorkspace::GetProjectFiles(const wxString& projectName, wxArrayString& files) const
{
    wxUnusedVar(projectName);
}

wxString clDockerWorkspace::GetProjectFromFile(const wxFileName& filename) const
{
    wxUnusedVar(filename);
    return "";
}

void clDockerWorkspace::GetWorkspaceFiles(wxArrayString& files) const { wxUnusedVar(files); }

wxArrayString clDockerWorkspace::GetWorkspaceProjects() const { return wxArrayString(); }

bool clDockerWorkspace::IsBuildSupported() const { return true; }

bool clDockerWorkspace::IsProjectSupported() const { return false; }

static clDockerWorkspace* g_workspace = nullptr;

clDockerWorkspace* clDockerWorkspace::Get() { return g_workspace; }

void clDockerWorkspace::Initialise(Docker* plugin)
{
    if(!g_workspace) {
        g_workspace = new clDockerWorkspace(true, plugin, plugin->GetDriver());
    }
}

void clDockerWorkspace::Shutdown() { wxDELETE(g_workspace); }

void clDockerWorkspace::OnOpenWorkspace(clCommandEvent& event)
{
    event.Skip();

    // Close any opened workspace
    auto frame = EventNotifier::Get()->TopFrame();
    wxCommandEvent eventCloseWsp(wxEVT_COMMAND_MENU_SELECTED, XRCID("close_workspace"));
    eventCloseWsp.SetEventObject(frame);
    frame->GetEventHandler()->ProcessEvent(eventCloseWsp);

    // load the current workspace
    wxFileName workspaceFile(event.GetFileName());

    // Test that this is our workspace
    clDockerWorkspaceSettings conf;
    conf.Load(workspaceFile);
    if(!conf.IsOk()) {
        return;
    }

    // This is a Docker workspace, stop event processing by calling
    // event.Skip(false)
    event.Skip(false);

    if(IsOpen()) {
        Close();
    }
    Open(workspaceFile);
}

void clDockerWorkspace::OnCloseWorkspace(clCommandEvent& event)
{
    event.Skip();
    if(IsOpen()) {
        event.Skip(false);
        Close();
    }
}

void clDockerWorkspace::Open(const wxFileName& path)
{
    m_filename = path;
    m_settings.Load(m_filename);
    m_isOpen = m_settings.Load(m_filename).IsOk();
    if(!IsOpen()) {
        m_filename.Clear();
        m_settings.Clear();
        GetView()->Clear();
    } else {

        //===------------------------------------------
        // Finalize the workspace open process:
        //===------------------------------------------

        // Notify codelite that NodeJS workspace is opened
        clGetManager()->GetWorkspaceView()->SelectPage(GetWorkspaceType());
        clWorkspaceManager::Get().SetWorkspace(this);

        // Keep the old clang state before we disable it
        const TagsOptionsData& options = TagsManagerST::Get()->GetCtagsOptions();
        m_clangOldFlag = (options.GetClangOptions() & CC_CLANG_ENABLED);

        clGetManager()->EnableClangCodeCompletion(false);

        // Notify that the a new workspace is loaded
        clWorkspaceEvent open_event(wxEVT_WORKSPACE_LOADED);
        open_event.SetFileName(m_filename.GetFullPath());
        open_event.SetString(m_filename.GetFullPath());
        open_event.SetWorkspaceType(GetWorkspaceType());
        EventNotifier::Get()->AddPendingEvent(open_event);

        // and finally, request codelite to keep this workspace in the recently opened workspace list
        clGetManager()->AddWorkspaceToRecentlyUsedList(m_filename);

        // Load the workspace session (if any)
        CallAfter(&clDockerWorkspace::RestoreSession);
    }
}

void clDockerWorkspace::Close()
{
    if(IsOpen()) {
        // Store the session
        clGetManager()->StoreWorkspaceSession(m_filename);

        // disable clang for NodeJS
        clGetManager()->EnableClangCodeCompletion(m_clangOldFlag);

        // Clear the UI
        GetView()->Clear();

        // notify codelite to close all opened files
        wxCommandEvent eventClose(wxEVT_MENU, wxID_CLOSE_ALL);
        eventClose.SetEventObject(EventNotifier::Get()->TopFrame());
        EventNotifier::Get()->TopFrame()->GetEventHandler()->ProcessEvent(eventClose);

        // Notify workspace closed event
        clWorkspaceEvent event_workspace_closed(wxEVT_WORKSPACE_CLOSED);
        EventNotifier::Get()->ProcessEvent(event_workspace_closed);

        m_filename.Clear();
        m_settings.Clear();
        m_isOpen = false;
    }
}

bool clDockerWorkspace::IsOpen() const { return m_isOpen; }

void clDockerWorkspace::OnNewWorkspace(clCommandEvent& event)
{
    event.Skip();
    if(event.GetString() == GetWorkspaceType()) {
        event.Skip(false);

        // Create a new NodeJS workspace
        NewDockerWorkspaceDlg dlg(EventNotifier::Get()->TopFrame());
        if(dlg.ShowModal() != wxID_OK)
            return;

        wxFileName workspaceFile = dlg.GetWorkspaceFile();
        if(!workspaceFile.GetDirCount()) {
            ::wxMessageBox(_("Can not create workspace in the root folder"), _("New Workspace"),
                           wxICON_ERROR | wxOK | wxCENTER);
            return;
        }

        // Ensure that the path the workspace exists
        workspaceFile.Mkdir(wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);

        if(!Create(workspaceFile)) {
            ::wxMessageBox(_("Failed to create workspace\nWorkspace already exists"), _("New Workspace"),
                           wxICON_ERROR | wxOK | wxCENTER);
            return;
        }
        Open(workspaceFile);
    }
}

bool clDockerWorkspace::Create(const wxFileName& filename)
{
    // Already exists
    if(filename.FileExists()) {
        return false;
    }
    return m_settings.Save(filename).Load(filename).IsOk();
}

void clDockerWorkspace::RestoreSession()
{
    if(IsOpen()) {
        clGetManager()->LoadWorkspaceSession(m_filename);
    }
}

void clDockerWorkspace::OnSaveSession(clCommandEvent& event)
{
    event.Skip();
    if(IsOpen()) {
        event.Skip(false);
        clGetManager()->StoreWorkspaceSession(m_filename);
    }
}

void clDockerWorkspace::OnIsBuildInProgress(clBuildEvent& event)
{
    CHECK_EVENT(event);
    event.SetIsRunning(m_driver->IsRunning());
}

void clDockerWorkspace::OnBuildStarting(clBuildEvent& event)
{
    CHECK_EVENT(event);
    IEditor* editor = clGetManager()->GetActiveEditor();
    CHECK_PTR_RET(editor);
    if(editor->GetFileName().GetFullName() == "Dockerfile") {
        if(event.GetKind() == "build") {
            BuildDockerfile(editor->GetFileName());
        }
    }
}

void clDockerWorkspace::OnStopBuild(clBuildEvent& event)
{
    CHECK_EVENT(event);
    if(m_driver->IsRunning()) {
        m_driver->Stop();
    }
}

void clDockerWorkspace::OnRun(clExecuteEvent& event)
{
    CHECK_EVENT(event);
    IEditor* editor = clGetManager()->GetActiveEditor();
    CHECK_PTR_RET(editor);
    if(editor->GetFileName().GetFullName() == "Dockerfile") {
        RunDockerfile(editor->GetFileName());
    }
}

void clDockerWorkspace::OnStop(clExecuteEvent& event)
{
    CHECK_EVENT(event);
    if(m_driver->IsRunning()) {
        m_driver->Stop();
    }
}

void clDockerWorkspace::BuildDockerfile(const wxFileName& dockerfile) { m_driver->Build(dockerfile, m_settings); }

void clDockerWorkspace::RunDockerfile(const wxFileName& dockerfile) { m_driver->Run(dockerfile, m_settings); }

void clDockerWorkspace::BuildDockerCompose(const wxFileName& docker_compose)
{
    m_driver->Build(docker_compose, m_settings);
}

void clDockerWorkspace::RunDockerCompose(const wxFileName& docker_compose)
{
    m_driver->Run(docker_compose, m_settings);
}

void clDockerWorkspace::SetProjectActive(const wxString& project) { wxUnusedVar(project); }