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

#initializeApplication

Returns a new instance of Application



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

def initialize
  self.options = {}
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options



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

def options
  @options
end

Class Method Details

.run(args = ARGV) ⇒ Object



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

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

Instance Method Details

#helpObject



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
138
139
140
141
142
# File 'lib/serve/application.rb', line 71

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}                        # launch server in current directory",
    "  #{program} 2100                   # launch server on port 2100",
    "  #{program} project                # launch server for project directory",
    "  #{program} create project         # new structured project in project dir",
    "  #{program} convert project        # convert a Compass project in directory",
    "  #{program} export project output  # export project to output directory",
    "  ",
    "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.",
    "   ",
    "  export",
    "    Exports a Serve project to regular HTML and CSS.",
    "   ",
    "Options:",
    "  -j, --javascript  The name of the JavaScript Framework you'd like to use.",
    "                    (Only for create and convert.)",
    "  -t, --template    The name of the template project to base the project on.",
    "                    Builtins: blank, default. (Only for create.)",
    "  -h, --help        Show this message and quit.",
    "  -v, --version     Show the program version number and quit.",
    "  ",
    "Further information:",
    "  http://get-serve.com/documentation"
  ].join("\n")
end

#parse(args) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/serve/application.rb', line 52

def parse(args)
  args = normalize_args(args)
  options[:help]        = extract_boolean(args, '-h', '--help')
  options[:version]     = extract_boolean(args, '-v', '--version')
  options[:create]      = extract_create(args)
  options[:convert]     = extract_convert(args)
  options[:export]      = extract_export(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

#run(args = ARGV) ⇒ Object



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

def run(args = ARGV)
  @options = parse(args)
  case
  when options[:create]
    require 'serve/project'
    Serve::Project.create(options[:create])
  when options[:convert]
    require 'serve/project'
    Serve::Project.convert(options[:convert])
  when options[:export]
    require 'serve/export'
    Serve.export(options[:export])
  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

#versionObject



67
68
69
# File 'lib/serve/application.rb', line 67

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