SHELL := /usr/bin/env bash

GHERKIN_LANGUAGES_JSON = ../gherkin-languages.json
GHERKIN_DIALECTS = lib/src/language/dialects_builtin.g.dart
GHERKIN_DIALECTS_JQ = dialects_builtin.g.dart.jq
GHERKIN_PARSER = lib/src/parser/parser.g.dart
GHERKIN_RAZOR = gherkin-dart.razor
GHERKIN = dart run bin/gherkin.dart
GHERKIN_GENERATE_TOKENS = dart run bin/gherkin_generate_tokens.dart

GOOD_FEATURE_FILES = $(shell find ../testdata/good -name "*.feature")
BAD_FEATURE_FILES  = $(shell find ../testdata/bad -name "*.feature")

TOKENS       = $(patsubst ../testdata/%,acceptance/testdata/%.tokens,$(GOOD_FEATURE_FILES))
ASTS         = $(patsubst ../testdata/%,acceptance/testdata/%.ast.ndjson,$(GOOD_FEATURE_FILES))
PICKLES      = $(patsubst ../testdata/%,acceptance/testdata/%.pickles.ndjson,$(GOOD_FEATURE_FILES))
SOURCES      = $(patsubst ../testdata/%,acceptance/testdata/%.source.ndjson,$(GOOD_FEATURE_FILES))
ERRORS       = $(patsubst ../testdata/%,acceptance/testdata/%.errors.ndjson,$(BAD_FEATURE_FILES))

.DEFAULT_GOAL = help

help: ## Show this help
	@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n  make <target>\n\nWhere <target> is one of:\n"} /^[$$()% a-zA-Z_-]+:.*?##/ { printf "  \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
.PHONY: help

generate: $(GHERKIN_PARSER) ## Generate gherkin parser files
.PHONY: generate

$(GHERKIN_PARSER): $(GHERKIN_RAZOR) ../gherkin.berp
	berp -g ../gherkin.berp -t $(GHERKIN_RAZOR) -o $@ --noBOM

clean-generate: ## Remove generated Gherkin parser files
	rm -f $(GHERKIN_PARSER)
.PHONY: clean-generate

copy-gherkin-languages: $(GHERKIN_DIALECTS) ## Generate derived files from gherkin-languages.json
.PHONY: copy-gherkin-languages

$(GHERKIN_DIALECTS): $(GHERKIN_LANGUAGES_JSON) $(GHERKIN_DIALECTS_JQ)
	jq --sort-keys --from-file $(GHERKIN_DIALECTS_JQ) --raw-output --compact-output $< > $@

clean-gherkin-languages: ## Remove derived files generated from gherkin-languages.json
	rm -f $(GHERKIN_DIALECTS)
.PHONY: clean-gherkin-languages

mostlyclean:
	rm -rf .built
	rm -rf acceptance
.PHONY: mostlyclean

clean: mostlyclean ## Remove all build artifacts and files generated by the acceptance tests
.PHONY: clean

acceptance: .built $(TOKENS) $(ASTS) $(PICKLES) $(ERRORS) $(SOURCES) ## Build acceptance test dir and compare results with reference

.built: pubspec.yaml pubspec.lock
	dart pub get
	@touch $@

acceptance/testdata/%.tokens: ../testdata/% ../testdata/%.tokens
	mkdir -p $(@D)
	$(GHERKIN_GENERATE_TOKENS) $< > $@
	diff --unified $<.tokens $@

acceptance/testdata/%.ast.ndjson: ../testdata/% ../testdata/%.ast.ndjson
	mkdir -p $(@D)
	$(GHERKIN) --predictable-ids --no-source --no-pickles $< | jq --sort-keys --compact-output "." > $@
	diff --unified <(jq --sort-keys "." $<.ast.ndjson) <(jq --sort-keys "." $@)

acceptance/testdata/%.pickles.ndjson: ../testdata/% ../testdata/%.pickles.ndjson
	mkdir -p $(@D)
	$(GHERKIN) --predictable-ids --no-source --no-ast $< | jq --sort-keys --compact-output "." > $@
	diff --unified <(jq --sort-keys "." $<.pickles.ndjson) <(jq --sort-keys "." $@)

acceptance/testdata/%.source.ndjson: ../testdata/% ../testdata/%.source.ndjson
	mkdir -p $(@D)
	$(GHERKIN) --no-ast --no-pickles $< | jq --sort-keys --compact-output "." > $@
	diff --unified <(jq --sort-keys "." $<.source.ndjson) <(jq --sort-keys "." $@)

acceptance/testdata/%.errors.ndjson: ../testdata/% ../testdata/%.errors.ndjson
	mkdir -p $(@D)
	$(GHERKIN) --predictable-ids --no-source $< | jq --sort-keys --compact-output "." > $@
	diff --unified <(jq --sort-keys "." $<.errors.ndjson) <(jq --sort-keys "." $@)
.PHONY: acceptance

.DELETE_ON_ERROR:

