class Blix::Cli::Application

Constants

DIR_LIST
FILE_LIST
TEMPLATE_DIR
VALID_RESPONSE
VUE_FILES
WEBPACK_FILES

Attributes

options[R]
root[R]

Public Class Methods

new(root, options={}) click to toggle source
# File lib/blix/cli/application.rb, line 50
def initialize(root, options={})
  @root = root
  @options = Thor::CoreExt::HashWithIndifferentAccess.new.merge options
  @options[:vuebasic] = options[:vue] && !options[:webpack]
end

Public Instance Methods

check_command(name, version=nil) click to toggle source
# File lib/blix/cli/application.rb, line 102
def check_command(name, version=nil)
   std, err, stat = Open3.capture3("#{name} --version")
   raise Error, err unless stat.success?
   raise Error, "need #{name} version #{version} or higher" if version && (std < version)
   true
end
confirm(message) click to toggle source
# File lib/blix/cli/application.rb, line 111
def confirm(message)
  return true if options[:y]
  resp = nil
  loop do
    print message + '? (y/n): '
    resp = VALID_RESPONSE.index $stdin.gets.chomp.strip.downcase
    break if resp
  end
  resp < 2   # the yes responses.
end
ensure_directory(name) click to toggle source

ensure that the directory exists.

# File lib/blix/cli/application.rb, line 57
def ensure_directory(name)
  path = File.join(root,name)
  if File.exist?(path)
    if File.directory?(path)
      NullOperation.new("have directory   #{path}")
    else
      raise Error, "cannot create directory:#{path}"
    end
  else
    DirectoryOperation.new(path)
  end
end
ensure_file(name) click to toggle source

ensure that the file exists.

# File lib/blix/cli/application.rb, line 83
def ensure_file(name)
  path = File.join(root,name)
  new_contents = get_template(name)
  if File.exist?(path)
    if File.file?(path)
      old_contents = File.read(path)
      if (old_contents != new_contents) && (options[:overwrite] || confirm("overwrite file #{path}"))
        FileOperation.new(path, new_contents )
      else
        NullOperation.new("have file   #{path}")
      end
    else
      raise Error, "cannot create file:#{path}"
    end
  else
    FileOperation.new(path, new_contents )
  end
end
framework() click to toggle source

generate a new application framework

# File lib/blix/cli/application.rb, line 164
def framework
  ensure_directory('')
  DIR_LIST.sort.each{|p| ensure_directory(p) }
  FILE_LIST.sort.each{|p| ensure_file(p) }
  CommandOperation.new(File.join(root,'bin'), 'chmod a+x *', 'set permissions on bin dir') 
end
get_template(name) click to toggle source
# File lib/blix/cli/application.rb, line 70
def get_template(name)
  template_name = name.gsub('/','-')
  template_path = File.join(TEMPLATE_DIR,template_name)
  if File.file?( template_path )
    File.read( template_path )
    template = Liquid::Template.parse(File.read( template_path ))
    template.render(options)
  else
    nil
  end
end
node_package(package) click to toggle source

add a node package unless it exists.

# File lib/blix/cli/application.rb, line 123
def node_package(package)
  if File.exist? File.join(root,'node_modules',package)
    NullOperation.new("have node package   #{package}")
  else
    CommandOperation.new(root, "npm install #{package} --save-dev", "install     #{package}")
  end
end
setup() click to toggle source

setup the environment for the given options.

# File lib/blix/cli/application.rb, line 172
def setup
  framework

  if options[:webpack]
     webpack
  end

  if (options[:'dry-run'])
    puts "dry run .."
    Operation.describe_all
  else
    Operation.execute_all
  end
end
webpack() click to toggle source

setup webpack npm init -y # created package.json file npm install webpack webpack-cli –save-dev npm install coffeescript coffee-loader –save-dev

# File lib/blix/cli/application.rb, line 136
def webpack

  check_command("node", '4.0.0')
  check_command("npm", '6.0.0')

  CommandOperation.new(root, 'npm init -y', 'initialise       node.js') unless File.exist?(File.join(root,'package.json'))
  node_package('webpack')
  node_package('webpack-cli')
  node_package('coffeescript')
  node_package('coffee-loader')


  if options[:vue]
    node_package('vue')
    node_package('vue-template-compiler')
    node_package('vue-loader')
    node_package('vue-style-loader')
    node_package('vue-router')
    node_package('css-loader')
    node_package('sass-loader')
    ensure_directory('assets/js/components')
    VUE_FILES.sort.each{|p| ensure_file(p) }
  end

  WEBPACK_FILES.sort.each{|p| ensure_file(p) }
end