diff --git a/Userland/DevTools/HackStudio/ProjectTemplate.cpp b/Userland/DevTools/HackStudio/ProjectTemplate.cpp index 415329b8b3..5a8466a6df 100644 --- a/Userland/DevTools/HackStudio/ProjectTemplate.cpp +++ b/Userland/DevTools/HackStudio/ProjectTemplate.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2021, Nick Vella + * Copyright (c) 2024, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -10,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -61,27 +63,22 @@ RefPtr ProjectTemplate::load_from_manifest(ByteString const& ma return adopt_ref(*new ProjectTemplate(id, name, description, icon, priority)); } -Result ProjectTemplate::create_project(ByteString const& name, ByteString const& path) +ErrorOr ProjectTemplate::create_project(ByteString const& name, ByteString const& path) { // Check if a file or directory already exists at the project path if (FileSystem::exists(path)) - return ByteString("File or directory already exists at specified location."); + return Error::from_string_literal("File or directory already exists at specified location."); dbgln("Creating project at path '{}' with name '{}'", path, name); // Verify that the template content directory exists. If it does, copy it's contents. // Otherwise, create an empty directory at the project path. if (FileSystem::is_directory(content_path())) { - auto result = FileSystem::copy_file_or_directory(path, content_path()); dbgln("Copying {} -> {}", content_path(), path); - if (result.is_error()) - return ByteString::formatted("Failed to copy template contents. Error code: {}", static_cast(result.error())); + TRY(FileSystem::copy_file_or_directory(path, content_path())); } else { dbgln("No template content directory found for '{}', creating an empty directory for the project.", m_id); - int rc; - if ((rc = mkdir(path.characters(), 0755)) < 0) { - return ByteString::formatted("Failed to mkdir empty project directory, error: {}, rc: {}.", strerror(errno), rc); - } + TRY(Core::System::mkdir(path, 0755)); } // Check for an executable post-create script in $TEMPLATES_DIR/$ID.postcreate, @@ -96,24 +93,17 @@ Result ProjectTemplate::create_project(ByteString const& name, // Generate a namespace-safe project name (replace hyphens with underscores) auto namespace_safe = name.replace("-"sv, "_"sv, ReplaceMode::All); - pid_t child_pid; char const* argv[] = { postcreate_script_path.characters(), name.characters(), path.characters(), namespace_safe.characters(), nullptr }; - if ((errno = posix_spawn(&child_pid, postcreate_script_path.characters(), nullptr, nullptr, const_cast(argv), environ))) { - perror("posix_spawn"); - return ByteString("Failed to spawn project post-create script."); - } + pid_t child_pid = TRY(Core::System::posix_spawn(postcreate_script_path, nullptr, nullptr, const_cast(argv), environ)); // Command spawned, wait for exit. - int status; - if (waitpid(child_pid, &status, 0) < 0) - return ByteString("Failed to spawn project post-create script."); - - int child_error = WEXITSTATUS(status); + auto waitpid_result = TRY(Core::System::waitpid(child_pid, 0)); + int child_error = WEXITSTATUS(waitpid_result.status); dbgln("Post-create script exited with code {}", child_error); if (child_error != 0) - return ByteString("Project post-creation script exited with non-zero error code."); + return Error::from_string_literal("Project post-creation script exited with non-zero error code."); } return {}; diff --git a/Userland/DevTools/HackStudio/ProjectTemplate.h b/Userland/DevTools/HackStudio/ProjectTemplate.h index e8075421b2..edee50348b 100644 --- a/Userland/DevTools/HackStudio/ProjectTemplate.h +++ b/Userland/DevTools/HackStudio/ProjectTemplate.h @@ -10,7 +10,6 @@ #include #include #include -#include #include #include @@ -24,7 +23,7 @@ public: explicit ProjectTemplate(ByteString const& id, ByteString const& name, ByteString const& description, const GUI::Icon& icon, int priority); - Result create_project(ByteString const& name, ByteString const& path); + ErrorOr create_project(ByteString const& name, ByteString const& path); ByteString const& id() const { return m_id; } ByteString const& name() const { return m_name; }