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

Fix tests for 0.7 #139

Closed
wants to merge 11 commits into from
4 changes: 2 additions & 2 deletions deps/build.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ module BuildBlink
include(joinpath(@__DIR__, "../src/AtomShell/install.jl"))

function get_installed_version()
_path = is_apple() ?
_path = isapple() ?
joinpath(folder(), "version") :
joinpath(folder(), "atom", "version")
strip(readstring(_path), 'v')
strip(read(_path, String), 'v')
end

if isinstalled() && !(version == get_installed_version())
Expand Down
4 changes: 3 additions & 1 deletion src/AtomShell/AtomShell.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module AtomShell

using Compat; import Compat.String
using Compat
using Compat.Sockets
using Compat.Sys: isapple, isunix, islinux, iswindows

abstract type Shell end

Expand Down
14 changes: 7 additions & 7 deletions src/AtomShell/install.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ const version = "2.0.5"

folder() = normpath(joinpath(@__FILE__, "../../../deps"))

@static if is_apple()
@static if isapple()
uninstall() = map(rm′, filter(x -> !endswith(x, "build.jl"), readdir(folder())))
else
uninstall() = rm′(joinpath(folder(), "atom"))
end

isinstalled() = is_apple() ?
isinstalled() = isapple() ?
isfile(joinpath(folder(), "version")) :
isdir(joinpath(folder(), "atom"))


function install()
dir = folder()
if is_apple()
const _icons = normpath(joinpath(@__FILE__, "../../../res/julia-icns.icns"))
if isapple()
_icons = normpath(joinpath(@__FILE__, "../../../res/julia-icns.icns"))
end
!isdir(dir) && mkpath(dir)
uninstall()
Expand All @@ -29,7 +29,7 @@ function install()

download("http://junolab.s3.amazonaws.com/blink/julia.png")

if is_apple()
if isapple()
file = "electron-v$version-darwin-x64.zip"
download("https://github.com/electron/electron/releases/download/v$version/$file")
run(`unzip -q $file`)
Expand All @@ -41,15 +41,15 @@ function install()
run(`touch Julia.app`) # Apparently this is necessary to tell the OS to double-check for the new icons.
end

if is_windows()
if iswindows()
arch = Int == Int64 ? "x64" : "ia32"
file = "electron-v$version-win32-$arch.zip"
download("https://github.com/electron/electron/releases/download/v$version/$file")
run(`7z x $file -oatom`)
rm(file)
end

if is_linux()
if islinux()
arch = Int == Int64 ? "x64" : "ia32"
file = "electron-v$version-linux-$arch.zip"
download("https://github.com/electron/electron/releases/download/v$version/$file")
Expand Down
56 changes: 39 additions & 17 deletions src/AtomShell/process.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
using Lazy, JSON, MacroTools

hascommand(c) =
try readstring(`which $c`); true catch e false end
try read(`which $c`, String); true catch e false end

spawn_rdr(cmd) = spawn(cmd, Base.spawn_opts_inherit()...)
spawn_rdr(cmd) = run(cmd, Base.spawn_opts_inherit()...; wait=false)

resolve(pkg, path...) =
joinpath(Base.find_in_path(pkg, nothing), "..","..", path...) |> normpath
"""
resolve_blink_asset(path...)

Find a file, expressed as a relative path from the Blink package
folder. Example:

resolve_blink_asset("src", "Blink.jl") -> /home/<user>/.julia/v0.6/Blink/src/Blink.jl
"""
resolve_blink_asset(path...) = joinpath(@__DIR__, "..", "..", path...)

@deprecate resolve(pkg, path...) resolve_blink_asset(path...)

# node-inspector

Expand All @@ -27,26 +36,26 @@ end

# atom-shell

import Base: Process, TCPSocket
import Base: Process

export Electron

type Electron <: Shell
mutable struct Electron <: Shell
proc::Process
sock::TCPSocket
handlers::Dict{String, Any}
end

Electron(proc, sock) = Electron(proc, sock, Dict())

@static if is_apple()
const _electron = resolve("Blink", "deps/Julia.app/Contents/MacOS/Julia")
elseif is_linux()
const _electron = resolve("Blink", "deps/atom/electron")
elseif is_windows()
const _electron = resolve("Blink", "deps", "atom", "electron.exe")
@static if isapple()
const _electron = resolve_blink_asset("deps/Julia.app/Contents/MacOS/Julia")
elseif islinux()
const _electron = resolve_blink_asset("deps/atom/electron")
elseif iswindows()
const _electron = resolve_blink_asset("deps", "atom", "electron.exe")
end
const mainjs = resolve("Blink", "src", "AtomShell", "main.js")
const mainjs = resolve_blink_asset("src", "AtomShell", "main.js")

function electron()
path = get(ENV, "ELECTRON_PATH", _electron)
Expand All @@ -72,7 +81,12 @@ function init(; debug = false)
p, dp = port(), port()
debug && inspector(dp)
dbg = debug ? "--debug=$dp" : []
proc = (debug ? spawn_rdr : spawn)(`$(electron()) $dbg $mainjs port $p`)
electron_cmd = `$(electron()) $dbg $mainjs port $p`
proc = if debug
spawn_rdr(electron_cmd)
else
run(electron_cmd; wait=false)
end
conn = try_connect(ip"127.0.0.1", p)
shell = Electron(proc, conn)
initcbs(shell)
Expand All @@ -89,9 +103,17 @@ handlers(shell::Electron) = shell.handlers

function initcbs(shell)
enable_callbacks!(shell)
@schedule begin
while active(shell)
@errs handle_message(shell, JSON.parse(shell.sock))
@static if VERSION < v"0.7.0-alpha.0"
@schedule begin
while active(shell)
@errs handle_message(shell, JSON.parse(shell.sock))
end
end
else
@async begin
while active(shell)
@errs handle_message(shell, JSON.parse(shell.sock))
end
end
end
end
Expand Down
18 changes: 11 additions & 7 deletions src/AtomShell/window.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export Window, flashframe, shell, progress, title,
centre, floating, loadurl, opentools, closetools, tools,
loadhtml, loadfile, css, front

type Window
mutable struct Window
id::Int
shell::Shell
content
Expand All @@ -20,17 +20,17 @@ const window_defaults = @d(:url => "about:blank",
:title => "Julia",
"node-integration" => false,
"use-content-size" => true,
:icon => resolve("Blink", "deps", "julia.png"))
:icon => resolve_blink_asset("deps", "julia.png"))

raw_window(a::Electron, opts) = @js a createWindow($(merge(window_defaults, opts)))

function Window(a::Shell, opts::Associative = Dict())
function Window(a::Shell, opts::AbstractDict = Dict())
return haskey(opts, :url) ?
Window(raw_window(a, opts), a, nothing) :
Window(a, Page(), opts)
end

function Window(a::Shell, content::Page, opts::Associative = Dict())
function Window(a::Shell, content::Page, opts::AbstractDict = Dict())
opts = merge(opts, Dict(:url => Blink.localurl(content)))
return Window(raw_window(a, opts), a, content)
end
Expand Down Expand Up @@ -120,15 +120,15 @@ close(win::Window) =

# Window content APIs

active(::Void) = false
active(::Nothing) = false

handlers(w::Window) = handlers(w.content)

msg(win::Window, m) = msg(win.content, m)

js(win::Window, s::JSString; callback = true) =
active(win.content) ? js(win.content, s, callback = callback) :
dot(win, :(this.webContents.executeJavaScript($(jsstring(s)))), callback = callback)
dot(win, :(this.webContents.executeJavaScript($(s.s))), callback = callback)

const initcss = """
<style>html,body{margin:0;padding:0;border:0;text-align:center;}</style>
Expand All @@ -141,6 +141,10 @@ function loadhtml(win::Window, html::AbstractString)
println(io, html)
end
loadfile(win, tmp)
@schedule (sleep(1); rm(tmp))
@static if VERSION < v"0.7.0-alpha.0"
@schedule (sleep(1); rm(tmp))
else
@async (sleep(1); rm(tmp))
end
return
end
8 changes: 6 additions & 2 deletions src/Blink.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ __precompile__()
module Blink

using Reexport
using Compat; import Compat.String
using Compat
using Compat.Distributed: Future
using Compat.Sys: isunix, islinux, isapple, iswindows
using Compat.Sockets
using Compat.Base64: stringmime

include("rpc/rpc.jl")
include("content/content.jl")

include("AtomShell/AtomShell.jl")
export AtomShell
@reexport using .AtomShell
import .AtomShell: resolve
import .AtomShell: resolve_blink_asset

end # module
14 changes: 8 additions & 6 deletions src/content/config.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
export localips

@init global const port = get(ENV, "BLINK_PORT", rand(2_000:10_000))
const port = Ref{Int}()

@init port[] = haskey(ENV, "BLINK_PORT") ? parse(Int, get(ENV, "BLINK_PORT")) : rand(2_000:10_000)

const ippat = r"([0-9]+\.){3}[0-9]+"

@static if is_unix()
@static if isunix()
localips() = map(IPv4, readlines(`ifconfig` |>
`grep -Eo $("inet (addr:)?$(ippat.pattern)")` |>
`grep -Eo $(ippat.pattern)` |>
Expand All @@ -13,14 +15,14 @@ end

# Browser Window

@static if is_apple()
@static if isapple()
launch(x) = run(`open $x`)
elseif is_linux()
elseif islinux()
launch(x) = run(`xdg-open $x`)
elseif is_windows()
elseif iswindows()
launch(x) = run(`cmd /C start $x`)
end

localurl(p::Page) = "http://127.0.0.1:$port/$(id(p))"
localurl(p::Page) = "http://127.0.0.1:$(port[])/$(id(p))"

launch(p::Page) = (launch(localurl(p)); p)
8 changes: 5 additions & 3 deletions src/content/content.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ include("api.jl")

# Content

type Page
mutable struct Page
id::Int
sock::WebSocket
handlers::Dict{String, Any}
Expand All @@ -20,7 +20,9 @@ type Page
init == nothing || (p.handlers["init"] = init)
enable_callbacks!(p)
pool[p.id] = WeakRef(p)
finalizer(p, p -> delete!(pool, p.id))
finalizer(p) do p
delete!(pool, p.id)
end
return p
end
end
Expand Down Expand Up @@ -52,5 +54,5 @@ end
include("server.jl")

@init for r in ["blink.js", "blink.css", "reset.css", "spinner.css"]
resource(resolve("Blink", "res", r))
resource(resolve_blink_asset("res", r))
end
10 changes: 5 additions & 5 deletions src/content/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const maintp = Mustache.template_from_file(joinpath(dirname(@__FILE__), "main.ht
app(f) = req -> render(maintp, d("id"=>Page(f).id))

function page_handler(req)
id = try parse(req[:params][:id]) catch e @goto fail end
id = try parse(Int, req[:params][:id]) catch e @goto fail end
haskey(pool, id) || @goto fail
active(pool[id].value) && @goto fail

Expand All @@ -28,14 +28,14 @@ function page_handler(req)
end

function ws_handler(req)
id = try parse(req[:path][end]) catch e @goto fail end
id = try parse(Int, req[:path][end]) catch e @goto fail end
client = req[:socket]
haskey(pool, id) || @goto fail
p = pool[id].value
active(p) && @goto fail

p.sock = client
@schedule @errs get(handlers(p), "init", identity)(p)
@async @errs get(handlers(p), "init", identity)(p)
put!(p.cb, true)
while active(p)
local data
Expand Down Expand Up @@ -77,7 +77,7 @@ function serve()
serving && return
serving = true
http = Mux.http_handler(Mux.App(http_default))
delete!(http.events, "listen")
#delete!(http.events, "listen")
ws = Mux.ws_handler(Mux.App(ws_default))
Mux.serve(Mux.Server(http, ws), port, host = ip"127.0.0.1")
@async WebSockets.serve(WebSockets.ServerWS(http, ws), ip"127.0.0.1", port[], false)
end
4 changes: 2 additions & 2 deletions src/rpc/rpc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export js, @js, @js_, @var, @new

include("callbacks.jl")

type JSError <: Exception
mutable struct JSError <: Exception
name::String
msg::String
end
Expand All @@ -32,7 +32,7 @@ function js(o, js::JSString; callback = true)

if callback
val = wait(cond)
if isa(val, Associative) && get(val, "type", "") == "error"
if isa(val, AbstractDict) && get(val, "type", "") == "error"
err = JSError(get(val, "name", "unknown"), get(val, "message", "blank"))
throw(err)
end
Expand Down
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Blink
using Base.Test
using Compat.Test
using Compat.Sockets


cleanup = !AtomShell.isinstalled()

Expand Down