Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows Support #60

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 92 additions & 40 deletions Rakefile
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
require 'erb'
require 'net/http'

desc "Install dotjs"
task :install => 'install:all'

DAEMON_INSTALL_DIR = "/usr/local/bin"
WINDOWS = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? true : false
# Setup
if WINDOWS
JS_DIR_NAME = "_js"
JS_DIR = File.join(ENV['HOME'], JS_DIR_NAME)
DAEMON_INSTALL_DIR = File.join(JS_DIR, "bin")
AGENT_FILE = "djsd.vbs"
AGENT_DIR = DAEMON_INSTALL_DIR
else
JS_DIR_NAME = ".js"
JS_DIR = File.join(ENV['HOME'], JS_DIR_NAME)
DAEMON_INSTALL_DIR = "/usr/local/bin"
AGENT_FILE = "com.github.dotjs.plist"
AGENT_DIR = "~/Library/LaunchAgents"
end
AGENT = File.expand_path(File.join(AGENT_DIR, AGENT_FILE))

def colorize(string, color)
return string if WINDOWS
case color
when "green"
"\e[1m\e[32m#{string}\e[0m"
when "red"
"\e[31m#{string}\e[0m"
when "bold"
"\e[1m#{string}\e[0m"
end
end

namespace :install do
task :all => [ :prompt, :daemon, :create_dir, :agent, :chrome, :done ]
task :all => [ :prompt, :create_dir, :agent, :daemon, :chrome, :start, :done ]

task :prompt do
puts "\e[1m\e[32mdotjs\e[0m"
puts "\e[1m-----\e[0m"
puts colorize("dotjs", "green")
puts colorize("-----", "bold")
puts "I will install:", ""
puts "1. The 'dotjs' Google Chrome Extension"
puts "2. djsd(1) in #{DAEMON_INSTALL_DIR}"
puts "3. com.github.dotjs in ~/Library/LaunchAgents",""
puts "3. #{AGENT_FILE} in #{AGENT_DIR}",""
print "Ok? (y/n) "

begin
Expand All @@ -30,30 +58,26 @@ namespace :install do
end

task :done do
if system("curl http://localhost:3131 &> /dev/null")
puts "\e[1m\e[32mdotjs installation worked\e[0m"
puts "drop files like google.com.js in ~/.js and enjoy hacking the web"
else
puts "\e[31mdotjs installation failed\e[0m"
begin
Net::HTTP.get_response(URI('http://localhost:3131'))
puts colorize("dotjs installation worked", "green")
puts "drop files like google.com.js in ~/#{JS_DIR_NAME} and enjoy hacking the web"
rescue Errno::ECONNREFUSED
puts colorize("dotjs installation failed", "red")
puts "check console.app or open an issue"
end
end

desc "Install launch agent"
task :agent do
plist = "com.github.dotjs.plist"
agent_dir = File.expand_path("~/Library/LaunchAgents/")
agent = File.join(agent_dir, plist)
Dir.mkdir(agent_dir) unless File.exists?(agent_dir)
File.open(agent, "w") do |f|
f.puts ERB.new(IO.read(plist)).result(binding)
path = File.expand_path(AGENT_DIR)
if !File.directory? path
mkdir path
end

chmod 0644, agent
puts "starting djdb..."
sh "launchctl load -w #{agent}"
# wait for server to start
sleep 5
File.open(AGENT, "w") do |f|
f.puts ERB.new(IO.read(AGENT_FILE)).result(binding)
end
end

desc "Install dotjs daemon"
Expand All @@ -63,16 +87,35 @@ namespace :install do

desc "Create ~/.js"
task :create_dir do
if !File.directory? js_dir = File.join(ENV['HOME'], ".js")
mkdir js_dir
chmod 0755, js_dir
if !File.directory? JS_DIR
mkdir JS_DIR
chmod 0755, JS_DIR
end
end

desc "Install Google Chrome extension"
task :chrome do
puts "Installing Google Chrome extension..."
sh "open -a 'Google Chrome' builds/dotjs.crx &"
if WINDOWS
puts "--------------------------------------------------------------"
puts "Windows specific setup:"
puts " Please drag builds/dotjs.crx onto a chrome window to install"
puts "--------------------------------------------------------------"
else
puts "Installing Google Chrome extension..."
sh "open -a 'Google Chrome' builds/dotjs.crx &"
end
end

desc "Start dotjs server"
task :start do
if WINDOWS
sh "cmd /C \"#{AGENT}\" setup"
else
chmod 0644, AGENT
sh "launchctl load -w #{AGENT}"
end
# wait for server to start
sleep 5
end
end

Expand All @@ -83,14 +126,14 @@ namespace :uninstall do
task :all => [ :prompt, :daemon, :agent, :chrome, :done ]

task :prompt do
puts "\e[1m\e[32mdotjs\e[0m"
puts "\e[1m-----\e[0m"
puts colorize("dotjs", "green")
puts colorize("-----", "bold")
puts "I will remove:", ""
puts "1. djsd(1) from #{DAEMON_INSTALL_DIR}"
puts "2. com.github.dotjs from ~/Library/LaunchAgents"
puts "2. #{AGENT_FILE} from #{AGENT_DIR}"
puts "3. The 'dotjs' Google Chrome Extension",""
puts "I will not remove:", ""
puts "1. ~/.js", ""
puts "1. ~/#{JS_DIR_NAME}", ""
print "Ok? (y/n) "

begin
Expand All @@ -106,21 +149,30 @@ namespace :uninstall do
end

task :done do
if system("curl http://localhost:3131 &> /dev/null")
puts "\e[31mdotjs uninstall failed\e[0m"
puts "djsd is still running"
if WINDOWS
puts "dotjs uninstall worked"
puts "The dotjs server will continue to run until you restart"
puts "your ~/#{JS_DIR_NAME} was not touched"
else
puts "\e[1m\e[32mdotjs uninstall worked\e[0m"
puts "your ~/.js was not touched"
begin
Net::HTTP.get_response(URI('http://localhost:3131'))
puts colorize("dotjs uninstall failed", "red")
puts "djsd is still running"
rescue Errno::ECONNREFUSED
puts colorize("dotjs uninstall worked", "green")
puts "your ~/#{JS_DIR_NAME} was not touched"
end
end
end

desc "Uninstall launch agent"
task :agent do
plist = "com.github.dotjs.plist"
agent = File.expand_path("~/Library/LaunchAgents/#{plist}")
sh "launchctl unload #{agent}"
rm agent, :verbose => true
if WINDOWS
sh "cmd /C \"#{AGENT}\" uninstall"
else
sh "launchctl unload #{AGENT}"
end
rm AGENT, :verbose => true
end

desc "Uninstall dotjs daemon"
Expand All @@ -130,7 +182,7 @@ namespace :uninstall do

desc "Uninstall Google Chrome extension"
task :chrome do
puts "\e[1mplease uninstall the google chrome extension manually:\e[0m"
puts colorize("please uninstall the google chrome extension manually:", "bold")
puts "google chrome > window > extensions > dotjs > uninstall"
end
end
Expand Down
20 changes: 20 additions & 0 deletions djsd.vbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
set sh = WScript.CreateObject("Wscript.Shell")
set fso = WScript.CreateObject("Scripting.FileSystemObject")

if WScript.Arguments.length > 0 then
folder = sh.SpecialFolders("Startup")
shortcut_file = folder & "\dotjs.lnk"
if WScript.Arguments(0) = "setup" then
set shortcut = sh.CreateShortcut(shortcut_file)
shortcut.TargetPath = WScript.ScriptFullName
shortcut.WindowStyle = 1
shortcut.WorkingDirectory = "<%= JS_DIR %>"
shortcut.Save
end if
if WScript.Arguments(0) = "uninstall" then
fso.DeleteFile shortcut_file
Wscript.Quit(0)
end if
end if

sh.Run "ruby ""<%= File.join(DAEMON_INSTALL_DIR, "djsd") %>""", 0, False