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