Skip to content

Instantly share code, notes, and snippets.

@dave1707
Created January 6, 2016 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dave1707/09a1c1b989efd800f658 to your computer and use it in GitHub Desktop.
Save dave1707/09a1c1b989efd800f658 to your computer and use it in GitHub Desktop.
function setup()
path = os.getenv("HOME").."/Documents/"
dds="/Documents/Dropbox.assets/"
pat1=":::backup"
pat2=":::name:::"
plistData=""
t=os.date("*t")
projectTable={}
backupName=string.format("bkup%02d%02d-%02d%02d",t.month,t.day,t.hour,t.min)
fileSaved={}
func=none
projectListed=false
for a,b in pairs(listProjectTabs()) do
if string.sub(b,1,4)~="Main" then
saveProjectTab(b,nil) -- delete previously extracted project tabs
print("tab "..b.." deleted.")
end
end
showParams()
msg="Press\n\n[ Create a backup file ]\n\nor\n\n[ Show all backup files ]\n\n\nQuestions, contact @dave1707 "
fill(255)
end
function draw()
background(0)
func()
text(msg,WIDTH/2,200)
end
function none()
-- just wastes time until you decide what you want to do.
end
function startBackup()
output.clear()
showParams()
readPlist()
createProjectTable()
saveBackup()
end
function showParams()
parameter.action("Create a backup file",startBackup)
parameter.action("Show all backup files",showBkups)
if showSaved then
parameter.action("Show saved projects",startExtract)
end
if showExtracted then
parameter.action("Extract",getProject)
end
end
function readPlist()
local file = path.."plist.codea/Info.plist"
local rFd = io.open(file,"r")
if rFd then
plistData = rFd:read("a")
rFd:close()
end
end
function createProjectTable()
local matchString=string.match(plistData,"Dependencies(.+)</array>")
for name in string.gmatch(matchString,"<string>(.-)</string>") do
table.insert(projectTable,name)
end
table.sort(projectTable)
end
function saveBackup()
local tempTab={}
local listTab={}
local errTab={}
print(os.date().."\n")
table.insert(tempTab,os.date())
for _,name in pairs(projectTable) do
listTab=listProjectTabs(name)
table.insert(tempTab,"\n\n"..pat1..pat2..name.."\n\n")
if #listTab>0 then
for _,tab in pairs(listTab) do
local str=readProjectTab(name..":"..tab)
local s=string.find(str,"--# "..tab)
if s==nil then
table.insert(tempTab,string.format("\n--# %s\n",tab))
end
table.insert(tempTab,str)
end
print("saved - "..name)
else
table.insert(errTab,"Dependency error - "..name)
end
end
table.insert(tempTab,"\n\n"..pat1..pat2.."end of backups")
local temp=table.concat(tempTab)
saveText("Dropbox:"..backupName,temp)
print("\nFile created. "..backupName)
print("\nNumber of projects ",#projectTable)
print("Number of bytes ",#temp)
print("\n\n")
-- show dependency names of previously deleted projects
for a,b in pairs(errTab) do
print(b)
end
end
function showBkups()
local found=0
msg="Tap a backup file name\n\nthen press\n\n[ Show saved projects ]\n\n\nFile name format\n\nbkupmmdd-hhmm"
output.clear()
local lst=assetList("Dropbox")
for a,b in pairs(lst) do
if string.find(b,"bkup") then
print(b)
found=found+1
end
end
if found==0 then
msg="No backup files found,\n\nor none were created yet."
end
pasteboard.copy("")
showSaved=true
showExtracted=false
parameter.clear()
showParams()
end
function startExtract()
output.clear()
msg="Tap a project name\n\nthen press\n\n[ Extract ]"
projectNotFound=""
func=extractDisplay
projectListed=true
showExtracted=true
listProjects()
parameter.clear()
showParams()
end
function listProjects()
local dt=false
if pasteboard.text==nil then
showSaved=false
showExtracted=false
msg="A project wasn't selected."
return
end
backupName=string.gsub(pasteboard.text,"\n","")
local bkupStr=readText("Dropbox:"..backupName)
local d=string.match(bkupStr,"(.-)\n") -- get date from file
for name in string.gmatch(bkupStr,pat1..pat2.."(.-)\n") do
if not dt then -- print header
print(d)
print("*** Projects in "..backupName.." ***")
dt=true
end
print(name)
end
end
function extractDisplay()
if #fileSaved>0 then
msg="Extract more projects,\n\nor exit the program to see the extracted tabs.\n\n\nLimit the number of extracted tabs per run."
text("Tabs created",WIDTH/2,HEIGHT-250)
for a,b in pairs(fileSaved) do
text(b,WIDTH/2,HEIGHT-250-40*a)
end
end
if projectNotFound~="" then
text("Project not found",WIDTH/2,HEIGHT-100)
text(projectNotFound,WIDTH/2,HEIGHT-125)
end
end
function getProject()
if not projectListed then
output.clear()
end
if pasteboard.text==nil then
return
end
parameter.clear()
func=extractDisplay
readBackup(string.gsub(pasteboard.text,"\n",""))
showParams()
end
function readBackup(proj)
projectNotFound=proj
local bkupStr=readText("Dropbox:"..backupName)
local fstr=pat1..pat2
local temp=string.format("%s%s(.-)%s",fstr,proj,fstr)
for i in string.gmatch(bkupStr,temp) do
projectNotFound=""
saveProjectTab(proj,"--[====[ Select all, then Copy, then Paste into project"..i.."--]====]")
table.insert(fileSaved,proj)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment