ProjectTemplate.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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(const String& id, const String& name, const String& 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(const String& manifest_path)
  28. {
  29. auto config = Core::ConfigFile::open(manifest_path);
  30. if (!config->has_group("HackStudioTemplate")
  31. || !config->has_key("HackStudioTemplate", "Name")
  32. || !config->has_key("HackStudioTemplate", "Description")
  33. || !config->has_key("HackStudioTemplate", "IconName32x"))
  34. return {};
  35. auto id = LexicalPath::title(manifest_path);
  36. auto name = config->read_entry("HackStudioTemplate", "Name");
  37. auto description = config->read_entry("HackStudioTemplate", "Description");
  38. int priority = config->read_num_entry("HackStudioTemplate", "Priority", 0);
  39. // Attempt to read in the template icons
  40. // Fallback to a generic executable icon if one isn't found
  41. auto icon = GUI::Icon::default_icon("filetype-executable");
  42. auto bitmap_path_32 = String::formatted("/res/icons/hackstudio/templates-32x32/{}.png", config->read_entry("HackStudioTemplate", "IconName32x"));
  43. if (Core::File::exists(bitmap_path_32)) {
  44. auto bitmap_or_error = Gfx::Bitmap::try_load_from_file(bitmap_path_32);
  45. if (!bitmap_or_error.is_error())
  46. icon = GUI::Icon(bitmap_or_error.release_value());
  47. }
  48. return adopt_ref(*new ProjectTemplate(id, name, description, icon, priority));
  49. }
  50. Result<void, String> ProjectTemplate::create_project(const String& name, const String& path)
  51. {
  52. // Check if a file or directory already exists at the project path
  53. if (Core::File::exists(path))
  54. return String("File or directory already exists at specified location.");
  55. dbgln("Creating project at path '{}' with name '{}'", path, name);
  56. // Verify that the template content directory exists. If it does, copy it's contents.
  57. // Otherwise, create an empty directory at the project path.
  58. if (Core::File::is_directory(content_path())) {
  59. auto result = Core::File::copy_file_or_directory(path, content_path());
  60. dbgln("Copying {} -> {}", content_path(), path);
  61. if (result.is_error())
  62. return String::formatted("Failed to copy template contents. Error code: {}", static_cast<Error const&>(result.error()));
  63. } else {
  64. dbgln("No template content directory found for '{}', creating an empty directory for the project.", m_id);
  65. int rc;
  66. if ((rc = mkdir(path.characters(), 0755)) < 0) {
  67. return String::formatted("Failed to mkdir empty project directory, error: {}, rc: {}.", strerror(errno), rc);
  68. }
  69. }
  70. // Check for an executable post-create script in $TEMPLATES_DIR/$ID.postcreate,
  71. // and run it with the path and name
  72. auto postcreate_script_path = LexicalPath::canonicalized_path(String::formatted("{}/{}.postcreate", templates_path(), m_id));
  73. struct stat postcreate_st;
  74. int result = stat(postcreate_script_path.characters(), &postcreate_st);
  75. if (result == 0 && (postcreate_st.st_mode & S_IXOTH) == S_IXOTH) {
  76. dbgln("Running post-create script '{}'", postcreate_script_path);
  77. // Generate a namespace-safe project name (replace hyphens with underscores)
  78. auto namespace_safe = name.replace("-", "_", true);
  79. pid_t child_pid;
  80. const char* argv[] = { postcreate_script_path.characters(), name.characters(), path.characters(), namespace_safe.characters(), nullptr };
  81. if ((errno = posix_spawn(&child_pid, postcreate_script_path.characters(), nullptr, nullptr, const_cast<char**>(argv), environ))) {
  82. perror("posix_spawn");
  83. return String("Failed to spawn project post-create script.");
  84. }
  85. // Command spawned, wait for exit.
  86. int status;
  87. if (waitpid(child_pid, &status, 0) < 0)
  88. return String("Failed to spawn project post-create script.");
  89. int child_error = WEXITSTATUS(status);
  90. dbgln("Post-create script exited with code {}", child_error);
  91. if (child_error != 0)
  92. return String("Project post-creation script exited with non-zero error code.");
  93. }
  94. return {};
  95. }
  96. }