File: macromanager.cpp

package info (click to toggle)
codelite 6.1.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 48,992 kB
  • ctags: 43,502
  • sloc: cpp: 334,263; ansic: 18,441; xml: 4,713; yacc: 2,653; lex: 2,449; python: 1,188; sh: 385; makefile: 40
file content (222 lines) | stat: -rw-r--r-- 8,315 bytes parent folder | download
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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2014 The CodeLite Team
// file name            : macromanager.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 "macromanager.h"
#include "environmentconfig.h"
#include "build_config.h"
#include "project.h"
#include "workspace.h"
#include "imanager.h"
#include <wx/regex.h>

MacroManager::MacroManager()
{
}

MacroManager::~MacroManager()
{
}

MacroManager* MacroManager::Instance()
{
    static MacroManager ms_instance;
    return &ms_instance;
}

wxString MacroManager::Expand(const wxString& expression, IManager* manager, const wxString& project, const wxString &confToBuild)
{
    wxString   errMsg;
    wxString   expandedString(expression);
    Workspace *workspace = WorkspaceST::Get();
    
    DollarEscaper de(expandedString);
    
    if ( workspace ) {
        expandedString.Replace(wxT("$(WorkspaceName)"), workspace->GetName());
        ProjectPtr proj = workspace->FindProjectByName(project, errMsg);
        if (proj) {
            wxString prjBuildWd;
            wxString prjRunWd;

            wxString project_name(proj->GetName());

            //make sure that the project name does not contain any spaces
            project_name.Replace(wxT(" "), wxT("_"));

            BuildConfigPtr bldConf = workspace->GetProjBuildConf(proj->GetName(), confToBuild);
            if (bldConf) {
                bool isCustom = bldConf->IsCustomBuild();
                expandedString.Replace(wxT("$(ProjectOutputFile)"), bldConf->GetOutputFileName());

                // When custom build project, use the working directory set in the
                // custom build tab, otherwise use the project file's path
                prjBuildWd = isCustom ? bldConf->GetCustomBuildWorkingDir() : proj->GetFileName().GetPath();
                prjRunWd   = bldConf->GetWorkingDirectory();
            }

            expandedString.Replace(wxT("$(ProjectWorkingDirectory)"),    prjBuildWd);
            expandedString.Replace(wxT("$(ProjectRunWorkingDirectory)"), prjRunWd);
            expandedString.Replace(wxT("$(ProjectPath)"), proj->GetFileName().GetPath(wxPATH_GET_VOLUME|wxPATH_GET_SEPARATOR));
            expandedString.Replace(wxT("$(WorkspacePath)"), workspace->GetWorkspaceFileName().GetPath(wxPATH_GET_VOLUME|wxPATH_GET_SEPARATOR));
            expandedString.Replace(wxT("$(ProjectName)"), project_name);

            if (bldConf) {
                expandedString.Replace(wxT("$(IntermediateDirectory)"), bldConf->GetIntermediateDirectory());
                expandedString.Replace(wxT("$(ConfigurationName)"), bldConf->GetName());
                expandedString.Replace(wxT("$(OutDir)"), bldConf->GetIntermediateDirectory());
            }

            if(expandedString.Find(wxT("$(ProjectFiles)")) != wxNOT_FOUND)
                expandedString.Replace(wxT("$(ProjectFiles)"),   proj->GetFiles());

            if(expandedString.Find(wxT("$(ProjectFilesAbs)")) != wxNOT_FOUND)
                expandedString.Replace(wxT("$(ProjectFilesAbs)"),proj->GetFiles(true));


        }
    }
    
    if ( manager ) {
        IEditor *editor = manager->GetActiveEditor();
        if (editor) {
            wxFileName fn(editor->GetFileName());

            expandedString.Replace(wxT("$(CurrentFileName)"), fn.GetName());

            wxString fpath(fn.GetPath());
            fpath.Replace(wxT("\\"), wxT("/"));
            expandedString.Replace(wxT("$(CurrentFilePath)"), fpath);
            expandedString.Replace(wxT("$(CurrentFileExt)"), fn.GetExt());

            wxString ffullpath(fn.GetFullPath());
            ffullpath.Replace(wxT("\\"), wxT("/"));
            expandedString.Replace(wxT("$(CurrentFileFullPath)"), ffullpath);
            expandedString.Replace(wxT("$(CurrentSelection)"), editor->GetSelection());
            if(expandedString.Find(wxT("$(CurrentSelectionRange)")) != wxNOT_FOUND) {
                int start=editor->GetSelectionStart(),
                    end  =editor->GetSelectionEnd();

                wxString output=wxString::Format(wxT("%i:%i"),start,end);
                expandedString.Replace(wxT("$(CurrentSelectionRange)"), output);
            }
        }
        
    }

    //exapand common macros
    wxDateTime now = wxDateTime::Now();
    expandedString.Replace(wxT("$(User)"), wxGetUserName());
    expandedString.Replace(wxT("$(Date)"), now.FormatDate());

    if (manager) {
        expandedString.Replace(wxT("$(CodeLitePath)"), manager->GetInstallDirectory());
        expandedString = manager->GetEnv()->ExpandVariables(expandedString, true);
    }
    return expandedString;
}

wxString MacroManager::Replace(const wxString& inString, const wxString& variableName, const wxString& replaceWith, bool bIgnoreCase)
{
    size_t flags = wxRE_DEFAULT;
    if ( bIgnoreCase ) flags |= wxRE_ICASE;

    wxString strRe1;
    wxString strRe2;
    wxString strRe3;
    wxString strRe4;

    strRe1 << wxT("\\$\\((") << variableName << wxT(")\\)");
    strRe2 << wxT("\\$\\{(") << variableName << wxT(")\\}");
    strRe3 << wxT("\\$(")    << variableName << wxT(")");
    strRe4 << wxT("%(")   << variableName << wxT(")%");

    wxRegEx reOne  (strRe1, flags); // $(variable)
    wxRegEx reTwo  (strRe2, flags); // ${variable}
    wxRegEx reThree(strRe3, flags); // $variable
    wxRegEx reFour (strRe4, flags); // %variable%

    wxString result = inString;
    if ( reOne.Matches(result) ) {
        reOne.ReplaceAll(&result, replaceWith);
    }

    if ( reTwo.Matches(result) ) {
        reTwo.ReplaceAll(&result, replaceWith);
    }

    if ( reThree.Matches(result) ) {
        reThree.ReplaceAll(&result, replaceWith);
    }

    if ( reFour.Matches(result) ) {
        reFour.ReplaceAll(&result, replaceWith);
    }
    return result;
}

bool MacroManager::FindVariable(const wxString& inString, wxString& name, wxString& fullname)
{
    size_t flags = wxRE_DEFAULT | wxRE_ICASE;

    wxString strRe1;
    wxString strRe2;
    wxString strRe3;
    wxString strRe4;

    strRe1 << wxT("\\$\\((") << wxT("[a-z_0-9]+") << wxT(")\\)");
    strRe2 << wxT("\\$\\{(") << wxT("[a-z_0-9]+") << wxT(")\\}");
    strRe3 << wxT("\\$(")    << wxT("[a-z_0-9]+") << wxT(")");
    strRe4 << wxT("%(")   << wxT("[a-z_0-9]+") << wxT(")%");

    static wxRegEx reOne  (strRe1, flags); // $(variable)
    static wxRegEx reTwo  (strRe2, flags); // ${variable}
    static wxRegEx reThree(strRe3, flags); // $variable
    static wxRegEx reFour (strRe4, flags); // %variable%

    if ( reOne.Matches(inString) ) {
        name = reOne.GetMatch(inString, 1);
        fullname = reOne.GetMatch(inString);
        return true;
    }

    if ( reTwo.Matches(inString) ) {
        name = reTwo.GetMatch(inString, 1);
        fullname = reTwo.GetMatch(inString);
        return true;
    }

    if ( reThree.Matches(inString) ) {
        name = reThree.GetMatch(inString, 1);
        fullname = reThree.GetMatch(inString);
        return true;
    }

    if ( reFour.Matches(inString) ) {
        name = reFour.GetMatch(inString, 1);
        fullname = reFour.GetMatch(inString);
        return true;
    }
    return false;
}