Class: Nimbu::Command::Server

Inherits:
Base
  • Object
show all
Includes:
Term::ANSIColor
Defined in:
lib/nimbu/command/server.rb

Overview

running a local server to speed up designing Nimbu themes

Instance Attribute Summary

Attributes inherited from Base

#args, #options

Instance Method Summary collapse

Methods inherited from Base

#initialize, namespace, #nimbu

Methods included from Helpers

#action, #ask, #confirm, #confirm_billing, #confirm_command, #create_git_remote, #deprecate, disable_error_capture, #display, #display_header, #display_object, #display_row, #display_table, enable_error_capture, #error, error_with_failure, error_with_failure=, extended, extended_into, #fail, #format_bytes, #format_date, #format_error, #format_with_bang, #get_terminal_environment, #git, #has_git?, #home_directory, #hprint, #hputs, included, included_into, #json_decode, #json_encode, #launchy, #line_formatter, #longest, #output, #output_with_arrow, #output_with_bang, #quantify, #redisplay, #retry_on_exception, #run_command, #running_on_a_mac?, #running_on_windows?, #set_buffer, #shell, #spinner, #status, #string_distance, #styled_array, #styled_error, #styled_hash, #styled_header, #suggestion, #time_ago, #truncate, #with_tty

Methods included from Helpers::System

#browser_launcher, #command?, #osx?, #tmp_dir, #which, #windows?

Constructor Details

This class inherits a constructor from Nimbu::Command::Base

Instance Method Details

#indexObject

server

starts a local development server, using the data from the Nimbu cloud in real time.

-p PORT, –port PORT # set the port on which to start the http server -h, –haml # start local HAML watcher -c, –compass # start local Compass watcher -d, –debug # enable debugging output



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/nimbu/command/server.rb', line 20

def index
  # Check if config file is present?
  if !Nimbu::Auth.read_configuration || !Nimbu::Auth.read_credentials
    print red(bold("ERROR")), ": this directory does not seem to contain any Nimbu theme or your credentials are not set. \n ==> Run \"", bold { "nimbu init"}, "\" to initialize this directory."
  else
    no_compilation = true #! options[:'no-compile']
    with_haml = options[:haml]
    with_compass = options[:compass]

    if with_compass
      require 'compass'
      require 'compass/exec'
    end

    if with_haml
      require 'haml'
    end

    services = []
    services << "HAML" if with_haml
    services << "Compass" if with_compass
    title = "Starting up Nimbu Server"
    title << "(with local #{services.join(' and ')} watcher)" if with_compass || with_haml
    title << "..."
    puts white("\n#{title}")
    puts green(nimbu_header)
    puts green("\nConnnected to '#{Nimbu::Auth.site}.#{Nimbu::Auth.admin_host}', using '#{Nimbu::Auth.theme}' theme#{Nimbu.debug ? ' (in debug mode)'.red : nil}.\n")

    server_read, server_write = IO::pipe
    haml_read, haml_write = IO::pipe
    compass_read, compass_write = IO::pipe
    compiler_read, compiler_write = IO::pipe

    server_pid = Process.fork do
      $stdout.reopen(server_write)
      server_read.close
      puts "Starting server..."
      server_options = {
        :Port               => options[:port] || 4567,
        :DocumentRoot       => Dir.pwd
      }
      Rack::Handler::Thin.run Nimbu::Server::Base, server_options  do |server|
        [:INT, :TERM].each { |sig| trap(sig) { server.respond_to?(:stop!) ? server.stop! : server.stop } }
      end
    end

    # assets_pid = Process.fork do
    #   $stdout.reopen(compiler_write)
    #   compiler_read.close
    #   puts "Starting watcher..."
    #   HamlWatcher.watch
    # end unless no_compilation

    haml_pid = Process.fork do
      $stdout.reopen(haml_write)
      haml_read.close
      puts "Starting..."
      haml_listener = HamlWatcher.watch
      [:INT, :TERM].each do |sig|
        Signal.trap(sig) do
          puts green("== Stopping HAML watcher\n")
          Thread.new { haml_listener.stop }
        end
      end
      Process.waitall
    end if with_haml

    compass_pid = Process.fork do
      $stdout.reopen(compass_write)
      compass_read.close
      puts "Starting..."
      Compass::Exec::SubCommandUI.new(["watch","."]).run!
    end if with_compass

    watch_server_pid = Process.fork do
      trap('INT') { exit }
      server_write.close
      server_read.each do |line|
        print cyan("SERVER:  ") + white(line) + ""
      end
    end

    # watch_assets_pid = Process.fork do
    #   trap('INT') { exit }
    #   compiler_write.close
    #   compiler_read.each do |line|
    #     print magenta("ASSETS:    ") + white(line) + ""
    #   end
    # end unless no_compilation

    watch_haml_pid = Process.fork do
      trap('INT') { exit }
      haml_write.close
      haml_read.each do |line|
        print magenta("HAML:    ") + white(line) + ""
      end
    end if with_haml

    watch_compass_pid = Process.fork do
      trap('INT') { exit }
      compass_write.close
      compass_read.each do |line|
        print yellow("COMPASS: ") + white(line) + ""
      end
    end if with_compass

    [:INT, :TERM].each do |sig|
      trap(sig) do
        puts yellow("\n== Waiting for all processes to finish...")
        Process.kill('INT', haml_pid) if haml_pid && running?(haml_pid)
        Process.waitall
        puts green("== Nimbu has ended its work " + bold("(crowd applauds!)\n"))
      end
    end

    Process.waitall
  end
end