From 10658b28017ea7db4db807061f055431fbd06310 Mon Sep 17 00:00:00 2001 From: mlangkabel Date: Fri, 5 Oct 2018 13:32:25 +0200 Subject: [PATCH] ui: display multi-line maven error messages (issue #622) --- src/lib/utility/text/TextAccess.cpp | 5 ++ src/lib/utility/text/TextAccess.h | 1 + .../QtProjectWizzardContentPath.cpp | 14 +--- src/lib_java/project/SourceGroupJavaMaven.cpp | 25 +++--- src/lib_java/utility/utilityMaven.cpp | 80 ++++++++++++------- src/lib_java/utility/utilityMaven.h | 2 +- 6 files changed, 71 insertions(+), 56 deletions(-) diff --git a/src/lib/utility/text/TextAccess.cpp b/src/lib/utility/text/TextAccess.cpp index 1ec2bcaa..6b01b05b 100644 --- a/src/lib/utility/text/TextAccess.cpp +++ b/src/lib/utility/text/TextAccess.cpp @@ -75,6 +75,11 @@ unsigned int TextAccess::getLineCount() const return m_lines.size(); } +bool TextAccess::isEmpty() const +{ + return m_lines.empty(); +} + FilePath TextAccess::getFilePath() const { return m_filePath; diff --git a/src/lib/utility/text/TextAccess.h b/src/lib/utility/text/TextAccess.h index eb8c2c07..110456f4 100644 --- a/src/lib/utility/text/TextAccess.h +++ b/src/lib/utility/text/TextAccess.h @@ -16,6 +16,7 @@ public: virtual ~TextAccess(); unsigned int getLineCount() const; + bool isEmpty() const; FilePath getFilePath() const; diff --git a/src/lib_gui/qt/window/project_wizzard/QtProjectWizzardContentPath.cpp b/src/lib_gui/qt/window/project_wizzard/QtProjectWizzardContentPath.cpp index 6efceba1..819d1ea1 100644 --- a/src/lib_gui/qt/window/project_wizzard/QtProjectWizzardContentPath.cpp +++ b/src/lib_gui/qt/window/project_wizzard/QtProjectWizzardContentPath.cpp @@ -563,17 +563,11 @@ std::vector QtProjectWizzardContentPathSourceMaven::getFilePaths() con dialogView->setParentWindow(m_window); dialogView->showUnknownProgressDialog(L"Preparing Project", L"Maven\nGenerating Source Files"); - const bool success = utility::mavenGenerateSources(mavenPath, mavenProjectRoot); - if (!success) + const std::wstring errorMessage = utility::mavenGenerateSources(mavenPath, mavenProjectRoot); + if (!errorMessage.empty()) { - const std::wstring dialogMessage = - L"Sourcetrail was unable to locate Maven on this machine.\n" - "Please make sure to provide the correct Maven Path in the preferences."; - - MessageStatus(dialogMessage, true, false).dispatch(); - - Application::getInstance()->handleDialog(dialogMessage); - + MessageStatus(errorMessage, true, false).dispatch(); + Application::getInstance()->handleDialog(errorMessage); return std::vector(); } } diff --git a/src/lib_java/project/SourceGroupJavaMaven.cpp b/src/lib_java/project/SourceGroupJavaMaven.cpp index 10ccd3ff..3956a072 100644 --- a/src/lib_java/project/SourceGroupJavaMaven.cpp +++ b/src/lib_java/project/SourceGroupJavaMaven.cpp @@ -94,26 +94,19 @@ bool SourceGroupJavaMaven::prepareMavenData() dialogView->hideUnknownProgressDialog(); }); - bool success = utility::mavenGenerateSources(mavenPath, projectRootPath); - - if (!success) + const std::wstring errorMessage = utility::mavenGenerateSources(mavenPath, projectRootPath); + if (!errorMessage.empty()) { - const std::wstring dialogMessage = - L"Sourcetrail was unable to locate Maven on this machine.\n" - "Please make sure to provide the correct Maven Path in the preferences."; - - MessageStatus(dialogMessage, true, false).dispatch(); - Application::getInstance()->handleDialog(dialogMessage); + MessageStatus(errorMessage, true, false).dispatch(); + Application::getInstance()->handleDialog(errorMessage); + return false; } - if (success) - { - dialogView->showUnknownProgressDialog(L"Preparing Project", L"Maven\nExporting Dependencies"); + dialogView->showUnknownProgressDialog(L"Preparing Project", L"Maven\nExporting Dependencies"); - success = utility::mavenCopyDependencies( - mavenPath, projectRootPath, m_settings->getMavenDependenciesDirectoryExpandedAndAbsolute() - ); - } + bool success = utility::mavenCopyDependencies( + mavenPath, projectRootPath, m_settings->getMavenDependenciesDirectoryExpandedAndAbsolute() + ); return success; } diff --git a/src/lib_java/utility/utilityMaven.cpp b/src/lib_java/utility/utilityMaven.cpp index d6409da2..76868f8b 100644 --- a/src/lib_java/utility/utilityMaven.cpp +++ b/src/lib_java/utility/utilityMaven.cpp @@ -58,48 +58,76 @@ namespace putenv(const_cast(("JAVA_HOME=" + javaHomePath.str()).c_str())); } } + + std::wstring getErrorMessageFromMavenOutput(std::shared_ptr mavenOutput) + { + const std::string errorPrefix = "[ERROR]"; + const std::string fatalPrefix = "[FATAL]"; + + std::wstring errorMessage; + + for (const std::string& line : mavenOutput->getAllLines()) + { + const std::string trimmedLine = utility::trim(line); + + if (utility::isPrefix(errorPrefix, trimmedLine)) + { + errorMessage += utility::decodeFromUtf8(utility::trim(trimmedLine.substr(errorPrefix.size())) + "\n"); + } + else if (utility::isPrefix(fatalPrefix, trimmedLine)) + { + errorMessage += utility::decodeFromUtf8(trimmedLine + "\n"); + } + } + + if (!errorMessage.empty()) + { + errorMessage = L"The following error occurred while executing a Maven command:\n\n" + errorMessage; + } + + return errorMessage; + } } namespace utility { - bool mavenGenerateSources(const FilePath& mavenPath, const FilePath& projectDirectoryPath) + std::wstring mavenGenerateSources(const FilePath& mavenPath, const FilePath& projectDirectoryPath) { setJavaHomeVariableIfNotExists(); - const std::string output = utility::executeProcessUntilNoOutput( + std::shared_ptr outputAccess = TextAccess::createFromString(utility::executeProcessUntilNoOutput( "\"" + mavenPath.str() + "\" generate-sources", projectDirectoryPath, 60000 - ); - return !output.empty(); + )); + + if (outputAccess->isEmpty()) + { + return L"Sourcetrail was unable to locate Maven on this machine.\nPlease make sure to provide the correct Maven Path in the preferences."; + } + + return getErrorMessageFromMavenOutput(outputAccess); } bool mavenCopyDependencies(const FilePath& mavenPath, const FilePath& projectDirectoryPath, const FilePath& outputDirectoryPath) { setJavaHomeVariableIfNotExists(); - const std::string output = utility::executeProcessUntilNoOutput( + std::shared_ptr outputAccess = TextAccess::createFromString(utility::executeProcessUntilNoOutput( "\"" + mavenPath.str() + "\" dependency:copy-dependencies -DoutputDirectory=" + outputDirectoryPath.str(), projectDirectoryPath, 60000 - ); + )); - std::shared_ptr outputAccess = TextAccess::createFromString(output); - for (const std::string& line: outputAccess->getAllLines()) + const std::wstring errorMessage = getErrorMessageFromMavenOutput(outputAccess); + if (!errorMessage.empty()) { - if (utility::isPrefix("[ERROR]", utility::trim(line))) - { - // TODO: move error handling to caller of this function - const std::wstring dialogMessage = - L"The following error occurred while executing a Maven command:\n\n" + - utility::decodeFromUtf8(utility::replace(line, "\r\n", "\n")); - MessageStatus(dialogMessage, true, false).dispatch(); - Application::getInstance()->handleDialog(dialogMessage); - return false; - } + MessageStatus(errorMessage, true, false).dispatch(); + Application::getInstance()->handleDialog(errorMessage); + return false; } - return !output.empty(); + return !outputAccess->isEmpty(); } std::vector mavenGetAllDirectoriesFromEffectivePom(const FilePath& mavenPath, const FilePath& projectDirectoryPath, bool addTestDirectories) @@ -112,17 +140,11 @@ namespace utility 60000 )); - if (outputAccess->getLineCount() > 0 && utility::isPrefix("Error", utility::trim(outputAccess->getLine(1)))) + const std::wstring errorMessage = getErrorMessageFromMavenOutput(outputAccess); + if (!errorMessage.empty()) { - // TODO: move error handling to caller of this function - const std::wstring dialogMessage = - L"The following error occurred while executing a Maven command:\n\n" + - utility::decodeFromUtf8(utility::replace(outputAccess->getText(), "\r\n", "\n")); - - MessageStatus(dialogMessage, true, false).dispatch(); - - Application::getInstance()->handleDialog(dialogMessage); - + MessageStatus(errorMessage, true, false).dispatch(); + Application::getInstance()->handleDialog(errorMessage); return std::vector(); } diff --git a/src/lib_java/utility/utilityMaven.h b/src/lib_java/utility/utilityMaven.h index feddc1f1..9965b0fb 100644 --- a/src/lib_java/utility/utilityMaven.h +++ b/src/lib_java/utility/utilityMaven.h @@ -7,7 +7,7 @@ class FilePath; namespace utility { - bool mavenGenerateSources(const FilePath& mavenPath, const FilePath& projectDirectoryPath); + std::wstring mavenGenerateSources(const FilePath& mavenPath, const FilePath& projectDirectoryPath); bool mavenCopyDependencies(const FilePath& mavenPath, const FilePath& projectDirectoryPath, const FilePath& outputDirectoryPath); std::vector mavenGetAllDirectoriesFromEffectivePom(const FilePath& mavenPath, const FilePath& projectDirectoryPath, bool addTestDirectories); }