Module: FzfRuby::Tasks

Extended by:
Rake::DSL
Defined in:
lib/fzf/tasks.rb

Constant Summary collapse

FZF_REPO =

Source repository for fzf

"https://github.com/junegunn/fzf.git"
GO_HOME =

Go's recommended install location (per https://go.dev/doc/install)

"/usr/local/go"
GO_DL_BASE =

Go download base URL

"https://go.dev/dl"

Class Method Summary collapse

Class Method Details

.detect_archObject

Detect the host architecture in Go's naming convention



43
44
45
# File 'lib/fzf/tasks.rb', line 43

def self.detect_arch
  FzfRuby::Platform.arch || abort("Unsupported arch for Go auto-install: #{RbConfig::CONFIG["host_cpu"]}")
end

.detect_osObject

Detect the host OS in Go's naming convention



38
39
40
# File 'lib/fzf/tasks.rb', line 38

def self.detect_os
  FzfRuby::Platform.os || abort("Unsupported OS for Go auto-install: #{RbConfig::CONFIG["host_os"]}")
end

.ensure_goObject

Return the path to a working go binary, installing if necessary.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fzf/tasks.rb', line 56

def self.ensure_go
  # Check for system Go first
  system_go = `which go 2>/dev/null`.strip
  unless system_go.empty?
    puts "Using system Go: #{`go version`.strip}"
    return system_go
  end

  # Check for previously-installed Go in GO_HOME
  local_go = File.join(GO_HOME, "bin", "go")
  if File.executable?(local_go)
    puts "Using Go from #{GO_HOME}: #{`#{local_go} version`.strip}"
    return local_go
  end

  # Auto-install Go to GO_HOME
  install_go
end

.fetch_go_versionObject

Fetch the latest stable Go version string (e.g. "go1.26.5")



48
49
50
51
52
53
# File 'lib/fzf/tasks.rb', line 48

def self.fetch_go_version
  # go.dev/VERSION?m=text returns the version on the first line
  URI.open("https://go.dev/VERSION?m=text") { |f| f.readline.strip }
rescue => e
  abort "Failed to fetch latest Go version: #{e.message}"
end

.install_goObject

Download and install Go to /usr/local/go (per Go's official docs). Requires sudo since /usr/local is root-owned.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/fzf/tasks.rb', line 77

def self.install_go
  version = fetch_go_version
  os      = detect_os
  arch    = detect_arch
  tarball = "#{version}.#{os}-#{arch}.tar.gz"
  url     = "#{GO_DL_BASE}/#{tarball}"

  puts "Go not found — installing #{version} to #{GO_HOME}..."
  puts "Downloading #{url}..."

  # Download the tarball
  dl_path = File.join(Dir.tmpdir, tarball)
  unless system("curl", "-fSL", "--progress-bar", url, "-o", dl_path)
    abort "Failed to download Go from #{url}"
  end

  # Remove any stale partial install, then extract.
  # The tarball contains a `go/` directory; extracting to /usr/local
  # places it at /usr/local/go — exactly where Go docs recommend.
  parent = File.dirname(GO_HOME)
  puts "Installing to #{GO_HOME} (requires sudo)..."
  unless system("sudo", "rm", "-rf", GO_HOME)
    abort "Failed to remove old Go installation (sudo required)"
  end
  unless system("sudo", "tar", "-xzf", dl_path, "-C", parent)
    abort "Failed to extract Go tarball (sudo required)"
  end

  # Clean up downloaded tarball
  FileUtils.rm_f(dl_path)

  go_bin = File.join(GO_HOME, "bin", "go")
  unless File.executable?(go_bin)
    abort "Go installation succeeded but binary not found at #{go_bin}"
  end

  puts "Installed #{`#{go_bin} version`.strip} to #{GO_HOME}"
  go_bin
end