Module: Tomo::Plugin::Core::Helpers

Defined in:
lib/tomo/plugin/core/helpers.rb

Instance Method Summary collapse

Instance Method Details

#capture(*command, **run_opts) ⇒ Object



7
8
9
10
# File 'lib/tomo/plugin/core/helpers.rb', line 7

def capture(*command, **run_opts)
  result = run(*command, silent: true, **run_opts)
  result.stdout
end

#command_available?(command_name, **run_opts) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/tomo/plugin/core/helpers.rb', line 48

def command_available?(command_name, **run_opts)
  run?("which", command_name, silent: true, **run_opts)
end

#directory?(directory, **run_opts) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/tomo/plugin/core/helpers.rb', line 60

def directory?(directory, **run_opts)
  flag?("-d", directory, **run_opts)
end

#executable?(file, **run_opts) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/tomo/plugin/core/helpers.rb', line 56

def executable?(file, **run_opts)
  flag?("-x", file, **run_opts)
end

#file?(file, **run_opts) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/tomo/plugin/core/helpers.rb', line 52

def file?(file, **run_opts)
  flag?("-f", file, **run_opts)
end

#list_files(directory = nil, **run_opts) ⇒ Object



44
45
46
# File 'lib/tomo/plugin/core/helpers.rb', line 44

def list_files(directory=nil, **run_opts)
  capture("ls", "-A1", directory, **run_opts).strip.split("\n")
end

#ln_sf(target, link, **run_opts) ⇒ Object



28
29
30
# File 'lib/tomo/plugin/core/helpers.rb', line 28

def ln_sf(target, link, **run_opts)
  run("ln", "-sf", target, link, **run_opts)
end

#ln_sfn(target, link, **run_opts) ⇒ Object



32
33
34
# File 'lib/tomo/plugin/core/helpers.rb', line 32

def ln_sfn(target, link, **run_opts)
  run("ln", "-sfn", target, link, **run_opts)
end

#mkdir_p(*directories, **run_opts) ⇒ Object



36
37
38
# File 'lib/tomo/plugin/core/helpers.rb', line 36

def mkdir_p(*directories, **run_opts)
  run("mkdir", "-p", *directories, **run_opts)
end

#rm_rf(*paths, **run_opts) ⇒ Object



40
41
42
# File 'lib/tomo/plugin/core/helpers.rb', line 40

def rm_rf(*paths, **run_opts)
  run("rm", "-rf", *paths, **run_opts)
end

#run?(*command, **run_opts) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
# File 'lib/tomo/plugin/core/helpers.rb', line 12

def run?(*command, **run_opts)
  result = run(*command, **run_opts, raise_on_error: false)
  result.success?
end

#write(to:, text: nil, template: nil, append: false, **run_opts) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/tomo/plugin/core/helpers.rb', line 17

def write(to:, text: nil, template: nil, append: false, **run_opts)
  assert_text_or_template_required!(text, template)
  text = merge_template(template) unless template.nil?
  message = "Writing #{text.bytesize} bytes to #{to}"
  run(
    "echo -n #{text.shellescape} #{append ? '>>' : '>'} #{to.shellescape}",
    echo: message,
    **run_opts
  )
end