Project.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 "HackStudio.h"
  28. #include <AK/LexicalPath.h>
  29. #include <AK/QuickSort.h>
  30. #include <AK/StringBuilder.h>
  31. #include <LibCore/DirIterator.h>
  32. #include <LibCore/File.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <sys/stat.h>
  36. #include <unistd.h>
  37. namespace HackStudio {
  38. struct Project::ProjectTreeNode : public RefCounted<ProjectTreeNode> {
  39. enum class Type {
  40. Invalid,
  41. Project,
  42. Directory,
  43. File,
  44. };
  45. ProjectTreeNode& find_or_create_subdirectory(const String& name)
  46. {
  47. for (auto& child : children) {
  48. if (child->type == Type::Directory && child->name == name)
  49. return *child;
  50. }
  51. auto new_child = adopt(*new ProjectTreeNode);
  52. new_child->type = Type::Directory;
  53. new_child->name = name;
  54. new_child->parent = this;
  55. auto* ptr = new_child.ptr();
  56. children.append(move(new_child));
  57. return *ptr;
  58. }
  59. void sort()
  60. {
  61. if (type == Type::File)
  62. return;
  63. quick_sort(children, [](auto& a, auto& b) {
  64. return a->name < b->name;
  65. });
  66. for (auto& child : children)
  67. child->sort();
  68. }
  69. Type type { Type::Invalid };
  70. String name;
  71. String path;
  72. Vector<NonnullRefPtr<ProjectTreeNode>> children;
  73. ProjectTreeNode* parent { nullptr };
  74. };
  75. class ProjectModel final : public GUI::Model {
  76. public:
  77. explicit ProjectModel(Project& project)
  78. : m_project(project)
  79. {
  80. }
  81. virtual int row_count(const GUI::ModelIndex& index) const override
  82. {
  83. if (!index.is_valid())
  84. return 1;
  85. auto* node = static_cast<Project::ProjectTreeNode*>(index.internal_data());
  86. return node->children.size();
  87. }
  88. virtual int column_count(const GUI::ModelIndex&) const override
  89. {
  90. return 1;
  91. }
  92. virtual GUI::Variant data(const GUI::ModelIndex& index, GUI::ModelRole role) const override
  93. {
  94. auto* node = static_cast<Project::ProjectTreeNode*>(index.internal_data());
  95. if (role == GUI::ModelRole::Display) {
  96. return node->name;
  97. }
  98. if (role == GUI::ModelRole::Custom) {
  99. return node->path;
  100. }
  101. if (role == GUI::ModelRole::Icon) {
  102. if (node->type == Project::ProjectTreeNode::Type::Project)
  103. return m_project.m_project_icon;
  104. if (node->type == Project::ProjectTreeNode::Type::Directory)
  105. return m_project.m_directory_icon;
  106. if (node->name.ends_with(".cpp"))
  107. return m_project.m_cplusplus_icon;
  108. if (node->name.ends_with(".frm"))
  109. return m_project.m_form_icon;
  110. if (node->name.ends_with(".h"))
  111. return m_project.m_header_icon;
  112. if (node->name.ends_with(".hsp"))
  113. return m_project.m_hackstudio_icon;
  114. if (node->name.ends_with(".js"))
  115. return m_project.m_javascript_icon;
  116. return m_project.m_file_icon;
  117. }
  118. if (role == GUI::ModelRole::Font) {
  119. if (node->name == currently_open_file())
  120. return Gfx::Font::default_bold_font();
  121. return {};
  122. }
  123. return {};
  124. }
  125. virtual GUI::ModelIndex index(int row, int column = 0, const GUI::ModelIndex& parent = GUI::ModelIndex()) const override
  126. {
  127. if (!parent.is_valid()) {
  128. return create_index(row, column, &m_project.root_node());
  129. }
  130. auto& node = *static_cast<Project::ProjectTreeNode*>(parent.internal_data());
  131. return create_index(row, column, node.children.at(row).ptr());
  132. }
  133. GUI::ModelIndex parent_index(const GUI::ModelIndex& index) const override
  134. {
  135. if (!index.is_valid())
  136. return {};
  137. auto& node = *static_cast<Project::ProjectTreeNode*>(index.internal_data());
  138. if (!node.parent)
  139. return {};
  140. if (!node.parent->parent) {
  141. return create_index(0, 0, &m_project.root_node());
  142. ASSERT_NOT_REACHED();
  143. return {};
  144. }
  145. for (size_t row = 0; row < node.parent->parent->children.size(); ++row) {
  146. if (node.parent->parent->children[row].ptr() == node.parent)
  147. return create_index(row, 0, node.parent);
  148. }
  149. ASSERT_NOT_REACHED();
  150. return {};
  151. }
  152. virtual void update() override
  153. {
  154. did_update();
  155. }
  156. private:
  157. Project& m_project;
  158. };
  159. Project::Project(const String& path, Vector<String>&& filenames)
  160. : m_path(path)
  161. {
  162. m_name = LexicalPath(m_path).basename();
  163. m_file_icon = GUI::Icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-unknown.png"));
  164. m_cplusplus_icon = GUI::Icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-cplusplus.png"));
  165. m_header_icon = GUI::Icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-header.png"));
  166. m_directory_icon = GUI::Icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-folder.png"));
  167. m_project_icon = GUI::Icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/hackstudio-project.png"));
  168. m_javascript_icon = GUI::Icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-javascript.png"));
  169. m_hackstudio_icon = GUI::Icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-hackstudio.png"));
  170. m_form_icon = GUI::Icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-form.png"));
  171. for (auto& filename : filenames) {
  172. m_files.append(ProjectFile::construct_with_name(filename));
  173. }
  174. m_model = adopt(*new ProjectModel(*this));
  175. rebuild_tree();
  176. }
  177. Project::~Project()
  178. {
  179. }
  180. OwnPtr<Project> Project::load_from_file(const String& path)
  181. {
  182. auto file = Core::File::construct(path);
  183. if (!file->open(Core::File::ReadOnly))
  184. return nullptr;
  185. auto type = ProjectType::Cpp;
  186. Vector<String> files;
  187. auto add_glob = [&](String path) {
  188. auto split = path.split('*', true);
  189. for (auto& item : split) {
  190. dbg() << item;
  191. }
  192. ASSERT(split.size() == 2);
  193. auto cwd = getcwd(nullptr, 0);
  194. Core::DirIterator it(cwd, Core::DirIterator::Flags::SkipParentAndBaseDir);
  195. while (it.has_next()) {
  196. auto path = it.next_path();
  197. if (!split[0].is_empty() && !path.starts_with(split[0]))
  198. continue;
  199. if (!split[1].is_empty() && !path.ends_with(split[1]))
  200. continue;
  201. files.append(path);
  202. }
  203. };
  204. for (;;) {
  205. auto line = file->read_line(1024);
  206. if (line.is_null())
  207. break;
  208. auto path = String::copy(line, Chomp);
  209. if (path.contains("*"))
  210. add_glob(path);
  211. else
  212. files.append(path);
  213. }
  214. for (auto& file : files) {
  215. if (file.ends_with(".js")) {
  216. type = ProjectType::JavaScript;
  217. break;
  218. }
  219. }
  220. quick_sort(files);
  221. auto project = adopt_own(*new Project(path, move(files)));
  222. project->m_type = type;
  223. return project;
  224. }
  225. bool Project::add_file(const String& filename)
  226. {
  227. m_files.append(ProjectFile::construct_with_name(filename));
  228. rebuild_tree();
  229. m_model->update();
  230. return save();
  231. }
  232. bool Project::remove_file(const String& filename)
  233. {
  234. if (!get_file(filename))
  235. return false;
  236. m_files.remove_first_matching([filename](auto& file) { return file->name() == filename; });
  237. rebuild_tree();
  238. m_model->update();
  239. return save();
  240. }
  241. bool Project::save()
  242. {
  243. auto project_file = Core::File::construct(m_path);
  244. if (!project_file->open(Core::File::WriteOnly))
  245. return false;
  246. for (auto& file : m_files) {
  247. // FIXME: Check for error here. IODevice::printf() needs some work on error reporting.
  248. project_file->printf("%s\n", file.name().characters());
  249. }
  250. if (!project_file->close())
  251. return false;
  252. return true;
  253. }
  254. ProjectFile* Project::get_file(const String& filename)
  255. {
  256. for (auto& file : m_files) {
  257. if (LexicalPath(file.name()).string() == LexicalPath(filename).string())
  258. return &file;
  259. }
  260. return nullptr;
  261. }
  262. String Project::default_file() const
  263. {
  264. if (m_type == ProjectType::Cpp)
  265. return "main.cpp";
  266. if (m_files.size() > 0)
  267. return m_files.first().name();
  268. ASSERT_NOT_REACHED();
  269. }
  270. void Project::rebuild_tree()
  271. {
  272. auto root = adopt(*new ProjectTreeNode);
  273. root->name = m_name;
  274. root->type = ProjectTreeNode::Type::Project;
  275. for (auto& file : m_files) {
  276. LexicalPath path(file.name());
  277. ProjectTreeNode* current = root.ptr();
  278. StringBuilder partial_path;
  279. for (size_t i = 0; i < path.parts().size(); ++i) {
  280. auto& part = path.parts().at(i);
  281. if (part == ".")
  282. continue;
  283. if (i != path.parts().size() - 1) {
  284. current = &current->find_or_create_subdirectory(part);
  285. continue;
  286. }
  287. struct stat st;
  288. if (lstat(path.string().characters(), &st) < 0)
  289. continue;
  290. if (S_ISDIR(st.st_mode)) {
  291. current = &current->find_or_create_subdirectory(part);
  292. continue;
  293. }
  294. auto file_node = adopt(*new ProjectTreeNode);
  295. file_node->name = part;
  296. file_node->path = path.string();
  297. file_node->type = Project::ProjectTreeNode::Type::File;
  298. file_node->parent = current;
  299. current->children.append(move(file_node));
  300. break;
  301. }
  302. }
  303. root->sort();
  304. #if 0
  305. Function<void(ProjectTreeNode&, int indent)> dump_tree = [&](ProjectTreeNode& node, int indent) {
  306. for (int i = 0; i < indent; ++i)
  307. new_out(" ");
  308. if (node.name.is_null())
  309. outln("(null)");
  310. else
  311. outln("{}", node.name);
  312. for (auto& child : node.children) {
  313. dump_tree(*child, indent + 2);
  314. }
  315. };
  316. dump_tree(*root, 0);
  317. #endif
  318. m_root_node = move(root);
  319. m_model->update();
  320. }
  321. }