faastruby 0.5.23 → 0.5.24

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: 4bb0861a4131568e00f7008cea2ad1a83f179b739788ad09d21780e82c9edf41
4
- data.tar.gz: be634ded83c995f6a5b86293960d09535a10509c119f49f425825664c0b0567a
3
+ metadata.gz: b9e8d511a386cfc4cf3ed084c04c10398ad7d93c298a3bd202f2690ae9f72a7d
4
+ data.tar.gz: 4ce5ed8f21a33e3c648fe60f13f41b3938b31f02122952d1b66765cf7dd861aa
5
5
  SHA512:
6
- metadata.gz: 778433fef0a7ad573166042f0c0bc559e9ccb4218fe0ab70b845d1ff64a4e294007f817f1de452ad815574264e04b6cd928ac7a6537aaf51cf09e635ddce8cb1
7
- data.tar.gz: 23df4f5fc80cdf5c6216d542ec6065022d605c0ce625b5810560a9a1839d3c6bd5b02852a7eb60a0de416807ebdb507e7c55ae4d51ed099c235bae359b6c50e3
6
+ metadata.gz: dd373b632709eee2ac1283ca11454dc5572258a7ca8e7f2b32376f7e473b3d2fd4640e384ee24051f22f3a67bf91e098b195655ae3b01ed4985c24474468cbd0
7
+ data.tar.gz: 4999518623a9c3d4409e4a93c963cb9a83860e24fe814b28c1b93593d67134dcf4f06e9c93dfe3108bec2dd1ae2292bb779d635f4ab5315781ee91f878ed6f4a
data/.gitlab-ci.yml ADDED
@@ -0,0 +1,35 @@
1
+ # This file is a template, and might need editing before it works on your project.
2
+ # Official docker image.
3
+ image: ruby:2.6
4
+
5
+ stages:
6
+ - test
7
+ - build
8
+ - deploy
9
+
10
+ test:
11
+ stage: test
12
+ script:
13
+ - bundle install --with test
14
+ - bundle exec rspec
15
+
16
+ build:
17
+ stage: build
18
+ script:
19
+ - gem build faastruby.gemspec
20
+ only:
21
+ - tags
22
+ except:
23
+ - branches
24
+ - master
25
+
26
+ push:
27
+ stage: deploy
28
+ when: manual
29
+ script:
30
+ - gem push faastruby-*.gem
31
+ only:
32
+ - tags
33
+ except:
34
+ - branches
35
+ - master
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.5.24 - Apr 17 2019
4
+ - Add experimental support for Opal
5
+ - Add new project configuration keys: `opal_dir` and `opal_js_destination`
6
+ - Add helper method `render_template` to the Ruby runtime.
7
+
3
8
  ## 0.5.23 - Apr 7 2019
4
9
  - Move regions configuration to a separate file, so it can be shared among different components of the gem
5
10
  - Replace hardcoded URLs by the correct variables
data/Gemfile.lock CHANGED
@@ -1,11 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- faastruby (0.5.22)
4
+ faastruby (0.5.24)
5
5
  colorize (~> 0.8)
6
6
  faastruby-rpc (~> 0.2.7)
7
7
  listen (~> 3.1)
8
8
  oj (~> 3.6)
9
+ opal (~> 0.11.4)
10
+ opal-jquery (~> 0.4.3)
9
11
  puma (~> 3.12)
10
12
  rest-client (~> 2.0)
11
13
  rouge (~> 3.3)
@@ -21,6 +23,7 @@ GEM
21
23
  specs:
22
24
  addressable (2.5.2)
23
25
  public_suffix (>= 2.0.2, < 4.0)
26
+ ast (2.4.0)
24
27
  backports (3.13.0)
25
28
  colorize (0.8.1)
26
29
  crack (0.4.3)
@@ -34,6 +37,7 @@ GEM
34
37
  oj (~> 3.6)
35
38
  ffi (1.10.0)
36
39
  hashdiff (0.3.7)
40
+ hike (1.2.3)
37
41
  http-cookie (1.0.3)
38
42
  domain_name (~> 0.5)
39
43
  listen (3.1.5)
@@ -47,7 +51,16 @@ GEM
47
51
  mustermann (1.0.3)
48
52
  necromancer (0.4.0)
49
53
  netrc (0.11.0)
50
- oj (3.7.11)
54
+ oj (3.7.12)
55
+ opal (0.11.4)
56
+ ast (>= 2.3.0)
57
+ hike (~> 1.2)
58
+ parser (= 2.3.3.1)
59
+ sourcemap (~> 0.1.0)
60
+ opal-jquery (0.4.3)
61
+ opal (>= 0.10.0, < 0.12.0)
62
+ parser (2.3.3.1)
63
+ ast (~> 2.2)
51
64
  pastel (0.7.2)
52
65
  equatable (~> 0.5.0)
53
66
  tty-color (~> 0.4.0)
@@ -93,6 +106,7 @@ GEM
93
106
  rack-protection (= 2.0.5)
94
107
  sinatra (= 2.0.5)
95
108
  tilt (>= 1.3, < 3)
109
+ sourcemap (0.1.1)
96
110
  strings (0.1.5)
97
111
  strings-ansi (~> 0.1)
98
112
  unicode-display_width (~> 1.5)
data/faastruby.gemspec CHANGED
@@ -24,6 +24,8 @@ Gem::Specification.new do |spec|
24
24
  spec.add_runtime_dependency 'tmuxinator', '~> 0.15'
25
25
  spec.add_runtime_dependency 'rouge', '~> 3.3'
26
26
  spec.add_runtime_dependency 'listen', '~> 3.1'
27
+ spec.add_runtime_dependency 'opal', '~> 0.11.4'
28
+ spec.add_runtime_dependency 'opal-jquery', '~> 0.4.3'
27
29
 
28
30
 
29
31
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
@@ -159,6 +159,16 @@ module FaaStRuby
159
159
  ## Defaults to 'functions'.
160
160
  # functions_dir: functions
161
161
 
162
+ ## Opal support (experimental)
163
+ ## You can use Opal to write Ruby code and have it autocompiled to
164
+ ## Javascript and placed inside the public assets folder.
165
+ ## If 'opal_dir' is set, folders inside 'opal_dir' that contain
166
+ ## a file named 'main.rb' will be compiled using opal
167
+ # opal_dir: opal
168
+ ## Destination folder for .js files created by Opal.
169
+ ## Defaults to PUBLIC_DIR/assets/javascripts
170
+ # opal_js_destination: public/assets/javascripts
171
+
162
172
  ## The name of the function that will respond to requests made
163
173
  ## to '/'. Defaults to 'root'
164
174
  # root_to: root
@@ -71,6 +71,7 @@ module FaaStRuby
71
71
  DEFAULT_RUBY_RUNTIME = "ruby:#{CURRENT_MINOR_RUBY}".freeze
72
72
  FUNCTIONS_EVENT_QUEUE = Queue.new
73
73
  PUBLIC_EVENT_QUEUE = Queue.new
74
+ OPAL_EVENT_QUEUE = Queue.new
74
75
  WORKSPACE_BASE_HOST = ENV['FAASTRUBY_WORKSPACE_BASE_HOST'] || 'faast.cloud'
75
76
  URL_PROTOCOL = ENV['FAASTRUBY_URL_PROTOCOL'] || 'https'
76
77
  puts "Using '#{DEFAULT_RUBY_RUNTIME}' as default Ruby runtime." if RUBY_ENABLED
@@ -125,6 +126,18 @@ module FaaStRuby
125
126
  YAML.load(File.read(SECRETS_YAML_FILE))['secrets'] || {}
126
127
  end
127
128
 
129
+ def self.opal_enabled?
130
+ !project_config['opal_dir'].nil?
131
+ end
132
+
133
+ def self.opal_dir
134
+ "#{SERVER_ROOT}/#{project_config['opal_dir']}"
135
+ end
136
+
137
+ def self.opal_js_destination
138
+ project_config['opal_js_destination'] || "#{public_dir}/assets/javascripts"
139
+ end
140
+
128
141
  def self.start!
129
142
  Listen::Adapter::Linux::DEFAULTS[:events] << :modify
130
143
  debug "self.start!"
@@ -135,10 +148,19 @@ module FaaStRuby
135
148
  puts "Detecting existing functions."
136
149
  puts "Ruby functions: #{ruby_functions.inspect}"
137
150
  puts "Crystal functions: #{crystal_functions.inspect}"
151
+
138
152
  listen_on_functions_dir
153
+ if opal_enabled?
154
+ puts "Opal Ruby files will be auto-compiled to Javascript and added to '#{opal_js_destination}'."
155
+ FileUtils.mkdir_p(Local.opal_dir)
156
+ listen_on_opal_dir
157
+ end
139
158
  listen_on_public_dir if SYNC_ENABLED
159
+
140
160
  FunctionProcessor.new(FUNCTIONS_EVENT_QUEUE).start
161
+ OpalProcessor.new(OPAL_EVENT_QUEUE).start if opal_enabled?
141
162
  StaticFileProcessor.new(PUBLIC_EVENT_QUEUE).start if SYNC_ENABLED
163
+
142
164
  # initial_compile
143
165
  puts "Listening for changes."
144
166
  puts "faastRuby Local is ready at http://localhost:3000"
@@ -148,6 +170,7 @@ module FaaStRuby
148
170
  puts "Stopping Watchdog..."
149
171
  Local::Listener.functions_listener.each(&:stop)
150
172
  Local::Listener.public_listener.each(&:stop)
173
+ Local::Listener.opal_listener.each(&:stop)
151
174
  end
152
175
 
153
176
  def self.initial_compile
@@ -176,6 +199,14 @@ module FaaStRuby
176
199
  Local::Listener.functions_listener << listener
177
200
  end
178
201
 
202
+ def self.listen_on_opal_dir
203
+ debug "self.listen_on_opal_dir"
204
+ debug "Listening for changes in '#{opal_dir}'"
205
+ listener = Listener.new(directory: opal_dir, queue: OPAL_EVENT_QUEUE)
206
+ listener.start
207
+ Local::Listener.opal_listener << listener
208
+ end
209
+
179
210
  def self.listen_on_public_dir
180
211
  debug "self.listen_on_public_dir"
181
212
  debug "Listening for changes in '#{public_dir}'"
@@ -7,6 +7,10 @@ module FaaStRuby
7
7
  def self.public_listener
8
8
  @@public_listener ||= []
9
9
  end
10
+ def self.opal_listener
11
+ @@opal_listener ||= []
12
+ end
13
+
10
14
  include Local::Logger
11
15
  attr_accessor :listener, :directory, :queue
12
16
  def initialize(directory:, queue:)
@@ -103,6 +107,10 @@ module FaaStRuby
103
107
  filename == 'faastruby.yml'
104
108
  end
105
109
 
110
+ def file_is_opal_main?
111
+ filename == 'main.rb'
112
+ end
113
+
106
114
  # def file_was_just_added?
107
115
  # debug __method__
108
116
  # Time.now.to_i - File.ctime(@full_path).to_i <= 1
@@ -3,5 +3,6 @@ module FaaStRuby
3
3
  require 'faastruby/local/processors/processor'
4
4
  require 'faastruby/local/processors/function'
5
5
  require 'faastruby/local/processors/static_file'
6
+ require 'faastruby/local/processors/opal'
6
7
  end
7
8
  end
@@ -0,0 +1,92 @@
1
+ module FaaStRuby
2
+ module Local
3
+ class OpalProcessor < Processor
4
+
5
+ def added(event)
6
+ debug "added(#{event.inspect})"
7
+ # does nothing
8
+ return true
9
+ end
10
+
11
+ def modified(event)
12
+ debug "modified(#{event.inspect})"
13
+ # This should trigger
14
+ # - compile to JS
15
+ compile(event)
16
+ end
17
+
18
+ def removed(event)
19
+ debug "removed(#{event.inspect})"
20
+ # This should trigger
21
+ # - Remove from workspace
22
+ if event.file_is_opal_main?
23
+ remove_from_public(event)
24
+ else
25
+ compile(event)
26
+ end
27
+ end
28
+
29
+ def remove_from_public(event)
30
+ delete_path = event.relative_path_dirname.sub(/^#{Local.opal_dir}\//, "#{Local.public_dir}/")
31
+ # return false unless delete_path.match(/^#{Local.public_dir}\//)
32
+ return false if delete_path.nil? || delete_path.match(/^\s*$/)
33
+ puts "Deleting: #{Local.opal_js_destination}/#{delete_path}"
34
+ FileUtils.rm_rf("#{Local.opal_js_destination}/#{delete_path}")
35
+ dir = "#{Local.opal_js_destination}/#{delete_path}"
36
+ delete_recursive_folder(dir)
37
+ end
38
+
39
+ def delete_recursive_folder(entry)
40
+ dirname = File.dirname(entry)
41
+ return if dirname == Local.opal_js_destination
42
+ glob = Dir.glob("#{dirname}/*")
43
+ return if glob.any?
44
+ puts "Deleting: #{dirname}"
45
+ FileUtils.rmdir(dirname)
46
+ delete_recursive_folder(dirname)
47
+ end
48
+
49
+ def compile(event)
50
+ debug "compile"
51
+ js_path = event.relative_path_dirname.sub(/^#{Local.opal_dir}\//, "")
52
+ FileUtils.mkdir_p("#{Local.opal_js_destination}/#{js_path}")
53
+ compile_cmd = [
54
+ "opal",
55
+ "-I",
56
+ "#{event.dirname}",
57
+ "--gem",
58
+ 'jquery',
59
+ "--gem",
60
+ 'opal-jquery',
61
+ "--compile",
62
+ "#{event.dirname}/main.rb",
63
+ ">",
64
+ "#{Local.opal_js_destination}/#{js_path}/main.js"
65
+ ]
66
+ # run it:
67
+ run(event.dirname, 'compile') {`#{compile_cmd.join(' ')}`; puts "Done"}
68
+ puts "Running: #{compile_cmd.join(' ')}"
69
+ end
70
+
71
+ def first_parent_of(entry, event_type)
72
+ debug "first_parent_of(#{entry.inspect})"
73
+ absolute_folder = get_parent_folder_for(entry)
74
+ if event_type == :removed
75
+ name = absolute_folder.dup
76
+ name.slice!("#{Local.opal_dir}/")
77
+ end
78
+ absolute_folder
79
+ end
80
+
81
+ def get_parent_folder_for(entry)
82
+ return File.dirname(entry) if File.basename(entry) == 'main.rb'
83
+ debug "get_parent_folder_for(#{entry.inspect})"
84
+ dirname = File.dirname(entry)
85
+ raise MissingConfigurationFileError.new("ERROR: Could not determine the parent. Make sure your opal folders a main file 'main.rb'.") if dirname == SERVER_ROOT
86
+ return dirname if File.file?("#{dirname}/main.rb")
87
+ get_parent_folder_for(dirname)
88
+ end
89
+
90
+ end
91
+ end
92
+ end
@@ -55,6 +55,7 @@ module FaaStRuby
55
55
  require 'faastruby/server/event_channel'
56
56
  require 'faastruby/server/subscriber'
57
57
  # require 'faastruby/server/event_hub'
58
+ require 'faastruby/server/template'
58
59
  require 'faastruby/server/runner_methods'
59
60
  require 'faastruby/server/function_object'
60
61
  require 'faastruby/server/runner'
@@ -73,6 +73,14 @@ module FaaStRuby
73
73
  response = function_object.handler(event, *args) || function_object.render_nothing
74
74
  response = FaaStRuby::Response.invalid_response unless response.is_a?(FaaStRuby::Response)
75
75
  rescue Exception => e
76
+ if e.class == SyntaxError
77
+ line, *m = e.message.split(' ')
78
+ line_no = line.split(':')[1]
79
+ real_no = line_no.to_i - 13
80
+ STDOUT.puts "handler.rb:#{real_no}: #{m.join(' ')}"
81
+ else
82
+ STDOUT.puts e.full_message
83
+ end
76
84
  error = Oj.dump({
77
85
  'error' => e.message,
78
86
  'location' => e.backtrace&.first
@@ -88,6 +88,10 @@ module FaaStRuby
88
88
  respond_with(nil, status: status, headers: headers, binary: false)
89
89
  end
90
90
 
91
+ def render_template(template_path, status: 200, headers: {}, variables: {})
92
+ render html: FaaStRuby::Template.new(variables: variables).render(template_path), status: status, headers: headers
93
+ end
94
+
91
95
  def redirect_to(function: nil, url: nil, status: 303)
92
96
  headers = {"Location" => function || url}
93
97
  respond_with(nil, status: status, headers: headers, binary: false)
@@ -0,0 +1,13 @@
1
+ module FaaStRuby
2
+ class Template
3
+ def initialize(variables: {})
4
+ variables.each do |var, value|
5
+ instance_variable_set("@#{var}", value)
6
+ end
7
+ end
8
+
9
+ def render(file)
10
+ ERB.new(File.read(file)).result(binding)
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module FaaStRuby
2
- VERSION = '0.5.23'
2
+ VERSION = '0.5.24'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faastruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.23
4
+ version: 0.5.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paulo Arruda
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-07 00:00:00.000000000 Z
11
+ date: 2019-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -192,6 +192,34 @@ dependencies:
192
192
  - - "~>"
193
193
  - !ruby/object:Gem::Version
194
194
  version: '3.1'
195
+ - !ruby/object:Gem::Dependency
196
+ name: opal
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - "~>"
200
+ - !ruby/object:Gem::Version
201
+ version: 0.11.4
202
+ type: :runtime
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - "~>"
207
+ - !ruby/object:Gem::Version
208
+ version: 0.11.4
209
+ - !ruby/object:Gem::Dependency
210
+ name: opal-jquery
211
+ requirement: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - "~>"
214
+ - !ruby/object:Gem::Version
215
+ version: 0.4.3
216
+ type: :runtime
217
+ prerelease: false
218
+ version_requirements: !ruby/object:Gem::Requirement
219
+ requirements:
220
+ - - "~>"
221
+ - !ruby/object:Gem::Version
222
+ version: 0.4.3
195
223
  - !ruby/object:Gem::Dependency
196
224
  name: bundler
197
225
  requirement: !ruby/object:Gem::Requirement
@@ -257,6 +285,7 @@ extensions: []
257
285
  extra_rdoc_files: []
258
286
  files:
259
287
  - ".gitignore"
288
+ - ".gitlab-ci.yml"
260
289
  - ".rspec"
261
290
  - ".travis.yml"
262
291
  - CHANGELOG.md
@@ -319,6 +348,7 @@ files:
319
348
  - lib/faastruby/local/monkey_patch.rb
320
349
  - lib/faastruby/local/processors.rb
321
350
  - lib/faastruby/local/processors/function.rb
351
+ - lib/faastruby/local/processors/opal.rb
322
352
  - lib/faastruby/local/processors/processor.rb
323
353
  - lib/faastruby/local/processors/static_file.rb
324
354
  - lib/faastruby/local/static_files.rb
@@ -341,6 +371,7 @@ files:
341
371
  - lib/faastruby/server/runner.rb
342
372
  - lib/faastruby/server/runner_methods.rb
343
373
  - lib/faastruby/server/subscriber.rb
374
+ - lib/faastruby/server/template.rb
344
375
  - lib/faastruby/spec_helper.rb
345
376
  - lib/faastruby/supported_runtimes.rb
346
377
  - lib/faastruby/user.rb