kOS/lib/boot.ks

86 lines
2.3 KiB
Plaintext
Raw Normal View History

// "bootfile" should be the path to the bootfile, so it can be deleted.
// "programs" is a List() of programs to install in the CPU root.
// "debug" will skip compiling if enabled, and only copy files.
// if "init" is true, the first program in the programs List will become the new boot file.
// pass false in here if you want CPU control from the terminal.
function Bootstrap {
parameter bootFile, programs, debug, init is true.
local initProgram is "".
if init {
set initProgram to programs[0]:Replace("/prog", "").
}
// create a list of libraries that we need to compile
local libs is UniqueSet().
for program in programs {
addLibs(libs, program).
}
// compile the main program files
for program in programs {
compileFile(program, program:Replace("/prog", ""), debug).
}
// compile the libraries
for lib in libs {
2021-08-20 03:54:40 +00:00
compileFile(lib, lib, debug).
}
// Either run init with a terminal open...
if debug {
// Open a terminal and run init.
CORE:PART:GETMODULE("kOSProcessor"):DOEVENT("Open Terminal").
if init <> "" {
RunPath(initProgram).
}
// ... or delete the bootstrapping file, configure to boot from the init program,
// and reboot.
} else {
DeletePath("1:" + bootfile).
set CORE:BOOTFILENAME to initProgram.
reboot.
}
}
// compile the target ('src') file on volume 0, compare compiled size with source size,
// copy the smaller file to 'dest' on volume 1.
// if debug is true, instead just copies src (volume 0) to dest (volume 1).
function compileFile {
parameter src, dest, debug is false.
if debug {
CopyPath("0:" + src + ".ks", "1:" + dest + ".ks").
return.
}
compile "0:" + src.
2021-08-20 03:54:40 +00:00
local srcVF is Open("0:" + src + ".ks").
local compiledVF is Open("0:" + src + ".ksm").
if srcVF:SIZE < compiledVF:SIZE {
CopyPath("0:" + src + ".ks", "1:" + dest + ".ks").
} else {
CopyPath("0:" + src + ".ksm", "1:" + dest + ".ksm").
}
DeletePath(compiledVF).
}
function addLibs {
parameter libs, src.
local srcVF is Open("0:" + src).
local contents is srcVF:ReadAll().
for line in contents {
if line:Contains("RunOncePath") {
2021-08-20 03:54:40 +00:00
local start is line:Find(char(34)).
local end is line:FindLast(char(34)).
local libFile is line:Substring(start + 1, end - start - 1).
libs:Add(libFile).
addLibs(libs, libFile).
}
}
}