command_proposal 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8248eae2534ebab49931beca4a77666ef958bc854eeb39a790808ae6112443b1
4
- data.tar.gz: 8915714938d8caf78dc757cf32f47f71a447078204290711403c9ee1eecce60a
3
+ metadata.gz: 8cc9baadf971edd54dc7558fbec28c8f515e9fb572975705f886b18a68693b7b
4
+ data.tar.gz: 2793d9b73e85d8e0d509974cf9b6f7ed36517c4adbfec9a23f52bbc66d4effff
5
5
  SHA512:
6
- metadata.gz: 2147b8ebcda3d8b747db413981592c915dfdae50b27ba07e1a39b6b9130b416a11bd9b5f1e728e5a2b2edc73c25302c21473d509165be38030160af7b62c70d5
7
- data.tar.gz: 0d07701e418d21123a28463180104b5464ca0f45a7398015a44302e94af34f8f88be6f9592315505aa9fe95cf56c2b25cf46f0125d7c3a18f80a40c80c1b979d
6
+ metadata.gz: b80eb953b76e09aa2dbbc62c0df2884c15833e8dd1abc4bd66141f0c6b0adc608fcbda97efb80647ec31a1d92ac20d43fda79675560815b74cd93a878a79dc5f
7
+ data.tar.gz: f26da392c2ed15eaf1f362d0e0bd131ac8c9a05df30390871acc56a9fb8543cb5e69c88816be1fc36cb78478797659222a0edf8e4945b948538f17b97e206579
@@ -1,4 +1,4 @@
1
- function docReady(fn) {
1
+ function cmdDocReady(fn) {
2
2
  // see if DOM is already available
3
3
  if (document.readyState === "complete" || document.readyState === "interactive") {
4
4
  // call on next available tick
@@ -7,3 +7,64 @@ function docReady(fn) {
7
7
  document.addEventListener("DOMContentLoaded", fn)
8
8
  }
9
9
  }
10
+
11
+ function HttpClient() {
12
+ this.request = function(method, url, opts) {
13
+ var req = new XMLHttpRequest()
14
+ req.onreadystatechange = function() {
15
+ if (req.status != 0 && req.readyState == 4 && opts.done) {
16
+ opts.done(req.responseText, req.status, req)
17
+ }
18
+ }
19
+
20
+ req.open(method, url, true)
21
+ if (opts.headers) {
22
+ Object.keys(opts.headers).forEach(function(key) {
23
+ req.setRequestHeader(key, opts.headers[key])
24
+ })
25
+ }
26
+ req.send(opts.body)
27
+ }
28
+
29
+ this.get = function(url, opts) {
30
+ this.request("GET", url, opts)
31
+ }
32
+
33
+ this.post = function(url, opts) {
34
+ this.request("POST", url, opts)
35
+ }
36
+ }
37
+
38
+ function CommandQueue() {
39
+ this.queue = []
40
+ this.eventCurrentlyRunning = false
41
+ this.runningQueue = null
42
+
43
+ this.run = function() {
44
+ if (!this.eventCurrentlyRunning) {
45
+ if (this.queue.length == 0) {
46
+ clearInterval(this.runningQueue)
47
+ this.runningQueue = null
48
+ return
49
+ }
50
+ var nextEvent = this.queue.shift()
51
+ this.eventCurrentlyRunning = true
52
+ nextEvent(this)
53
+ }
54
+ }
55
+
56
+ this.add = function(queued_function) {
57
+ this.queue.push(queued_function)
58
+ this.process()
59
+ }
60
+
61
+ this.finish = function(ms) {
62
+ this.eventCurrentlyRunning = false
63
+ }
64
+
65
+ this.process = function() {
66
+ if (this.runningQueue) { return }
67
+ var q = this
68
+ this.runningQueue = setInterval(function() { q.run() }, 1)
69
+ }
70
+ }
@@ -1,11 +1,11 @@
1
1
  // Add tab / shift-tab for changing indents
2
- docReady(function() {
2
+ cmdDocReady(function() {
3
3
  var cmdconsole = document.querySelector(".cmd-console")
4
4
 
5
5
  if (cmdconsole) {
6
6
  var console_input = document.querySelector(".cmd-console .cmd-entry")
7
7
  var lines = document.querySelector(".cmd-console .lines")
8
- var queue = Promise.resolve()
8
+ var queue = new CommandQueue
9
9
  var history_cmd_idx = undefined
10
10
  var stored_entry = undefined
11
11
  var commands = getPrevCommands()
@@ -152,43 +152,38 @@ docReady(function() {
152
152
  if (/^[\s\n]*$/.test(line.textContent)) { return }
153
153
 
154
154
  commands.push(line.textContent)
155
- queue = queue.then(async function() {
155
+ queue.add(function(evt) {
156
156
  $.rails.refreshCSRFTokens()
157
157
 
158
158
  var params = { code: line.textContent, task_id: cmdconsole.dataset.task }
159
159
 
160
- var res = await fetch(cmdconsole.dataset.exeUrl, {
161
- method: "POST",
160
+ var client = new HttpClient()
161
+ client.post(cmdconsole.dataset.exeUrl, {
162
162
  headers: {
163
163
  "Content-Type": "application/json",
164
164
  "X-CSRF-Token": $.rails.csrfToken()
165
165
  },
166
- body: JSON.stringify(params)
167
- }).then(function(res) {
168
- if (res.ok) {
169
- return res.json()
170
- } else {
171
- throw new Error("Server error")
172
- }
173
- }).catch(function(err) {
174
- return {
175
- error: err,
166
+ body: JSON.stringify(params),
167
+ done: function(res, status, req) {
168
+ if (status == 200) {
169
+ var json = JSON.parse(res)
170
+ var result = document.createElement("div")
171
+ result.classList.add("result")
172
+
173
+ if (json.error) {
174
+ result.classList.add("cmd-error")
175
+ result.textContent = json.error
176
+ } else {
177
+ result.textContent = json.result
178
+ }
179
+
180
+ line.appendChild(result)
181
+ } else {
182
+ console.log("Error: ", res, req);
183
+ }
184
+ evt.finish()
176
185
  }
177
186
  })
178
-
179
- var json = await res
180
-
181
- var result = document.createElement("div")
182
- result.classList.add("result")
183
-
184
- if (json.error) {
185
- result.classList.add("cmd-error")
186
- result.textContent = json.error
187
- } else {
188
- result.textContent = json.result
189
- }
190
-
191
- line.appendChild(result)
192
187
  })
193
188
  }
194
189
  }
@@ -1,6 +1,6 @@
1
- docReady(function() {
1
+ cmdDocReady(function() {
2
2
  var terminals = document.querySelectorAll("[data-feed]")
3
- var queue = Promise.resolve()
3
+ var queue = new CommandQueue
4
4
  var continue_statuses = ["started", "approved", "cancelling"]
5
5
 
6
6
  terminals.forEach(function(terminal) {
@@ -10,42 +10,39 @@ docReady(function() {
10
10
  })
11
11
 
12
12
  function pingFeed(terminal) {
13
- queue = queue.then(async function() {
13
+ queue.add(function(evt) {
14
14
  $.rails.refreshCSRFTokens()
15
15
 
16
- var res = await fetch(terminal.dataset.feed, {
17
- method: "GET",
16
+ var client = new HttpClient()
17
+ client.get(terminal.dataset.feed, {
18
18
  headers: {
19
19
  "Content-Type": "text/html",
20
20
  "X-CSRF-Token": $.rails.csrfToken()
21
21
  },
22
- }).then(function(res) {
23
- if (res.ok) {
24
- return res.json()
25
- } else {
26
- throw new Error("Server error")
27
- }
28
- }).catch(function(err) {
29
- console.log("err:", err);
30
- })
31
-
32
- var json = await res
22
+ done: function(res, status, req) {
23
+ if (status == 200) {
24
+ var json = JSON.parse(res)
25
+ if (terminal.nextElementSibling && terminal.nextElementSibling.CodeMirror) {
26
+ terminal.nextElementSibling.CodeMirror.doc.setValue(json.result || "")
27
+ } else {
28
+ terminal.innerHTML = json.result_html
29
+ }
30
+ document.querySelector("td[data-iteration-status]").innerText = json.status
31
+ document.querySelector("td[data-iteration-duration]").innerText = json.duration
33
32
 
34
- if (terminal.nextElementSibling && terminal.nextElementSibling.CodeMirror) {
35
- terminal.nextElementSibling.CodeMirror.doc.setValue(json.result || "")
36
- } else {
37
- terminal.innerHTML = json.result_html
38
- }
39
- document.querySelector("td[data-iteration-status]").innerText = json.status
40
- document.querySelector("td[data-iteration-duration]").innerText = json.duration
41
-
42
- if (continue_statuses.includes(json.status)) {
43
- setTimeout(function() { pingFeed(terminal) }, 1000)
44
- } else {
45
- if (document.querySelector(".cancel-btn")) {
46
- document.querySelector(".cancel-btn").remove()
33
+ if (continue_statuses.includes(json.status)) {
34
+ setTimeout(function() { pingFeed(terminal) }, 1000)
35
+ } else {
36
+ if (document.querySelector(".cancel-btn")) {
37
+ document.querySelector(".cancel-btn").remove()
38
+ }
39
+ }
40
+ } else {
41
+ console.log("Error: ", res, req);
42
+ }
43
+ evt.finish()
47
44
  }
48
- }
45
+ })
49
46
  })
50
47
  }
51
48
  })
@@ -1,4 +1,4 @@
1
- docReady(function() {
1
+ cmdDocReady(function() {
2
2
  var terminals = document.querySelectorAll(".cmd-terminal")
3
3
 
4
4
  function setReadOnlyUI(cm) {
@@ -1,5 +1,7 @@
1
1
  module CommandProposal
2
2
  module ApplicationHelper
3
+ include ActionDispatch::Routing::PolymorphicRoutes
4
+ include Rails.application.routes.url_helpers
3
5
  # In order to keep the regular app's routes working in the base template, we have to manually
4
6
  # render the engine routes. Built a helper for this because it's long and nasty otherwise.
5
7
  def cmd_path(*args)
@@ -19,39 +21,22 @@ module CommandProposal
19
21
  args << { host: host, port: nil } if host.present?
20
22
 
21
23
  begin
22
- router.url_for(args.compact)
24
+ command_proposal_engine.url_for(args.compact)
23
25
  rescue NoMethodError => e
24
- raise "Error generating route! Please make sure `default_url_options` are set."
26
+ raise "Error generating route! Please make sure `config.action_mailer.default_url_options` are set."
25
27
  end
26
28
  end
27
29
 
28
30
  def string_path(*args)
29
- [router.command_proposal_tasks_url + args.shift, args.to_param.presence].compact.join("?")
31
+ [command_proposal_engine.command_proposal_tasks_url + args.shift, args.to_param.presence].compact.join("?")
30
32
  end
31
33
 
32
34
  # Runner controller doesn't map to a model, so needs special handling
33
35
  def runner_path(task, iteration=nil)
34
36
  if iteration.present?
35
- router.command_proposal_task_runner_url(task, iteration)
37
+ command_proposal_engine.command_proposal_task_runner_url(task, iteration)
36
38
  else
37
- router.command_proposal_task_runner_index_url(task)
38
- end
39
- end
40
-
41
- def router
42
- @@router ||= begin
43
- routes = ::CommandProposal::Engine.routes
44
- routes.default_url_options = rails_default_url_options
45
- routes.url_helpers
46
- end
47
- end
48
-
49
- def rails_default_url_options
50
- Rails.application.config.action_mailer.default_url_options.tap do |default_opts|
51
- default_opts ||= {}
52
- default_opts[:host] ||= "localhost"
53
- default_opts[:port] ||= "3000"
54
- default_opts[:protocol] ||= "http"
39
+ command_proposal_engine.command_proposal_task_runner_index_url(task)
55
40
  end
56
41
  end
57
42
  end
@@ -30,7 +30,7 @@ module CommandProposal
30
30
  elsif cmd_config.controller_var.blank?
31
31
  nil
32
32
  else
33
- try(cmd_config.controller_var)
33
+ send(cmd_config.controller_var)
34
34
  end
35
35
  end
36
36
  end
@@ -19,10 +19,10 @@ class ::CommandProposal::Task < ApplicationRecord
19
19
  enum session_type: {
20
20
  # Task will have multiple iterations that are all essentially the same just with code changes
21
21
  task: 0,
22
- # Console iterations are actually line by line, so order matters
23
- console: 1,
24
22
  # Function iterations are much like tasks
25
- function: 2,
23
+ function: 1,
24
+ # Console iterations are actually line by line, so order matters
25
+ console: 2,
26
26
  # Modules are included in tasks and not run independently
27
27
  module: 3,
28
28
  }
@@ -1,3 +1,3 @@
1
1
  module CommandProposal
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: command_proposal
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rocco Nicholls
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-25 00:00:00.000000000 Z
11
+ date: 2021-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails