Project.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Project.h"
  27. #include <AK/FileSystemPath.h>
  28. #include <AK/QuickSort.h>
  29. #include <AK/StringBuilder.h>
  30. #include <LibCore/DirIterator.h>
  31. #include <LibCore/File.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <sys/stat.h>
  35. #include <unistd.h>
  36. struct Project::ProjectTreeNode : public RefCounted<ProjectTreeNode> {
  37. enum class Type {
  38. Invalid,
  39. Project,
  40. Directory,
  41. File,
  42. };
  43. ProjectTreeNode& find_or_create_subdirectory(const String& name)
  44. {
  45. for (auto& child : children) {
  46. if (child->type == Type::Directory && child->name == name)
  47. return *child;
  48. }
  49. auto new_child = adopt(*new ProjectTreeNode);
  50. new_child->type = Type::Directory;
  51. new_child->name = name;
  52. new_child->parent = this;
  53. auto* ptr = new_child.ptr();
  54. children.append(move(new_child));
  55. return *ptr;
  56. }
  57. void sort()
  58. {
  59. if (type == Type::File)
  60. return;
  61. quick_sort(children, [](auto& a, auto& b) {
  62. return a->name < b->name;
  63. });
  64. for (auto& child : children)
  65. child->sort();
  66. }
  67. Type type { Type::Invalid };
  68. String name;
  69. String path;
  70. Vector<NonnullRefPtr<ProjectTreeNode>> children;
  71. ProjectTreeNode* parent { nullptr };
  72. };
  73. class ProjectModel final : public GUI::Model {
  74. public:
  75. explicit ProjectModel(Project& project)
  76. : m_project(project)
  77. {
  78. }
  79. virtual int row_count(const GUI::ModelIndex& index) const override
  80. {
  81. if (!index.is_valid())
  82. return 1;
  83. auto* node = static_cast<Project::ProjectTreeNode*>(index.internal_data());
  84. return node->children.size();
  85. }
  86. virtual int column_count(const GUI::ModelIndex&) const override
  87. {
  88. return 1;
  89. }
  90. virtual GUI::Variant data(const GUI::ModelIndex& index, Role role = Role::Display) const override
  91. {
  92. auto* node = static_cast<Project::ProjectTreeNode*>(index.internal_data());
  93. if (role == Role::Display) {
  94. return node->name;
  95. }
  96. if (role == Role::Custom) {
  97. return node->path;
  98. }
  99. if (role == Role::Icon) {
  100. if (node->type == Project::ProjectTreeNode::Type::Project)
  101. return m_project.m_project_icon;
  102. if (node->type == Project::ProjectTreeNode::Type::Directory)
  103. return m_project.m_directory_icon;
  104. if (node->name.ends_with(".cpp"))
  105. return m_project.m_cplusplus_icon;
  106. if (node->name.ends_with(".h"))
  107. return m_project.m_header_icon;
  108. return m_project.m_file_icon;
  109. }
  110. if (role == Role::Font) {
  111. extern String g_currently_open_file;
  112. if (node->name == g_currently_open_file)
  113. return Gfx::Font::default_bold_font();
  114. return {};
  115. }
  116. return {};
  117. }
  118. virtual GUI::ModelIndex index(int row, int column = 0, const GUI::ModelIndex& parent = GUI::ModelIndex()) const override
  119. {
  120. if (!parent.is_valid()) {
  121. return create_index(row, column, &m_project.root_node());
  122. }
  123. auto& node = *static_cast<Project::ProjectTreeNode*>(parent.internal_data());
  124. return create_index(row, column, node.children.at(row).ptr());
  125. }
  126. GUI::ModelIndex parent_index(const GUI::ModelIndex& index) const override
  127. {
  128. if (!index.is_valid())
  129. return {};
  130. auto& node = *static_cast<Project::ProjectTreeNode*>(index.internal_data());
  131. if (!node.parent)
  132. return {};
  133. if (!node.parent->parent) {
  134. return create_index(0, 0, &m_project.root_node());
  135. ASSERT_NOT_REACHED();
  136. return {};
  137. }
  138. for (size_t row = 0; row < node.parent->parent->children.size(); ++row) {
  139. if (node.parent->parent->children[row].ptr() == node.parent)
  140. return create_index(row, 0, node.parent);
  141. }
  142. ASSERT_NOT_REACHED();
  143. return {};
  144. }
  145. virtual void update() override
  146. {
  147. did_update();
  148. }
  149. private:
  150. Project& m_project;
  151. };
  152. Project::Project(const String& path, Vector<String>&& filenames)
  153. : m_path(path)
  154. {
  155. m_name = FileSystemPath(m_path).basename();
  156. m_file_icon = GUI::Icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-unknown.png"));
  157. m_cplusplus_icon = GUI::Icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-cplusplus.png"));
  158. m_header_icon = GUI::Icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-header.png"));
  159. m_directory_icon = GUI::Icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-folder.png"));
  160. m_project_icon = GUI::Icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-hack-studio.png"));
  161. for (auto& filename : filenames) {
  162. m_files.append(ProjectFile::construct_with_name(filename));
  163. }
  164. m_model = adopt(*new ProjectModel(*this));
  165. rebuild_tree();
  166. }
  167. Project::~Project()
  168. {
  169. }
  170. OwnPtr<Project> Project::load_from_file(const String& path)
  171. {
  172. auto file = Core::File::construct(path);
  173. if (!file->open(Core::File::ReadOnly))
  174. return nullptr;
  175. auto type = ProjectType::Cpp;
  176. Vector<String> files;
  177. auto add_glob = [&](String path) {
  178. auto split = path.split('*', true);
  179. for (auto& item : split) {
  180. dbg() << item;
  181. }
  182. ASSERT(split.size() == 2);
  183. auto cwd = getcwd(nullptr, 0);
  184. Core::DirIterator it(cwd, Core::DirIterator::Flags::SkipParentAndBaseDir);
  185. while (it.has_next()) {
  186. auto path = it.next_path();
  187. if (!split[0].is_empty() && !path.starts_with(split[0]))
  188. continue;
  189. if (!split[1].is_empty() && !path.ends_with(split[1]))
  190. continue;
  191. files.append(path);
  192. }
  193. };
  194. for (;;) {
  195. auto line = file->read_line(1024);
  196. if (line.is_null())
  197. break;
  198. auto path = String::copy(line, Chomp);
  199. if (path.contains("*"))
  200. add_glob(path);
  201. else
  202. files.append(path);
  203. }
  204. for (auto& file : files) {
  205. if (file.ends_with(".js")) {
  206. type = ProjectType::Javascript;
  207. break;
  208. }
  209. }
  210. quick_sort(files);
  211. auto project = OwnPtr(new Project(path, move(files)));
  212. project->m_type = type;
  213. return project;
  214. }
  215. bool Project::add_file(const String& filename)
  216. {
  217. m_files.append(ProjectFile::construct_with_name(filename));
  218. rebuild_tree();
  219. m_model->update();
  220. return save();
  221. }
  222. bool Project::remove_file(const String& filename)
  223. {
  224. if (!get_file(filename))
  225. return false;
  226. m_files.remove_first_matching([filename](auto& file) { return file->name() == filename; });
  227. rebuild_tree();
  228. m_model->update();
  229. return save();
  230. }
  231. bool Project::save()
  232. {
  233. auto project_file = Core::File::construct(m_path);
  234. if (!project_file->open(Core::File::WriteOnly))
  235. return false;
  236. for (auto& file : m_files) {
  237. // FIXME: Check for error here. IODevice::printf() needs some work on error reporting.
  238. project_file->printf("%s\n", file.name().characters());
  239. }
  240. if (!project_file->close())
  241. return false;
  242. return true;
  243. }
  244. ProjectFile* Project::get_file(const String& filename)
  245. {
  246. for (auto& file : m_files) {
  247. if (FileSystemPath(file.name()).string() == FileSystemPath(filename).string())
  248. return &file;
  249. }
  250. return nullptr;
  251. }
  252. String Project::default_file() const
  253. {
  254. if (m_type == ProjectType::Cpp)
  255. return "main.cpp";
  256. if (m_files.size() > 0)
  257. return m_files.first().name();
  258. ASSERT_NOT_REACHED();
  259. }
  260. void Project::rebuild_tree()
  261. {
  262. auto root = adopt(*new ProjectTreeNode);
  263. root->name = m_name;
  264. root->type = ProjectTreeNode::Type::Project;
  265. for (auto& file : m_files) {
  266. FileSystemPath path(file.name());
  267. ProjectTreeNode* current = root.ptr();
  268. StringBuilder partial_path;
  269. for (size_t i = 0; i < path.parts().size(); ++i) {
  270. auto& part = path.parts().at(i);
  271. if (part == ".")
  272. continue;
  273. if (i != path.parts().size() - 1) {
  274. current = &current->find_or_create_subdirectory(part);
  275. continue;
  276. }
  277. struct stat st;
  278. if (lstat(path.string().characters(), &st) < 0)
  279. continue;
  280. if (S_ISDIR(st.st_mode)) {
  281. current = &current->find_or_create_subdirectory(part);
  282. continue;
  283. }
  284. auto file_node = adopt(*new ProjectTreeNode);
  285. file_node->name = part;
  286. file_node->path = path.string();
  287. file_node->type = Project::ProjectTreeNode::Type::File;
  288. file_node->parent = current;
  289. current->children.append(move(file_node));
  290. break;
  291. }
  292. }
  293. root->sort();
  294. #if 0
  295. Function<void(ProjectTreeNode&, int indent)> dump_tree = [&](ProjectTreeNode& node, int indent) {
  296. for (int i = 0; i < indent; ++i)
  297. printf(" ");
  298. if (node.name.is_null())
  299. printf("(null)\n");
  300. else
  301. printf("%s\n", node.name.characters());
  302. for (auto& child : node.children) {
  303. dump_tree(*child, indent + 2);
  304. }
  305. };
  306. dump_tree(*root, 0);
  307. #endif
  308. m_root_node = move(root);
  309. m_model->update();
  310. }