Module: Spoor

Defined in:
lib/spoor.rb,
lib/spoor/cli.rb,
lib/spoor/version.rb

Overview

Spoor ClI

Defined Under Namespace

Classes: CLI

Constant Summary collapse

VERSION =
"0.1.3"

Class Method Summary collapse

Class Method Details

.check_dockerObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/spoor.rb', line 19

def self.check_docker
  unless docker_installed?
    puts "Docker is not installed. Please install Docker and try again."
    exit 1
  end

  return if docker_running?

  puts "Docker is not running. Please start Docker and try again."
  exit 1
end

.detect_ruby_versionObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/spoor.rb', line 31

def self.detect_ruby_version
  # Check for .ruby-version file
  return File.read(".ruby-version").strip if File.exist?(".ruby-version")

  # Check for Ruby version in Gemfile
  if File.exist?("Gemfile")
    gemfile_content = File.read("Gemfile")
    if match = gemfile_content.match(/ruby ['"]([^'"]+)['"]/)
      return match[1]
    end
  end

  # Default to a supported Ruby version
  "3.2.0"
end

.docker_installed?Boolean

Returns:

  • (Boolean)


9
10
11
12
# File 'lib/spoor.rb', line 9

def self.docker_installed?
  # Check if Docker is installed
  system("docker --version > /dev/null 2>&1")
end

.docker_running?Boolean

Returns:

  • (Boolean)


14
15
16
17
# File 'lib/spoor.rb', line 14

def self.docker_running?
  # Check if Docker is running
  system("docker info > /dev/null 2>&1")
end

.install(force_dockerfile: false) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/spoor.rb', line 47

def self.install(force_dockerfile: false) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  check_docker

  # Copy docker-compose.yml
  FileUtils.cp(
    File.join(__dir__, "spoor", "templates", "docker-compose.yml"),
    "docker-compose.yml"
  )

  # Copy .env
  FileUtils.cp(
    File.join(__dir__, "spoor", "templates", ".env.example"),
    ".env"
  )

  # Copy Dockerfile only if it doesn't exist or if forced
  dockerfile_path = File.join(Dir.pwd, "Dockerfile")
  if force_dockerfile || !File.exist?(dockerfile_path)
    ruby_version = detect_ruby_version
    dockerfile_content = ERB.new(File.read(File.join(__dir__, "spoor", "templates",
                                                     "Dockerfile.erb"))).result(binding)
    File.write(dockerfile_path, dockerfile_content)
  end

  puts "Spoor has been installed! Run `spoor up` to start the environment."
end