Class: Serve::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/serve/application.rb

Defined Under Namespace

Classes: InvalidArgumentsError

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Application) initialize

Returns a new instance of Application



14
15
16
# File 'lib/serve/application.rb', line 14

def initialize
  self.options = {}
end

Instance Attribute Details

- (Object) options

Returns the value of attribute options



8
9
10
# File 'lib/serve/application.rb', line 8

def options
  @options
end

Class Method Details

+ (Object) run(args = ARGV)



10
11
12
# File 'lib/serve/application.rb', line 10

def self.run(args = ARGV)
  new.run(args)
end

Instance Method Details

- (Object) help



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
# File 'lib/serve/application.rb', line 66

def help
  program = File.basename($0)
  [
    "Serve is a rapid prototyping framework for Web applications. This is a basic ",
    "help message containing pointers to more information.",
    "  ",
    "Usage:",
    "  #{program} -h/--help",
    "  #{program} -v/--version",
    "  #{program} [port] [environment] [directory]",
    "  #{program} [address:port] [environment] [directory]",
    "  #{program} [address] [port] [environment] [directory]",
    "  #{program} command [arguments] [options]",
    "  ",
    "Examples:",
    "  #{program}                   # start Serve in current directory",
    "  #{program} 2100              # start Serve on port 2100",
    "  #{program} /path/to/project  # start Serve for a specific directory",
    "  #{program} create mockups    # create a Serve project in mockups directory",
    "  #{program} convert mockups   # convert a Compass project in mockups",
    "  ",
    "Description:",
    "  Starts a web server on the specified address and port with its document root ",
    "  set to the current working directory. (Optionally, you can specify the ",
    "  directory as the last parameter.) By default the command uses 0.0.0.0 for ",
    "  the address and 4000 for the port. This means that once the command has ",
    "  been started you can access the documents in the current directory with any ",
    "  web browser at:",
    "  ",
    "    http://localhost:4000/",
    "  ",
    "  If the haml, slim, redcloth, or bluecloth gems are installed the command can ",
    "  handle Haml, Slim, Sass, SCSS, Textile, and Markdown for documents with haml, ",
    "  slim, sass, scss, textile, and markdown file extensions.",
    "  ",
    "  If the Rails command script/server exists in the current directory the ",
    "  script will start that instead.",
    "  ",
    "  If a Rack configuration file (config.ru) exists in the current directory the ",
    "  script will start that using the `rackup` command.",
    "  ",
    "  A Rails or Rack app will start with the environment specified or the ",
    "  development environment if none is specified. Rails and Rack apps are ",
    "  started by default on port 3000.",
    "  ",
    "Additional Commands:",
    "  In addition the functionality listed above the following commands are ",
    "  supported by Serve.",
    "  ",
    "  create",
    "    Creates a new Rack-based Serve project with support for Haml, Sass, and",
    "    Compass with the appropriate directory structure and configuration files.",
    "  ",
    "  convert",
    "    Converts an existing Compass project into a Rack-based Serve project.",
    "   ",
    "Options:",
    "  -j, --javascript  The name of the JavaScript Framework you'd like to use.",
    "                    (Only valid for create and convert commands.)",
    "  -h, --help        Show this message and quit.",
    "  -v, --version     Show the program version number and quit.",
    "  ",
    "Further information:",
    "  http://github.com/jlong/serve/blob/master/README.rdoc"
  ].join("\n")
end

- (Object) parse(args)



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/serve/application.rb', line 48

def parse(args)
  args = normalize_args(args)
  options[:help]        = extract_boolean(args, '-h', '--help')
  options[:version]     = extract_boolean(args, '-v', '--version')
  options[:create]      = extract_creation(args)
  options[:convert]     = extract_conversion(args)
  options[:environment] = extract_environment(args)
  options[:root]        = extract_root(args)
  options[:address]     = extract_address(args)
  options[:port]        = extract_port(args)
  raise InvalidArgumentsError if args.size > 0
  options
end

- (Object) run(args = ARGV)



18
19
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
# File 'lib/serve/application.rb', line 18

def run(args = ARGV)
  @options = parse(args)
  case
  when options[:create]
    require 'serve/project'
    Serve::Project.new(options[:create]).create
  when options[:convert]
    require 'serve/project'
    Serve::Project.new(options[:convert]).convert
  when options[:version]
    puts version
  when options[:help]
    puts help
  else
    Dir.chdir(options[:root])
    case
    when rails_app?
      run_rails_app
    when rack_app?
      run_rack_app
    else
      run_server
    end
  end
rescue InvalidArgumentsError
  puts "invalid arguments"
  puts
  puts help
end

- (Object) version



62
63
64
# File 'lib/serve/application.rb', line 62

def version
  "Serve #{Serve.version}"
end