Class: YAMG::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/yamg/cli.rb

Overview

Command line interface

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = {}) ⇒ CLI

Returns a new instance of CLI.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/yamg/cli.rb', line 6

def initialize(argv = {})
  puts
  puts Rainbow('     Y              A               M               G').red
  puts
  if argv.join =~ /debug/
    YAMG.debug = true
    puts Rainbow('!!! DEBUG !!!').red
    argv.delete('debug')
  end
  return YAMG.init if argv.join =~ /init/
  YAMG.load_config # (argv)
  @works = YAMG.config['compile']
  @scope = argv.empty? ? nil : argv.join
end

Instance Attribute Details

#scopeObject

Returns the value of attribute scope.



4
5
6
# File 'lib/yamg/cli.rb', line 4

def scope
  @scope
end

#worksObject

Returns the value of attribute works.



4
5
6
# File 'lib/yamg/cli.rb', line 4

def works
  @works
end

Instance Method Details

#compileObject



90
91
92
93
94
# File 'lib/yamg/cli.rb', line 90

def compile
  works.select! { |k, _v| k =~ /#{scope}/ } if scope
  puts Rainbow("Tasks: #{works.keys.join(', ')}").yellow
  works.each { |out, opts| compile_work(out, opts) }
end

#compile_docs(opts) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/yamg/cli.rb', line 66

def compile_docs(opts)
  out = opts['path']
  %w(manifest.json browserconfig.xml).each do |doc|
    next if File.exist?(out)
    puts Rainbow("{DOCS} #{out}/#{doc} created. Please review.").red
    src = File.expand_path("assets/#{doc}", File.dirname(__FILE__))
    FileUtils.cp(src, out)
  end
end

#compile_icon(i, size, setup) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/yamg/cli.rb', line 46

def compile_icon(i, size, setup)
  folder = setup['icon'] || YAMG.config['icon']['path']
  # Don' use || here, we are after false
  bg = setup['bg'] || setup['background']
  round = setup['rounded']
  round = YAMG.config['icon']['rounded'] if round.nil?
  radius = setup['radius']
  Icon.new(folder, size, bg, round, radius).image(home_for(i, setup))
  print Rainbow(round ? '(i)' : '[i]').black
  YAMG.info("Icon    #{size}px -> #{setup['path']}#{i} ", :black)
end

#compile_media(i, size, setup) ⇒ Object



43
44
# File 'lib/yamg/cli.rb', line 43

def compile_media(i, size, setup)
end

#compile_screenshots(ss, size, setup) ⇒ Object



36
37
38
39
40
41
# File 'lib/yamg/cli.rb', line 36

def compile_screenshots(ss, size, setup)
  return unless YAMG.config['screenshot'].respond_to?(:[])
  raise 'No url provided' unless (url = YAMG.config['screenshot']['url'])
  Screenshot.new(ss, 'size' => size, 'url' => url).work(setup['path'])
  puts Rainbow("[o]SS #{ss}").black
end

#compile_splash(s, size, setup) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/yamg/cli.rb', line 58

def compile_splash(s, size, setup)
  path = setup['splash'] || YAMG.config['splash']['path']
  background = YAMG.config['splash']['background']
  Splash.new(path, size, background).image(home_for(s, setup))
  print Rainbow('{S}').black
  YAMG.info("Splash #{size.join('x')}px #{setup['path']}#{s}", :black)
end

#compile_work(job, opts) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/yamg/cli.rb', line 76

def compile_work(job, opts)
  task = YAMG::TEMPLATES[job] || (works[job] && works[job]['export'])
  %w(icon logo splash media screenshots).each do |subtask|
    next unless (work = task[subtask])

    work.each do |asset, size|
      Thread.new do # 500% speed up with 8 cores
        send(:"compile_#{subtask}", asset, size, setup_for(opts))
      end
    end
  end
  compile_docs(opts) if task['docs']
end

#home_for(asset, setup) ⇒ Object



30
31
32
33
34
# File 'lib/yamg/cli.rb', line 30

def home_for(asset, setup)
  path = setup['path']
  FileUtils.mkdir_p path unless File.exist?(path)
  File.join(path, asset)
end

#screenshotObject



96
97
98
99
100
101
102
103
104
# File 'lib/yamg/cli.rb', line 96

def screenshot
  return unless YAMG.config['screenshots'].respond_to?(:each)
  YAMG.config['screenshots'].each do |ss|
    Thread.new do
      Screenshot.new(ss).work('./export')
      puts Rainbow("[o]SS #{ss[0]} #{ss[1]}").black
    end
  end
end

#setup_for(opts) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/yamg/cli.rb', line 21

def setup_for(opts)
  case opts
  when Hash then { 'path' => './export/' }.merge(opts)
  when String then { 'path' => opts }
  when TrueClass then { 'path' => './export/' }
  else raise
  end
end

#work!Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/yamg/cli.rb', line 106

def work!
  time = Time.now
  compile
  screenshot if scope.nil? || scope =~ /ss|shot|screen/
  puts
  puts Rainbow(Thread.list.size.to_s + ' jobs to go').black
  Thread.list.reject { |t| t == Thread.current }.each(&:join)
  puts Rainbow("\n" + '-' * 59).black
  puts Rainbow("Done compile #{(Time.now - time).to_i}s").red
end