Class: Spoom::Context
- Inherits:
-
Object
- Object
- Spoom::Context
- 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
-
#absolute_path ⇒ Object
readonly
Returns the value of attribute absolute_path.
Class Method Summary collapse
Instance Method Summary collapse
- #absolute_path_to(relative_path) ⇒ Object
- #bundle(command, version: nil) ⇒ Object
- #bundle_exec(command, version: nil) ⇒ Object
- #bundle_install!(version: nil) ⇒ Object
- #destroy! ⇒ Object
- #exec(command, capture_err: true) ⇒ Object
- #exist? ⇒ Boolean
- #file?(relative_path) ⇒ Boolean
- #git(command) ⇒ Object
- #git_checkout!(ref: "main") ⇒ Object
- #git_current_branch ⇒ Object
- #git_init!(branch: "main") ⇒ Object
- #git_last_commit(short_sha: true) ⇒ Object
- #glob(pattern = "**/*") ⇒ Object
-
#initialize(absolute_path) ⇒ Context
constructor
A new instance of Context.
- #list ⇒ Object
- #mkdir! ⇒ Object
- #move!(from_relative_path, to_relative_path) ⇒ Object
- #read(relative_path) ⇒ Object
- #read_file_strictness(relative_path) ⇒ Object
- #read_gemfile ⇒ Object
- #read_sorbet_config ⇒ Object
- #remove!(relative_path) ⇒ Object
- #srb(command) ⇒ Object
- #write!(relative_path, contents = "", append: false) ⇒ Object
- #write_gemfile!(contents, append: false) ⇒ Object
- #write_sorbet_config!(contents, append: false) ⇒ Object
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.(absolute_path), String) end |
Instance Attribute Details
#absolute_path ⇒ Object (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
156 157 158 159 |
# File 'lib/spoom/context.rb', line 156 def bundle(command, version: nil) command = "_#{version}_ #{command}" if version exec("bundle #{command}") end |
#bundle_exec(command, version: nil) ⇒ Object
169 170 171 |
# File 'lib/spoom/context.rb', line 169 def bundle_exec(command, version: nil) bundle("exec #{command}", version: version) end |
#bundle_install!(version: nil) ⇒ Object
163 164 165 |
# File 'lib/spoom/context.rb', line 163 def bundle_install!(version: nil) bundle("install", version: version) end |
#destroy! ⇒ Object
118 119 120 |
# File 'lib/spoom/context.rb', line 118 def destroy! FileUtils.rm_rf(@absolute_path) end |
#exec(command, capture_err: true) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/spoom/context.rb', line 126 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 |
#exist? ⇒ Boolean
51 52 53 |
# File 'lib/spoom/context.rb', line 51 def exist? File.directory?(@absolute_path) end |
#file?(relative_path) ⇒ Boolean
78 79 80 |
# File 'lib/spoom/context.rb', line 78 def file?(relative_path) File.file?(absolute_path_to(relative_path)) end |
#git(command) ⇒ Object
177 178 179 |
# File 'lib/spoom/context.rb', line 177 def git(command) exec("git #{command}") end |
#git_checkout!(ref: "main") ⇒ Object
189 190 191 |
# File 'lib/spoom/context.rb', line 189 def git_checkout!(ref: "main") git("checkout #{ref}") end |
#git_current_branch ⇒ Object
195 196 197 |
# File 'lib/spoom/context.rb', line 195 def git_current_branch Spoom::Git.current_branch(path: @absolute_path) end |
#git_init!(branch: "main") ⇒ Object
183 184 185 |
# File 'lib/spoom/context.rb', line 183 def git_init!(branch: "main") git("init -q -b #{branch}") end |
#git_last_commit(short_sha: true) ⇒ Object
201 202 203 |
# File 'lib/spoom/context.rb', line 201 def git_last_commit(short_sha: true) Spoom::Git.last_commit(path: @absolute_path, short_sha: short_sha) end |
#glob(pattern = "**/*") ⇒ Object
64 65 66 67 68 |
# File 'lib/spoom/context.rb', line 64 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 |
#list ⇒ Object
72 73 74 |
# File 'lib/spoom/context.rb', line 72 def list glob("*") end |
#mkdir! ⇒ Object
57 58 59 60 |
# File 'lib/spoom/context.rb', line 57 def mkdir! FileUtils.rm_rf(@absolute_path) FileUtils.mkdir_p(@absolute_path) end |
#move!(from_relative_path, to_relative_path) ⇒ Object
108 109 110 111 112 |
# File 'lib/spoom/context.rb', line 108 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
86 87 88 |
# File 'lib/spoom/context.rb', line 86 def read(relative_path) File.read(absolute_path_to(relative_path)) end |
#read_file_strictness(relative_path) ⇒ Object
227 228 229 |
# File 'lib/spoom/context.rb', line 227 def read_file_strictness(relative_path) Spoom::Sorbet::Sigils.file_strictness(absolute_path_to(relative_path)) end |
#read_gemfile ⇒ Object
144 145 146 |
# File 'lib/spoom/context.rb', line 144 def read_gemfile read("Gemfile") end |
#read_sorbet_config ⇒ Object
215 216 217 |
# File 'lib/spoom/context.rb', line 215 def read_sorbet_config read(Spoom::Sorbet::CONFIG_PATH) end |
#remove!(relative_path) ⇒ Object
102 103 104 |
# File 'lib/spoom/context.rb', line 102 def remove!(relative_path) FileUtils.rm_rf(absolute_path_to(relative_path)) end |
#srb(command) ⇒ Object
209 210 211 |
# File 'lib/spoom/context.rb', line 209 def srb(command) bundle_exec("srb #{command}") end |
#write!(relative_path, contents = "", append: false) ⇒ Object
94 95 96 97 98 |
# File 'lib/spoom/context.rb', line 94 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
150 151 152 |
# File 'lib/spoom/context.rb', line 150 def write_gemfile!(contents, append: false) write!("Gemfile", contents, append: append) end |
#write_sorbet_config!(contents, append: false) ⇒ Object
221 222 223 |
# File 'lib/spoom/context.rb', line 221 def write_sorbet_config!(contents, append: false) write!(Spoom::Sorbet::CONFIG_PATH, contents, append: append) end |