-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: determine package name, ci, etc
- Loading branch information
1 parent
3c2c97d
commit 1fd1582
Showing
9 changed files
with
247 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
name: CI | ||
|
||
permissions: read-all | ||
|
||
on: | ||
push: | ||
paths: | ||
- **/*.go | ||
- .github/workflows/ci.yml | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.ref }} | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
format: | ||
name: Format | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout Repository | ||
uses: actions/checkout@v4 | ||
- name: Set up Go | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: "1.23" | ||
- name: Get Go Cache Paths | ||
id: go-cache-paths | ||
run: | | ||
echo "go-build=$(go env GOCACHE)" >> $GITHUB_OUTPUT | ||
echo "go-mod=$(go env GOMODCACHE)" >> $GITHUB_OUTPUT | ||
- name: Install gofumpt | ||
run: go install mvdan.cc/gofumpt@latest | ||
- name: Go Build Cache | ||
uses: actions/cache@v4 | ||
with: | ||
path: ${{ steps.go-cache-paths.outputs.go-build }} | ||
key: ${{ runner.os }}-go-build-${{ hashFiles('**/go.sum') }} | ||
- name: Go Mod Cache | ||
uses: actions/cache@v4 | ||
with: | ||
path: ${{ steps.go-cache-paths.outputs.go-mod }} | ||
key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }} | ||
- name: Check Code Formatting | ||
run: | | ||
unformatted_files=$(gofumpt -l .) | ||
if [ -n "$unformatted_files" ]; then | ||
echo "Files not formatted:" | ||
echo "$unformatted_files" | ||
exit 1 | ||
fi | ||
lint: | ||
name: Lint | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
pull-requests: read | ||
checks: write | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-go@v5 | ||
with: | ||
go-version: "1.23" | ||
- name: Get Go Cache Paths | ||
id: go-cache-paths | ||
run: | | ||
echo "go-build=$(go env GOCACHE)" >> $GITHUB_OUTPUT | ||
echo "go-mod=$(go env GOMODCACHE)" >> $GITHUB_OUTPUT | ||
- name: Go Build Cache | ||
uses: actions/cache@v4 | ||
with: | ||
path: ${{ steps.go-cache-paths.outputs.go-build }} | ||
key: ${{ runner.os }}-go-build-${{ hashFiles('**/go.sum') }} | ||
- name: Go Mod Cache | ||
uses: actions/cache@v4 | ||
with: | ||
path: ${{ steps.go-cache-paths.outputs.go-mod }} | ||
key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }} | ||
- name: golangci-lint | ||
uses: golangci/golangci-lint-action@v5 | ||
with: | ||
version: latest | ||
working-directory: ./ | ||
args: --timeout=5m | ||
|
||
test: | ||
name: Test | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout Repository | ||
uses: actions/checkout@v4 | ||
- name: Set up Go | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: "1.23" | ||
- name: Get Go Cache Paths | ||
id: go-cache-paths | ||
run: | | ||
echo "go-build=$(go env GOCACHE)" >> $GITHUB_OUTPUT | ||
echo "go-mod=$(go env GOMODCACHE)" >> $GITHUB_OUTPUT | ||
- name: Go Build Cache | ||
uses: actions/cache@v4 | ||
with: | ||
path: ${{ steps.go-cache-paths.outputs.go-build }} | ||
key: ${{ runner.os }}-go-build-${{ hashFiles('**/go.sum') }} | ||
- name: Go Mod Cache | ||
uses: actions/cache@v4 | ||
with: | ||
path: ${{ steps.go-cache-paths.outputs.go-mod }} | ||
key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }} | ||
- name: Run Tests with Coverage | ||
run: go test -v -race -coverprofile=coverage.txt ./... | ||
- name: Print Coverage | ||
run: go tool cover -func=coverage.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
package generatecmd | ||
package snips | ||
|
||
import "strings" | ||
|
||
func IsCodeFile(name string) bool { | ||
func ContainsDotCodeDot(name string) bool { | ||
index := strings.LastIndex(name, ".code.") | ||
return index != -1 && index < len(name)-6 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package snips | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func PackageName(dir string) (name string) { | ||
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if !info.IsDir() && strings.HasSuffix(info.Name(), ".templ") { | ||
content, err := os.ReadFile(path) | ||
if err != nil { | ||
return err | ||
} | ||
lines := strings.Split(string(content), "\n") | ||
for _, line := range lines { | ||
if strings.HasPrefix(strings.TrimSpace(line), "package ") { | ||
name = strings.TrimSpace(strings.TrimPrefix(line, "package")) | ||
return filepath.SkipAll // stop walking, we found a package name | ||
} | ||
} | ||
} | ||
return nil | ||
}) | ||
|
||
if err != nil || name == "" { | ||
return fallback(dir) | ||
} | ||
|
||
return name | ||
} | ||
|
||
func fallback(dir string) (name string) { | ||
var ( | ||
parts = strings.Split(filepath.ToSlash(dir), "/") | ||
n = len(parts) | ||
) | ||
|
||
if n > 1 { | ||
return parts[n-1] | ||
} | ||
|
||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package snips_test | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/garrettladley/snips" | ||
) | ||
|
||
func TestPackageNameSameAsDirectory(t *testing.T) { | ||
dir := createTempDir(t) | ||
filePath := filepath.Join(dir, "views", "foo", "bar.templ") | ||
createTempFile(t, filePath, "package foo\n\ntempl Hello(name string) {\n <div>Hello, { name }</div>\n}") | ||
|
||
pkg := snips.PackageName(filepath.Join(dir, "views", "foo")) | ||
if pkg != "foo" { | ||
t.Fatalf("expected package name to be 'foo', got '%s'", pkg) | ||
} | ||
} | ||
|
||
func TestPackageNameDifferentFromDirectory(t *testing.T) { | ||
dir := createTempDir(t) | ||
filePath := filepath.Join(dir, "views", "foo", "bar.templ") | ||
createTempFile(t, filePath, "package bar\n\ntempl Hello(name string) {\n <div>Hello, { name }</div>\n}") | ||
|
||
pkg := snips.PackageName(filepath.Join(dir, "views", "foo")) | ||
if pkg != "bar" { | ||
t.Fatalf("expected package name to be 'bar', got '%s'", pkg) | ||
} | ||
} | ||
|
||
func TestPackageNameFallback(t *testing.T) { | ||
dir := createTempDir(t) | ||
filePath := filepath.Join(dir, "views", "foo", "ex.rs") | ||
createTempFile(t, filePath, "fn main() {\n println!(\"Hello World!\");\n}") | ||
|
||
pkg := snips.PackageName(filepath.Join(dir, "views", "foo")) | ||
if pkg != "foo" { | ||
t.Fatalf("expected package name to be 'foo', got '%s'", pkg) | ||
} | ||
} | ||
|
||
func createTempDir(t *testing.T) string { | ||
t.Helper() | ||
dir, err := os.MkdirTemp("", "snips") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Cleanup(func() { | ||
_ = os.RemoveAll(dir) | ||
}) | ||
return dir | ||
} | ||
|
||
func createTempFile(t *testing.T, path, content string) { | ||
t.Helper() | ||
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := os.WriteFile(path, []byte(content), 0644); err != nil { | ||
t.Fatal(err) | ||
} | ||
} |