Sha256: 88828c0550e2b35bdefce72fba2f900ce0e45400d8f6a857649999b1af43261a

Contents?: true

Size: 1.75 KB

Versions: 8

Compression:

Stored size: 1.75 KB

Contents

module Susanoo::CLI
  module Commands
    # Provide the `server` command for project wide usage.
    module Server
      extend ::ActiveSupport::Concern

      included do
        map 's' => :server

        method_option :debug, default: true
        method_option :built, default: false
        method_option :port, default: 3000
        desc 'server [PLATFROM]', 'Run development server. Simulate the given PLATFORM'
        def server(platform = 'android')
          port = options[:port]

          if options[:built]
            return not_built unless built?
            app = static_server_for platform

          else
            require File.join(project_root, 'config/routes')
            # Set global debug flag
            Susanoo::Project.debug = options[:debug]
            app = ROUTER

          end
          # Run the selected application to serve the project
          Rack::Server.start(app: app, server: :thin, Port: port,
                             debug: options[:debug])
        end
      end

      private

      # Does project is built?
      def built?
        return false unless File.directory? File.join(project_root, 'www')
        true
      end

      def not_built
        error "'www' directory is not present. Build you app first."
      end

      # Return a `Rack` applciation which will serve the
      # already built project under specific platform path
      def static_server_for(platform)
        require 'rack/rewrite'

        root = project_root

        app = Rack::Builder.new do
          use Rack::Rewrite do
            rewrite   '/',  "/#{platform}_asset/www"
          end

          map "/#{platform}_asset/www/" do
            run Rack::Directory.new File.join(root, 'www')
          end
        end

      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
susanoo-0.14.0 lib/susanoo/cli/project_interface/server.rb
susanoo-0.13.2 lib/susanoo/cli/project_interface/server.rb
susanoo-0.13.1 lib/susanoo/cli/project_interface/server.rb
susanoo-0.13.0 lib/susanoo/cli/project_interface/server.rb
susanoo-0.12.3 lib/susanoo/cli/project_interface/server.rb
susanoo-0.12.2 lib/susanoo/cli/project_interface/server.rb
susanoo-0.12.1 lib/susanoo/cli/project_interface/server.rb
susanoo-0.12.0 lib/susanoo/cli/project_interface/server.rb