From a75939cd020bc9e4b58c93e2754e0356ef1336a6 Mon Sep 17 00:00:00 2001 From: Tobias Nett Date: Wed, 30 Oct 2019 20:15:11 +0100 Subject: [PATCH] Improve logging in module update script Adds colored logging to `groovyw module update-all` and restructures the logging output a bit. Overall, the noise is reduced, and the logged messages now contain info about the branch that is currently checked out. --- config/groovy/common.groovy | 72 +++++++++++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 14 deletions(-) diff --git a/config/groovy/common.groovy b/config/groovy/common.groovy index 9f322f71f63..c712bcb73ab 100644 --- a/config/groovy/common.groovy +++ b/config/groovy/common.groovy @@ -10,6 +10,8 @@ import org.ajoberstar.grgit.exception.GrgitException import org.ajoberstar.grgit.Remote import org.eclipse.jgit.errors.RepositoryNotFoundException +import static Ansi.* + class common { /** For preparing an instance of the target item type utility class we want to work with */ @@ -185,10 +187,9 @@ class common { * @param itemName the name of the item to update */ def updateItem(String itemName) { - println "Attempting to update $itemType $itemName" File targetDir = new File(targetDirectory, itemName) if (!targetDir.exists()) { - println "$itemType \"$itemName\" not found" + println color("$itemType \"$itemName\" not found", Ansi.RED) return } try { @@ -200,26 +201,28 @@ class common { it.name == defaultRemote }?.url if (targetUrl == null || !isUrlValid(targetUrl)) { - println "While updating $itemName found its '$defaultRemote' remote invalid or its URL unresponsive: $targetUrl" + println color("While updating $itemName found its '$defaultRemote' remote invalid or its URL unresponsive: $targetUrl", Ansi.RED) return } // At this point we should have a valid remote to pull from. If local repo is clean then pull! def clean = itemGit.status().clean - println "Is \"$itemName\" clean? $clean" - if (!clean) { - println "$itemType has uncommitted changes. Skipping." - return - } - println "Updating $itemType $itemName" + def branchName = itemGit.branch.getCurrent().fullName + + print "$itemType '$itemName' [$branchName]: " - try { - itemGit.pull remote: defaultRemote - } catch (GrgitException exception) { - println "Unable to update $itemName, Skipping: ${exception.getMessage()}" + if (!clean) { + println color("uncommitted changes. Skipping.", Ansi.YELLOW) + } else { + println color("updating $itemType $itemName", Ansi.GREEN) + try { + itemGit.pull remote: defaultRemote + } catch (GrgitException exception) { + println color("Unable to update $itemName, Skipping: ${exception.getMessage()}", Ansi.RED) + } } } catch(RepositoryNotFoundException exception) { - println "Skipping update for $itemName: no repository found (probably engine module)" + println color("Skipping update for $itemName: no repository found (probably engine module)", Ansi.LIGHT_YELLOW) } } @@ -437,3 +440,44 @@ class common { itemListCached = false } } + +/** + * Small ANSI coloring utility. + * @see https://gist.github.com/tvinke/db4d21dfdbdae49e6f92dcf1ca6120de + * @see http://www.bluesock.org/~willg/dev/ansi.html + * @see https://gist.github.com/dainkaplan/4651352 + */ +class Ansi { + + static final String NORMAL = "\u001B[0m" + + static final String BOLD = "\u001B[1m" + static final String ITALIC = "\u001B[3m" + static final String UNDERLINE = "\u001B[4m" + static final String BLINK = "\u001B[5m" + static final String RAPID_BLINK = "\u001B[6m" + static final String REVERSE_VIDEO = "\u001B[7m" + static final String INVISIBLE_TEXT = "\u001B[8m" + + static final String BLACK = "\u001B[30m" + static final String RED = "\u001B[31m" + static final String GREEN = "\u001B[32m" + static final String YELLOW = "\u001B[33m" + static final String BLUE = "\u001B[34m" + static final String MAGENTA = "\u001B[35m" + static final String CYAN = "\u001B[36m" + static final String WHITE = "\u001B[37m" + + static final String DARK_GRAY = "\u001B[1;30m" + static final String LIGHT_RED = "\u001B[1;31m" + static final String LIGHT_GREEN = "\u001B[1;32m" + static final String LIGHT_YELLOW = "\u001B[1;33m" + static final String LIGHT_BLUE = "\u001B[1;34m" + static final String LIGHT_PURPLE = "\u001B[1;35m" + static final String LIGHT_CYAN = "\u001B[1;36m" + + static String color(String text, String ansiValue) { + ansiValue + text + NORMAL + } + +} \ No newline at end of file