PureBasic: SpiderBasicCopier

Share your advanced knowledge/code with the community.
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

PureBasic: SpiderBasicCopier

Post by Peter »

Hello,

this is a code (written in PB) to copy your SpiderBasic-Code (including the necessary JS-files) to your local webserver.

The Code is designed to run as a SpiderBasic-Tool:

Image

The first argument is the path & name of your sourcefile: "%FILE"

The second argument is the path of your local webserver.

Code: Select all

EnableExplicit

#AppName = "SpiderBasicCopier"

CompilerIf #PB_Compiler_OS = #PB_OS_Linux Or #PB_Compiler_OS = #PB_OS_MacOS
	#Slash = "/"
CompilerElse
	#Slash = "\"
CompilerEndIf

Procedure.s LoadTextFile(TextFilename.s)
	
	Protected FF, StringFormat
	Protected ReturnValue.s
	Protected TextBuffer, TextBufferSize
	
	If FileSize(TextFilename) <= 0 : ProcedureReturn "" : EndIf
	FF = ReadFile(#PB_Any, TextFilename)
	If FF = 0 : ProcedureReturn "" : EndIf
	StringFormat = ReadStringFormat(FF)
	TextBufferSize = Lof(FF)
	TextBuffer = AllocateMemory(TextBufferSize)
	If TextBuffer = 0 : CloseFile(FF) : ProcedureReturn "" : EndIf
	ReadData(FF, TextBuffer, TextBufferSize)
	CloseFile(FF)
	ReturnValue = PeekS(TextBuffer, TextBufferSize, StringFormat)
	FreeMemory(TextBuffer)
	
	ProcedureReturn ReturnValue
	
EndProcedure

Procedure SaveTextFile(TextFileContent.s, TextFileName.s, WriteStringFormat = #PB_Ascii)
	
	Protected FF
	FF = CreateFile(#PB_Any, TextFileName)
	If FF 
		WriteString(FF, TextFileContent, WriteStringFormat)
		CloseFile(FF)
	EndIf
	
EndProcedure

Define SourceFile.s
Define SourceFolder.s
Define PathToCopy.s
Define WebserverAddress.s
Define SpiderCompiler.s
Define JavaScriptPath.s

OpenConsole(#AppName)

SourceFile = ProgramParameter(0)

If FileSize(SourceFile) = -1
	MessageRequester(#AppName, "'" + SourceFile + "' doesn't exists")
	End
EndIf

SourceFolder = GetPathPart(SourceFile)

If Right(SourceFolder, 1) <> #Slash : SourceFolder + #Slash : EndIf

PathToCopy = ProgramParameter(1)

If Right(PathToCopy, 1) <> #Slash : PathToCopy + #Slash : EndIf

If FileSize(PathToCopy) <> -2
	MessageRequester(#AppName, "'" + PathToCopy + "' doesn't exists")
	End
EndIf

WebserverAddress = "http://localhost/" + StringField(PathToCopy, CountString(PathToCopy, #Slash), #Slash) + "/index.html" ; Adjust this for your needs

SpiderCompiler = GetEnvironmentVariable("PB_TOOL_Compiler") 

JavaScriptPath = ReplaceString(SpiderCompiler, "Compilers" + #Slash + "sbcompiler.exe", "Libraries" + #Slash + "JavaScript" + #Slash , #PB_String_NoCase)

PrintN("SourceFile: "     + SourceFile)
PrintN("SourceFolder: "   + SourceFolder)
PrintN("PathToCopy: "     + PathToCopy)
PrintN("SpiderCompiler: " + SpiderCompiler)
PrintN("JavaScriptPath: " + JavaScriptPath)

Define Debugger.s = ""

If GetEnvironmentVariable("PB_TOOL_Debugger") = "1"
	Debugger = "/DEBUGGER"
EndIf

RunProgram(Chr(34) + SpiderCompiler + Chr(34), 
           Chr(34) + SourceFile + Chr(34) + " " + Debugger + " /EXE " + Chr(34) + SourceFolder + "index.html" + Chr(34),
           SourceFolder,
           #PB_Program_Wait)

; ----

If FileSize(PathToCopy + "spiderbasic") <> -2
	
	PrintN("Copy content of '" + JavaScriptPath + "'...")
	
	PrintN("(This may take a while)")
	
	CreateDirectory(PathToCopy + "spiderbasic" + #Slash)
	CreateDirectory(PathToCopy + "spiderbasic" + #Slash + "libraries" + #Slash)
	CreateDirectory(PathToCopy + "spiderbasic" + #Slash + "libraries" + #Slash + "javascript" + #Slash)
	
	CopyDirectory(JavaScriptPath, PathToCopy + "spiderbasic" + #Slash + "libraries" + #Slash + "javascript" + #Slash, "", #PB_FileSystem_Recursive)
	
EndIf

; ----

PrintN("Copy content of '" + SourceFolder + "'...")

Define ED = ExamineDirectory(#PB_Any, SourceFolder, "*.*")  
Define FileContent.s 

If ED
	While NextDirectoryEntry(ED)
		If DirectoryEntryType(ED) = #PB_DirectoryEntry_File
			CopyFile(SourceFolder + DirectoryEntryName(ED), PathToCopy + DirectoryEntryName(ED))
			If LCase(GetExtensionPart(PathToCopy + DirectoryEntryName(ED))) = "html"
				; adjust the spiderbasic-path
				FileContent = LoadTextFile(PathToCopy + DirectoryEntryName(ED))
				FileContent = ReplaceString(FileContent, Chr(34) + "/spiderbasic/libraries/", Chr(34) + "spiderbasic/libraries/")
				SaveTextFile(FileContent, PathToCopy + DirectoryEntryName(ED))
			EndIf
		EndIf
	Wend
	FinishDirectory(ED)
EndIf

PrintN("Ready")

CloseConsole()

RunProgram(WebserverAddress)
Greetings ... Peter
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: PureBasic: SpiderBasicCopier

Post by Peter »

New Version is available. Code of the Posting above was updated.

Please note, that the first argument is now '%FILE' (and not '%PATH').

Greetings ... Peter
falsam
Posts: 280
Joined: Mon May 05, 2014 9:49 pm
Location: France
Contact:

Re: PureBasic: SpiderBasicCopier

Post by falsam »

Thanks for this tool Peter. Works fine :)

➽ Windows 11 - JDK 1.8 - SB 2.40 - Android 13
http://falsam.com

Sorry for my poor english
Post Reply