class Volt::CLI

Public Class Methods

source_root() click to toggle source
# File lib/volt/cli.rb, line 172
def self.source_root
  File.expand_path(File.join(File.dirname(__FILE__), '../../templates'))
end

Public Instance Methods

console() click to toggle source
# File lib/volt/cli.rb, line 37
def console
  require 'volt/cli/console'
  Console.start
end
drop_collection(collection) click to toggle source
# File lib/volt/cli.rb, line 109
def drop_collection(collection)
  ENV['SERVER'] = 'true'
  move_to_root
  require 'volt/boot'

  Volt.boot(Dir.pwd)

  db = Volt::DataStore.fetch
  drop = db.drop_collection(collection)

  say("Collection #{collection} could not be dropped", :red) if drop == false
  say("Collection #{collection} dropped", :green) if drop == true
end
move_to_root() click to toggle source
# File lib/volt/cli.rb, line 147
def move_to_root
  unless Gem.win_platform?
    # Change CWD to the root of the volt project
    pwd = Dir.pwd
    changed = false
    loop do
      if File.exists?(pwd + '/config.ru') || File.exists?(pwd + '/Gemfile')
        Dir.chdir(pwd) if changed
        break
      else
        changed = true

        # Move up a directory and try again
        pwd = pwd.gsub(/\/[^\/]+$/, '')

        if pwd == ''
          puts "You are not currently in a volt project directory"
          exit 1
        end
      end
    end
  end
end
new(name) click to toggle source
# File lib/volt/cli.rb, line 22
def new(name)
  new_project(name)

  say ""
  say "Your app is now ready in the #{name} directory.", :green
  say ""
  say "To run your app: "
  say ""
  say "  cd #{name}"
  say "  bundle exec volt server"
end
new_project(name, skip_gemfile = false, disable_encryption = false) click to toggle source

The logic for creating a new project. We want to be able to invoke this inside of a method so we can run it with Dir.chdir

# File lib/volt/cli.rb, line 126
def new_project(name, skip_gemfile = false, disable_encryption = false)
  require 'securerandom'

  # Grab the current volt version
  directory('project', name, {
    version: Volt::Version::STRING,
    name: name,
    domain: name.dasherize.downcase,
    app_name: name.capitalize,
    disable_encryption: disable_encryption
  })

  unless skip_gemfile
    # Move into the directory
    Dir.chdir(name) do
      # bundle
      bundle_command('install')
    end
  end
end
precompile() click to toggle source
# File lib/volt/cli/asset_compile.rb, line 5
def precompile
  move_to_root
  compile
end
runner(file_path) click to toggle source
# File lib/volt/cli.rb, line 99
def runner(file_path)
  ENV['SERVER'] = 'true'
  move_to_root
  require 'volt/cli/runner'

  Volt::CLI::Runner.run_file(file_path)
end
server() click to toggle source
# File lib/volt/cli.rb, line 46
def server
  move_to_root
  if ENV['PROFILE_BOOT']
    begin
      require 'ruby-prof'

      RubyProf.start
    rescue LoadError => e
      puts "To run volt in a profile mode, you must add ruby-prof gem to the app's Gemfile"
    end
  end

  require 'fileutils'
  require 'volt/server'
  require 'volt/utils/recursive_exists'

  # If we're in a Volt project, clear the temp directory
  # TODO: this is a work around for a bug when switching between
  # source maps and non-source maps.
  if RecursiveExists.exists_here_or_up?('config.ru') && RecursiveExists.exists_here_or_up?('Gemfile')
    # FileUtils.rm_rf('tmp/sass')
    # FileUtils.rm_rf('tmp/sprockets')
  else
    say('Current folder is not a Volt project', :red)
    return
  end

  ENV['SERVER'] = 'true'

  app = Volt::Server.new.app

  server = Rack::Handler.get(RUNNING_SERVER)

  opts = {}
  opts[:Port] = options[:port] || 3000
  opts[:Host] = options[:bind] if options[:bind]

  server.run(app, opts) do |server|
    case RUNNING_SERVER
    when 'thin'
      server.maximum_persistent_connections = 300
      server.maximum_connections = 500 unless Gem.win_platform?
      server.threaded = true

      # We need to disable the timeout on thin, otherwise it will keep
      # disconnecting the websockets.
      server.timeout = 0
    end
  end
end

Private Instance Methods

compile() click to toggle source
# File lib/volt/cli/asset_compile.rb, line 12
def compile
  say "Starting Precompile...", :red
  require 'fileutils'
  ENV['SERVER'] = 'true'
  ENV['MAPS'] = 'false'
  ENV['NO_FORKING'] = 'true'

  if !ENV['VOLT_ENV'] && !ENV['RACK_ENV']
    # Set the default env for compile
    ENV['VOLT_ENV'] = 'production'
  end

  require 'opal'
  require 'rack'
  require 'volt'
  require 'volt/volt/core'
  require 'volt/boot'
  require 'volt/server'
  require 'volt/server/rack/component_paths'
  require 'volt/server/rack/component_code'

  @root_path ||= Dir.pwd
  Volt.root  = @root_path

  @volt_app = Volt.boot(@root_path)

  @app_path = File.expand_path(File.join(@root_path, 'app'))

  say 'Compiling RB, JS, CSS, and Images...', :red
  write_files_and_manifest
  compile_manifests
  say 'Write index files...', :red
  write_index
  say "Done", :green
end
compile_manifests() click to toggle source
# File lib/volt/cli/asset_compile.rb, line 87
def compile_manifests
  manifest = Sprockets::Manifest.new(@volt_app.sprockets, "./public#{@volt_app.app_url}/manifest.json")

  # Compile the files (and linked assets)
  manifest.compile('main/app.js')
  manifest.compile('main/app.css')

  # Clear temp files
  @tmp_files.each {|path| FileUtils.rm(path) }

  # Remove the temp files
  FileUtils.rm(Volt.root + "#{@volt_app.app_url}/main/app.js")
  FileUtils.rm(Volt.root + "#{@volt_app.app_url}/main/app.scss")
end
write_file(path, data) click to toggle source
# File lib/volt/cli/asset_compile.rb, line 114
def write_file(path, data)
  FileUtils.mkdir_p(File.dirname(path))

  File.open(path, 'wb') do |file|
    file.write(data)
  end
end
write_files_and_manifest() click to toggle source
# File lib/volt/cli/asset_compile.rb, line 48
def write_files_and_manifest
  asset_files = AssetFiles.from_cache(@volt_app.app_url, 'main', @volt_app.component_paths)
  # Write a temp css file
  js = asset_files.javascript(@volt_app)
  css = asset_files.css
  @tmp_files = []

  File.open(Volt.root + '/app/main/app.js', 'wb') do |file|
    js.each do |type, src_or_body|
      if type == :src
        src = src_or_body
        url = src.gsub(/^#{@volt_app.app_url}\//, '')
        file.write("//= require '#{url}'\n")
      else
        body = src_or_body

        # Write to a tempfile, since sprockets can't mix requires and
        # code.

        require 'securerandom'
        hex = SecureRandom.hex
        tmp_path = Volt.root + "/app/main/__#{hex}.js"
        url = "main/__#{hex}"
        file.write("//= require '#{url}'\n")

        @tmp_files << tmp_path
        File.open(tmp_path, 'wb') {|f| f.write("#{body}\n") }
      end
    end
  end

  File.open(Volt.root + '/app/main/app.scss', 'wb') do |file|
    css.each do |link|
      url = link.gsub(/^#{@volt_app.app_url}\//, '')
      file.write("//= require '#{url}'\n")
    end
  end
end
write_index() click to toggle source
# File lib/volt/cli/asset_compile.rb, line 102
def write_index
  require 'volt/cli/base_index_renderer'

  output_path = "#{@root_path}/public/index.html"
  require 'json'

  @manifest = JSON.parse(File.read(@root_path + "/public#{@volt_app.app_url}/manifest.json"))
  output_html = BaseIndexRenderer.new(@volt_app, @manifest).html

  write_file(output_path, output_html)
end