From 7fd0b879dbf7cc032e3b67957fdaef5104fbd35a Mon Sep 17 00:00:00 2001 From: Noel Tautges Date: Thu, 30 May 2024 14:06:24 -0500 Subject: [PATCH 1/2] Track custom action usage using a queue Each action used is checked for new custom action uses. This makes it so custom actions that are only referenced within other custom actions are marked as used and included in the final program. --- custom_actions.go | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/custom_actions.go b/custom_actions.go index 2a727ed..2870f93 100644 --- a/custom_actions.go +++ b/custom_actions.go @@ -115,16 +115,26 @@ func collectParameterDefinitions() (arguments []parameterDefinition) { func checkCustomActionUsage() { var actionUsageRegex = regexp.MustCompile(`(action )?([a-zA-Z0-9]+)\(`) - var matches = actionUsageRegex.FindAllStringSubmatch(contents, -1) - if len(matches) == 0 { - return - } - for _, match := range matches { - var ref = strings.TrimSpace(match[2]) - if _, found := customActions[ref]; found { - customActions[ref].used = true + + customActions[""] = &customAction{body: contents} + + var actionQueue = []string{""} + for len(actionQueue) > 0 { + var matches = actionUsageRegex.FindAllStringSubmatch(customActions[actionQueue[0]].body, -1) + actionQueue = actionQueue[1:] + if len(matches) == 0 { + continue + } + for _, match := range matches { + var ref = strings.TrimSpace(match[2]) + if _, found := customActions[ref]; found && !customActions[ref].used { + customActions[ref].used = true + actionQueue = append(actionQueue, ref) + } } } + + delete(customActions, "") } func makeCustomActionsHeader() { From 84758454c417d25e625f510f346ff78e9e813c35 Mon Sep 17 00:00:00 2001 From: Noel Tautges Date: Sat, 1 Jun 2024 09:36:42 -0500 Subject: [PATCH 2/2] Check custom action usage with the loop outside of the checking function --- custom_actions.go | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/custom_actions.go b/custom_actions.go index 2870f93..ffdc508 100644 --- a/custom_actions.go +++ b/custom_actions.go @@ -7,9 +7,10 @@ package main import ( "encoding/json" "fmt" - "github.com/electrikmilk/args-parser" "regexp" "strings" + + "github.com/electrikmilk/args-parser" ) // customAction contains the collected declaration of a custom action. @@ -43,7 +44,12 @@ func parseCustomActions() { resetParse() - checkCustomActionUsage() + checkCustomActionUsage(contents) + for _, action := range customActions { + if strings.ContainsAny(action.body, "()") { + checkCustomActionUsage(action.body) + } + } makeCustomActionsHeader() if args.Using("debug") { @@ -113,28 +119,18 @@ func collectParameterDefinitions() (arguments []parameterDefinition) { return } -func checkCustomActionUsage() { +func checkCustomActionUsage(content string) { var actionUsageRegex = regexp.MustCompile(`(action )?([a-zA-Z0-9]+)\(`) - - customActions[""] = &customAction{body: contents} - - var actionQueue = []string{""} - for len(actionQueue) > 0 { - var matches = actionUsageRegex.FindAllStringSubmatch(customActions[actionQueue[0]].body, -1) - actionQueue = actionQueue[1:] - if len(matches) == 0 { - continue - } - for _, match := range matches { - var ref = strings.TrimSpace(match[2]) - if _, found := customActions[ref]; found && !customActions[ref].used { - customActions[ref].used = true - actionQueue = append(actionQueue, ref) - } + var matches = actionUsageRegex.FindAllStringSubmatch(content, -1) + if len(matches) == 0 { + return + } + for _, match := range matches { + var ref = strings.TrimSpace(match[2]) + if _, found := customActions[ref]; found { + customActions[ref].used = true } } - - delete(customActions, "") } func makeCustomActionsHeader() {