prism-cli 0.0.7 → 0.0.8

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.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/bin/prism +8 -49
  3. data/build/bundle.js +6206 -0
  4. data/build/bundle.wasm +0 -0
  5. data/build/bundle.wasm.map +1 -0
  6. data/build/bundle.wast +618101 -0
  7. data/{dist → build}/prism.js +64 -17
  8. data/src/prism.rb +39 -0
  9. data/wasm-server.js +78 -2
  10. metadata +26 -58
  11. data/main.c +0 -127
  12. data/mruby/build/emscripten/lib/libmruby.a +0 -0
  13. data/mruby/include/mrbconf.h +0 -149
  14. data/mruby/include/mruby.h +0 -1291
  15. data/mruby/include/mruby/array.h +0 -296
  16. data/mruby/include/mruby/boxing_nan.h +0 -102
  17. data/mruby/include/mruby/boxing_no.h +0 -56
  18. data/mruby/include/mruby/boxing_word.h +0 -144
  19. data/mruby/include/mruby/class.h +0 -97
  20. data/mruby/include/mruby/common.h +0 -77
  21. data/mruby/include/mruby/compile.h +0 -195
  22. data/mruby/include/mruby/data.h +0 -76
  23. data/mruby/include/mruby/debug.h +0 -66
  24. data/mruby/include/mruby/dump.h +0 -196
  25. data/mruby/include/mruby/error.h +0 -75
  26. data/mruby/include/mruby/gc.h +0 -91
  27. data/mruby/include/mruby/hash.h +0 -229
  28. data/mruby/include/mruby/irep.h +0 -75
  29. data/mruby/include/mruby/istruct.h +0 -47
  30. data/mruby/include/mruby/khash.h +0 -274
  31. data/mruby/include/mruby/numeric.h +0 -161
  32. data/mruby/include/mruby/object.h +0 -44
  33. data/mruby/include/mruby/opcode.h +0 -69
  34. data/mruby/include/mruby/ops.h +0 -117
  35. data/mruby/include/mruby/proc.h +0 -131
  36. data/mruby/include/mruby/range.h +0 -73
  37. data/mruby/include/mruby/re.h +0 -16
  38. data/mruby/include/mruby/string.h +0 -450
  39. data/mruby/include/mruby/throw.h +0 -55
  40. data/mruby/include/mruby/value.h +0 -313
  41. data/mruby/include/mruby/variable.h +0 -141
  42. data/mruby/include/mruby/version.h +0 -110
@@ -943,7 +943,19 @@ function rubyVTreeToSnabbdom(rvtree) {
943
943
  );
944
944
  }
945
945
 
946
- var currentContainer = document.getElementById('root');
946
+
947
+ var currentContainer;
948
+ var allLoaded = false;
949
+ var modulesToLoad = [];
950
+
951
+ function run(element, main) {
952
+ currentContainer = document.getElementById('root');
953
+ modulesToLoad = [fetchAndLoad("/prism-ruby/prism.rb"), fetchAndLoad(main)];
954
+
955
+ load(modulesToLoad, main);
956
+ }
957
+
958
+ window.Prism = {run};
947
959
 
948
960
  function render() {
949
961
  const rvtree = JSON.parse(Module.ccall("render", "string", []));
@@ -956,22 +968,57 @@ function render() {
956
968
 
957
969
  window.render = render;
958
970
 
959
- window.Module = {
960
- preRun: [],
961
- postRun: [ render ],
962
- print: function() {
963
- return function(e) {
964
- 1 < arguments.length && (e = Array.prototype.slice.call(arguments).join(" ")), console.log(e)
965
- }
966
- }(),
967
- printErr: function(e) {
968
- 1 < arguments.length && (e = Array.prototype.slice.call(arguments).join(" ")), console.error(e)
969
- },
970
- canvas: function() {}(),
971
- setStatus: function() {},
972
- totalDependencies: 0,
973
- monitorRunDependencies: function(e) {}
974
- };
971
+ function fetchAndLoad(name) {
972
+ return fetch(name).then(r => r.text().then(t => ({ok: r.ok, text: t}))).then(({ok, text}) => {
973
+ if (!ok) {
974
+ throw new Error(`Prism: Could not load ${name}`, text);
975
+ }
976
+
977
+ return {name, text};
978
+ });
979
+ }
980
+
981
+ function load(modulesToLoad, main) {
982
+ modulePromise.then(() => {
983
+ Promise.all(modulesToLoad).then((modules) => {
984
+ for (let m of modules) {
985
+ const parts = m.name.split('/').filter(a => a.trim() !== '');
975
986
 
987
+ const directories = parts.slice(0, -1);
988
+ const basename = parts.slice(-1)[0];
989
+
990
+ const pwd = [];
991
+ for (let d of directories) {
992
+ FS.mkdir('./' + pwd.concat(d).join('/'));
993
+ pwd.push(d);
994
+ }
995
+
996
+ FS.writeFile(`./${m.name}`, m.text);
997
+ }
998
+
999
+ Module.ccall("load", "void", ["string"], [main]);
1000
+ render();
1001
+ });
1002
+ });
1003
+ }
1004
+
1005
+ const modulePromise = new Promise((resolve, reject) => {
1006
+ window.Module = {
1007
+ preRun: [],
1008
+ postRun: [ resolve ],
1009
+ print: function() {
1010
+ return function(e) {
1011
+ 1 < arguments.length && (e = Array.prototype.slice.call(arguments).join(" ")), console.log(e)
1012
+ }
1013
+ }(),
1014
+ printErr: function(e) {
1015
+ 1 < arguments.length && (e = Array.prototype.slice.call(arguments).join(" ")), console.error(e)
1016
+ },
1017
+ canvas: function() {}(),
1018
+ setStatus: function() {},
1019
+ totalDependencies: 0,
1020
+ monitorRunDependencies: function(e) {}
1021
+ };
1022
+ });
976
1023
 
977
1024
  },{"snabbdom":10,"snabbdom/h":1,"snabbdom/modules/attributes":4,"snabbdom/modules/class":5,"snabbdom/modules/dataset":6,"snabbdom/modules/eventlisteners":7,"snabbdom/modules/props":8,"snabbdom/modules/style":9}]},{},[13]);
@@ -33,6 +33,10 @@ module Prism
33
33
  def event(eventJSON, id)
34
34
  DOM.event(JSON::parse(eventJSON), id)
35
35
  end
36
+
37
+ def http_response(responseJSON, id)
38
+ HTTP._response(HTTP::Response.from_json(responseJSON), id)
39
+ end
36
40
  end
37
41
 
38
42
  class Component
@@ -319,3 +323,38 @@ module DOM
319
323
  end
320
324
  end
321
325
  end
326
+
327
+ module HTTP
328
+ @@event_id = 0
329
+ @@listeners = {}
330
+
331
+ def self.get_event_id
332
+ @@event_id += 1
333
+
334
+ @@event_id.to_s
335
+ end
336
+
337
+ def self.add_listener(id, &block)
338
+ @@listeners[id] = block
339
+ end
340
+
341
+ def self.get(url, &block)
342
+ id = HTTP.get_event_id
343
+
344
+ InternalHTTP.http_request(url, id)
345
+
346
+ HTTP.add_listener(id, &block)
347
+ end
348
+
349
+ def self._response(text, id)
350
+ @@listeners[id].call(text)
351
+ end
352
+
353
+ class Response < Struct.new(:body)
354
+ def self.from_json(json)
355
+ data = JSON::parse(json)
356
+
357
+ new(data["body"])
358
+ end
359
+ end
360
+ end
@@ -2,12 +2,63 @@ const http = require('http');
2
2
  const fs = require('fs');
3
3
  const path = require('path');
4
4
 
5
+ function renderFileLink(p, name) {
6
+ return `<li><a href="${path.join(p, name)}">${name}</a></li>`;
7
+ }
8
+
9
+ function isDirectory(p) {
10
+ try {
11
+ return fs.statSync(p).isDirectory();
12
+ } catch (e) {
13
+ return false;
14
+ }
15
+ }
16
+
5
17
  const port = parseInt(process.argv[2], 10) || 3042;
6
18
  const proxy = http.createServer((req, res) => {
7
19
  let p = path.join('.', req.url);
8
20
 
9
- if (req.url === '/') {
10
- p = path.join('.', 'index.html');
21
+ console.log(p);
22
+
23
+ if (p.startsWith('prism-assets')) {
24
+ p = path.join(__dirname, 'build', path.basename(p));
25
+ }
26
+
27
+ if (p.startsWith('prism-ruby')) {
28
+ p = path.join(__dirname, 'src', path.basename(p));
29
+ }
30
+
31
+ if (isDirectory(p)) {
32
+ const files = fs.readdirSync(p);
33
+
34
+ res.write(`
35
+ <!DOCTYPE html>
36
+ <html lang=en-us>
37
+
38
+ <head>
39
+ <meta charset=utf-8>
40
+ <meta content="text/html; charset=utf-8" http-equiv=Content-Type>
41
+ <title>Prism Server</title>
42
+ <style>
43
+ html, body {
44
+ font-family: sans-serif;
45
+ }
46
+ </style>
47
+ </head>
48
+
49
+ <body>
50
+ <h1>Prism Dev Server</h1>
51
+ <h2>${path.join(process.cwd(), p)}</h2>
52
+
53
+ <ul>
54
+ ${files.map(f => renderFileLink(req.url, f)).join("\n")}
55
+ </ul>
56
+
57
+ </body>
58
+ </html>
59
+ `);
60
+ res.end();
61
+ return;
11
62
  }
12
63
 
13
64
  try {
@@ -17,6 +68,31 @@ const proxy = http.createServer((req, res) => {
17
68
  if (p.endsWith('.js')) {
18
69
  res.setHeader("Content-Type", "text/javascript")
19
70
  }
71
+ if (p.endsWith('.rb') && req.headers.accept.includes("text/html")) {
72
+ res.write(`
73
+ <!DOCTYPE html>
74
+ <html lang=en-us>
75
+
76
+ <head>
77
+ <meta charset=utf-8>
78
+ <meta content="text/html; charset=utf-8" http-equiv=Content-Type>
79
+ </head>
80
+ <body>
81
+ <div id="root"></div>
82
+ </body>
83
+ <script type="text/javascript" src="/prism-assets/prism.js"></script>
84
+ <script type="text/javascript" async src="/prism-assets/bundle.js"></script>
85
+ <script type="text/javascript">
86
+ Prism.run("#root", "/${p}");
87
+ </script>
88
+ </html>
89
+ `);
90
+ res.end();
91
+ return;
92
+ }
93
+ if (p.endsWith('rb')) {
94
+ res.setHeader('Content-Type', "application/ruby");
95
+ }
20
96
  res.write(fs.readFileSync(p));
21
97
  } catch (e) {
22
98
  res.write(e.toString());
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prism-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Johnstone
@@ -10,33 +10,29 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2019-10-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: "\n## Prism\n\nBuild React-style web apps with Ruby and WebAssembly \n\n###
14
- Introduction\n\nPrism is a tool that uses [mruby](https://github.com/mruby/mruby)
15
- and [emscripten](https://emscripten.org/) to compile ruby code to WebAssembly. It
16
- also provides a runtime layer for working with the DOM and events, so you can use
17
- it make SPAs.\n\n⚡️ Prism is currently in extremely early alpha. Expect bugs, breaking
18
- API changes, missing functionality and rough edges. ⚡️\n\n### Getting started\n\nPrism
19
- requires that you have both [mruby](https://github.com/mruby/mruby) and [emscripten](https://emscripten.org/)
20
- installed and available on your path.\n\nYou can install mruby through your preferred
21
- package manager, such as homebrew, apt-get or nix.\n\nPackage managers often have
22
- outdated versions of emscripten, so it's recommended that you install via `emsdk`.
23
- Instructions are available on the [emscripten website](https://emscripten.org/docs/getting_started/downloads.html).\n\nYou
24
- can install Prism using `gem install prism-cli`.\n\n### CLI Usage\n\nYou can initialize
25
- a new Prism app by running `prism init`. This simply creates a hello world sample
26
- application, by default at `./app.rb` but you can customize the location by providing
27
- an argument to `prism init`.\n\nYou can then run `prism build` to compile your application.
28
- You can provide an entrypoint, but `./app.rb` is assumed by default.\n\n`prism build`
29
- creates a sample html file, a bundle for Prism's runtime JS code, the emscripten
30
- runtime code and the ruby compiled to Wasm.\n\nTo test your application, you can
31
- run `prism server` and open `localhost:3042` in your browser. You should see a hello
32
- world application with an input and some text. You should be able to change the
33
- input and see the text change.\n\n### Writing a Prism App\n\nPrism apps are written
13
+ description: "\n## Prism\n\n[![Join the chat at https://gitter.im/prism-rb/community](https://badges.gitter.im/prism-rb/community.svg)](https://gitter.im/prism-rb/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)\n\nBuild
14
+ frontend web apps with Ruby and WebAssembly \n\n### Introduction\n\nPrism is a framework
15
+ that helps you make frontend web applications with Ruby and WebAssembly. It uses
16
+ [mruby](https://github.com/mruby/mruby) and [emscripten](https://emscripten.org/)
17
+ to compile ruby code to WebAssembly. It also provides a runtime layer for working
18
+ with the DOM and events.\n\n⚡️ Prism is currently in extremely early alpha. Expect
19
+ bugs, breaking API changes, missing functionality and rough edges. ⚡️\n\n### Getting
20
+ started\n\nYou can install Prism from RubyGems using `gem install prism-cli`.\n\n###
21
+ CLI Usage\n\nYou can initialize a new Prism app by running `prism init`. This simply
22
+ creates a hello world sample application, by default at `./app.rb` but you can customize
23
+ the location by providing an argument to `prism init`.\n\nYou can then run `prism
24
+ server`, which will start a development server. If you then navigate to `localhost:3042/app.rb`,
25
+ you should see the sample application. Try changing the code and reloading the page,
26
+ and the app will update.\n\nIf an error occurs, it will be printed out to the browser
27
+ console.\n\nBuilding production releases of Prism apps through the command line
28
+ is still a work in progress.\n\n### Writing a Prism App\n\nPrism apps are written
34
29
  in mruby. mruby is a lightweight implementation of Ruby that's suitable for compiling
35
30
  to the web.\n\nmruby is similar in many ways to cruby and will be a familiar experience
36
31
  for someone who has only used the mainline interpreter. The most notable exception
37
32
  is that mruby only supports syntax up to ruby 1.9, which means there are no keyword
38
33
  arguments or safe traversal operator.\n\nThere are a number of other small differences,
39
- and it's worth reviewing the [mruby limitations documentation](https://github.com/mruby/mruby/blob/master/doc/limitations.md).\n\nIf
34
+ and it's worth reviewing the [mruby limitations documentation](https://github.com/mruby/mruby/blob/master/doc/limitations.md).
35
+ You might also want to refer to the [mruby API docs](http://mruby.org/docs/api/).\n\nIf
40
36
  you run `prism init`, it will create a sample application that makes a good starting
41
37
  point. This is the code it outputs:\n\n```ruby\nclass HelloWorld < Prism::Component\n
42
38
  \ attr_accessor :name\n\n def initialize(name = \"World\")\n @name = name\n
@@ -148,8 +144,8 @@ description: "\n## Prism\n\nBuild React-style web apps with Ruby and WebAssembly
148
144
  starting Prism with funding is that I want as much of the work that's done on Prism
149
145
  as possible to be reimbursed, no matter who's doing it. The other aspect is that
150
146
  I don't have very much spare time for projects but if I can get paid for my work
151
- I can do Prism as part of my day to day contract work.\n\n*[Support Prism on Open
152
- Collective]*(https://opencollective.com/prism)\n\n### License\n\nPrism is available
147
+ I can do Prism as part of my day to day contract work.\n\n**[Support Prism on Open
148
+ Collective](https://opencollective.com/prism)**\n\n### License\n\nPrism is available
153
149
  under the MIT license. Please see the LICENSE file for more details.\n"
154
150
  email: ncwjohnstone@gmail.com
155
151
  executables:
@@ -158,39 +154,11 @@ extensions: []
158
154
  extra_rdoc_files: []
159
155
  files:
160
156
  - bin/prism
161
- - dist/prism.js
162
- - main.c
163
- - mruby/build/emscripten/lib/libmruby.a
164
- - mruby/include/mrbconf.h
165
- - mruby/include/mruby.h
166
- - mruby/include/mruby/array.h
167
- - mruby/include/mruby/boxing_nan.h
168
- - mruby/include/mruby/boxing_no.h
169
- - mruby/include/mruby/boxing_word.h
170
- - mruby/include/mruby/class.h
171
- - mruby/include/mruby/common.h
172
- - mruby/include/mruby/compile.h
173
- - mruby/include/mruby/data.h
174
- - mruby/include/mruby/debug.h
175
- - mruby/include/mruby/dump.h
176
- - mruby/include/mruby/error.h
177
- - mruby/include/mruby/gc.h
178
- - mruby/include/mruby/hash.h
179
- - mruby/include/mruby/irep.h
180
- - mruby/include/mruby/istruct.h
181
- - mruby/include/mruby/khash.h
182
- - mruby/include/mruby/numeric.h
183
- - mruby/include/mruby/object.h
184
- - mruby/include/mruby/opcode.h
185
- - mruby/include/mruby/ops.h
186
- - mruby/include/mruby/proc.h
187
- - mruby/include/mruby/range.h
188
- - mruby/include/mruby/re.h
189
- - mruby/include/mruby/string.h
190
- - mruby/include/mruby/throw.h
191
- - mruby/include/mruby/value.h
192
- - mruby/include/mruby/variable.h
193
- - mruby/include/mruby/version.h
157
+ - build/bundle.js
158
+ - build/bundle.wasm
159
+ - build/bundle.wasm.map
160
+ - build/bundle.wast
161
+ - build/prism.js
194
162
  - src/prism.rb
195
163
  - wasm-server.js
196
164
  homepage: https://github.com/prism/prism-rb
data/main.c DELETED
@@ -1,127 +0,0 @@
1
- #include <emscripten.h>
2
- #include <stdarg.h>
3
- #include <mruby.h>
4
- #include <mruby/irep.h>
5
- #include <mruby/array.h>
6
- #include <mruby/proc.h>
7
- #include <mruby/compile.h>
8
- #include <mruby/dump.h>
9
- #include <mruby/string.h>
10
- #include <mruby/variable.h>
11
- #include <mruby/throw.h>
12
- #include "bundle.c"
13
-
14
- mrb_value app;
15
- mrb_state *mrb;
16
-
17
-
18
- mrb_value
19
- add_event_listener(mrb_state *mrb, mrb_value self){
20
- mrb_value selector, event, id;
21
- mrb_get_args(mrb, "SSS", &selector, &event, &id);
22
-
23
- EM_ASM_({
24
- var selector = UTF8ToString($0);
25
- var eventName = UTF8ToString($1);
26
- var id = UTF8ToString($2);
27
- var elements;
28
-
29
- if (selector === 'document') {
30
- elements = [window.document];
31
- } else if (selector === 'body') {
32
- elements = [window.document.body];
33
- } else {
34
- elements = document.querySelectorAll(selector);
35
- }
36
-
37
- for (var i = 0; i < elements.length; i++) {
38
- var element = elements[i];
39
-
40
- element.addEventListener(eventName, function(event) {
41
- Module.ccall(
42
- 'event',
43
- 'void',
44
- ['string', 'string', 'string'],
45
- [stringifyEvent(event), id]
46
- );
47
-
48
- render();
49
- });
50
- };
51
- }, RSTRING_PTR(selector), RSTRING_PTR(event), RSTRING_PTR(id));
52
- return mrb_nil_value();
53
- }
54
-
55
- int
56
- main(int argc, const char * argv[])
57
- {
58
- struct RClass *dom_class;
59
-
60
- mrb = mrb_open();
61
-
62
- if (!mrb) { /* handle error */ }
63
- dom_class = mrb_define_class(mrb, "InternalDOM", mrb->object_class);
64
- mrb_define_class_method(
65
- mrb,
66
- dom_class,
67
- "add_event_listener",
68
- add_event_listener,
69
- MRB_ARGS_REQ(3)
70
- );
71
-
72
- app = mrb_load_irep(mrb, bundle);
73
- mrb_gc_register(mrb, app);
74
-
75
- return 1;
76
- }
77
-
78
- char* render() {
79
- mrb_value result = mrb_funcall(mrb, app, "render", 0);
80
- if (mrb->exc) {
81
- mrb_print_error(mrb);
82
- mrb->exc = NULL;
83
- }
84
- return RSTRING_PTR(result);
85
- }
86
-
87
- void dispatch(char* message) {
88
- mrb_value str = mrb_str_new_cstr(mrb, message);
89
- mrb_gc_register(mrb, str);
90
- mrb_funcall(mrb, app, "dispatch", 1, str);
91
- if (mrb->exc) {
92
- mrb_print_error(mrb);
93
- mrb->exc = NULL;
94
- }
95
- mrb_gc_unregister(mrb, str);
96
- }
97
-
98
- void event(char* message, char* id) {
99
- mrb_value str = mrb_str_new_cstr(mrb, message);
100
- mrb_value str2 = mrb_str_new_cstr(mrb, id);
101
- mrb_funcall(mrb, app, "event", 2, str, str2);
102
-
103
- if (mrb->exc) {
104
- mrb_print_error(mrb);
105
- mrb->exc = NULL;
106
- }
107
- }
108
-
109
- // main
110
- //
111
- // make the ruby component
112
- // return a reference?
113
- //
114
- // dispatch
115
- //
116
- // call the ruby component with an event triggered by event handler
117
- // we might need the reference?
118
- //
119
- // what is the mruby api for calling methods?
120
-
121
- /**
122
- * Gets a class.
123
- * @param mrb The current mruby state.
124
- * @param name The name of the class.
125
- * @return [struct RClass *] A reference to the class.
126
- */
127
- // MRB_API struct RClass * mrb_class_get(mrb_state *mrb, const char *name);