Class: Spoom::Context

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/spoom/context.rb

Overview

An abstraction to a Ruby project context

A context maps to a directory in the file system. It is used to manipulate files and run commands in the context of this directory.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(absolute_path) ⇒ Context

Returns a new instance of Context.



37
38
39
# File 'lib/spoom/context.rb', line 37

def initialize(absolute_path)
  @absolute_path = T.let(::File.expand_path(absolute_path), String)
end

Instance Attribute Details

#absolute_pathObject (readonly)

Returns the value of attribute absolute_path.



17
18
19
# File 'lib/spoom/context.rb', line 17

def absolute_path
  @absolute_path
end

Class Method Details

.mktmp!(name = nil) ⇒ Object



27
28
29
# File 'lib/spoom/context.rb', line 27

def mktmp!(name = nil)
  new(::Dir.mktmpdir(name))
end

Instance Method Details

#absolute_path_to(relative_path) ⇒ Object



43
44
45
# File 'lib/spoom/context.rb', line 43

def absolute_path_to(relative_path)
  File.join(@absolute_path, relative_path)
end

#bundle(command, version: nil) ⇒ Object



150
151
152
153
# File 'lib/spoom/context.rb', line 150

def bundle(command, version: nil)
  command = "_#{version}_ #{command}" if version
  exec("bundle #{command}")
end

#bundle_exec(command, version: nil) ⇒ Object



163
164
165
# File 'lib/spoom/context.rb', line 163

def bundle_exec(command, version: nil)
  bundle("exec #{command}", version: version)
end

#bundle_install!(version: nil) ⇒ Object



157
158
159
# File 'lib/spoom/context.rb', line 157

def bundle_install!(version: nil)
  bundle("install", version: version)
end

#destroy!Object



112
113
114
# File 'lib/spoom/context.rb', line 112

def destroy!
  FileUtils.rm_rf(@absolute_path)
end

#exec(command, capture_err: true) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/spoom/context.rb', line 120

def exec(command, capture_err: true)
  Bundler.with_unbundled_env do
    opts = T.let({ chdir: @absolute_path }, T::Hash[Symbol, T.untyped])

    if capture_err
      out, err, status = Open3.capture3(command, opts)
      ExecResult.new(out: out, err: err, status: T.must(status.success?), exit_code: T.must(status.exitstatus))
    else
      out, status = Open3.capture2(command, opts)
      ExecResult.new(out: out, err: "", status: T.must(status.success?), exit_code: T.must(status.exitstatus))
    end
  end
end

#file?(relative_path) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/spoom/context.rb', line 72

def file?(relative_path)
  File.file?(absolute_path_to(relative_path))
end

#git(command) ⇒ Object



171
172
173
# File 'lib/spoom/context.rb', line 171

def git(command)
  exec("git #{command}")
end

#git_checkout!(ref: "main") ⇒ Object



183
184
185
# File 'lib/spoom/context.rb', line 183

def git_checkout!(ref: "main")
  git("checkout #{ref}")
end

#git_current_branchObject



189
190
191
# File 'lib/spoom/context.rb', line 189

def git_current_branch
  Spoom::Git.current_branch(path: @absolute_path)
end

#git_init!(branch: "main") ⇒ Object



177
178
179
# File 'lib/spoom/context.rb', line 177

def git_init!(branch: "main")
  git("init -q -b #{branch}")
end

#glob(pattern = "**/*") ⇒ Object



58
59
60
61
62
# File 'lib/spoom/context.rb', line 58

def glob(pattern = "**/*")
  Dir.glob(absolute_path_to(pattern)).map do |path|
    Pathname.new(path).relative_path_from(@absolute_path).to_s
  end.sort
end

#listObject



66
67
68
# File 'lib/spoom/context.rb', line 66

def list
  glob("*")
end

#mkdir!Object



51
52
53
54
# File 'lib/spoom/context.rb', line 51

def mkdir!
  FileUtils.rm_rf(@absolute_path)
  FileUtils.mkdir_p(@absolute_path)
end

#move!(from_relative_path, to_relative_path) ⇒ Object



102
103
104
105
106
# File 'lib/spoom/context.rb', line 102

def move!(from_relative_path, to_relative_path)
  destination_path = absolute_path_to(to_relative_path)
  FileUtils.mkdir_p(File.dirname(destination_path))
  FileUtils.mv(absolute_path_to(from_relative_path), destination_path)
end

#read(relative_path) ⇒ Object



80
81
82
# File 'lib/spoom/context.rb', line 80

def read(relative_path)
  File.read(absolute_path_to(relative_path))
end

#read_file_strictness(relative_path) ⇒ Object



215
216
217
# File 'lib/spoom/context.rb', line 215

def read_file_strictness(relative_path)
  Spoom::Sorbet::Sigils.file_strictness(absolute_path_to(relative_path))
end

#read_gemfileObject



138
139
140
# File 'lib/spoom/context.rb', line 138

def read_gemfile
  read("Gemfile")
end

#read_sorbet_configObject



203
204
205
# File 'lib/spoom/context.rb', line 203

def read_sorbet_config
  read(Spoom::Sorbet::CONFIG_PATH)
end

#remove!(relative_path) ⇒ Object



96
97
98
# File 'lib/spoom/context.rb', line 96

def remove!(relative_path)
  FileUtils.rm_rf(absolute_path_to(relative_path))
end

#srb(command) ⇒ Object



197
198
199
# File 'lib/spoom/context.rb', line 197

def srb(command)
  bundle_exec("srb #{command}")
end

#write!(relative_path, contents = "", append: false) ⇒ Object



88
89
90
91
92
# File 'lib/spoom/context.rb', line 88

def write!(relative_path, contents = "", append: false)
  absolute_path = absolute_path_to(relative_path)
  FileUtils.mkdir_p(File.dirname(absolute_path))
  File.write(absolute_path, contents, mode: append ? "a" : "w")
end

#write_gemfile!(contents, append: false) ⇒ Object



144
145
146
# File 'lib/spoom/context.rb', line 144

def write_gemfile!(contents, append: false)
  write!("Gemfile", contents, append: append)
end

#write_sorbet_config!(contents, append: false) ⇒ Object



209
210
211
# File 'lib/spoom/context.rb', line 209

def write_sorbet_config!(contents, append: false)
  write!(Spoom::Sorbet::CONFIG_PATH, contents, append: append)
end