mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 15:12:45 +00:00 
			
		
		
		
	HackStudio: Port to Core::Stream::File :^)
				
					
				
			This commit is contained in:
		
							parent
							
								
									697d6ffb1d
								
							
						
					
					
						commit
						4784ad66b2
					
				
					 8 changed files with 72 additions and 66 deletions
				
			
		|  | @ -17,6 +17,7 @@ | ||||||
| #include <LibConfig/Client.h> | #include <LibConfig/Client.h> | ||||||
| #include <LibCore/DirIterator.h> | #include <LibCore/DirIterator.h> | ||||||
| #include <LibCore/File.h> | #include <LibCore/File.h> | ||||||
|  | #include <LibCore/Stream.h> | ||||||
| #include <LibCore/Timer.h> | #include <LibCore/Timer.h> | ||||||
| #include <LibCpp/SemanticSyntaxHighlighter.h> | #include <LibCpp/SemanticSyntaxHighlighter.h> | ||||||
| #include <LibCpp/SyntaxHighlighter.h> | #include <LibCpp/SyntaxHighlighter.h> | ||||||
|  | @ -231,14 +232,19 @@ void Editor::show_documentation_tooltip_if_available(DeprecatedString const& hov | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     dbgln_if(EDITOR_DEBUG, "opening {}", it->value); |     dbgln_if(EDITOR_DEBUG, "opening {}", it->value); | ||||||
|     auto file = Core::File::construct(it->value); |     auto file_or_error = Core::Stream::File::open(it->value, Core::Stream::OpenMode::Read); | ||||||
|     if (!file->open(Core::OpenMode::ReadOnly)) { |     if (file_or_error.is_error()) { | ||||||
|         dbgln("failed to open {}, {}", it->value, file->error_string()); |         dbgln("Failed to open {}, {}", it->value, file_or_error.error()); | ||||||
|         return; |         return; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     auto man_document = Markdown::Document::parse(file->read_all()); |     auto buffer_or_error = file_or_error.release_value()->read_until_eof(); | ||||||
|  |     if (buffer_or_error.is_error()) { | ||||||
|  |         dbgln("Couldn't read file: {}", buffer_or_error.error()); | ||||||
|  |         return; | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|  |     auto man_document = Markdown::Document::parse(buffer_or_error.release_value()); | ||||||
|     if (!man_document) { |     if (!man_document) { | ||||||
|         dbgln("failed to parse markdown"); |         dbgln("failed to parse markdown"); | ||||||
|         return; |         return; | ||||||
|  |  | ||||||
|  | @ -8,6 +8,7 @@ | ||||||
| #include "../Dialogs/Git/GitCommitDialog.h" | #include "../Dialogs/Git/GitCommitDialog.h" | ||||||
| #include "GitFilesModel.h" | #include "GitFilesModel.h" | ||||||
| #include <LibCore/File.h> | #include <LibCore/File.h> | ||||||
|  | #include <LibCore/Stream.h> | ||||||
| #include <LibDiff/Format.h> | #include <LibDiff/Format.h> | ||||||
| #include <LibGUI/Application.h> | #include <LibGUI/Application.h> | ||||||
| #include <LibGUI/BoxLayout.h> | #include <LibGUI/BoxLayout.h> | ||||||
|  | @ -156,15 +157,9 @@ void GitWidget::set_view_diff_callback(ViewDiffCallback callback) | ||||||
| void GitWidget::show_diff(DeprecatedString const& file_path) | void GitWidget::show_diff(DeprecatedString const& file_path) | ||||||
| { | { | ||||||
|     if (!m_git_repo->is_tracked(file_path)) { |     if (!m_git_repo->is_tracked(file_path)) { | ||||||
|         auto file = Core::File::construct(file_path); |         auto file = Core::Stream::File::open(file_path, Core::Stream::OpenMode::Read).release_value_but_fixme_should_propagate_errors(); | ||||||
|         if (!file->open(Core::OpenMode::ReadOnly)) { |         auto content = file->read_until_eof().release_value_but_fixme_should_propagate_errors(); | ||||||
|             perror("open"); |         m_view_diff_callback("", Diff::generate_only_additions(content)); | ||||||
|             VERIFY_NOT_REACHED(); |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         auto content = file->read_all(); |  | ||||||
|         DeprecatedString content_string((char*)content.data(), content.size()); |  | ||||||
|         m_view_diff_callback("", Diff::generate_only_additions(content_string)); |  | ||||||
|         return; |         return; | ||||||
|     } |     } | ||||||
|     auto const& original_content = m_git_repo->original_file_content(file_path); |     auto const& original_content = m_git_repo->original_file_content(file_path); | ||||||
|  |  | ||||||
|  | @ -31,6 +31,7 @@ | ||||||
| #include <LibCore/EventLoop.h> | #include <LibCore/EventLoop.h> | ||||||
| #include <LibCore/File.h> | #include <LibCore/File.h> | ||||||
| #include <LibCore/FileWatcher.h> | #include <LibCore/FileWatcher.h> | ||||||
|  | #include <LibCore/System.h> | ||||||
| #include <LibDebug/DebugSession.h> | #include <LibDebug/DebugSession.h> | ||||||
| #include <LibDesktop/Launcher.h> | #include <LibDesktop/Launcher.h> | ||||||
| #include <LibGUI/Action.h> | #include <LibGUI/Action.h> | ||||||
|  | @ -550,9 +551,9 @@ NonnullRefPtr<GUI::Action> HackStudioWidget::create_new_file_action(DeprecatedSt | ||||||
| 
 | 
 | ||||||
|         filepath = DeprecatedString::formatted("{}{}", filepath, filename); |         filepath = DeprecatedString::formatted("{}{}", filepath, filename); | ||||||
| 
 | 
 | ||||||
|         auto file = Core::File::construct(filepath); |         auto file_or_error = Core::Stream::File::open(filepath, Core::Stream::OpenMode::Write | Core::Stream::OpenMode::MustBeNew); | ||||||
|         if (!file->open((Core::OpenMode)(Core::OpenMode::WriteOnly | Core::OpenMode::MustBeNew))) { |         if (file_or_error.is_error()) { | ||||||
|             GUI::MessageBox::show(window(), DeprecatedString::formatted("Failed to create '{}'", filepath), "Error"sv, GUI::MessageBox::Type::Error); |             GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Failed to create '{}': {}", filepath, file_or_error.error())); | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|         open_file(filepath); |         open_file(filepath); | ||||||
|  | @ -1731,21 +1732,31 @@ NonnullRefPtr<GUI::Action> HackStudioWidget::create_open_project_configuration_a | ||||||
|         auto parent_directory = LexicalPath::dirname(Project::config_file_path); |         auto parent_directory = LexicalPath::dirname(Project::config_file_path); | ||||||
|         auto absolute_config_file_path = LexicalPath::absolute_path(m_project->root_path(), Project::config_file_path); |         auto absolute_config_file_path = LexicalPath::absolute_path(m_project->root_path(), Project::config_file_path); | ||||||
| 
 | 
 | ||||||
|         if (!Core::File::exists(absolute_config_file_path)) { |         DeprecatedString formatted_error_string_holder; | ||||||
|  |         auto save_configuration_or_error = [&]() -> ErrorOr<void> { | ||||||
|  |             if (Core::File::exists(absolute_config_file_path)) | ||||||
|  |                 return {}; | ||||||
|  | 
 | ||||||
|             if (Core::File::exists(parent_directory) && !Core::File::is_directory(parent_directory)) { |             if (Core::File::exists(parent_directory) && !Core::File::is_directory(parent_directory)) { | ||||||
|                 GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Cannot create the '{}' directory because there is already a file with that name", parent_directory)); |                 formatted_error_string_holder = DeprecatedString::formatted("Cannot create directory the '{}' directory because there is already a file with that name", parent_directory); | ||||||
|                 return; |                 return Error::from_string_view(formatted_error_string_holder); | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             mkdir(LexicalPath::absolute_path(m_project->root_path(), parent_directory).characters(), 0755); |             auto maybe_error = Core::System::mkdir(LexicalPath::absolute_path(m_project->root_path(), parent_directory), 0755); | ||||||
|  |             if (maybe_error.is_error() && maybe_error.error().code() != EEXIST) | ||||||
|  |                 return maybe_error.error(); | ||||||
| 
 | 
 | ||||||
|             auto file = Core::File::open(absolute_config_file_path, Core::OpenMode::WriteOnly); |             auto file = TRY(Core::Stream::File::open(absolute_config_file_path, Core::Stream::OpenMode::Write)); | ||||||
|             file.value()->write( |             TRY(file->write_entire_buffer( | ||||||
|                 "{\n" |                 "{\n" | ||||||
|                 "    \"build_command\": \"your build command here\",\n" |                 "    \"build_command\": \"your build command here\",\n" | ||||||
|                 "    \"run_command\": \"your run command here\"\n" |                 "    \"run_command\": \"your run command here\"\n" | ||||||
|                 "}\n"sv); |                 "}\n"sv.bytes())); | ||||||
|             file.value()->close(); |             return {}; | ||||||
|  |         }(); | ||||||
|  |         if (save_configuration_or_error.is_error()) { | ||||||
|  |             GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Saving configuration failed: {}.", save_configuration_or_error.error())); | ||||||
|  |             return; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         open_file(Project::config_file_path); |         open_file(Project::config_file_path); | ||||||
|  |  | ||||||
|  | @ -8,7 +8,6 @@ | ||||||
| 
 | 
 | ||||||
| #include <AK/LexicalPath.h> | #include <AK/LexicalPath.h> | ||||||
| #include <AK/NonnullRefPtr.h> | #include <AK/NonnullRefPtr.h> | ||||||
| #include <LibCore/File.h> |  | ||||||
| 
 | 
 | ||||||
| namespace LanguageServers { | namespace LanguageServers { | ||||||
| 
 | 
 | ||||||
|  | @ -37,10 +36,12 @@ Optional<DeprecatedString> FileDB::get_or_read_from_filesystem(StringView filena | ||||||
|     if (document) |     if (document) | ||||||
|         return document->text(); |         return document->text(); | ||||||
| 
 | 
 | ||||||
|     document = create_from_filesystem(absolute_path); |     auto document_or_error = create_from_filesystem(absolute_path); | ||||||
|     if (document) |     if (document_or_error.is_error()) { | ||||||
|         return document->text(); |         dbgln("Failed to create document '{}': {}", absolute_path, document_or_error.error()); | ||||||
|     return {}; |         return {}; | ||||||
|  |     } | ||||||
|  |     return document_or_error.value()->text(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| bool FileDB::is_open(DeprecatedString const& filename) const | bool FileDB::is_open(DeprecatedString const& filename) const | ||||||
|  | @ -50,11 +51,13 @@ bool FileDB::is_open(DeprecatedString const& filename) const | ||||||
| 
 | 
 | ||||||
| bool FileDB::add(DeprecatedString const& filename, int fd) | bool FileDB::add(DeprecatedString const& filename, int fd) | ||||||
| { | { | ||||||
|     auto document = create_from_fd(fd); |     auto document_or_error = create_from_fd(fd); | ||||||
|     if (!document) |     if (document_or_error.is_error()) { | ||||||
|  |         dbgln("Failed to create document: {}", document_or_error.error()); | ||||||
|         return false; |         return false; | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|     m_open_files.set(to_absolute_path(filename), document.release_nonnull()); |     m_open_files.set(to_absolute_path(filename), document_or_error.release_value()); | ||||||
|     return true; |     return true; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -68,26 +71,16 @@ DeprecatedString FileDB::to_absolute_path(DeprecatedString const& filename) cons | ||||||
|     return LexicalPath { DeprecatedString::formatted("{}/{}", m_project_root, filename) }.string(); |     return LexicalPath { DeprecatedString::formatted("{}/{}", m_project_root, filename) }.string(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| RefPtr<GUI::TextDocument> FileDB::create_from_filesystem(DeprecatedString const& filename) const | ErrorOr<NonnullRefPtr<GUI::TextDocument>> FileDB::create_from_filesystem(DeprecatedString const& filename) const | ||||||
| { | { | ||||||
|     auto file = Core::File::open(to_absolute_path(filename), Core::OpenMode::ReadOnly); |     auto file = TRY(Core::Stream::File::open(to_absolute_path(filename), Core::Stream::OpenMode::Read)); | ||||||
|     if (file.is_error()) { |     return create_from_file(move(file)); | ||||||
|         dbgln("failed to create document for {} from filesystem", filename); |  | ||||||
|         return nullptr; |  | ||||||
|     } |  | ||||||
|     return create_from_file(*file.value()); |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| RefPtr<GUI::TextDocument> FileDB::create_from_fd(int fd) const | ErrorOr<NonnullRefPtr<GUI::TextDocument>> FileDB::create_from_fd(int fd) const | ||||||
| { | { | ||||||
|     auto file = Core::File::construct(); |     auto file = TRY(Core::Stream::File::adopt_fd(fd, Core::Stream::OpenMode::Read)); | ||||||
|     if (!file->open(fd, Core::OpenMode::ReadOnly, Core::File::ShouldCloseFileDescriptor::Yes)) { |     return create_from_file(move(file)); | ||||||
|         errno = file->error(); |  | ||||||
|         perror("open"); |  | ||||||
|         dbgln("Failed to open project file"); |  | ||||||
|         return nullptr; |  | ||||||
|     } |  | ||||||
|     return create_from_file(*file); |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| class DefaultDocumentClient final : public GUI::TextDocument::Client { | class DefaultDocumentClient final : public GUI::TextDocument::Client { | ||||||
|  | @ -107,12 +100,11 @@ public: | ||||||
| }; | }; | ||||||
| static DefaultDocumentClient s_default_document_client; | static DefaultDocumentClient s_default_document_client; | ||||||
| 
 | 
 | ||||||
| RefPtr<GUI::TextDocument> FileDB::create_from_file(Core::File& file) const | ErrorOr<NonnullRefPtr<GUI::TextDocument>> FileDB::create_from_file(NonnullOwnPtr<Core::Stream::File> file) const | ||||||
| { | { | ||||||
|     auto content = file.read_all(); |     auto content = TRY(file->read_until_eof()); | ||||||
|     StringView content_view(content); |  | ||||||
|     auto document = GUI::TextDocument::create(&s_default_document_client); |     auto document = GUI::TextDocument::create(&s_default_document_client); | ||||||
|     document->set_text(content_view); |     document->set_text(content); | ||||||
|     return document; |     return document; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -10,6 +10,7 @@ | ||||||
| #include <AK/HashMap.h> | #include <AK/HashMap.h> | ||||||
| #include <AK/NonnullRefPtr.h> | #include <AK/NonnullRefPtr.h> | ||||||
| #include <LibCodeComprehension/FileDB.h> | #include <LibCodeComprehension/FileDB.h> | ||||||
|  | #include <LibCore/Stream.h> | ||||||
| #include <LibGUI/TextDocument.h> | #include <LibGUI/TextDocument.h> | ||||||
| 
 | 
 | ||||||
| namespace LanguageServers { | namespace LanguageServers { | ||||||
|  | @ -31,9 +32,9 @@ public: | ||||||
|     bool is_open(DeprecatedString const& filename) const; |     bool is_open(DeprecatedString const& filename) const; | ||||||
| 
 | 
 | ||||||
| private: | private: | ||||||
|     RefPtr<GUI::TextDocument> create_from_filesystem(DeprecatedString const& filename) const; |     ErrorOr<NonnullRefPtr<GUI::TextDocument>> create_from_filesystem(DeprecatedString const& filename) const; | ||||||
|     RefPtr<GUI::TextDocument> create_from_fd(int fd) const; |     ErrorOr<NonnullRefPtr<GUI::TextDocument>> create_from_fd(int fd) const; | ||||||
|     RefPtr<GUI::TextDocument> create_from_file(Core::File&) const; |     ErrorOr<NonnullRefPtr<GUI::TextDocument>> create_from_file(NonnullOwnPtr<Core::Stream::File>) const; | ||||||
|     static RefPtr<GUI::TextDocument> create_with_content(DeprecatedString const&); |     static RefPtr<GUI::TextDocument> create_with_content(DeprecatedString const&); | ||||||
| 
 | 
 | ||||||
| private: | private: | ||||||
|  |  | ||||||
|  | @ -8,6 +8,7 @@ | ||||||
| #include <AK/LexicalPath.h> | #include <AK/LexicalPath.h> | ||||||
| #include <LibCore/Command.h> | #include <LibCore/Command.h> | ||||||
| #include <LibCore/File.h> | #include <LibCore/File.h> | ||||||
|  | #include <LibCore/Stream.h> | ||||||
| #include <LibRegex/Regex.h> | #include <LibRegex/Regex.h> | ||||||
| #include <fcntl.h> | #include <fcntl.h> | ||||||
| #include <sys/stat.h> | #include <sys/stat.h> | ||||||
|  | @ -111,7 +112,8 @@ ErrorOr<void> ProjectBuilder::build_serenity_component() | ||||||
| 
 | 
 | ||||||
| ErrorOr<DeprecatedString> ProjectBuilder::component_name(StringView cmake_file_path) | ErrorOr<DeprecatedString> ProjectBuilder::component_name(StringView cmake_file_path) | ||||||
| { | { | ||||||
|     auto content = TRY(Core::File::open(cmake_file_path, Core::OpenMode::ReadOnly))->read_all(); |     auto file = TRY(Core::Stream::File::open(cmake_file_path, Core::Stream::OpenMode::Read)); | ||||||
|  |     auto content = TRY(file->read_until_eof()); | ||||||
| 
 | 
 | ||||||
|     static Regex<ECMA262> const component_name(R"~~~(serenity_component\([\s]*(\w+)[\s\S]*\))~~~"); |     static Regex<ECMA262> const component_name(R"~~~(serenity_component\([\s]*(\w+)[\s\S]*\))~~~"); | ||||||
|     RegexResult result; |     RegexResult result; | ||||||
|  | @ -133,8 +135,8 @@ ErrorOr<void> ProjectBuilder::initialize_build_directory() | ||||||
|     if (Core::File::exists(cmake_file_path)) |     if (Core::File::exists(cmake_file_path)) | ||||||
|         MUST(Core::File::remove(cmake_file_path, Core::File::RecursionMode::Disallowed, false)); |         MUST(Core::File::remove(cmake_file_path, Core::File::RecursionMode::Disallowed, false)); | ||||||
| 
 | 
 | ||||||
|     auto cmake_file = TRY(Core::File::open(cmake_file_path, Core::OpenMode::WriteOnly)); |     auto cmake_file = TRY(Core::Stream::File::open(cmake_file_path, Core::Stream::OpenMode::Write)); | ||||||
|     cmake_file->write(generate_cmake_file_content()); |     TRY(cmake_file->write_entire_buffer(generate_cmake_file_content().bytes())); | ||||||
| 
 | 
 | ||||||
|     TRY(m_terminal->run_command(DeprecatedString::formatted("cmake -S {} -DHACKSTUDIO_BUILD=ON -DHACKSTUDIO_BUILD_CMAKE_FILE={}" |     TRY(m_terminal->run_command(DeprecatedString::formatted("cmake -S {} -DHACKSTUDIO_BUILD=ON -DHACKSTUDIO_BUILD_CMAKE_FILE={}" | ||||||
|                                                             " -DENABLE_UNICODE_DATABASE_DOWNLOAD=OFF", |                                                             " -DENABLE_UNICODE_DATABASE_DOWNLOAD=OFF", | ||||||
|  |  | ||||||
|  | @ -6,7 +6,7 @@ | ||||||
| 
 | 
 | ||||||
| #include "ProjectConfig.h" | #include "ProjectConfig.h" | ||||||
| #include <AK/NonnullOwnPtr.h> | #include <AK/NonnullOwnPtr.h> | ||||||
| #include <LibCore/File.h> | #include <LibCore/Stream.h> | ||||||
| 
 | 
 | ||||||
| namespace HackStudio { | namespace HackStudio { | ||||||
| 
 | 
 | ||||||
|  | @ -17,11 +17,10 @@ ProjectConfig::ProjectConfig(JsonObject config) | ||||||
| 
 | 
 | ||||||
| ErrorOr<NonnullOwnPtr<ProjectConfig>> ProjectConfig::try_load_project_config(DeprecatedString path) | ErrorOr<NonnullOwnPtr<ProjectConfig>> ProjectConfig::try_load_project_config(DeprecatedString path) | ||||||
| { | { | ||||||
|     auto file = TRY(Core::File::open(path, Core::OpenMode::ReadOnly)); |     auto file = TRY(Core::Stream::File::open(path, Core::Stream::OpenMode::Read)); | ||||||
|     auto file_contents = file->read_all(); |     auto file_contents = TRY(file->read_until_eof()); | ||||||
|     file->close(); |  | ||||||
| 
 | 
 | ||||||
|     auto json = TRY(JsonValue::from_string(StringView { file_contents })); |     auto json = TRY(JsonValue::from_string(file_contents)); | ||||||
|     if (!json.is_object()) |     if (!json.is_object()) | ||||||
|         return Error::from_string_literal("The topmost JSON element is not an object"); |         return Error::from_string_literal("The topmost JSON element is not an object"); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -5,7 +5,7 @@ | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| #include "ProjectFile.h" | #include "ProjectFile.h" | ||||||
| #include <LibCore/File.h> | #include <LibCore/Stream.h> | ||||||
| 
 | 
 | ||||||
| namespace HackStudio { | namespace HackStudio { | ||||||
| 
 | 
 | ||||||
|  | @ -54,7 +54,7 @@ void ProjectFile::create_document_if_needed() const | ||||||
|         return; |         return; | ||||||
| 
 | 
 | ||||||
|     m_document = CodeDocument::create(m_name); |     m_document = CodeDocument::create(m_name); | ||||||
|     auto file_or_error = Core::File::open(m_name, Core::OpenMode::ReadOnly); |     auto file_or_error = Core::Stream::File::open(m_name, Core::Stream::OpenMode::Read); | ||||||
|     if (file_or_error.is_error()) { |     if (file_or_error.is_error()) { | ||||||
|         warnln("Couldn't open '{}': {}", m_name, file_or_error.error()); |         warnln("Couldn't open '{}': {}", m_name, file_or_error.error()); | ||||||
|         // This is okay though, we'll just go with an empty document and create the file when saving.
 |         // This is okay though, we'll just go with an empty document and create the file when saving.
 | ||||||
|  | @ -62,7 +62,7 @@ void ProjectFile::create_document_if_needed() const | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     auto& file = *file_or_error.value(); |     auto& file = *file_or_error.value(); | ||||||
|     m_could_render_text = m_document->set_text(file.read_all()); |     m_could_render_text = m_document->set_text(file.read_until_eof().release_value_but_fixme_should_propagate_errors()); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Karol Kosek
						Karol Kosek