jetski 0.3.7 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ca235e070d85558c0d606ca2883f75c1bec7d9bb9e73ed1cde448c678478244
4
- data.tar.gz: e55510b4db502090268d2aae1dc604c5202ca8598effb5435e8bac60a7ade7bc
3
+ metadata.gz: ffe71a8de5f4201c446a70331fae92f982af3887b388426e5b64f1b6c3d897c8
4
+ data.tar.gz: 73a1faada7420962abfdbab298f7f81157431b995452ca5fc2adefd903d074ec
5
5
  SHA512:
6
- metadata.gz: 6fbd7c1108433ea9415787e6810b9bd86242b697ceabd4ed4acbce9e88dc72686a43ada361cca52ae8385dff3a153eb48faf47a41b5e3bd6486bbc015445debd
7
- data.tar.gz: 1a2d75bcb797b8e355fcaa5a9990014b522755ac7dc62f706e63b0983bf9dc2da17840fb3893e46e3ff0d0a36e878592e3317b8f93033147c9c3fb456927ca3e
6
+ metadata.gz: 35e4cfce787089dfe046575b38969927013e09a91b855dee64af15394872eed1660a087ecdd801fb4c0ede590b491da3759b5d68f1ff2c92a49363426f5a8f38
7
+ data.tar.gz: '049857042cd6299e591dab667ef9f3943637649905fe77e09174eabdc3a1131abb9849e93e35f07adb32d9c09dda067ffc19c8d671ddade29715d74ff0c9acfa'
data/bin/jetski CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'optparse'
4
3
  require 'jetski'
4
+ require 'optparse'
5
5
  require 'thor'
6
6
  require_relative './jetski_cli_helpers/generate.rb'
7
7
  require_relative './jetski_cli_helpers/destroy.rb'
@@ -15,8 +15,8 @@ class JetskiCLI < Thor
15
15
  FileUtils.mkdir_p(name)
16
16
  say "Creating #{name} app with Jetski!"
17
17
  directory "base", "#{name}"
18
- gsub_file "#{name}/app/views/layouts/application.html", /APP_NAME/, name.capitalize
19
- gsub_file "#{name}/app/views/pages/home.html", /APP_NAME/, name.capitalize
18
+ gsub_file "#{name}/app/views/layouts/application.html.erb", /APP_NAME/, name.capitalize
19
+ gsub_file "#{name}/app/views/pages/home.html.erb", /APP_NAME/, name.capitalize
20
20
  run "cd #{name} && bundle install"
21
21
  say "🌊 Your app #{name} has been generated!"
22
22
  end
@@ -37,7 +37,6 @@ class JetskiCLI < Thor
37
37
 
38
38
  desc "server", "starts the jetski app"
39
39
  def server
40
- ENV['JETSKI_PROJECT_PATH'] = __dir__
41
40
  Jetski::Server.new.call
42
41
  end
43
42
 
@@ -5,27 +5,36 @@ module JetskiCLIHelpers
5
5
  desc "controller NAME ACTION_NAMES", "Create a controller with matching actions"
6
6
  def controller(name, *actions)
7
7
  controller_file_path = "app/controllers/#{name}_controller.rb"
8
- copy_file("controllers/example_controller", controller_file_path)
9
- gsub_file(controller_file_path, /CONTROLLER_NAME/, name.capitalize)
10
- actions.each.with_index do |action_name, idx|
11
- action_content = "def #{action_name}\n end"
12
- if actions.size < (idx + 1)
13
- action_content += "\nACTION_NAME"
8
+ create_file controller_file_path
9
+ append_to_file controller_file_path, <<~CONTROLLER
10
+ class #{name.capitalize}Controller < Jetski::BaseController
11
+
14
12
  end
15
- gsub_file(controller_file_path, /ACTION_NAME/, action_content.strip)
16
- path_to_view = "app/views/#{name}/#{action_name}.html"
13
+ CONTROLLER
14
+
15
+ actions.each.with_index do |action_name, idx|
16
+ action_content = <<~ACTION_CONTENT
17
+ def #{action_name}
18
+
19
+ end
20
+ ACTION_CONTENT
21
+ insert_into_file(controller_file_path, indent_code(action_content, 1), before: "\nend")
22
+ path_to_view = "app/views/#{name}/#{action_name}.html.erb"
17
23
 
18
24
  empty_directory("app/views/#{name}")
19
- copy_file("views/example.html", path_to_view)
20
- gsub_file(path_to_view, /CONTROLLER_NAME/, name)
21
- gsub_file(path_to_view, /ACTION_NAME/, action_name)
22
- gsub_file(path_to_view, /PATH_TO_VIEW/, path_to_view)
25
+ create_file(path_to_view)
26
+ append_to_file path_to_view, <<~EXAMPLEFILE
27
+ <h1> #{name}##{action_name} </h1>
28
+ <p> edit the content of this page at app/views/#{path_to_view}/#{action_name}.html.erb </p>
29
+ EXAMPLEFILE
30
+
23
31
  say "🌊 View your new page at http://localhost:8000/#{name}/#{action_name}"
24
32
  end
25
33
  end
26
34
 
27
- def self.source_root
28
- File.join(File.dirname(__FILE__), '..', 'templates')
35
+ private
36
+ def indent_code(code, level = 1)
37
+ code.strip.split("\n").map { |l| (1..level).map { |lvl| " " }.join + l }.join("\n")
29
38
  end
30
39
  end
31
40
  end
@@ -0,0 +1 @@
1
+ console.log("🌊 Hello Jetski JS!")
@@ -1,5 +1,5 @@
1
1
  class PagesController < Jetski::BaseController
2
- root
3
2
  def home
3
+ @root = true
4
4
  end
5
5
  end
@@ -4,11 +4,9 @@
4
4
  <meta charset="UTF-8">
5
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
6
  <title> APP_NAME </title>
7
- <link rel="stylesheet" href="/application.css">
8
7
  <link rel="icon" type="image/x-icon" href="/jetski-logo.png">
9
- DYNAMIC_CSS
10
8
  </head>
11
9
  <body>
12
- YIELD_CONTENT
10
+ <%= yield %>
13
11
  </body>
14
12
  </html>
@@ -4,6 +4,7 @@ module Jetski
4
4
  attr_accessor :action_name, :controller_name, :controller_path, :params
5
5
  attr_reader :res
6
6
  attr_reader :performed_render
7
+ attr_writer :root, :path, :request_method
7
8
 
8
9
  class << self
9
10
  def request_method(method)
@@ -50,23 +51,72 @@ module Jetski
50
51
  res.set_redirect(WEBrick::HTTPStatus::Found, url)
51
52
  end
52
53
 
54
+ def is_root?
55
+ @root == true
56
+ end
57
+
58
+ def custom_path
59
+ @path
60
+ end
61
+
62
+ def custom_request_method
63
+ @request_method
64
+ end
65
+
53
66
  private
54
67
  def render_template_file
55
- views_folder = File.join(Jetski.app_root, 'app/views')
56
- assets_folder = File.join(Jetski.app_root, 'app/assets/stylesheets')
57
- layout_content = File.read(File.join(views_folder, "layouts/application.html"))
58
- path_to_controller = controller_path[1..-1]
59
- page_content = File.read(File.join(views_folder, path_to_controller, "#{action_name}.html"))
60
- page_with_layout = layout_content.gsub("YIELD_CONTENT", page_content)
61
- action_css_file = File.join(assets_folder, "#{path_to_controller}.css")
62
- css_content = if File.exist? action_css_file
63
- "<link rel='stylesheet' href='/#{path_to_controller}.css'>"
64
- else
65
- ''
66
- end
67
- page_with_css = page_with_layout.gsub("DYNAMIC_CSS", css_content)
68
+ view_render = page_with_layout.gsub("\n</head>", "#{content_for_head}\n</head>")
68
69
  res.content_type = "text/html"
69
- res.body = page_with_css
70
+ res.body = view_render
71
+ end
72
+
73
+ def page_with_layout
74
+ process_erb(File.read(File.join(views_folder, "layouts/application.html.erb"))) do
75
+ process_erb(File.read(File.join(views_folder, path_to_controller, "#{action_name}.html.erb")))
76
+ end
77
+ end
78
+
79
+ def content_for_head
80
+ _content_for_head = ''
81
+
82
+ application_css_file = File.join(assets_folder, "stylesheets", "application.css")
83
+ if File.exist? application_css_file
84
+ _content_for_head += "<link rel='stylesheet' href='/application.css'>\n"
85
+ end
86
+
87
+ controller_css_file = File.join(assets_folder, "stylesheets", "#{path_to_controller}.css")
88
+ if File.exist? controller_css_file
89
+ _content_for_head += "<link rel='stylesheet' href='/#{path_to_controller}.css'>\n"
90
+ end
91
+
92
+ application_js_file = File.join(assets_folder, "javascript", "application.js")
93
+ if File.exist? application_js_file
94
+ _content_for_head += "<script src='application.js' defer></script>\n"
95
+ end
96
+
97
+ controller_js_file = File.join(assets_folder, "javascript", "#{path_to_controller}.js")
98
+ if File.exist? controller_js_file
99
+ _content_for_head += "<script src='/#{path_to_controller}.js' defer></script>\n"
100
+ end
101
+
102
+ _content_for_head
103
+ end
104
+
105
+ def views_folder
106
+ File.join(Jetski.app_root, 'app/views')
107
+ end
108
+
109
+ def assets_folder
110
+ File.join(Jetski.app_root, 'app/assets')
111
+ end
112
+
113
+ def path_to_controller
114
+ controller_path[1..-1]
115
+ end
116
+
117
+ def process_erb(content)
118
+ template = ERB.new(content)
119
+ template.result(binding)
70
120
  end
71
121
  end
72
122
  end
@@ -5,85 +5,84 @@ module Jetski
5
5
  def compile_routes
6
6
  auto_found_routes = []
7
7
  controller_file_paths = Dir.glob([File.join(Jetski.app_root, 'app', 'controllers', '**', '*_controller.rb')])
8
- controller_file_paths.each do |file_path|
8
+ controller_file_paths.each do |file_path|
9
+ require_relative file_path
9
10
  controller_file_name = file_path.split('app/controllers')[1]
10
11
  controller_path = controller_file_name.gsub(/_controller.rb/, '')
11
12
  controller_name = controller_path.split("/").last
12
- controller_classname = controller_path.split("/").reject(&:empty?).map(&:capitalize).join("::") + "Controller"
13
- controller_file_readlines = File.readlines(file_path)
14
- controller_file_readlines.each.with_index do |line, idx|
15
- strp_line = line.strip
16
- if strp_line.start_with?('def')
17
- action_name = strp_line.split("def").last.strip
18
- base_opts = {
19
- controller_classname: controller_classname,
20
- controller_file_name: controller_file_name,
21
- controller_name: controller_name,
22
- controller_path: controller_path,
23
- }
24
- case action_name
25
- when "new"
26
- auto_found_routes << base_opts.merge({
27
- url: controller_path + "/new",
28
- method: "GET",
29
- action_name: action_name,
30
- })
31
- when "create"
32
- auto_found_routes << base_opts.merge({
33
- url: controller_path,
34
- method: "POST",
35
- action_name: action_name,
36
- })
37
- when "show"
38
- auto_found_routes << base_opts.merge({
39
- url: controller_path + "/:id",
40
- method: "GET",
41
- action_name: action_name,
42
- })
43
- when "edit"
44
- auto_found_routes << base_opts.merge({
45
- url: controller_path + "/:id/edit",
46
- method: "GET",
47
- action_name: action_name,
48
- })
49
- when "update"
50
- auto_found_routes << base_opts.merge({
51
- url: controller_path + "/:id",
52
- method: "PUT",
53
- action_name: action_name,
54
- })
55
- when "destroy"
56
- auto_found_routes << base_opts.merge({
57
- url: controller_path + "/:id",
58
- method: "DELETE",
59
- action_name: action_name,
60
- })
13
+ controller_classname = controller_path
14
+ .split("/")
15
+ .reject(&:empty?)
16
+ .map(&:capitalize)
17
+ .map { |c| c.split("_").map(&:capitalize).join }
18
+ .join("::") + "Controller"
19
+ controller_class = Module.const_get(controller_classname)
20
+ action_names = controller_class.instance_methods(false)
21
+ base_opts = {
22
+ controller_classname: controller_classname,
23
+ controller_file_name: controller_file_name,
24
+ controller_name: controller_name,
25
+ controller_path: controller_path,
26
+ }
27
+ action_names.each do |action_name|
28
+ action_name = action_name.to_s
29
+ case action_name
30
+ when "new"
31
+ auto_found_routes << base_opts.merge({
32
+ url: controller_path + "/new",
33
+ method: "GET",
34
+ action_name: action_name,
35
+ })
36
+ when "create"
37
+ auto_found_routes << base_opts.merge({
38
+ url: controller_path,
39
+ method: "POST",
40
+ action_name: action_name,
41
+ })
42
+ when "show"
43
+ auto_found_routes << base_opts.merge({
44
+ url: controller_path + "/:id",
45
+ method: "GET",
46
+ action_name: action_name,
47
+ })
48
+ when "edit"
49
+ auto_found_routes << base_opts.merge({
50
+ url: controller_path + "/:id/edit",
51
+ method: "GET",
52
+ action_name: action_name,
53
+ })
54
+ when "update"
55
+ auto_found_routes << base_opts.merge({
56
+ url: controller_path + "/:id",
57
+ method: "PUT",
58
+ action_name: action_name,
59
+ })
60
+ when "destroy"
61
+ auto_found_routes << base_opts.merge({
62
+ url: controller_path + "/:id",
63
+ method: "DELETE",
64
+ action_name: action_name,
65
+ })
66
+ else
67
+ tmp_controller_instance = controller_class.new("")
68
+ tmp_controller_instance.send(action_name)
69
+ is_root = tmp_controller_instance.is_root?
70
+ custom_path = tmp_controller_instance.custom_path
71
+ url_to_use = if is_root
72
+ "/"
73
+ elsif custom_path
74
+ custom_path
61
75
  else
62
- method_route_options = controller_file_readlines[(idx - 2)..(idx - 1)].map(&:strip)
63
- custom_request_method = method_route_options.find { |line| line.start_with? "request_method" }
64
- custom_request_method = if custom_request_method
65
- custom_request_method.split(" ")[1].gsub('"', '').upcase
66
- else
67
- "GET"
68
- end
69
- custom_path_option = method_route_options.find { |line| line.start_with? "path" }
70
- check_root = controller_file_readlines[idx - 1].strip
71
- url_to_use = if check_root.include?("root")
72
- "/"
73
- else
74
- if custom_path_option
75
- custom_path_option.split(" ")[1].gsub('"', '')
76
- else
77
- url_friendly_action_name = action_name.split("_").join("-")
78
- controller_path + "/#{url_friendly_action_name}"
79
- end
80
- end
81
- auto_found_routes << base_opts.merge({
82
- url: url_to_use,
83
- method: custom_request_method,
84
- action_name: action_name,
85
- })
76
+ url_friendly_action_name = action_name.split("_").join("-")
77
+ controller_path + "/#{url_friendly_action_name}"
86
78
  end
79
+ custom_request_method = tmp_controller_instance.custom_request_method
80
+
81
+ auto_found_routes << base_opts.merge({
82
+ url: url_to_use,
83
+ method: custom_request_method || "GET",
84
+ action_name: action_name,
85
+ })
87
86
  end
88
87
  end
89
88
  end
data/lib/jetski/router.rb CHANGED
@@ -57,7 +57,7 @@ module Jetski
57
57
 
58
58
  def host_assets
59
59
  # Render stylesheets css via url
60
- host_css && host_images
60
+ host_css && host_images && host_javascript
61
61
  end
62
62
 
63
63
  def host_css
@@ -66,6 +66,7 @@ module Jetski
66
66
  filename = file_path.split("app/assets/stylesheets/").last
67
67
  asset_url = "/#{filename}"
68
68
  server.mount_proc asset_url do |req, res|
69
+ res.content_type = "text/css"
69
70
  res.body = File.read(File.join(Jetski.app_root,"app/assets/stylesheets/#{filename}"))
70
71
  end
71
72
  end
@@ -81,9 +82,22 @@ module Jetski
81
82
  filename = file_path.split("/").last
82
83
  asset_url = "/#{filename}"
83
84
  server.mount_proc asset_url do |req, res|
85
+ res.content_type = "image/*"
84
86
  res.body = File.read(File.join(Jetski.app_root,"app/assets/images/#{filename}"))
85
87
  end
86
88
  end
87
89
  end
90
+
91
+ def host_javascript
92
+ js_files = Dir[File.join(Jetski.app_root,'app/assets/javascript/**/*.js')]
93
+ js_files.each do |file_path|
94
+ filename = file_path.split("app/assets/javascript/").last
95
+ asset_url = "/#{filename}"
96
+ server.mount_proc asset_url do |req, res|
97
+ res.content_type = "text/javascript"
98
+ res.body = File.read(File.join(Jetski.app_root,"app/assets/javascript/#{filename}"))
99
+ end
100
+ end
101
+ end
88
102
  end
89
103
  end
@@ -1,3 +1,3 @@
1
1
  module Jetski
2
- VERSION = "0.3.7"
2
+ VERSION = "0.4.0"
3
3
  end
data/lib/jetski.rb CHANGED
@@ -1,15 +1,16 @@
1
+ require "webrick"
2
+ require "json"
3
+ require "json"
4
+ require "ostruct"
5
+ require "erb"
6
+
1
7
  require_relative './jetski/version'
2
8
  require_relative './jetski/base_controller'
3
9
  require_relative './jetski/server'
4
10
  require_relative './jetski/router/parser'
5
11
  require_relative './jetski/router'
6
- require "webrick"
7
- require "json"
8
- require "json"
9
- require "ostruct"
10
12
 
11
13
  module Jetski
12
- # Debug stage add constants here for debugging.
13
14
  extend self
14
15
  def app_root
15
16
  Dir.pwd
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jetski
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.7
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Indigo Tech Tutorials
@@ -51,6 +51,20 @@ dependencies:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
53
  version: 0.6.2
54
+ - !ruby/object:Gem::Dependency
55
+ name: erb
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: 5.1.2
61
+ type: :runtime
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: 5.1.2
54
68
  description: 'Would you rather ride on a train or a jetski? that is the question you
55
69
  might use when comparing using our framework or the popular Ruby on Rails framework. '
56
70
  email: indigo@tech.tut
@@ -64,13 +78,12 @@ files:
64
78
  - bin/jetski_cli_helpers/generate.rb
65
79
  - bin/templates/base/Gemfile
66
80
  - bin/templates/base/app/assets/images/jetski-logo.png
81
+ - bin/templates/base/app/assets/javascript/application.js
67
82
  - bin/templates/base/app/assets/stylesheets/application.css
68
83
  - bin/templates/base/app/assets/stylesheets/pages.css
69
84
  - bin/templates/base/app/controllers/pages_controller.rb
70
- - bin/templates/base/app/views/layouts/application.html
71
- - bin/templates/base/app/views/pages/home.html
72
- - bin/templates/controllers/example_controller
73
- - bin/templates/views/example.html
85
+ - bin/templates/base/app/views/layouts/application.html.erb
86
+ - bin/templates/base/app/views/pages/home.html.erb
74
87
  - lib/jetski.rb
75
88
  - lib/jetski/base_controller.rb
76
89
  - lib/jetski/router.rb
@@ -1,3 +0,0 @@
1
- class CONTROLLER_NAMEController < Jetski::BaseController
2
- ACTION_NAME
3
- end
@@ -1,2 +0,0 @@
1
- <h1> CONTROLLER_NAME#ACTION_NAME </h1>
2
- <p> edit the content of this page at app/views/PATH_TO_VIEW/ACTION_NAME.html </p>