From 8c14862ae76fee30e1c9fb190e5b614947a04971 Mon Sep 17 00:00:00 2001 From: Hein Date: Sat, 7 Dec 2024 12:13:43 +0200 Subject: [PATCH] Updated os issues and release conf --- .github/workflows/ci.yaml | 6 ++++ .goreleaser.yml | 59 ++++++++++++++++++++++++++++++ README.pdf | Bin 42744 -> 42744 bytes main.go | 74 +++++++++++++++++++++++++++++++++++--- 4 files changed, 135 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index f8dc9e2..a55d849 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -19,6 +19,12 @@ jobs: with: go-version: '1.21' + - name: Install wkhtmltopdf + run: | + sudo apt-get update + sudo apt-get install -y wkhtmltopdf + wkhtmltopdf --version + - name: Install dependencies run: go mod download diff --git a/.goreleaser.yml b/.goreleaser.yml index e69de29..657c418 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -0,0 +1,59 @@ +before: + hooks: + - go mod tidy + +builds: + - env: + - CGO_ENABLED=0 + goos: + - linux + - windows + - darwin + goarch: + - amd64 + - arm64 + ignore: + - goos: windows + goarch: arm64 + binary: go-mdtopdf-helper + ldflags: + - -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} + +archives: + - format: tar.gz + name_template: >- + {{ .ProjectName }}_ + {{- title .Os }}_ + {{- if eq .Arch "amd64" }}x86_64 + {{- else if eq .Arch "arm64" }}arm64 + {{- else }}{{ .Arch }}{{ end }} + format_overrides: + - goos: windows + format: zip + +checksum: + name_template: 'checksums.txt' + +changelog: + sort: asc + filters: + exclude: + - '^docs:' + - '^test:' + - '^ci:' + - Merge pull request + - Merge branch + +release: + prerelease: auto + +brews: + - name: go-mdtopdf-helper + homepage: "https://github.com/yourusername/go-mdtopdf-helper" + description: "A tool to convert Markdown files to PDF using wkhtmltopdf" + tap: + owner: yourusername + name: homebrew-tools + commit_author: + name: goreleaserbot + email: bot@goreleaser.com \ No newline at end of file diff --git a/README.pdf b/README.pdf index c56c33ee44896b6fc77d66b3e9746c5b680c3970..87043da10c0c559ab50f76d9bde08d868eba0024 100644 GIT binary patch delta 18 acmexymg&b?rU~7wMur9^78@sIECT>i)(8Cn delta 18 acmexymg&b?rU~7wh9+jlCL1SYECT>i^#}j} diff --git a/main.go b/main.go index fc861b1..60cad14 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strings" "sync" @@ -16,6 +17,12 @@ import ( "github.com/gomarkdown/markdown/parser" ) +const ( + Windows = "windows" + Linux = "linux" + MacOS = "darwin" +) + type Converter struct { inputDir string recursive bool @@ -29,21 +36,64 @@ func init() { os.Exit(1) } } - func ensureWkhtmltopdfInPath() error { - wkhtmlPath := `C:\Program Files\wkhtmltopdf\bin` + var wkhtmlPath string + pathSeparator := string(os.PathListSeparator) + switch runtime.GOOS { + case Windows: + wkhtmlPath = `C:\Program Files\wkhtmltopdf\bin` + case Linux: + // Common Linux installation paths + possiblePaths := []string{ + "/usr/local/bin", + "/usr/bin", + "/opt/wkhtmltopdf/bin", + } + + for _, path := range possiblePaths { + if _, err := os.Stat(filepath.Join(path, "wkhtmltopdf")); err == nil { + wkhtmlPath = path + break + } + } + case MacOS: + // Common MacOS installation paths + possiblePaths := []string{ + "/usr/local/bin", + "/opt/homebrew/bin", + "/opt/local/bin", + } + + for _, path := range possiblePaths { + if _, err := os.Stat(filepath.Join(path, "wkhtmltopdf")); err == nil { + wkhtmlPath = path + break + } + } + default: + return fmt.Errorf("unsupported operating system: %s", runtime.GOOS) + } + + if wkhtmlPath == "" { + return fmt.Errorf("wkhtmltopdf not found in common installation paths") + } + + // Check if the directory exists if _, err := os.Stat(wkhtmlPath); os.IsNotExist(err) { return fmt.Errorf("wkhtmltopdf directory not found at: %s", wkhtmlPath) } + // Get current PATH currentPath := os.Getenv("PATH") + // Check if the path is already in PATH if strings.Contains(currentPath, wkhtmlPath) { return nil // Already in PATH } - newPath := currentPath + ";" + wkhtmlPath + // Add to PATH using OS-specific path separator + newPath := currentPath + pathSeparator + wkhtmlPath if err := os.Setenv("PATH", newPath); err != nil { return fmt.Errorf("failed to update PATH: %w", err) } @@ -52,6 +102,14 @@ func ensureWkhtmltopdfInPath() error { return nil } +// Add this helper function to get the executable name based on OS +func getExecutableName() string { + if runtime.GOOS == Windows { + return "wkhtmltopdf.exe" + } + return "wkhtmltopdf" +} + func main() { conv := &Converter{} @@ -135,6 +193,8 @@ func (c *Converter) getStagedMarkdownFiles() ([]string, error) { func (c *Converter) stageGeneratedPDFs(files []string) error { for _, file := range files { pdfFile := strings.TrimSuffix(file, filepath.Ext(file)) + ".pdf" + // Convert to OS-specific path + pdfFile = filepath.FromSlash(pdfFile) cmd := exec.Command("git", "add", pdfFile) if err := cmd.Run(); err != nil { fmt.Printf("Warning: Could not stage %s\n", pdfFile) @@ -239,9 +299,15 @@ func (c *Converter) convertFile(inputFile string) error { pdfg, err := wkhtmltopdf.NewPDFGenerator() if err != nil { + if runtime.GOOS == Windows { + // Check if wkhtmltopdf exists in the expected path + expectedPath := filepath.Join(`C:\Program Files\wkhtmltopdf\bin`, "wkhtmltopdf.exe") + if _, err := os.Stat(expectedPath); os.IsNotExist(err) { + return fmt.Errorf("wkhtmltopdf not found at %s. Please ensure it's installed correctly", expectedPath) + } + } return fmt.Errorf("failed to create PDF generator: %w", err) } - page := wkhtmltopdf.NewPageReader(strings.NewReader(string(html))) page.EnableLocalFileAccess.Set(true) pdfg.AddPage(page)