Class: Tay::CLI::Root

Inherits:
Thor
  • Object
show all
Includes:
Helpers, Thor::Actions
Defined in:
lib/tay/cli.rb,
lib/tay/cli/new.rb,
lib/tay/cli/build.rb,
lib/tay/cli/watch.rb,
lib/tay/cli/minify.rb,
lib/tay/cli/package.rb,
lib/tay/cli/validate.rb

Constant Summary

Constants included from Helpers

Helpers::DEFAULT_TAYFILE

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



24
25
26
# File 'lib/tay/cli.rb', line 24

def self.source_root
  File.expand_path('cli/templates', File.dirname(__FILE__))
end

Instance Method Details

#buildObject



9
10
11
12
# File 'lib/tay/cli/build.rb', line 9

def build
  builder = Builder.new(spec, base_dir, build_dir)
  builder.build!
end

#minifyObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tay/cli/minify.rb', line 13

def minify
  unless options['skip-js']
    begin
      require 'uglifier'
      Dir[build_dir.join('**/*.js')].each do |path|
        content = File.read(path)
        File.open(path, 'w') do |f|
          f.write Uglifier.compile(content)
        end
      end
    rescue LoadError
      say('ERROR: please add the uglifier gem to your Gemfile to minfy javascripts', :red)
    end
  end

  unless options['skip-css']
    begin
      require 'yui/compressor'
      Dir[build_dir.join('**/*.css')].each do |path|
        content = File.read(path)
        File.open(path, 'w') do |f|
          f.write YUI::CssCompressor.new.compress(content)
        end
      end
    rescue LoadError
      say('ERROR: please add the yui-compressor gem to your Gemfile to minfy css files', :red)
    end
  end
end

#new(name) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/tay/cli/new.rb', line 13

def new(name)
  outdir = Pathname.new(Utils.filesystem_name(name))
  create_directory_structure(outdir)

  template('Gemfile', outdir.join('Gemfile')) unless options['no-gemfile']
  copy_file('gitignore', outdir.join('.gitignore')) unless options['no-gitignore']
  template('Tayfile', outdir.join('Tayfile'), {
    'name' => name
  }.merge(options))
end

#packageObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tay/cli/package.rb', line 11

def package
  unless %{zip crx} .include?(options['type'])
    raise InvalidPackageType.new("Invalid package type '#{options['type']}'")
  end

  packager = Packager.new(spec, base_dir, build_dir)

  if packager.private_key_exists?
    say("Using private key at #{Utils.relative_path_to(packager.full_key_path)}", :green)
  else
    say("Creating private key at #{Utils.relative_path_to(packager.full_key_path)}", :yellow)
    packager.write_new_key
  end

  empty_directory(base_dir.join('tmp'))
  empty_directory(base_dir.join('pkg'))

  temp_pkg_path = base_dir.join('tmp', 'package')
  temp_pkg_path.unlink if temp_pkg_path.exist?
  packager.send("write_#{options['type']}".to_sym, temp_pkg_path)

  filename = Utils.filesystem_name(spec.name) + '-' + spec.version + '.' + options['type']
  pkg_path = base_dir.join('pkg', filename)

  copy_file(temp_pkg_path, pkg_path)
  say("The extension's ID is #{packager.extension_id}")
  say("Wrote #{pkg_path.size} bytes to #{Utils.relative_path_to(pkg_path)}", :green)
  temp_pkg_path.unlink
end

#validateObject



9
10
11
12
13
14
15
16
17
18
# File 'lib/tay/cli/validate.rb', line 9

def validate
  validator = SpecificationValidator.new(spec, build_dir)
  validator.on_message = lambda do |type, message|
    say(type.upcase + ": " + message, type == 'warn' ? :yellow : :red)
  end

  if validator.validate!
    say("All OK!", :green)
  end
end

#watchObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/tay/cli/watch.rb', line 9

def watch
  begin
    require 'guard'
    require 'guard/tay'
  rescue LoadError
    say('ERROR: please add the guard and guard-tay gems to your Gemfile to enable auto compilation', :red)
    return
  end

  template_path = "#{::Guard.locate_guard('tay')}/lib/guard/tay/templates/Guardfile"

  copy_file(template_path, File.expand_path('Guardfile', '.'))

  say('Auto-compilation is now set up. Run `guard` to start watching for changes.', :green)
end