Module: Yuyi::Dsl

Included in:
Yuyi
Defined in:
lib/yuyi/dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

Returns the value of attribute menu_path.



10
11
12
# File 'lib/yuyi/dsl.rb', line 10

def menu_path
  @menu_path
end

#upgradeObject

Returns the value of attribute upgrade.



10
11
12
# File 'lib/yuyi/dsl.rb', line 10

def upgrade
  @upgrade
end

#verboseObject

Returns the value of attribute verbose.



10
11
12
# File 'lib/yuyi/dsl.rb', line 10

def verbose
  @verbose
end

Instance Method Details

#ask(question, options = {}) {|output| ... } ⇒ Object

Accepts the same arguments as #say

Yields:

  • (output)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/yuyi/dsl.rb', line 70

def ask question, options = {}, &block
  prompt = '>>> '.colorize(:green)
  options = {
    :readline => false,
  }.merge(options)

  say question, options

  output = if options[:readline]
    Readline.readline(prompt).chomp('/')
  else
    say prompt, :newline => false
    STDIN.gets
  end.rstrip

  say
  yield output if block
end

#command?(command) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/yuyi/dsl.rb', line 105

def command? command
  run command, :verbose => false, :boolean => true
end

#delete_from_file(file, *text) ⇒ Object

Delete specific lines from an existing file



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/yuyi/dsl.rb', line 125

def delete_from_file file, *text
  text.flatten!

  # get file text
  new_text = File.read(File.expand_path(file))

  # iterate through text and remove it
  text.each do |t|
    regex = /^.*#{Regexp.escape(t)}.*\n/
    new_text.gsub!(regex, '')
  end

  # write new text back to file
  File.open(File.expand_path(file), 'w') { |f| f.write(new_text) }
end

#osx_versionObject



141
142
143
# File 'lib/yuyi/dsl.rb', line 141

def osx_version
  run('/usr/bin/sw_vers -productVersion').chomp[/10\.\d+/].to_f
end

#run(command, args = {}) ⇒ Object

Run a command and output formatting success/errors



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/yuyi/dsl.rb', line 91

def run command, args = {}
  # check if in verbose mode
  verbose = args[:verbose] || @verbose
  output = `echo | #{command} 2>&1`
  success = $?.success?

  if verbose
    say "RUNNING: #{command}", :type => (success ? :success : :fail)
    say output
  end

  args[:boolean] ? success : output
end

#say(text = '', args = {}) ⇒ Object

Replacement for ‘puts` that accepts various stylistic arguments type: => [symbol] Preset colors for [:fail, :success, :warn] color: => [integer] See docs for #colorize for color codes justify: => [center|ljust|rjust] The type of justification to use padding: => [integer] The maximum string size to justify text in indent: => [integer] The maximum string size to justify text in newline: => [boolean] True if you want a newline after the output progressbar: => [boolean] True if you are adding a title to the progresbar overwrite: => [boolean] True if you want to overwrite the previous line



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/yuyi/dsl.rb', line 22

def say text = '', args = {}
  # defaults
  args = {
    :newline => true
  }.merge args

  # Justify options
  if args[:justify] && args[:padding]
    text = text.send args[:justify], args[:padding]
  end

  # Type options
  # process last due to the addition of special color codes
  text = case args[:type]
  when :fail
    text.colorize :red
  when :success
    text.colorize :green
  when :warn
    text.colorize :yellow
  else
    text
  end

  if args[:color]
    text = text.colorize(args[:color])
  end

  if args[:indent]
    text = (' ' * args[:indent]) + text
  end

  if args[:overwrite]
    STDOUT.flush
    text = text + "\r"
  end

  if args[:progressbar] && Yuyi.verbose != true && Yuyi::Menu.menu && Yuyi::Menu.menu.progressbar
    Yuyi::Menu.menu.progressbar.log text
  elsif !args[:newline] || args[:overwrite]
    STDOUT.print text
  else
    STDOUT.puts text
  end
end

#write_to_file(file, *text) ⇒ Object

Write several lines to to an existing file



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/yuyi/dsl.rb', line 111

def write_to_file file, *text
  text.flatten!

  File.open(File.expand_path(file), 'a+') do |file|
    full_text = (text * "\n") + "\n"

    unless file.read.include? full_text
      file.write full_text
    end
  end
end