Skip to content

Commit

Permalink
Merge branch 'release/1.1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
acanewby committed Jun 17, 2023
2 parents 6e38282 + 30be4ee commit 83f4625
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 28 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# CHANGELOG

## 1.1,1 (2023JUN17)

### New Features

* NONE

### Enhancements

* NONE

### Bugfixes

* [#5](https://github.com/acanewby/patrick/issues/5) - Incorrect handling of already-encountered resources
* [#7](https://github.com/acanewby/patrick/issues/7) - String literals in block comments should be ignored

## 1.1.0 (2023JUN14)

### New Features
Expand Down
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ __Output:__ out/mypackage/mypackage.resource
The literals have been identified, extracted, and associated with resource tokens.
```go
```
Resource_00010001 = "Saving analysis config: %+v"
Resource_00010002 = "Config key: %s"
```
Expand Down Expand Up @@ -88,7 +88,7 @@ func (cfg *Service) CreateOrUpdateAnalysisConfig(conf AnalysisConfig) {
* File format for the `excludesFile` is like this (e.g. one unqualified filename per line):
```shell
```
resource.go
resources.go
types.go
Expand All @@ -104,7 +104,7 @@ constants.go
Allows you to get a listing of the files that would be processed. Useful to test your input directory / exclusions configuration.
```shell
```
./patrick list --help   feature/implement-convert ● ? ↑1

Lists all files that will be targeted for processing, taking into account inclusion and exclusion criteria.
Expand All @@ -121,7 +121,7 @@ Global Flags:
--logLevel string log level (debug,info,warn,error,fatal) (default "info")
```
```shell
```
./patrick list --inputDir=/Users/Anewby/Dropbox/scratch/patrick/input --excludeFiles=/Users/Anewby/Dropbox/scratch/patrick/exclude.list
================================================================================
Input directory : /Users/Anewby/Dropbox/scratch/patrick/input
Expand Down Expand Up @@ -162,7 +162,7 @@ Processes files according to `inputDir` and `excludesFile`, as described above.
It identifies string literals and substitutes them in the output file with tokens recorded in the corresponding package-specific resource file.
```shell
```
./patrick convert --help
Processes the identified list of files and:
Expand Down Expand Up @@ -196,7 +196,7 @@ Global Flags:
The built-in defaults produce a resource file format, usable in a variety of contexts:
```shell
```
./patrick convert --inputDir=/Users/acanewby/Dropbox/scratch/patrick/input \
--outputDir=/Users/acanewby/Dropbox/scratch/patrick/output --overwriteOutput=true \
--excludeFiles=/Users/acanewby/Dropbox/scratch/patrick/exclude.list \
Expand Down Expand Up @@ -242,8 +242,9 @@ Processing file: /Users/acanewby/Dropbox/scratch/patrick/input/util/util.go
====================================================================================================
```
```shell
cat /Users/acanewby/Dropbox/scratch/patrick/output/config/config.resource
```
cat /Users/acanewby/Dropbox/scratch/patrick/output/config/config.resource

Resource_00010001 = "Saving analysis config: %+v"
Resource_00010002 = "Config key: %s"
<snip/>
Expand All @@ -256,7 +257,7 @@ Resource_00010063 = "Resetting config"
However, it is also possible to generate a wide variety of resource file formats:
```shell
```
./patrick convert --inputDir=/Users/acanewby/Dropbox/scratch/patrick/input \
--outputDir=/Users/acanewby/Dropbox/scratch/patrick/output --overwriteOutput=true \
--excludeFiles=/Users/acanewby/Dropbox/scratch/patrick/exclude.list \
Expand Down Expand Up @@ -305,8 +306,9 @@ Processing file: /Users/acanewby/Dropbox/scratch/patrick/input/util/util.go
```
```shell
cat /Users/acanewby/Dropbox/scratch/patrick/output/config/config.resource \
```
cat /Users/acanewby/Dropbox/scratch/patrick/output/config/config.resource
{Res#001001, "Saving analysis config: %+v"},
{Res#001002, "Config key: %s"},
{Res#001003, "%s.%s"},
Expand Down
4 changes: 2 additions & 2 deletions internal/common/resourceInventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ func (i *ResourceInventory) GetIndexForResource(pkg string, res string) (uint64,

// If not, remember it
if found {
LogDebugf(LogTemplateResourceFound, key, res)
LogDebugf(LogTemplateResourceFound, index, key)
} else {
index = i.getNextIndex()
isNew = true
i.packageResources[key] = index
LogDebugf(LogTemplateResourceGenerated, key, res)
LogDebugf(LogTemplateResourceGenerated, index, key)
}

return index, isNew
Expand Down
39 changes: 24 additions & 15 deletions internal/patrick/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,12 @@ It outputs two files:
func convertFile(inputFilePath string) error {

var (
err error
in *os.File
out *os.File
res *os.File
packageName string
codeState = inNormalCode
resourceToken string
err error
in *os.File
out *os.File
res *os.File
packageName string
codeState = inNormalCode
)

cfg := common.GetConfig()
Expand Down Expand Up @@ -152,7 +151,8 @@ func convertFile(inputFilePath string) error {
priorCodeState := codeState
codeState = updateCodeState(codeState, semanticLine, blockCommentBegan, blockCommentEnded)

if shouldParse(priorCodeState, codeState) {
parse := shouldParse(priorCodeState, codeState)
if parse {
// 4. Identify string literals
literals := extractStringLiterals(semanticLine)
if len(literals) != 0 {
Expand All @@ -166,10 +166,10 @@ func convertFile(inputFilePath string) error {

// get the resource index for this token
idx, isNew := inv.GetIndexForResource(packageName, resource)
resourceToken := inv.ResourceToken(idx)

// If it's a new one, write to the resource file
// If it's a new one, also write to the resource file
if isNew {
resourceToken = inv.ResourceToken(idx)
entry := resourceFileEntry(resourceToken, resource)
if _, err = res.WriteString(entry + "\n"); err != nil {
msg := fmt.Sprintf(common.ErrorTemplateIo, err)
Expand Down Expand Up @@ -269,9 +269,13 @@ func semanticLine(line string) (string, bool, bool) {
examination = examination[blockEndIdx+len(cfg.LanguageConfig.BlockCommentEndDelimiter) : blockBeginIdx]
}
} else {
// Treat as single-line comment
// Treat as single-line comment and exit loop
examination = examination[0:blockBeginIdx]
blockCommentBegan = true
// If the line is now empty, we can exit the loop
if examination == "" {
break
}
}
}

Expand All @@ -286,6 +290,10 @@ func semanticLine(line string) (string, bool, bool) {
examination = examination[blockEndIdx+len(cfg.LanguageConfig.BlockCommentEndDelimiter) : len(examination)]
}
blockCommentEnded = true
// If the line is now empty, we can exit the loop
if examination == "" {
break
}
}

common.LogDebugf(common.LogTemplateFileTrimmedLine, examination)
Expand Down Expand Up @@ -339,12 +347,13 @@ func updateCodeState(currentState codeState, line string, blockCommentBegan bool
cfg := common.GetConfig()

switch {
case line == "":
newState = onEmptyLine
case blockCommentBegan:
// We must evaluate block comments first, since they will be semantically resolved to an empty line if there is no actual code
case blockCommentBegan || (currentState == inCommentBlock && !blockCommentEnded):
newState = inCommentBlock
case blockCommentEnded:
newState = inNormalCode
case line == "":
newState = onEmptyLine
case line == cfg.LanguageConfig.ConstBlockBegin || (currentState == inConstBlock && line != cfg.LanguageConfig.ConstBlockEnd):
newState = inConstBlock
case line == cfg.LanguageConfig.ImportBlockBegin || (currentState == inImportBlock && line != cfg.LanguageConfig.ImportBlockEnd):
Expand Down Expand Up @@ -407,7 +416,7 @@ func shouldParse(was codeState, is codeState) bool {
// we are still in the comment block we were last iteration
parse = false
} else {
// even though this line ends with a block comment, we have parseable code
// even though this line ends with a block comment, we may have parseable code
parse = true
}
}
Expand Down

0 comments on commit 83f4625

Please sign in to comment.