| | | 1 | | // Package neovim implements ports.NeovimProvider. It downloads Neovim release |
| | | 2 | | // archives from GitHub, caches the extracted binary on disk, and returns the |
| | | 3 | | // path to the binary on each call to Ensure. |
| | | 4 | | package neovim |
| | | 5 | | |
| | | 6 | | import ( |
| | | 7 | | "context" |
| | | 8 | | "fmt" |
| | | 9 | | "path/filepath" |
| | | 10 | | |
| | | 11 | | "github.com/jedi-knights/neospec/internal/domain" |
| | | 12 | | ) |
| | | 13 | | |
| | | 14 | | // Provider satisfies ports.NeovimProvider. |
| | | 15 | | type Provider struct { |
| | | 16 | | cache *Cache |
| | | 17 | | downloader *Downloader |
| | | 18 | | } |
| | | 19 | | |
| | | 20 | | // NewProvider creates a Provider that stores binaries under cacheDir. |
| | | 21 | | func NewProvider(cacheDir string) *Provider { |
| | | 22 | | return &Provider{ |
| | | 23 | | cache: NewCache(cacheDir), |
| | | 24 | | downloader: NewDownloader(), |
| | | 25 | | } |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | // Ensure returns the path to an nvim binary of the requested version. |
| | | 29 | | // It checks the local cache first; on a cache miss it downloads and extracts |
| | | 30 | | // the archive, then caches the result. |
| | 5 | 31 | | func (p *Provider) Ensure(ctx context.Context, version domain.Version, platform domain.Platform) (string, error) { |
| | 5 | 32 | | binaryPath, ok := p.cache.Lookup(version, platform) |
| | 1 | 33 | | if ok { |
| | 1 | 34 | | return binaryPath, nil |
| | 1 | 35 | | } |
| | | 36 | | |
| | 4 | 37 | | assetName := version.AssetName(platform) |
| | 1 | 38 | | if assetName == "" { |
| | 1 | 39 | | return "", fmt.Errorf("unsupported platform: %s", platform) |
| | 1 | 40 | | } |
| | | 41 | | |
| | 3 | 42 | | archivePath := filepath.Join(p.cache.VersionDir(version, platform), assetName) |
| | 3 | 43 | | |
| | 1 | 44 | | if err := p.downloader.Download(ctx, version, platform, archivePath); err != nil { |
| | 1 | 45 | | return "", fmt.Errorf("downloading neovim %s for %s: %w", version, platform, err) |
| | 1 | 46 | | } |
| | | 47 | | |
| | 2 | 48 | | binaryPath, err := p.cache.Extract(version, platform, archivePath) |
| | 1 | 49 | | if err != nil { |
| | 1 | 50 | | return "", fmt.Errorf("extracting neovim archive: %w", err) |
| | 1 | 51 | | } |
| | | 52 | | |
| | 1 | 53 | | return binaryPath, nil |
| | | 54 | | } |