Skip to content

Commit

Permalink
Add importer-skip-update marker (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
rytswd committed Sep 30, 2021
1 parent 7f58a60 commit 5b30b85
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 10 deletions.
4 changes: 4 additions & 0 deletions internal/cli/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func TestUpdate(t *testing.T) {
inputFile: "../../testdata/markdown/import-with-exporter-before.md",
wantFile: "../../testdata/markdown/import-with-exporter-updated.md",
},
"markdown with skip update": {
inputFile: "../../testdata/markdown/skip-update.md",
wantFile: "../../testdata/markdown/skip-update.md",
},
}

for name, tc := range cases {
Expand Down
4 changes: 4 additions & 0 deletions internal/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ type File struct {

// Markers is an array holding onto each annotation block.
Markers map[int]*marker.Marker

// SkipUpdate is used to skip updating the file in place. This is set by a
// special marker syntax.
SkipUpdate bool
}
7 changes: 7 additions & 0 deletions internal/file/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ func (f *File) ReplaceWithAfter(options ...ReplaceOption) error {
opt(mode)
}

// If SkipUpdate flag is on, do not update the file.
if f.SkipUpdate {
// Perhaps it's more friendly to log that the file update is being
// skipped. If that's truly the case, handle it here.
mode.isDryRun = true
}

return replace(f.FileName, f.ContentAfter, mode)
}

Expand Down
12 changes: 12 additions & 0 deletions internal/file/replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ func TestReplaceWithAfter(t *testing.T) {
},
want: "", // not written
},
"skip-update found": {
input: &File{
FileName: "tmpfile.txt",
ContentBefore: []string{
"Some data",
"and more",
},
ContentAfter: []byte(`Completely different data`),
SkipUpdate: true,
},
want: "", // not written
},
}

for name, tc := range cases {
Expand Down
36 changes: 26 additions & 10 deletions internal/parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"path/filepath"
"strings"

"github.com/upsidr/importer/internal/errorsplus"
"github.com/upsidr/importer/internal/file"
Expand Down Expand Up @@ -33,28 +34,37 @@ var (
//
// If any of the above steps failed, it would return an error. This function
// does not populate the ContentAfter.
//
// TODO: Consider merging with the private parse method, as this public
// function is not adding any value at the moment.
func Parse(fileName string, input io.Reader) (*file.File, error) {
if input == nil {
return nil, ErrNoInput
}

fileType := filepath.Ext(fileName)
return parse(fileName, input)
}

// parse reads file input using scanner. This reads the input line by line, and
// store the data into File data. Parsing the data stores 3 sets of data: file
// content as is, marker details, and file content with all data between
// marker pairs purged.
func parse(fileName string, input io.Reader) (*file.File, error) {
var importerMarkerRegex string
var importerSkipMarker string

fileType := filepath.Ext(fileName)
switch fileType {
case ".md":
return parse(marker.ImporterMarkerMarkdown, fileName, input)
importerMarkerRegex = marker.ImporterMarkerMarkdown
importerSkipMarker = marker.ImporterSkipProcessingMarkdown
case ".yaml", ".yml":
return parse(marker.ImporterMarkerYAML, fileName, input)
importerMarkerRegex = marker.ImporterMarkerYAML
importerSkipMarker = marker.ImporterSkipProcessingYAML
default:
return nil, fmt.Errorf("%w, '%s' provided", ErrUnsupportedFileType, fileType)
}
}

// parse reads file input using scanner. This reads the input line by line, and
// store the data into File data. Parsing the data stores 3 sets of data: file
// content as is, marker details, and file content with all data between
// marker pairs purged.
func parse(markerRegex string, fileName string, input io.Reader) (*file.File, error) {
f := &file.File{
FileName: fileName,

Expand All @@ -80,8 +90,14 @@ func parse(markerRegex string, fileName string, input io.Reader) (*file.File, er
currentStr := scanner.Text()
f.ContentBefore = append(f.ContentBefore, currentStr)

// If skip marker is found, turn on the flag. This flag should disable
// in-place file update, but should not suppress generate or preview.
if strings.Contains(currentStr, importerSkipMarker) {
f.SkipUpdate = true
}

// Look for marker match
matches, err := regexpplus.MapWithNamedSubgroups(currentStr, markerRegex)
matches, err := regexpplus.MapWithNamedSubgroups(currentStr, importerMarkerRegex)
if err != nil {
if errors.Is(err, regexpplus.ErrNoMatch) {
// If the line appears within some other marker set, remove the line.
Expand Down
7 changes: 7 additions & 0 deletions testdata/markdown/skip-update.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!-- == importer-skip-update == -->
# Markdown Demo

<!-- == imptr: short-description / begin from: ./snippet-description.md#[for-demo] == -->
This part will not be deleted, because of the "importer-skip-update" marker at the top of the file.
This is useful for some files that use Importer Markers as "template", and use "importer generate" to generate a separate file.
<!-- == imptr: short-description / end == -->
6 changes: 6 additions & 0 deletions testdata/yaml/skip-update.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# == importer-skip-update ==

title: Demo of YAML Importer
# == import: description / begin from: ./snippet-description.yaml#[for-demo] ==
dummy: This data will NOT be removed
# == import: description / end ==

0 comments on commit 5b30b85

Please sign in to comment.