Skip to content

Commit

Permalink
Add first start to autoupdate code on OSX
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleconroy committed Sep 5, 2013
1 parent 08ae79c commit c3a9402
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 5 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ notes.html
*.bak
sparkle
*.pyc
Thumbs.db
Thumbs.db

save
src/test/fixtures/Fake.app
Journey\ to\ the\ Center\ of\ Hawkthorne.app
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ before_install:
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
install:
- sudo apt-get install python-virtualenv
- sudo apt-get install python-virtualenv unzip
- sudo make /usr/bin/love
- mkdir -p $TRAVIS_BUILD_DIR/share/love/
env:
Expand Down
106 changes: 106 additions & 0 deletions src/hawk/sparkle.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
-- Usage:
--
-- function love.update(dt)
-- if not updater:done() then
-- updater:update(dt)
-- return
-- end
-- end
--
-- function love.draw()
-- if not updater:done() then
-- updater:draw()
-- return
-- end
-- end

local middle = require 'hawk/middleclass'
local json = require 'hawk/json'
local inspect = require 'vendor/inspect'

local Updater = middle.class('Updater')

function Updater:initialize(version, url)
self.version = version
self.url = url
self._finished = false
end

function Updater:done()
return self._finished
end

function Updater:getApplicationPath(current_os, workingDirectory)
if current_os == "OS X" then
local path = workingDirectory:sub(0, -20)
if path:find(".app") then
return path
end
end
return ""
end

local function execute(command, msg)
local code = os.execute(command .. " > /dev/null 2>&1")

if code ~= 0 then
error(msg)
end
end


-- All paths must be absolute
function Updater:replace(current_os, zipfile, oldpath)
if current_os == "OS X" then
-- This hardcoded value scares me
local appname = "Journey to the Center of Hawkthorne.app"
local destination = love.filesystem.getSaveDirectory()

local newpath = destination .. "/" .. appname

execute(string.format("rm -rf \"%s\"", newpath),
string.format("Error removing previously downloaded %s", newpath))

execute(string.format("unzip -q -d \"%s\" \"%s\"", destination, zipfile),
string.format("Error unzipping %s", zipfile))

execute(string.format("rm -rf \"%s\"", oldpath),
string.format("Error removing previous install %s", oldpath))

execute(string.format("mv \"%s\" \"%s\"", newpath, oldpath),
string.format("Error moving new app %s to %s", newpath, oldpath))

return true
end

return error(string.format("Unknown operation system %s", os))
end


function Updater:update()
local workingdir = love.filesystem.getWorkingDirectory()
local path = self:getApplicationPath(love._os, workingdir)

if path == "" then
return
end

-- Check appcast
-- Download newest release
local download = "/tmp/foo.txt"

-- Remove Old

-- Unzip

-- Move new
print(string.format("mv %s %s", download, path))
end

local sparkle = {}

function sparkle.newUpdater(url)
return Updater(url)
end

return sparkle
6 changes: 6 additions & 0 deletions src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ local timer = require 'vendor/timer'
local cli = require 'vendor/cliargs'
local mixpanel = require 'vendor/mixpanel'

local sparkle = require 'hawk/sparkle'

local debugger = require 'debugger'
local camera = require 'camera'
local fonts = require 'fonts'
Expand Down Expand Up @@ -156,6 +158,10 @@ function love.load(arg)
cheat:on(arg)
end
end

-- Area for testing
local updater = sparkle.newUpdater("0.0.0", "http://foobar")
updater:update()
end

function love.update(dt)
Expand Down
Binary file added src/test/fixtures/tmp/hawkthorne-osx.zip
Binary file not shown.
1 change: 1 addition & 0 deletions src/test/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ lunatest.suite("test/test_queue")
lunatest.suite("test/test_dialog")
lunatest.suite("test/test_transition")
lunatest.suite("test/test_fsm")
lunatest.suite("test/test_updater")

lunatest.run()
53 changes: 53 additions & 0 deletions src/test/test_updater.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
local sparkle = require "hawk/sparkle"
local u = sparkle.newUpdater("0.0.0", "http://example.com")

function test_updater_get_osx_path()
local path = u:getApplicationPath("OS X", "/Users/joe/projects/hawkthorne-journey/Journey to the Center of Hawkthorne.app/Contents/Resources")
assert_equal("/Users/joe/projects/hawkthorne-journey/Journey to the Center of Hawkthorne.app", path)
end

function test_updater_no_root_path()
local path = u:getApplicationPath("OS X", "//")
assert_equal(path, "")
end

function test_updater_no_root_path()
local path = u:getApplicationPath("OS X", "//Contents/Resources")
assert_equal(path, "")
end

function test_updater_unknown_os()
local path = u:getApplicationPath("", "foobar")
assert_equal(path, "")
end

function test_updater_replace_unknown_os()
assert_error(function()
u:replace("symbian", "foo", "bar")
end)
end

function test_updater_unzip_unknown_file()
assert_error(function()
u:replace("OS X", "/foo/bar.zip", "bar")
end)
end

local function file_exists(name)
local f=io.open(name,"r")
if f~=nil then io.close(f) return true else return false end
end

function test_updater_unzip_and_overwrite()
local cwd = love.filesystem.getWorkingDirectory()
local zipfile = cwd .. "/src/test/fixtures/tmp/hawkthorne-osx.zip"
local apppath = cwd .. "/src/test/fixtures/Fake.app"
u:replace("OS X", zipfile, apppath)
assert_true(file_exists(apppath))
end






6 changes: 3 additions & 3 deletions src/vendor/inspect.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-----------------------------------------------------------------------------------------------------------------------
-- inspect.lua - v1.2.0 (2012-10)
-- inspect.lua - v1.2.1 (2013-01)
-- Enrique García Cota - enrique.garcia.cota [AT] gmail [DOT] com
-- human-readable representations of tables.
-- inspired by http://lua-users.org/wiki/TableSerialization
Expand Down Expand Up @@ -64,7 +64,7 @@ local function getDictionaryKeys(t)
end

local function getToStringResultSafely(t, mt)
local __tostring = type(mt) == 'table' and mt.__tostring
local __tostring = type(mt) == 'table' and rawget(mt, '__tostring')
local string, status
if type(__tostring) == 'function' then
status, string = pcall(__tostring, t)
Expand Down Expand Up @@ -110,10 +110,10 @@ function Inspector:countTableAppearances(t)
self:countTableAppearances(k)
self:countTableAppearances(v)
end
self:countTableAppearances(getmetatable(t))
else
self.tableAppearances[t] = self.tableAppearances[t] + 1
end
self:countTableAppearances(getmetatable(t))
end
end

Expand Down

0 comments on commit c3a9402

Please sign in to comment.