Skip to content

Commit

Permalink
Refactor with extendable setup for Exporter handling (#49)
Browse files Browse the repository at this point in the history
* Refactor to use regexpplus

* Add new placeholder indentation

* Add merge-gatekeeper

* Add CI setup to run importer update against merge-gatekeeper
  • Loading branch information
rytswd authored Sep 3, 2021
1 parent cbcdfc7 commit 0f6a43a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ jobs:
go-version: ${{ env.GO_VERSION }}
- name: Run Go Build
run: go build ./cmd/importer/

# These use locally built Importer, take off `./` for usual use cases
- name: Run Importer against README.md
run: ./importer update README.md
- name: Run Importer against README.md
run: ./importer update README.md # This uses locally built Importer, take off `./` for usual use cases
run: ./importer update .github/workflows/merge-gatekeeper.yaml

- name: Check if README.md has any change compared to the branch
run: |
Expand Down
19 changes: 19 additions & 0 deletions .github/workflows/merge-gatekeeper.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# == imptr: merge-gatekeeper / begin from: https://github.com/upsidr/merge-gatekeeper/blob/main/example/definitions.yaml#[standard-setup] ==
---
name: Merge Gatekeeper

on:
pull_request:
branches:
- main
- master

jobs:
merge-gatekeeper:
runs-on: ubuntu-latest
steps:
- name: Run Merge Gatekeeper
uses: upsidr/merge-gatekeeper@main
with:
token: ${{ secrets.GITHUB_TOKEN }}
# == imptr: merge-gatekeeper / end ==
1 change: 1 addition & 0 deletions internal/marker/indent.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func adjustIndentation(lineData []byte, exporterMarkerIndent int, importerIndent
lineData = prependWhitespaces(lineData, importerIndentation.Length)
case AlignIndentation:
lineData = handleAbsoluteIndentation(lineData, exporterMarkerIndent, importerIndentation.MarkerIndentation)
case KeepIndentation: // Explicitly handling this, as it is likely that the default behaviour woulld need to change
}
lineData = append(lineData, br)
return lineData
Expand Down
1 change: 1 addition & 0 deletions internal/marker/marker.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
AbsoluteIndentation IndentationMode = iota + 1
ExtraIndentation
AlignIndentation
KeepIndentation
)

// Indentation holds additional indentation handling option.
Expand Down
19 changes: 10 additions & 9 deletions internal/marker/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/upsidr/importer/internal/regexpplus"
Expand Down Expand Up @@ -69,7 +68,6 @@ const br = byte('\n')
func (m *Marker) processSingleMarkerMarkdown(file io.Reader) ([]byte, error) {
result := []byte{}

reExport := regexp.MustCompile(ExporterMarkerMarkdown)
withinExportMarker := false
currentLine := 0

Expand All @@ -78,14 +76,18 @@ func (m *Marker) processSingleMarkerMarkdown(file io.Reader) ([]byte, error) {
currentLine++

// Find Exporter Marker
match := reExport.FindStringSubmatch(scanner.Text())
if len(match) != 0 {
// match[1] is export_marker_name
if match[1] == m.TargetExportMarker {
matches, err := regexpplus.MapWithNamedSubgroups(scanner.Text(), ExporterMarkerMarkdown)
if err != nil && !errors.Is(err, regexpplus.ErrNoMatch) {
panic(err) // Unknown error, should not happen
}

if len(matches) != 0 {
if exporterName, found := matches["export_marker_name"]; found &&
exporterName == m.TargetExportMarker {
withinExportMarker = true
}
// match[2] is exporter_marker_condition
if match[2] == "end" {
if exporterCondition, found := matches["exporter_marker_condition"]; found &&
exporterCondition == "end" {
withinExportMarker = false
}
continue
Expand Down Expand Up @@ -119,7 +121,6 @@ func (m *Marker) processSingleMarkerMarkdown(file io.Reader) ([]byte, error) {
func (m *Marker) processSingleMarkerYAML(file io.Reader) ([]byte, error) {
result := []byte{}

// reExport := regexp.MustCompile(ExporterMarkerYAML)
isNested := false
nestedUnder := ""
exporterMarkerIndentation := 0
Expand Down

0 comments on commit 0f6a43a

Please sign in to comment.