1
//////////////////////////////////////////////////////////////////////////////
2
//////////////////////////////////////////////////////////////////////////////
4
// copyright : (C) 2008 by Eran Ifrah
5
// file name : project.cpp
7
// -------------------------------------------------------------------------
10
// / __ \ | | | | (_) |
11
// | / \/ ___ __| | ___| | _| |_ ___
12
// | | / _ \ / _ |/ _ \ | | | __/ _ )
13
// | \__/\ (_) | (_| | __/ |___| | || __/
14
// \____/\___/ \__,_|\___\_____/_|\__\___|
18
// This program is free software; you can redistribute it and/or modify
19
// it under the terms of the GNU General Public License as published by
20
// the Free Software Foundation; either version 2 of the License, or
21
// (at your option) any later version.
23
//////////////////////////////////////////////////////////////////////////////
24
//////////////////////////////////////////////////////////////////////////////
27
#include <wx/tokenzr.h>
28
#include "wx/arrstr.h"
31
const wxString Project::STATIC_LIBRARY = wxT("Static Library");
32
const wxString Project::DYNAMIC_LIBRARY = wxT("Dynamic Library");
33
const wxString Project::EXECUTABLE = wxT("Executable");
46
bool Project::Create(const wxString &name, const wxString &description, const wxString &path, const wxString &projType)
50
m_fileName = path + wxFileName::GetPathSeparator() + name + wxT(".project");
51
m_fileName.MakeAbsolute();
53
wxXmlNode *root = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("CodeLite_Project"));
55
m_doc.GetRoot()->AddProperty(wxT("Name"), name);
57
wxXmlNode *descNode = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("Description"));
58
XmlUtils::SetNodeContent(descNode, description);
59
m_doc.GetRoot()->AddChild(descNode);
61
// Create the default virtual directories
62
wxXmlNode *srcNode = NULL, *headNode = NULL;
64
srcNode = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("VirtualDirectory"));
65
srcNode->AddProperty(wxT("Name"), wxT("src"));
66
m_doc.GetRoot()->AddChild(srcNode);
68
headNode = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("VirtualDirectory"));
69
headNode->AddProperty(wxT("Name"), wxT("include"));
70
m_doc.GetRoot()->AddChild(headNode);
72
//creae dependencies node
73
wxXmlNode *depNode = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("Dependencies"));
74
root->AddChild(depNode);
76
m_doc.Save(m_fileName.GetFullPath());
77
//create build settings
78
SetSettings(new ProjectSettings(NULL));
79
ProjectSettingsPtr settings = GetSettings();
80
settings->SetProjectType(projType);
81
SetSettings(settings);
86
bool Project::Load(const wxString &path)
88
if ( !m_doc.Load(path) ) {
95
m_fileName.MakeAbsolute();
1
//////////////////////////////////////////////////////////////////////////////
2
//////////////////////////////////////////////////////////////////////////////
4
// copyright : (C) 2008 by Eran Ifrah
5
// file name : project.cpp
7
// -------------------------------------------------------------------------
10
// / __ \ | | | | (_) |
11
// | / \/ ___ __| | ___| | _| |_ ___
12
// | | / _ \ / _ |/ _ \ | | | __/ _ )
13
// | \__/\ (_) | (_| | __/ |___| | || __/
14
// \____/\___/ \__,_|\___\_____/_|\__\___|
18
// This program is free software; you can redistribute it and/or modify
19
// it under the terms of the GNU General Public License as published by
20
// the Free Software Foundation; either version 2 of the License, or
21
// (at your option) any later version.
23
//////////////////////////////////////////////////////////////////////////////
24
//////////////////////////////////////////////////////////////////////////////
27
#include <wx/tokenzr.h>
28
#include "wx/arrstr.h"
31
const wxString Project::STATIC_LIBRARY = wxT("Static Library");
32
const wxString Project::DYNAMIC_LIBRARY = wxT("Dynamic Library");
33
const wxString Project::EXECUTABLE = wxT("Executable");
46
bool Project::Create(const wxString &name, const wxString &description, const wxString &path, const wxString &projType)
50
m_fileName = path + wxFileName::GetPathSeparator() + name + wxT(".project");
51
m_fileName.MakeAbsolute();
53
wxXmlNode *root = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("CodeLite_Project"));
55
m_doc.GetRoot()->AddProperty(wxT("Name"), name);
57
wxXmlNode *descNode = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("Description"));
58
XmlUtils::SetNodeContent(descNode, description);
59
m_doc.GetRoot()->AddChild(descNode);
61
// Create the default virtual directories
62
wxXmlNode *srcNode = NULL, *headNode = NULL;
64
srcNode = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("VirtualDirectory"));
65
srcNode->AddProperty(wxT("Name"), wxT("src"));
66
m_doc.GetRoot()->AddChild(srcNode);
68
headNode = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("VirtualDirectory"));
69
headNode->AddProperty(wxT("Name"), wxT("include"));
70
m_doc.GetRoot()->AddChild(headNode);
72
//creae dependencies node
73
wxXmlNode *depNode = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("Dependencies"));
74
root->AddChild(depNode);
76
m_doc.Save(m_fileName.GetFullPath());
77
//create build settings
78
SetSettings(new ProjectSettings(NULL));
79
ProjectSettingsPtr settings = GetSettings();
80
settings->SetProjectType(projType);
81
SetSettings(settings);
86
bool Project::Load(const wxString &path)
88
if ( !m_doc.Load(path) ) {
95
m_fileName.MakeAbsolute();
100
100
wxXmlNode *Project::GetVirtualDir(const wxString &vdFullPath)
102
wxStringTokenizer tkz(vdFullPath, wxT(":"));
102
wxStringTokenizer tkz(vdFullPath, wxT(":"));
104
104
// test the cache
105
std::map<wxString, wxXmlNode*>::iterator iter = m_vdCache.find(vdFullPath);
106
if(iter != m_vdCache.end()){
105
std::map<wxString, wxXmlNode*>::iterator iter = m_vdCache.find(vdFullPath);
106
if(iter != m_vdCache.end()){
110
110
wxXmlNode *parent = m_doc.GetRoot();
111
111
while ( tkz.HasMoreTokens() ) {
112
112
parent = XmlUtils::FindNodeByName(parent, wxT("VirtualDirectory"), tkz.GetNextToken());
114
114
m_vdCache[vdFullPath] = NULL;
119
119
m_vdCache[vdFullPath] = parent;
689
701
result.Add(XmlUtils::ReadString(child, wxT("Name")));
691
703
child = child->GetNext();
695
707
node = node->GetNext();
698
// if we are here, it means no match for the given configuration
699
// return the default dependencies
710
// if we are here, it means no match for the given configuration
711
// return the default dependencies
700
712
return GetDependencies();
703
void Project::SetDependencies(wxArrayString& deps, const wxString& configuration)
705
// first try to locate the old node
706
wxXmlNode *node = m_doc.GetRoot()->GetChildren();
708
if ( node->GetName() == wxT("Dependencies") && node->GetPropVal(wxT("Name"), wxEmptyString) == configuration) {
710
node->GetParent()->RemoveChild(node);
714
node = node->GetNext();
717
// create new dependencies node
718
node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("Dependencies"));
719
node->AddProperty(wxT("Name"), configuration);
720
m_doc.GetRoot()->AddChild(node);
722
//create a node for each dependency in the array
723
for (size_t i=0; i<deps.GetCount(); i++) {
724
wxXmlNode *child = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("Project"));
725
child->AddProperty(wxT("Name"), deps.Item(i));
726
node->AddChild(child);
730
m_doc.Save(m_fileName.GetFullPath());
734
void Project::GetFiles(std::vector<wxFileName>& files, std::vector<wxFileName>& absFiles)
737
::wxSetWorkingDirectory(m_fileName.GetPath());
738
GetFiles(m_doc.GetRoot(), files, absFiles);
741
void Project::GetFiles(wxXmlNode *parent, std::vector<wxFileName>& files, std::vector<wxFileName>& absFiles)
747
wxXmlNode *child = parent->GetChildren();
749
if (child->GetName() == wxT("File")) {
750
wxString fileName = child->GetPropVal(wxT("Name"), wxEmptyString);
751
wxFileName tmp(fileName);
753
// append the file as it appears
754
files.push_back(tmp);
756
// convert to absolute path
758
absFiles.push_back(tmp);
760
} else if (child->GetChildren()) {// we could also add a check for VirtualDirectory only
761
GetFiles(child, files, absFiles);
763
child = child->GetNext();
767
bool Project::FastAddFile(const wxString& fileName, const wxString& virtualDir)
769
wxXmlNode *vd = GetVirtualDir(virtualDir);
774
// Convert the file path to be relative to
778
::wxSetWorkingDirectory(m_fileName.GetPath());
779
wxFileName tmp(fileName);
780
tmp.MakeRelativeTo(m_fileName.GetPath());
782
wxXmlNode *node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("File"));
783
node->AddProperty(wxT("Name"), tmp.GetFullPath());
785
if (!InTransaction()) {
786
m_doc.Save(m_fileName.GetFullPath());
792
void Project::DoGetVirtualDirectories(wxXmlNode* parent, TreeNode<wxString, VisualWorkspaceNode>* tree)
794
wxXmlNode *child = parent->GetChildren();
796
if(child->GetName() == wxT("VirtualDirectory")){
798
VisualWorkspaceNode data;
799
data.name = XmlUtils::ReadString(child, wxT("Name"));
800
data.type = ProjectItem::TypeVirtualDirectory;
802
TreeNode<wxString, VisualWorkspaceNode>* node = new TreeNode<wxString, VisualWorkspaceNode>(data.name, data, tree);
803
tree->AddChild(node);
805
// test to see if it has children
806
if(child->GetChildren()){
807
DoGetVirtualDirectories(child, node);
810
child = child->GetNext();
814
TreeNode<wxString, VisualWorkspaceNode>* Project::GetVirtualDirectories(TreeNode<wxString, VisualWorkspaceNode> *workspace)
816
VisualWorkspaceNode data;
817
data.name = GetName();
818
data.type = ProjectItem::TypeProject;
820
TreeNode<wxString, VisualWorkspaceNode> *parent = new TreeNode<wxString, VisualWorkspaceNode>(GetName(), data, workspace);
821
DoGetVirtualDirectories(m_doc.GetRoot(), parent);
822
workspace->AddChild(parent);
826
bool Project::GetUserData(const wxString& name, SerializedObject* obj)
833
wxXmlNode *userData = XmlUtils::FindFirstByTagName(m_doc.GetRoot(), wxT("UserData"));
835
wxXmlNode *dataNode = XmlUtils::FindNodeByName(userData, wxT("Data"), name);
837
arch.SetXmlNode(dataNode);
838
obj->DeSerialize(arch);
845
bool Project::SetUserData(const wxString& name, SerializedObject* obj)
853
// locate the 'UserData' node
854
wxXmlNode *userData = XmlUtils::FindFirstByTagName(m_doc.GetRoot(), wxT("UserData"));
856
userData = new wxXmlNode(m_doc.GetRoot(), wxXML_ELEMENT_NODE, wxT("UserData"));
859
// try to find a previous data stored under the same name, if we succeed - remove it
860
wxXmlNode *dataNode = XmlUtils::FindNodeByName(userData, wxT("Data"), name);
863
userData->RemoveChild(dataNode);
867
// create a new node and set the userData node as the parent
868
dataNode = new wxXmlNode(userData, wxXML_ELEMENT_NODE, wxT("Data"));
869
dataNode->AddProperty(wxT("Name"), name);
871
// serialize the data
872
arch.SetXmlNode(dataNode);
873
obj->Serialize(arch);
874
return m_doc.Save(m_fileName.GetFullPath());
877
void Project::SetProjectInternalType(const wxString& internalType)
879
XmlUtils::UpdateProperty(m_doc.GetRoot(), wxT("InternalType"), internalType);
882
wxString Project::GetProjectInternalType() const
884
return m_doc.GetRoot()->GetPropVal(wxT("InternalType"), wxEmptyString);
715
void Project::SetDependencies(wxArrayString& deps, const wxString& configuration)
717
// first try to locate the old node
718
wxXmlNode *node = m_doc.GetRoot()->GetChildren();
720
if ( node->GetName() == wxT("Dependencies") && node->GetPropVal(wxT("Name"), wxEmptyString) == configuration) {
722
node->GetParent()->RemoveChild(node);
726
node = node->GetNext();
729
// create new dependencies node
730
node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("Dependencies"));
731
node->AddProperty(wxT("Name"), configuration);
732
m_doc.GetRoot()->AddChild(node);
734
//create a node for each dependency in the array
735
for (size_t i=0; i<deps.GetCount(); i++) {
736
wxXmlNode *child = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("Project"));
737
child->AddProperty(wxT("Name"), deps.Item(i));
738
node->AddChild(child);
742
m_doc.Save(m_fileName.GetFullPath());
746
void Project::GetFiles(std::vector<wxFileName>& files, std::vector<wxFileName>& absFiles)
749
::wxSetWorkingDirectory(m_fileName.GetPath());
750
GetFiles(m_doc.GetRoot(), files, absFiles);
753
void Project::GetFiles(wxXmlNode *parent, std::vector<wxFileName>& files, std::vector<wxFileName>& absFiles)
759
wxXmlNode *child = parent->GetChildren();
761
if (child->GetName() == wxT("File")) {
762
wxString fileName = child->GetPropVal(wxT("Name"), wxEmptyString);
763
wxFileName tmp(fileName);
765
// append the file as it appears
766
files.push_back(tmp);
768
// convert to absolute path
770
absFiles.push_back(tmp);
772
} else if (child->GetChildren()) {// we could also add a check for VirtualDirectory only
773
GetFiles(child, files, absFiles);
775
child = child->GetNext();
779
bool Project::FastAddFile(const wxString& fileName, const wxString& virtualDir)
781
wxXmlNode *vd = GetVirtualDir(virtualDir);
786
// Convert the file path to be relative to
790
::wxSetWorkingDirectory(m_fileName.GetPath());
791
wxFileName tmp(fileName);
792
tmp.MakeRelativeTo(m_fileName.GetPath());
794
wxXmlNode *node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("File"));
795
node->AddProperty(wxT("Name"), tmp.GetFullPath());
797
if (!InTransaction()) {
798
m_doc.Save(m_fileName.GetFullPath());
804
void Project::DoGetVirtualDirectories(wxXmlNode* parent, TreeNode<wxString, VisualWorkspaceNode>* tree)
806
wxXmlNode *child = parent->GetChildren();
808
if(child->GetName() == wxT("VirtualDirectory")){
810
VisualWorkspaceNode data;
811
data.name = XmlUtils::ReadString(child, wxT("Name"));
812
data.type = ProjectItem::TypeVirtualDirectory;
814
TreeNode<wxString, VisualWorkspaceNode>* node = new TreeNode<wxString, VisualWorkspaceNode>(data.name, data, tree);
815
tree->AddChild(node);
817
// test to see if it has children
818
if(child->GetChildren()){
819
DoGetVirtualDirectories(child, node);
822
child = child->GetNext();
826
TreeNode<wxString, VisualWorkspaceNode>* Project::GetVirtualDirectories(TreeNode<wxString, VisualWorkspaceNode> *workspace)
828
VisualWorkspaceNode data;
829
data.name = GetName();
830
data.type = ProjectItem::TypeProject;
832
TreeNode<wxString, VisualWorkspaceNode> *parent = new TreeNode<wxString, VisualWorkspaceNode>(GetName(), data, workspace);
833
DoGetVirtualDirectories(m_doc.GetRoot(), parent);
834
workspace->AddChild(parent);
838
bool Project::GetUserData(const wxString& name, SerializedObject* obj)
845
wxXmlNode *userData = XmlUtils::FindFirstByTagName(m_doc.GetRoot(), wxT("UserData"));
847
wxXmlNode *dataNode = XmlUtils::FindNodeByName(userData, wxT("Data"), name);
849
arch.SetXmlNode(dataNode);
850
obj->DeSerialize(arch);
857
bool Project::SetUserData(const wxString& name, SerializedObject* obj)
865
// locate the 'UserData' node
866
wxXmlNode *userData = XmlUtils::FindFirstByTagName(m_doc.GetRoot(), wxT("UserData"));
868
userData = new wxXmlNode(m_doc.GetRoot(), wxXML_ELEMENT_NODE, wxT("UserData"));
871
// try to find a previous data stored under the same name, if we succeed - remove it
872
wxXmlNode *dataNode = XmlUtils::FindNodeByName(userData, wxT("Data"), name);
875
userData->RemoveChild(dataNode);
879
// create a new node and set the userData node as the parent
880
dataNode = new wxXmlNode(userData, wxXML_ELEMENT_NODE, wxT("Data"));
881
dataNode->AddProperty(wxT("Name"), name);
883
// serialize the data
884
arch.SetXmlNode(dataNode);
885
obj->Serialize(arch);
886
return m_doc.Save(m_fileName.GetFullPath());
889
void Project::SetProjectInternalType(const wxString& internalType)
891
XmlUtils::UpdateProperty(m_doc.GetRoot(), wxT("InternalType"), internalType);
894
wxString Project::GetProjectInternalType() const
896
return m_doc.GetRoot()->GetPropVal(wxT("InternalType"), wxEmptyString);