Skip to content

Instantly share code, notes, and snippets.

@Jan200101
Last active January 5, 2024 22:18
Show Gist options
  • Save Jan200101/224bb2a87ed691b33891dc393330b630 to your computer and use it in GitHub Desktop.
Save Jan200101/224bb2a87ed691b33891dc393330b630 to your computer and use it in GitHub Desktop.
ubuild bootstrap
// ubuild.src
if params.len != 2 or params[0] == "-h" or params[0] == "--help" then exit("<b>Usage: "+program_path.split("/")[-1]+" [CODE] [OUT]</b>")
shell = get_shell
computer = shell.host_computer
// start ../lib-src/pathsep.src
PATHSEP = "/"
// end ../lib-src/pathsep.src
// start ../lib-src/abspath.src
// start pathsep.src
PATHSEP = "/"
// end pathsep.src
abspath = function(path, cwd = null)
if cwd == null then cwd = current_path
if path[0] != PATHSEP then path = cwd + PATHSEP + path
pathseg = path.split(PATHSEP)
i = 0
while i < pathseg.len
if pathseg[i] == "." then
pathseg.remove(i)
continue
else if pathseg[i] == ".." then
pathseg.remove(i)
pathseg.remove(i-1)
i = i - 1
continue
end if
i = i + 1
end while
return pathseg.join(PATHSEP)
end function
// end ../lib-src/abspath.src
// start ../lib-src/dirname.src
// start pathsep.src
PATHSEP = "/"
// end pathsep.src
dirname = function(path)
l = path.len
if path[l-1] == PATHSEP then
return path
end if
for i in range(l-2, 0)
if path[i] == PATHSEP then
return path[0:i]
end if
end for
return path
end function
// end ../lib-src/dirname.src
// start ../lib-src/basename.src
// start pathsep.src
PATHSEP = "/"
// end pathsep.src
basename = function(path)
l = path.len
while path[l-1] == PATHSEP
l = l - 1
end while
for i in range(l-1, 0)
if path[i] == PATHSEP and path[i+1:l].len != 0 then
return path[i+1:l]
end if
end for
return path[0:l]
end function
// end ../lib-src/basename.src
// start ../lib-src/resimp.src
// resimp.src
// start abspath.src
// start pathsep.src
PATHSEP = "/"
// end pathsep.src
abspath = function(path, cwd = null)
if cwd == null then cwd = current_path
if path[0] != PATHSEP then path = cwd + PATHSEP + path
pathseg = path.split(PATHSEP)
i = 0
while i < pathseg.len
if pathseg[i] == "." then
pathseg.remove(i)
continue
else if pathseg[i] == ".." then
pathseg.remove(i)
pathseg.remove(i-1)
i = i - 1
continue
end if
i = i + 1
end while
return pathseg.join(PATHSEP)
end function
// end abspath.src
_dedup_list = []
resolve_import = function(comp, code, cwd=null)
if cwd == null then cwd = current_path
IMPORTCODELIT = "import_code"+"("
import_index = code.indexOf(IMPORTCODELIT)
in_comment = function(iindex)
line_start = iindex
while 0 < line_start and code[line_start-1] != char(10)
line_start = line_start - 1
end while
comment_start = code.indexOf("//", line_start-1)
if comment_start == null then return false
return comment_start < import_index
end function
// Resolve source code imports
while import_index != null
// check if we are in a comment first
if in_comment(import_index) == true then
// the import is in a comment, find the next one
import_index = code.indexOf(IMPORTCODELIT, import_index+1)
continue
end if
start_quote = code.indexOf("""", import_index + 1)
if start_quote == null then exit("could not find import_code string")
end_quote = code.indexOf("""", start_quote + 1)
if end_quote == null then exit("could not find end of import_code string")
import_end = code.indexOf(")", end_quote)
if import_end == null then exit("could not find end of import_code")
import_name = code[start_quote+1:end_quote]
import_file = comp.File(abspath(import_name, cwd))
if import_file == null then
exit("error: " + import_name + " not found")
else if import_file.is_binary then
// skip binary imports for now
import_index = import_end + 1
else
imp_code = code[import_index:import_end+1]
rep_code = resolve_import(comp, import_file.get_content, dirname(import_file.path))
start_comment = "// start " + import_name + char(10)
end_comment = "// end " + import_name + char(10)
code = code[0:import_index] + start_comment + rep_code + end_comment + code[import_end+1:code.len]
end if
// find next index
import_index = code.indexOf(IMPORTCODELIT, import_index-1)
end while
// deduplicate binary imports
import_index = code.indexOf(IMPORTCODELIT)
while import_index != null
// check if we are in a comment first
if in_comment(import_index) == true then
// the import is in a comment, find the next one
import_index = code.indexOf(IMPORTCODELIT, import_index+1)
continue
end if
start_quote = code.indexOf("""", import_index + 1)
if start_quote == null then exit("could not find import_code string")
end_quote = code.indexOf("""", start_quote + 1)
if end_quote == null then exit("could not find end of import_code string")
import_end = code.indexOf(")", end_quote)
if import_end == null then exit("could not find end of import_code")
import_name = code[start_quote+1:end_quote]
import_file = comp.File(import_name)
if import_file.is_binary == false then
exit("found a source import while trying to dedup")
end if
import_seg = code[import_index:import_end+1]
dup_index = code.indexOf(import_seg, import_end+1)
while dup_index != null
code = code[0:dup_index] + code[dup_index+import_seg.len:code.len]
dup_index = code.indexOf(import_seg, dup_index)
end while
// find next index
import_index = code.indexOf(IMPORTCODELIT, import_end)
end while
return code
end function
// end ../lib-src/resimp.src
// start ../lib-src/mktemp.src
// mktemp.src
// start getpid.src
// getpid.src
getpid = function(comp)
procs = comp.show_procs
list = procs.split(char(10))[1:]
for item in list
parsedItem = item.split(" ")
if parsedItem[4] == program_path.split("/")[-1] then
return parsedItem[1]
end if
end for
return null
end function
// end getpid.src
// start pathsep.src
PATHSEP = "/"
// end pathsep.src
mktemp = function(comp, template)
if template == null then exit("mktemp: no template specified")
filename = ".tmp." + template + getpid(comp)
res = comp.touch(home_dir, filename)
if typeof(res) == "string" then
print("mktemp: error " + res)
end if
return home_dir + PATHSEP + filename
end function
// end ../lib-src/mktemp.src
filename = abspath(params[0])
output = abspath(params[1])
fd = computer.File(abspath(filename))
if fd == null then
exit("[!] File not found")
end if
print("[*] Resolving imports")
code = resolve_import(computer, fd.get_content, dirname(filename))
print("[*] Creating source bundle")
src_file = computer.File(mktemp(computer, "ubuild"))
bin_path = src_file.path
src_path = src_file.path + ".src"
src_file.set_content(code)
res = src_file.rename(basename(src_path))
if res != "" then
src_file.delete
exit("[!] failed to created source bundle: " + res)
end if
src_file = computer.File(src_path)
print("[*] Building bundle")
res = shell.build(src_path, dirname(bin_path))
src_file.delete
if res != "" then
exit("[!] build failure: " + res)
end if
computer.File(bin_path).move(dirname(output), basename(output))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment