forked from eranif/codelite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyright.cpp
411 lines (345 loc) · 14.1 KB
/
copyright.cpp
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2008 by Eran Ifrah
// file name : copyright.cpp
//
// -------------------------------------------------------------------------
// A
// _____ _ _ _ _
// / __ \ | | | | (_) |
// | / \/ ___ __| | ___| | _| |_ ___
// | | / _ \ / _ |/ _ \ | | | __/ _ )
// | \__/\ (_) | (_| | __/ |___| | || __/
// \____/\___/ \__,_|\___\_____/_|\__\___|
//
// F i l e
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#include <wx/filefn.h>
#include <wx/log.h>
#include <wx/progdlg.h>
#include <wx/ffile.h>
#include <wx/tokenzr.h>
#include "workspace.h"
#include "copyrights_proj_sel_dlg.h"
#include <wx/app.h>
#include "progress_dialog.h"
#include "copyrights_options_dlg.h"
#include "cppwordscanner.h"
#include "globals.h"
#include <wx/msgdlg.h>
#include "cpptoken.h"
#include "copyright.h"
#include <wx/xrc/xmlres.h>
#include <wx/menu.h>
#include <vector>
#include "project.h"
#include "copyrightsconfigdata.h"
#include "globals.h"
#include "event_notifier.h"
static Copyright* thePlugin = NULL;
// Internal events used by this plugin
const wxEventType CR_copyrights_options = wxNewEventType();
const wxEventType CR_insert_copyrights = wxNewEventType();
const wxEventType CR_insert_prj_copyrights = wxNewEventType();
// Define the plugin entry point
CL_PLUGIN_API IPlugin* CreatePlugin(IManager* manager)
{
if(thePlugin == 0) {
thePlugin = new Copyright(manager);
}
return thePlugin;
}
CL_PLUGIN_API PluginInfo* GetPluginInfo()
{
static PluginInfo info;
info.SetAuthor(wxT("Eran Ifrah"));
info.SetName(wxT("Copyright"));
info.SetDescription(
_("Copyright Plugin - a small plugin that allows you to place copyright block on top of your source files"));
info.SetVersion(wxT("v1.0"));
return &info;
}
CL_PLUGIN_API int GetPluginInterfaceVersion() { return PLUGIN_INTERFACE_VERSION; }
Copyright::Copyright(IManager* manager)
: IPlugin(manager)
, m_projectSepItem(NULL)
, m_workspaceSepItem(NULL)
{
m_longName = _("Copyright Plugin - Place copyright block on top of your source files");
m_shortName = wxT("Copyright");
}
Copyright::~Copyright() {}
clToolBar* Copyright::CreateToolBar(wxWindow* parent)
{
wxUnusedVar(parent);
return NULL;
}
void Copyright::CreatePluginMenu(wxMenu* pluginsMenu)
{
wxMenu* menu = new wxMenu();
wxMenuItem* item(NULL);
item = new wxMenuItem(
menu, XRCID("CR_insert_copyrights"), _("Insert Copyright Block"), _("Insert Copyright Block"), wxITEM_NORMAL);
menu->Append(item);
item = new wxMenuItem(menu,
XRCID("CR_batch_insert_copyrights"),
_("Batch Insert of Copyright Block"),
_("Batch Insert of Copyright Block"),
wxITEM_NORMAL);
menu->Append(item);
menu->AppendSeparator();
item = new wxMenuItem(menu, XRCID("CR_copyrights_options"), _("Settings..."), wxEmptyString, wxITEM_NORMAL);
menu->Append(item);
pluginsMenu->Append(wxID_ANY, _("Copyrights"), menu);
// connect events
wxTheApp->Bind(wxEVT_MENU, &Copyright::OnOptions, this, XRCID("CR_copyrights_options"));
wxTheApp->Bind(wxEVT_MENU, &Copyright::OnInsertCopyrights, this, XRCID("CR_insert_copyrights"));
wxTheApp->Bind(wxEVT_MENU, &Copyright::OnBatchInsertCopyrights, this, XRCID("CR_batch_insert_copyrights"));
wxTheApp->Bind(wxEVT_MENU, &Copyright::OnProjectInsertCopyrights, this, XRCID("CR_insert_prj_copyrights"));
EventNotifier::Get()->Bind(wxEVT_CONTEXT_MENU_EDITOR, &Copyright::OnEditorContextMenu, this);
}
void Copyright::UnPlug()
{
wxTheApp->Unbind(wxEVT_MENU, &Copyright::OnOptions, this, XRCID("CR_copyrights_options"));
wxTheApp->Unbind(wxEVT_MENU, &Copyright::OnInsertCopyrights, this, XRCID("CR_insert_copyrights"));
wxTheApp->Unbind(wxEVT_MENU, &Copyright::OnBatchInsertCopyrights, this, XRCID("CR_batch_insert_copyrights"));
wxTheApp->Unbind(wxEVT_MENU, &Copyright::OnProjectInsertCopyrights, this, XRCID("CR_insert_prj_copyrights"));
EventNotifier::Get()->Bind(wxEVT_CONTEXT_MENU_EDITOR, &Copyright::OnEditorContextMenu, this);
}
void Copyright::OnInsertCopyrights(wxCommandEvent& e)
{
wxUnusedVar(e);
// read configuration
CopyrightsConfigData data;
m_mgr->GetConfigTool()->ReadObject(wxT("CopyrightsConfig"), &data);
// make sure that the template file exists
if(!wxFileName::FileExists(data.GetTemplateFilename())) {
wxMessageBox(
wxString::Format(_("Template file name '%s', does not exist!"), data.GetTemplateFilename().GetData()),
_("CodeLite"),
wxICON_WARNING | wxOK);
return;
}
// read the copyrights file
wxString content;
if(!ReadFileWithConversion(data.GetTemplateFilename(), content)) {
wxMessageBox(wxString::Format(_("Failed to read template file '%s'"), data.GetTemplateFilename().c_str()),
_("CodeLite"),
wxICON_WARNING | wxOK);
return;
}
IEditor* editor = m_mgr->GetActiveEditor();
if(!editor) {
wxMessageBox(wxString::Format(_("There is no active editor\n")), _("CodeLite"), wxICON_WARNING | wxOK);
return;
}
// verify that the file consist only with comment code
CppWordScanner scanner(data.GetTemplateFilename().mb_str().data());
CppTokensMap l;
scanner.FindAll(l);
if(!l.is_empty()) {
if(wxMessageBox(_("Template file contains text which is not comment, continue anyway?"),
_("CodeLite"),
wxICON_QUESTION | wxYES_NO) == wxNO) {
return;
}
}
// expand constants
wxString _content = ExpandAllVariables(
content, m_mgr->GetWorkspace(), wxEmptyString, wxEmptyString, editor->GetFileName().GetFullPath());
// we are good to go :)
wxString ignoreString = data.GetIgnoreString();
ignoreString = ignoreString.Trim().Trim(false);
if(ignoreString.IsEmpty() == false) {
if(editor->GetEditorText().Find(data.GetIgnoreString()) != wxNOT_FOUND) {
wxLogMessage(_("File contains ignore string, skipping it"));
return;
}
}
editor->InsertText(0, _content);
}
void Copyright::OnOptions(wxCommandEvent& e)
{
// pop up the options dialog
CopyrightsOptionsDlg dlg(m_mgr->GetTheApp()->GetTopWindow(), m_mgr->GetConfigTool());
dlg.ShowModal();
}
void Copyright::OnBatchInsertCopyrights(wxCommandEvent& e)
{
// pop up the projects selection dialog
if(m_mgr->IsWorkspaceOpen() == false) {
wxMessageBox(_("Batch insert requires a workspace to be opened"), _("CodeLite"), wxICON_WARNING | wxOK);
return;
}
if(!m_mgr->SaveAll()) return;
// read configuration
CopyrightsConfigData data;
m_mgr->GetConfigTool()->ReadObject(wxT("CopyrightsConfig"), &data);
wxString content;
if(!Validate(content)) {
return;
}
CopyrightsProjectSelDlg dlg(m_mgr->GetTheApp()->GetTopWindow(), m_mgr->GetWorkspace());
if(dlg.ShowModal() == wxID_OK) {
wxArrayString projects;
dlg.GetProjects(projects);
// expand constants
wxString err_msg;
std::vector<wxFileName> files;
std::vector<wxFileName> filtered_files;
// loop over the project and collect list of files to work with
for(size_t i = 0; i < projects.size(); i++) {
ProjectPtr p = m_mgr->GetWorkspace()->FindProjectByName(projects.Item(i), err_msg);
if(p) {
p->GetFiles(files, true);
}
}
wxString mask(data.GetFileMasking());
mask.Replace(wxT("*."), wxEmptyString);
mask = mask.Trim().Trim(false);
wxArrayString exts = wxStringTokenize(mask, wxT(";"));
// filter out non-matching files (according to masking)
for(size_t i = 0; i < files.size(); i++) {
if(exts.Index(files.at(i).GetExt(), false) != wxNOT_FOUND) {
// valid file
filtered_files.push_back(files.at(i));
}
}
if(filtered_files.empty() == false) {
MassUpdate(filtered_files, content);
}
}
}
void Copyright::OnProjectInsertCopyrights(wxCommandEvent& e)
{
// pop up the projects selection dialog
if(m_mgr->IsWorkspaceOpen() == false) {
wxMessageBox(_("Batch insert requires a workspace to be opened"), _("CodeLite"), wxICON_WARNING | wxOK);
return;
}
if(!m_mgr->SaveAll()) return;
// read configuration
CopyrightsConfigData data;
m_mgr->GetConfigTool()->ReadObject(wxT("CopyrightsConfig"), &data);
wxString content;
if(!Validate(content)) {
return;
}
// get the project to work on
TreeItemInfo info = m_mgr->GetSelectedTreeItemInfo(TreeFileView);
wxString project_name = info.m_text;
wxString err_msg;
std::vector<wxFileName> files;
std::vector<wxFileName> filtered_files;
// loop over the project and collect list of files to work with
ProjectPtr p = m_mgr->GetWorkspace()->FindProjectByName(project_name, err_msg);
if(!p) {
return;
}
p->GetFiles(files, true);
// filter non matched files
wxString mask(data.GetFileMasking());
mask.Replace(wxT("*."), wxEmptyString);
mask = mask.Trim().Trim(false);
wxArrayString exts = wxStringTokenize(mask, wxT(";"));
// filter out non-matching files (according to masking)
for(size_t i = 0; i < files.size(); i++) {
if(exts.Index(files.at(i).GetExt(), false) != wxNOT_FOUND) {
// valid file
filtered_files.push_back(files.at(i));
}
}
// update files
if(filtered_files.empty() == false) {
MassUpdate(filtered_files, content);
}
}
void Copyright::MassUpdate(const std::vector<wxFileName>& filtered_files, const wxString& content)
{
// last confirmation from the user
if(wxMessageBox(
wxString::Format(_("You are about to modify %u files. Continue?"), (unsigned int)filtered_files.size()),
_("CodeLite"),
wxYES_NO | wxICON_QUESTION) == wxNO) {
return;
}
clProgressDlg* prgDlg = NULL;
prgDlg = new clProgressDlg(NULL, _("Processing file ..."), wxT(""), (int)filtered_files.size());
CopyrightsConfigData data;
m_mgr->GetConfigTool()->ReadObject(wxT("CopyrightsConfig"), &data);
// now loop over the files and add copyrights block
for(size_t i = 0; i < filtered_files.size(); i++) {
wxFileName fn = filtered_files.at(i);
wxString file_content;
wxString _content =
ExpandAllVariables(content, m_mgr->GetWorkspace(), wxEmptyString, wxEmptyString, fn.GetFullPath());
if(ReadFileWithConversion(fn.GetFullPath(), file_content)) {
wxString msg;
// if the file contains the ignore string, skip this file
wxString ignoreString = data.GetIgnoreString();
ignoreString = ignoreString.Trim().Trim(false);
if(ignoreString.IsEmpty() == false && file_content.Find(data.GetIgnoreString()) != wxNOT_FOUND) {
msg << _("File contains ignore string, skipping it: ") << fn.GetFullName();
if(!prgDlg->Update(i, msg)) {
prgDlg->Destroy();
return;
}
} else {
msg << _("Inserting comment to file: ") << fn.GetFullName();
if(!prgDlg->Update(i, msg)) {
prgDlg->Destroy();
return;
}
file_content.Prepend(_content);
WriteFileWithBackup(fn.GetFullPath(), file_content, data.GetBackupFiles());
}
}
}
prgDlg->Destroy();
}
bool Copyright::Validate(wxString& content)
{
CopyrightsConfigData data;
m_mgr->GetConfigTool()->ReadObject(wxT("CopyrightsConfig"), &data);
// make sure that the template file exists
if(!wxFileName::FileExists(data.GetTemplateFilename())) {
wxMessageBox(
wxString::Format(_("Template file name '%s', does not exist!"), data.GetTemplateFilename().GetData()),
_("CodeLite"),
wxICON_WARNING | wxOK);
return false;
}
// read the copyrights file
if(!ReadFileWithConversion(data.GetTemplateFilename(), content)) {
wxMessageBox(wxString::Format(_("Failed to read template file '%s'"), data.GetTemplateFilename().c_str()),
_("CodeLite"),
wxICON_WARNING | wxOK);
return false;
}
// verify that the file consist only with comment code
CppWordScanner scanner(data.GetTemplateFilename().mb_str().data());
CppTokensMap l;
scanner.FindAll(l);
if(!l.is_empty()) {
if(wxMessageBox(_("Template file contains text which is not comment, continue anyways?"),
_("CodeLite"),
wxICON_QUESTION | wxYES_NO) == wxNO) {
return false;
}
}
content.Replace(wxT("`"), wxT("'"));
return true;
}
void Copyright::OnEditorContextMenu(clContextMenuEvent& event)
{
event.Skip();
event.GetMenu()->Append(XRCID("CR_insert_copyrights"), _("Insert Copyrights Block"));
}