Skip to content
Extraits de code Groupes Projets
Valider 5d5df527 rédigé par LaplongeJunior's avatar LaplongeJunior
Parcourir les fichiers

Delete xbuild.src

parent dfd710cf
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
"#import includes/utils.inc.src"
"#import includes/file.inc.src"
// To allow self-building : build it with build, then use it to build it's own source to include libraries
"#ifbuild"
if not globals.hasIndex("imports") then
imports = {}
imports.utils = {}
imports.file = {}
utils = imports.utils
file = imports.file
utils.GetProgName = function()
// To make sure we notice it's not updated
return program_path.split("/")[-1] +" (by build)"
end function
utils.Print = function(reason)
print(imports.utils.GetProgName()+": "+reason)
end function
utils.Exit = function(reason)
exit(imports.utils.GetProgName()+": "+reason)
end function
utils.ParamLength = function(params)
length = 0
for param in params
if param[0] != "-" then length = length + 1
end for
return length
end function
utils.GetParam = function(params, index)
i = 0
for param in params
if param[0] == "-" then continue
if (i == index) then return param
i = i+1
end for
return null
end function
utils.HasFlag = function(params, short, long)
// Not critical for a self-build
return long != "help"
end function
utils.leftSlice = function(str,stop)
if stop == 0 then return ""
return str[:stop]
end function
file.NEW_LINE = char(10)
file.AbsolutePath = function(path, current)
if path[0] == "/" then return path
if current == null then current = get_shell.host_computer.current_path
path = current + "/" + path
return path
end function
file.getFiles = function(path, recursive)
return [get_shell.host_computer.File(path)]
end function
file.ReadAllLines = function(target)
utils.Print("Reading file '"+path+"'")
// Enough to self-build
if typeof(target) == "string" then
target = get_shell.host_computer.File(target)
end if
return target.content.split(file.NEW_LINE)
end function
file.GetDir = function(path)
if typeof(path) == "file" then path = path.path
return path[0:path.lastIndexOf("/")]
end function
file.GetName = function(path)
return path[path.lastIndexOf("/")+1:]
end function
file.FileNameNoExt = function(path)
index = path.lastIndexOf("/")
if index != null then path = path[index+1:]
index = path.indexOf(".")
if index != null then path = path[:index]
return path
end function
end if
"#endif"
//command: xbuild
length = imports.utils.ParamLength(params)
if length < 2 or length > 4 or imports.utils.HasFlag(params, "h", "help") then exit("<b>Usage: "+imports.utils.GetProgName()+" [file] [build dir] [(opt) preproc dir] --save</b>")
comp = get_shell.host_computer
// Return null for an empty String, returns the last character otherwise
// Used to identify Strings
GetLastChar = function(str)
if not str then return null
if str.len < 1 then return null
return str[str.len-1]
end function
source = imports.utils.GetParam(params,0)
build = imports.utils.GetParam(params,1)
preproc = imports.utils.GetParam(params,2)
save = imports.utils.HasFlag(params,"s","save")
if not preproc then
preproc = comp.current_path
else
if GetLastChar(preproc) == "/" then preproc = preproc[0:preproc.len-1]
print(preproc)
preproc = imports.file.AbsolutePath(preproc)
print(preproc)
end if
// A " character
QUOTE = """"
// If a line is made of a String with the flag character, returns the text inside
StringInstruction = function(str)
if GetLastChar(str) != QUOTE then return null
if str[0] != QUOTE then return null
str = str[1:str.len-1]
if str.indexOf(QUOTE) != null then return null
str = str.trim()
if str[0] != "#" then return ""
return str[1:str.len]
end function
// Remove the comment at the end of a line, and the extra spaces at the start and the end
// Shouldn't affect the result of the script, but reduce the total character count
WithoutComment = function(str)
Count = function(str, char)
count = 0
for car in str
if car == char then count = count + 1
end for
return count
end function
COM = "//"
index = str.indexOf(COM)
if index == null then return str
result = imports.utils.leftSlice(str,index)
if Count(result, QUOTE) % 2 != 0 then
result = result + COM + WithoutComment(str[index+2:str.len])
end if
return result.trim()
end function
// List of already imported libraries to avoid duplicating the code
imported = []
// If two imports are trying to import each other *before the other*, will make sure the script errors instead of going in an infinite loop
check = []
// Method called to read-copy a source file (either the original script or its libraries)
InsertCode = function(path, original=false)
IMPORT = "import "
IFBUILD = "ifbuild"
ENDIF = "endif"
// Store the processed source for this specific file
content = ""
nextLine = false
skip = false
followImport = false
for line in imports.file.ReadAllLines(path)
// Removes the extra space, the comment and AGAIN the extra space once the comment is removed
line = WithoutComment(line.trim())
//if line == null or line.len == 0 then continue
if line.len == 0 then continue
isImport = false
instruct = StringInstruction(line)
if instruct != null then
if instruct.len == 0 then continue
// There's use of the "#command abc" syntax
line = null
isImport = false
for tag in [IMPORT, IFBUILD, ENDIF]
if instruct[0:tag.len] == tag then
if tag == IMPORT then
isImport = true
followImport = true
// If currently in "skip mode", obviously nothing should change :D
if not skip then
importPath = imports.file.AbsolutePath(instruct[tag.len:], imports.file.GetDir(path))
// If already imported, skip to save characters
if imported.indexOf(importPath) == null then
// If two imports tries to import each other, which one goes first? Nice paradox, right?
if check.indexOf(importPath) != null then exit("ERROR, cyclic import of "+importPath)
check.push(importPath)
// Recursive call
line = "imports.temp={}"+ imports.file.NEW_LINE + InsertCode(imports.file.AbsolutePath(importPath))
imported.push(importPath)
end if
end if
else
skip = (tag == IFBUILD)
end if
break
end if
end for
// No valid command? Unused string then skip it
if line == null then continue
end if
// followImport is memorized, isImport is reset after each line
if not isImport and followImport then
followImport = false
line = "imports.temp={}" + imports.file.NEW_LINE + line
end if
// No writing in skip mode
if skip or line == null then continue
// A new line char is not required for the first line
if nextLine then
content = content + imports.file.NEW_LINE
else
nextLine = true
end if
// FINALLY, we can add the preprocessed source :D
content = content + line
end for
// Wrong src path?
if content == "" then imports.utils.Print("Unable to read " + (path) + " !")
return content
end function
// Oh yes, we prepared the processing of source files, but we didn't even load any file at the moment!
sourcePath = imports.file.AbsolutePath(source,preproc)
// If we pass a dir path, all files in this dir will be loaded
for sourceFile in imports.file.getFiles(sourcePath, false)
check = sourceFile.name.split(".")
found = false
// Useful when building a whole folder, but it's maybe a bit too intrusive?
for i in range(1, check.len-1)
if check[i] == "inc" or check[i] == "temp" then
found = true
print("Temporary or library file found, skipping "+sourceFile.name)
break
end if
end for
if found then continue
// Imports are based on the file containing the currently read script
// In other words, an "import importing another import" is based on the path of the IMPORT, not the main script
sourceLoc = sourceFile.path[preproc.len+1:]
imported = []
check = []
content = InsertCode(sourceFile, true)
// Adding the imports map if there's at least one import
if imported.len > 0 then content = "imports={}" + imports.file.NEW_LINE + content
// No need to call build if there's no source!
if content.len == 0 then exit("Empty source!")
// Create a temp source file for build, then either delete it or store it as .temp.src
//targetLoc = sourceLoc[0:sourceLoc.lastIndexOf(".")] + ".temp"
targetLoc = imports.file.FileNameNoExt(sourceLoc) + ".temp"
targetPath = imports.file.AbsolutePath(targetLoc,preproc)
// If there's already a temp file, delete it
targetFile = comp.File(targetPath)
if targetFile then targetFile.delete
comp.touch(imports.file.GetDir(targetPath),imports.file.GetName(targetPath))
targetFile = comp.File(targetPath)
// Build the preprocessed source
print("building "+content.len+" characters for "+sourceLoc)
targetFile.set_content(content)
result = get_shell.build(targetPath, build)
// If there's no need to save the temp file, delete the OLD saved file to avoid checking an outdated source
if save then
targetFile.move(imports.file.GetDir(targetPath),imports.file.GetName(targetPath)+".src")
else
targetFile.delete
old = comp.File(targetPath+".src")
if old then old.delete
end if
// Pfew, we reached the end!
if result.len == 0 then
print("build successful.")
else
print(result)
end if
end for
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter