ProjectTemplate.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2021, Nick Vella <nick@nxk.io>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ProjectTemplate.h"
  7. #include <AK/LexicalPath.h>
  8. #include <AK/String.h>
  9. #include <AK/StringBuilder.h>
  10. #include <LibCore/ConfigFile.h>
  11. #include <LibCore/DirIterator.h>
  12. #include <LibCore/File.h>
  13. #include <fcntl.h>
  14. #include <spawn.h>
  15. #include <sys/stat.h>
  16. #include <sys/wait.h>
  17. #include <unistd.h>
  18. namespace HackStudio {
  19. ProjectTemplate::ProjectTemplate(String const& id, String const& name, String const& description, const GUI::Icon& icon, int priority)
  20. : m_id(id)
  21. , m_name(name)
  22. , m_description(description)
  23. , m_icon(icon)
  24. , m_priority(priority)
  25. {
  26. }
  27. RefPtr<ProjectTemplate> ProjectTemplate::load_from_manifest(String const& manifest_path)
  28. {
  29. auto maybe_config = Core::ConfigFile::open(manifest_path);
  30. if (maybe_config.is_error())
  31. return {};
  32. auto config = maybe_config.release_value();
  33. if (!config->has_group("HackStudioTemplate")
  34. || !config->has_key("HackStudioTemplate", "Name")
  35. || !config->has_key("HackStudioTemplate", "Description")
  36. || !config->has_key("HackStudioTemplate", "IconName32x"))
  37. return {};
  38. auto id = LexicalPath::title(manifest_path);
  39. auto name = config->read_entry("HackStudioTemplate", "Name");
  40. auto description = config->read_entry("HackStudioTemplate", "Description");
  41. int priority = config->read_num_entry("HackStudioTemplate", "Priority", 0);
  42. // Attempt to read in the template icons
  43. // Fallback to a generic executable icon if one isn't found
  44. auto icon = GUI::Icon::default_icon("filetype-executable");
  45. auto bitmap_path_32 = String::formatted("/res/icons/hackstudio/templates-32x32/{}.png", config->read_entry("HackStudioTemplate", "IconName32x"));
  46. if (Core::File::exists(bitmap_path_32)) {
  47. auto bitmap_or_error = Gfx::Bitmap::try_load_from_file(bitmap_path_32);
  48. if (!bitmap_or_error.is_error())
  49. icon = GUI::Icon(bitmap_or_error.release_value());
  50. }
  51. return adopt_ref(*new ProjectTemplate(id, name, description, icon, priority));
  52. }
  53. Result<void, String> ProjectTemplate::create_project(String const& name, String const& path)
  54. {
  55. // Check if a file or directory already exists at the project path
  56. if (Core::File::exists(path))
  57. return String("File or directory already exists at specified location.");
  58. dbgln("Creating project at path '{}' with name '{}'", path, name);
  59. // Verify that the template content directory exists. If it does, copy it's contents.
  60. // Otherwise, create an empty directory at the project path.
  61. if (Core::File::is_directory(content_path())) {
  62. auto result = Core::File::copy_file_or_directory(path, content_path());
  63. dbgln("Copying {} -> {}", content_path(), path);
  64. if (result.is_error())
  65. return String::formatted("Failed to copy template contents. Error code: {}", static_cast<Error const&>(result.error()));
  66. } else {
  67. dbgln("No template content directory found for '{}', creating an empty directory for the project.", m_id);
  68. int rc;
  69. if ((rc = mkdir(path.characters(), 0755)) < 0) {
  70. return String::formatted("Failed to mkdir empty project directory, error: {}, rc: {}.", strerror(errno), rc);
  71. }
  72. }
  73. // Check for an executable post-create script in $TEMPLATES_DIR/$ID.postcreate,
  74. // and run it with the path and name
  75. auto postcreate_script_path = LexicalPath::canonicalized_path(String::formatted("{}/{}.postcreate", templates_path(), m_id));
  76. struct stat postcreate_st;
  77. int result = stat(postcreate_script_path.characters(), &postcreate_st);
  78. if (result == 0 && (postcreate_st.st_mode & S_IXOTH) == S_IXOTH) {
  79. dbgln("Running post-create script '{}'", postcreate_script_path);
  80. // Generate a namespace-safe project name (replace hyphens with underscores)
  81. auto namespace_safe = name.replace("-", "_", ReplaceMode::All);
  82. pid_t child_pid;
  83. char const* argv[] = { postcreate_script_path.characters(), name.characters(), path.characters(), namespace_safe.characters(), nullptr };
  84. if ((errno = posix_spawn(&child_pid, postcreate_script_path.characters(), nullptr, nullptr, const_cast<char**>(argv), environ))) {
  85. perror("posix_spawn");
  86. return String("Failed to spawn project post-create script.");
  87. }
  88. // Command spawned, wait for exit.
  89. int status;
  90. if (waitpid(child_pid, &status, 0) < 0)
  91. return String("Failed to spawn project post-create script.");
  92. int child_error = WEXITSTATUS(status);
  93. dbgln("Post-create script exited with code {}", child_error);
  94. if (child_error != 0)
  95. return String("Project post-creation script exited with non-zero error code.");
  96. }
  97. return {};
  98. }
  99. }