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



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

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.



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

def absolute_path
  @absolute_path
end

Class Method Details

.mktmp!(name = nil) ⇒ Object



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

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

Instance Method Details

#absolute_path_to(relative_path) ⇒ Object



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

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

#bundle(command, version: nil) ⇒ Object



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

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

#bundle_exec(command, version: nil) ⇒ Object



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

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

#bundle_install!(version: nil) ⇒ Object



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

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

#destroy!Object



119
120
121
# File 'lib/spoom/context.rb', line 119

def destroy!
  FileUtils.rm_rf(@absolute_path)
end

#exec(command, capture_err: true) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/spoom/context.rb', line 127

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: nil, status: T.must(status.success?), exit_code: T.must(status.exitstatus))
    end
  end
end

#exist?Boolean



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

def exist?
  File.directory?(@absolute_path)
end

#file?(relative_path) ⇒ Boolean



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

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

#git(command) ⇒ Object



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

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

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



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

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

#git_current_branchObject



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

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

#git_init!(branch: nil) ⇒ Object



187
188
189
190
191
192
193
# File 'lib/spoom/context.rb', line 187

def git_init!(branch: nil)
  if branch
    git("init -b #{branch}")
  else
    git("init")
  end
end

#git_last_commit(short_sha: true) ⇒ Object



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

def git_last_commit(short_sha: true)
  Spoom::Git.last_commit(path: @absolute_path, short_sha: short_sha)
end

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



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

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



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

def list
  glob("*")
end

#mkdir!Object



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

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

#move!(from_relative_path, to_relative_path) ⇒ Object



109
110
111
112
113
# File 'lib/spoom/context.rb', line 109

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



87
88
89
# File 'lib/spoom/context.rb', line 87

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

#read_file_strictness(relative_path) ⇒ Object



235
236
237
# File 'lib/spoom/context.rb', line 235

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

#read_gemfileObject



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

def read_gemfile
  read("Gemfile")
end

#read_sorbet_configObject



223
224
225
# File 'lib/spoom/context.rb', line 223

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

#remove!(relative_path) ⇒ Object



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

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

#srb(command) ⇒ Object



217
218
219
# File 'lib/spoom/context.rb', line 217

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

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



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

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



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

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

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



229
230
231
# File 'lib/spoom/context.rb', line 229

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