Class: Radon::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/core/util.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.first_runObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/core/util.rb', line 6

def self.first_run
  return if (File.directory?(SETTINGS_DIR) && File.exist?(SETTINGS_FILE))
  puts "Performing first time setup..."
  
  # Make settings dir
  FileUtils.mkdir_p(SETTINGS_DIR)
  vprint("Creating #{SETTINGS_DIR}")

  puts "Welcome to radon! It looks like it's your first time running."
  email    = ask "  Email: "
  gh_uname = ask "  GitHub username: "

  data = {
    :email => email,
    :username => gh_uname
  }

  File.write(File.join(SETTINGS_DIR, 'settings.json'), JSON.pretty_generate(data))
  vprint "Writing settings to #{File.join(SETTINGS_DIR, 'settings.json')}"
end

.get_emailObject



27
28
29
# File 'lib/core/util.rb', line 27

def self.get_email
  JSON.parse(File.read(SETTINGS_FILE))['email']
end

.get_github_usernameObject



31
32
33
# File 'lib/core/util.rb', line 31

def self.get_github_username
  JSON.parse(File.read(SETTINGS_FILE))['username']
end

.which(cmd) ⇒ Object

Cross-platform way of finding an executable in the $PATH.

which('ruby') #=> /usr/bin/ruby


38
39
40
41
42
43
44
45
46
47
# File 'lib/core/util.rb', line 38

def self.which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each { |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    }
  end
  return nil
end

Instance Method Details

#open_in_atom(path) ⇒ Object



54
55
56
57
# File 'lib/core/util.rb', line 54

def open_in_atom(path)
  fail_with('`atom` program is not in $PATH') if Radon::Util.which('atom').nil?
  `cd #{path} && atom .`
end

#open_in_editor(opts, path) ⇒ Object



59
60
61
62
63
# File 'lib/core/util.rb', line 59

def open_in_editor(opts, path)
  open_in_vscode(path) if opts[:open_vscode]
rescue StandardError => e
  report_error_to_github(e)
end

#open_in_vscode(path) ⇒ Object



49
50
51
52
# File 'lib/core/util.rb', line 49

def open_in_vscode(path)
  fail_with('`code` program is not in $PATH') if Radon::Util.which('code').nil?
  `cd #{path} && code .`
end