Skip to content

Commit

Permalink
-jinzhu#125 scripts to reload extension automatically during dev -- u…
Browse files Browse the repository at this point in the history
…tils
  • Loading branch information
hbt committed Apr 1, 2012
1 parent 974bbca commit a0cd388
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ Gemfile.lock
/src/files/features.html
/src/files/changelog.html
/src/files/thanks.html
/nbproject/
13 changes: 13 additions & 0 deletions system/ruby/bin/vrome
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class VromeServer < WEBrick::HTTPServlet::AbstractServlet
response.status = status
response['Content-Type'] = content_type
response.body = body
response['Content-Type'] = "text/plain"
response['Access-Control-Allow-Origin'] = '*'
response['Access-Control-Allow-Methods'] = 'POST'
response['Access-Control-Allow-Headers'] = 'Content-Type, Cache-Control'
response['Access-Control-Max-Age'] = 5
end

def open_editor(request)
Expand All @@ -37,6 +42,14 @@ class VromeServer < WEBrick::HTTPServlet::AbstractServlet

return 200, "text/plain", text
end

def get_latest_version(request)
filename = File.dirname(File.expand_path(__FILE__)) + "/../../../utils/version.txt"
version = (File.read(filename)) if File.exists?(filename)
return 200, "text/plain", version
end


end

puts "Starting Vrome server..."
Expand Down
1 change: 1 addition & 0 deletions utils/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/version.txt
7 changes: 7 additions & 0 deletions utils/doc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version.txt -- generated version number. updated whenever a change is made to the js file by watch_and_do scripts

update_version.rb -- called by watch_and_do to update version.txt

reload_extension.js -- makes calls to server.rb every X seconds and reloads the extension if the version number has been updated i.e files have been changed

refresh_server.sh -- reloads the ruby server when files have been modified
10 changes: 10 additions & 0 deletions utils/refresh_server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

# script to restart server whenever changes are made
# watch_and_do /home/hassen/workspace/vrome/system/ruby/lib rb /home/hassen/workspace/vrome/refresh_server.sh
pid=`lsof -t -i :20000`
echo $pid
kill -9 $pid

echo "restarting server..."
nohup ./system/ruby/bin/vrome >> /tmp/vrome_server.out 2>> /tmp/vrome_server.err < /dev/null &
36 changes: 36 additions & 0 deletions utils/reload_extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// put your extension ID here
var myExtensionID = 'fecemdkpacpnmlbkjfadknbkfhadnnjm';
var latestVersion = -1;
var time = 500; // reload every X ms

function reload_ext()
{
document.getElementById("toggle-" + myExtensionID).click();
}

function reloadExtension()
{
var xhr = new XMLHttpRequest();
var url = 'http://127.0.0.1:20000';
xhr.open("POST", url, true);
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
if(latestVersion != xhr.responseText)
{
chrome.send('reload', [myExtensionID]);
// recent versions of chrome
reload_ext();
setTimeout('reload_ext()', 500);
console.log("RELOADING at " + new Date());
latestVersion = xhr.responseText;
}
}
}

xhr.setRequestHeader("Content-type", "text/plain");
xhr.send(JSON.stringify({
'method':'get_latest_version'
}));
}

window.setInterval('reloadExtension()', time);
15 changes: 15 additions & 0 deletions utils/update_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env ruby

filename = File.dirname(File.expand_path(__FILE__)) + "/version.txt"

version = 0
if File.exists? filename
version = eval(File.read(filename))
end
version += 1

tmpfile = File.new filename, "w"
tmpfile.write version
tmpfile.flush

puts version

0 comments on commit a0cd388

Please sign in to comment.