diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 4cab4303..ac43748f 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -23,6 +23,7 @@ jobs: - "**/*.sql" - "go.mod" - "go.sum" + - "Makefile" - name: Install Nix if: steps.filter.outputs.source_code == 'true' @@ -30,20 +31,24 @@ jobs: - name: Format check if: steps.filter.outputs.source_code == 'true' - run: nix develop -c bash -c "make fmt && git diff --exit-code" + run: nix develop .#ci -c bash -c "make fmt && git diff --exit-code" - name: Check if: steps.filter.outputs.source_code == 'true' - run: nix develop -c make check + run: nix develop .#ci -c make check - name: Sql check if: steps.filter.outputs.source_code == 'true' - run: nix develop -c make check-sql + run: nix develop .#ci -c make check-sql - name: Build if: steps.filter.outputs.source_code == 'true' - run: nix develop -c make build + run: nix develop .#ci -c make build + + - name: Generated files check + if: steps.filter.outputs.source_code == 'true' + run: nix develop .#ci -c bash -c "make generate && git diff --exit-code" - name: Test if: steps.filter.outputs.source_code == 'true' - run: nix develop -c make test + run: nix develop .#ci -c make test diff --git a/Makefile b/Makefile index c387c8c9..c7ecb7ab 100644 --- a/Makefile +++ b/Makefile @@ -10,8 +10,6 @@ GO_DEPS := $(GO_SOURCES) go.mod go.sum MIGRATE_DIR := sql/migrations DB_URL := sqlite3://dev.db -DOCS_DEPS := $(wildcard internal/api/handlers/*.go) -DOCS_TARGET := internal/api/docs/docs.go SQLC_DEPS := $(wildcard sql/migrations/*.sql) $(wildcard sql/queries/*.sql) SQLC_TARGET := internal/api/dbmodels/models.go @@ -25,7 +23,7 @@ build: api cli ## Build the api and cli binaries api: $(BIN_DIR)/$(API_NAME) ## Build the api binary -$(BIN_DIR)/$(API_NAME): $(GO_DEPS) $(SQLC_TARGET) +$(BIN_DIR)/$(API_NAME): $(GO_DEPS) sqlc @mkdir -p $(BIN_DIR) go build -ldflags "-X main.Version=$(VERSION)" -o $(BIN_DIR)/$(API_NAME) ./cmd/$(API_NAME) @@ -35,11 +33,13 @@ $(BIN_DIR)/$(CLI_NAME): $(GO_DEPS) @mkdir -p $(BIN_DIR) go build -ldflags "-X cli.Version=$(VERSION)" -o $(BIN_DIR)/$(CLI_NAME) ./cmd/$(CLI_NAME) -generate: $(DOCS_TARGET) $(SQLC_TARGET) ## Generate all necessary files with +generate: swag sqlc ## Generate all necessary files -$(DOCS_TARGET): $(DOCS_DEPS) +swag: ## Generate OpenAPI docs swag init -d cmd/acmcsuf-api,internal/api/handlers,internal/api/dbmodels -o internal/api/docs --parseDependency +sqlc: $(SQLC_TARGET) ## Generate dbmodels package with sqlc + $(SQLC_TARGET): $(SQLC_DEPS) sqlc generate @@ -47,7 +47,7 @@ fmt: ## Format all go files @go fmt ./... check: ## Run static analysis on all go files - staticcheck -f stylish ./... + staticcheck -f stylish ./... test: check ## Run all tests go test ./... diff --git a/flake.nix b/flake.nix index f46d6981..ba0c5d84 100644 --- a/flake.nix +++ b/flake.nix @@ -23,22 +23,26 @@ package = pkgs.callPackage ./nix/package.nix { version = version; }; - acmcsuf-api = { type = "app"; program = "${package}/bin/acmcsuf-api"; }; + acmcsuf-cli = { + type = "app"; + program = "${package}/bin/acmcsuf-cli"; + }; in { - packages.default = package; - devShells.default = pkgs.callPackage ./nix/shell.nix {}; + devShells = { + default = pkgs.callPackage ./nix/shell.nix {}; + full = pkgs.callPackage ./nix/shell.nix {full = true;}; + ci = pkgs.callPackage ./nix/shell.nix {isCI = true;}; + }; + packages.default = package; apps = { default = acmcsuf-api; acmcsuf-api = acmcsuf-api; - acmcsuf-cli = { - type = "app"; - program = "${package}/bin/acmcsuf-cli"; - }; + acmcsuf-cli = acmcsuf-cli; }; } ); diff --git a/nix/package.nix b/nix/package.nix index bc7f9fd6..3a15c393 100644 --- a/nix/package.nix +++ b/nix/package.nix @@ -4,10 +4,10 @@ buildGoModule, }: buildGoModule { - name = "acmcsuf-api"; + pname = "acmcsuf-api"; src = ../.; version = version; - vendorHash = "sha256-qr5wgHUKVv+Sv7TQGgOX9zRx9O6OgqFA16xwBTSysb4="; + vendorHash = "sha256-XCzKPPjCbO2Kp9XGZ94PpPedyJhFAs+ys+gna66jDSE="; subPackages = ["cmd/acmcsuf-api" "cmd/acmcsuf-cli"]; ldflags = ["-X main.Version=${version}"]; diff --git a/nix/shell.nix b/nix/shell.nix index 4b38b357..72a24342 100644 --- a/nix/shell.nix +++ b/nix/shell.nix @@ -1,5 +1,6 @@ { mkShell, + lib, go, gopls, delve, @@ -7,48 +8,54 @@ air, sqlfluff, gnumake, - curl, xh, - jq, go-swag, cobra-cli, go-tools, go-migrate, sqlite, sqlite-web, -}: - -let - go-migrate-sqlite = go-migrate.overrideAttrs (oldAttrs: { - tags = [ "sqlite3" ]; - }); + isCI ? false, + full ? false, +}: let + go-migrate-sqlite = go-migrate.overrideAttrs (oldAttrs: { + tags = ["sqlite3"]; + }); in -mkShell { - packages = [ - go - gopls # Go language server - go-tools - delve # Go debugger - sqlc # compiles SQL queries to Go code - air # run dev server with hot reload - sqlfluff # SQL linter - gnumake - curl - xh - jq - go-swag - cobra-cli - go-migrate-sqlite - sqlite - sqlite-web - ]; + mkShell { + packages = + [ + go + gopls # Go language server + go-tools + sqlc # compiles SQL queries to Go code + sqlfluff # SQL linter + gnumake + go-swag + ] + # Dev tools not required in CI go here + ++ lib.optionals (!isCI) [ + air # run dev server with hot reload + xh + go-migrate-sqlite + sqlite + cobra-cli + delve # Go debugger + ] + # Heavyweight or rarely used tools go here + ++ lib.optionals full [ + sqlite-web + ]; + + env = { + GOROOT = "${go}/share/go"; + }; - shellHook = '' - if [ ! -f .env ]; then - echo ".env file not found! Creating one from .env.example for you..." - cp .env.example .env - fi - echo -e "\e[32mLoaded nix dev shell\e[0m" - export GOROOT="${go}/share/go" - ''; -} + shellHook = lib.optionalString (!isCI) '' + if [ ! -f .env ]; then + echo ".env file not found! Creating one from .env.example for you..." + cp .env.example .env + fi + echo -e "\e[32mLoaded nix dev shell\e[0m" + ''; + }