Skip to content
Permalink
Browse files
Small changes to classes. Added Matcher class. Added igen program for…
… generating installers.
  • Loading branch information
James King committed Sep 14, 2013
1 parent 5e40419 commit c32c0b3
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 4 deletions.
@@ -7,7 +7,6 @@
--@import see.base.System
--@import see.util.ArgumentUtils

-- TODO: Look at possibly making this object purely mutable.
--[[
An Array-backed string. Faster and more memory efficient than native strings.
]]
@@ -65,4 +65,13 @@ end
]]
function System.resolve(path)
return Path.new(shell.resolve(path.pathString:lstr()))
end

function System.run(path, ...)
local args = Array.new(...)
for i = 1, args:length() do
System.print("h")
args[i] = cast(args[i], "string")
end
shell.run(path.pathString:lstr(), args:unpack())
end
@@ -44,4 +44,14 @@ end

function Files.delete(path)
fs.delete(path.pathString:lstr())
end

function Files.readAll(path)
System.print(typeof(path))
System.print(cast("hello", String))
System.print(path.pathString:lstr())
local handle = fs.open(path.pathString:lstr(), "r")
local ret = String.new(handle.readAll())
handle.close()
return ret
end
@@ -1,15 +1,16 @@
--@native fs
--@import see.base.String

function Path:init(pathString)
self.pathString = cast(pathString, String)
end

function Path:combine(p)
return Path.new(String.new(fs.combine(self.pathString, p.pathString)))
return Path.new(String.new(fs.combine(self.pathString:lstr(), p.pathString:lstr())))
end

function Path:getName()
return String.new(fs.getName(self.pathString))
return String.new(fs.getName(self.pathString:lstr()))
end

function Path:toString()
@@ -0,0 +1,23 @@
--@native string.find

--[[
Used to match substrings of a string to a particular pattern.
]]

--[[
Constructs a matcher with the given pattern.
@param string:pattern Pattern to match.
]]
function Matcher:init(pattern, str)
self.pattern = cast(pattern, "string")
self.string = cast(str, "string")
end

function Matcher:find()
self.left, self.right = self.string:find(pattern, (self.right or 0) + 1)
return self.left and true or false
end

function Matcher:matched()
return String.new(str:sub(self.left, self.right))
end
@@ -0,0 +1,83 @@
local _, err = pcall(function(...)
local USAGE = "Usage: igen <runnable> <name> <target>"

local args = { ... }
if #args < 3 then print(USAGE) end

local code =
[[
local _, err = pcall(function(...)
local programName = '$'

if not fs.exists("/.see") then
print(programName .. " requires SEE to be installed. Downloading SEE installer...")
local installerCode = http.get("https://raw.github.com/Yevano/see/master/see/programs/installer").readAll()

while true do
print("Running installer.")
local inst = loadstring(installerCode)
setfenv(inst, setmetatable({ shell = shell }, { __index = _G }))
local _, err = pcall(inst)
if err then print(err) end

if fs.exists("/.see") then
break
end

write("SEE was not installed. Quit installation? (y/n) ")
while true do
local response = read()
if string.lower(response):gsub("%s", "") == "y" then
return
elseif string.lower(response):gsub("%s", "") == "n" then
break
else
print("Invalid input.")
end
end
end
end

local runnableBytes = '$'

write("Choose an install path for " .. programName .. ": ")
local installPath = "/" .. shell.resolve(read())

local out = fs.open(installPath, "w")
out.write(runnableBytes)
out.close()

print(programName .. " was installed successfully.")
end, ...)

if err then
error(err)
end
]]

local runnableRead = fs.open("/" .. shell.resolve(args[1]), "r")
local runnableBytes = runnableRead.readAll()
runnableRead.close()
local name = args[2]
local target = "/" .. shell.resolve(args[3])

local runnableTable = { }
for i = 1, #runnableBytes do
runnableTable[i] = "\\" .. runnableBytes:sub(i, i):byte()
end

local runnableConverted = table.concat(runnableTable)

local f = code:find("$", 1, true)
code = code:sub(1, f - 1) .. name .. code:sub(f + 1)
f = code:find("$", f + #name, true)
code = code:sub(1, f - 1) .. runnableConverted .. code:sub(f + 1)

local targetWrite = fs.open(target, "w")
targetWrite.write(code)
targetWrite.close()
end, ...)

if err then
error(err)
end
@@ -82,7 +82,7 @@ while true do
while true do
local downloaded = 0

write("SEE Installer\nChoose an install path: ")
write("Choose an install path: ")
local installPath = "/" .. shell.resolve(read())

local configHandle = fs.open("/.see", "w")

0 comments on commit c32c0b3

Please sign in to comment.