#include-once ; #INDEX# ======================================================================================================================= ; Title .........: UVD (UDFVersionDetection) ; AutoIt Version : 3.3.18.1 ; Language ......: English ; Description ...: UDF to get UDF versions and possibly save them as variable in a script. ; (Including the running one, if it is not compiled.) ; Version .......: 1.0.0 ; =============================================================================================================================== ; #CURRENT# ===================================================================================================================== ; __UVD_SaveVersionToScript ; __UVD_VersionToString ; __UVD_FileGetVersion ; __UVD_GetVersion ; =============================================================================================================================== ; #INTERNAL_USE_ONLY# =========================================================================================================== ; __UVD__EscapeRegExp ; =============================================================================================================================== ; #GLOBAL CONSTANTS# ============================================================================================================ ; =============================================================================================================================== ; #INTERNAL_USE_ONLY GLOBAL VARIABLES # ========================================================================================= Global Const $__UVD_arEscapeCharacters = ['.','^','$','*','+','-','?','(',')','[',']','{','}','\','|'] ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name ..........: __UVD_SaveVersionToScript ; Description ...: Save the version as 'Global Const' to the top of a file. If the variable already exist, it is replaced instead. ; Syntax ........: __UVD_SaveVersionToScript($sFile, $sVariablePrefix = "__Version_UDF_", $sName = Default) ; Parameters ....: $sFile - the udf file to read the version from. ; $sVariablePrefix - [optional] Default '__Version_UDF_'. The prefix for the created variable. ; $sName - [optional] Default is the filename (without .au3). ; $sScriptPath - [optional] Default is @ScriptFullPath. ; Return values .: True on success ; Author ........: Kanashius ; Modified ......: ; Remarks .......: ; $sVariablePrefix and $sName must create a valid variable name together. ; ('$'&$sVariablePrefix&$sName must be a valid variable name). ; $sName may need to be set, if the UDF has a filename that would conflict with this. ; ; Errors: ; 1 - Parameter invalid (@extended: 1 - $sFile, 4 - $sScriptPath) ; 2 - Version could not be parsed (@extended contains the __UVD_FileGetVersion error) ; 3 - Version array string could not be created. ; 4 - $sScriptPath could not be read ; 5 - $sScriptPath could not be written ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func __UVD_SaveVersionToScript($sFile, $sVariablePrefix = "__Version_UDF_", $sName = Default, $sScriptPath = Default) If Not FileExists($sFile) Then Return SetError(1, 1, False) If $sScriptPath=Default Then $sScriptPath = @ScriptFullPath If Not FileExists($sScriptPath) Then Return SetError(1, 4, False) If $sName=Default Then Local $arName = StringRegExp($sFile, "^.*\\(.*?)\.au3$", 1) If UBound($arName)<>1 Then Return SetError(1, 0, False) $sName = $arName[0] EndIf Local $arVersion = __UVD_FileGetVersion($sFile) If Not IsArray($arVersion) Or UBound($arVersion, 0)<2 Or UBound($arVersion, 1)<2 Then Return SetError(2, @error, False) Local $sVersion = "" If $arVersion[0][0]>0 Then $sVersion&="["""&$arVersion[0][1]&"""" For $i=1 to $arVersion[0][0]-1 $sVersion &= ", " Local $sVersPart = $arVersion[0][1+$i] If StringRegExp($sVersPart, "\d+", 0) Then $sVersion &= $sVersPart Else $sVersion &= '"'&$sVersPart&'"' EndIf Next $sVersion&="]" If $arVersion[1][0]>0 Then $sVersion &= "," EndIf If $arVersion[1][0]>0 Then $sVersion&="["""&$arVersion[1][1]&"""" For $i=1 to $arVersion[1][0]-1 $sVersion &= ", " Local $sVersPart = $arVersion[1][1+$i] If StringRegExp($sVersPart, "\d+", 0) Then $sVersion &= $sVersPart Else $sVersion &= '"'&$sVersPart&'"' EndIf Next $sVersion&="]" EndIf If $sVersion = "" Then Return SetError(3, 0, False) Local $sVersionString = "Global Const $"&$sVariablePrefix&$sName&" = ["&$sVersion&"]"&@crlf Local $sScript = FileRead($sScriptPath) If @error Then Return SetError(4, 0, False) Local $arEscapeName = StringSplit($sName, "", 3) Local $sRegExp = "(?m)^(Global\s*Const\s*\$"&__UVD__EscapeRegExp($sVariablePrefix)&__UVD__EscapeRegExp($sName)&"\s*=\s*)(.*?)$" $sScript = StringRegExpReplace($sScript, $sRegExp, "$1["&$sVersion&"]") If @extended<1 Then ; add at the top $sScript = $sVersionString & $sScript EndIf Local $hFile = FileOpen($sScriptPath, 2) If @error Then Return SetError(5, 1, False) FileWrite($hFile, $sScript) If @error Then Return SetError(5, 2, False) FileClose($hFile) Return True EndFunc ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name ..........: __UVD__EscapeRegExp ; Description ...: Return the string with all regexp control characters escaped. ; Syntax ........: __UVD__EscapeRegExp($sData) ; Parameters ....: $sData - the string to escape ; Return values .: The escaped string. ; Author ........: Kanashius ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func __UVD__EscapeRegExp($sData) For $i=0 to UBound($__UVD_arEscapeCharacters)-1 Step 1 $sData = StringReplace($sData, $__UVD_arEscapeCharacters[$i], '\'&$__UVD_arEscapeCharacters[$i]) Next Return $sData EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: __UVD_VersionToString ; Description ...: Convert the version array from __UVD_GetVersion or __UVD_FileGetVersion to a readable string. ; Syntax ........: __UVD_VersionToString($arVersion, $sSep = " ") ; Parameters ....: $arVersion - the version array (see __UVD_GetVersion) ; $sSep - [optional] Default ' '. The seperator between the AutoIt-/UDF-version, if both are there ; Return values .: String with version information, or "" on failure ; Author ........: Kanashius ; Modified ......: ; Remarks .......: Errors: ; 1 - Parameter invalid (@extended: 1 - $arVersion) ; 2 - Version could not be parsed/converted ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func __UVD_VersionToString($arVersion, $sSep = " ") If Not IsArray($arVersion) Or UBound($arVersion, 0)<2 Or UBound($arVersion, 1)<2 Then Return SetError(1, 1, "") Local $sVersion = "" If $arVersion[0][0]>0 Then $sVersion &= "AutoIt Version: " $sVersion&=$arVersion[0][1]&" [" For $i=1 to $arVersion[0][0]-1 If $i>1 Then $sVersion &= ", " $sVersion &= $arVersion[0][1+$i] Next $sVersion&="]" If $arVersion[1][0]>0 Then $sVersion &= $sSep EndIf If $arVersion[1][0]>0 Then $sVersion &= "UDF Version: " $sVersion&=$arVersion[1][1]&" (" For $i=1 to $arVersion[1][0]-1 If $i>1 Then $sVersion &= ", " $sVersion &= $arVersion[1][1+$i] Next $sVersion&=")" EndIf If $sVersion = "" Then Return SetError(2, 0, "") Return $sVersion EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: __UVD_FileGetVersion ; Description ...: Get the AutoIt Version as well as the UDF Version for a script file. ; Syntax ........: __UVD_FileGetVersion($sUdfCode) ; Parameters ....: $sFile - the autoit (.au3) file to read ; Return values .: Array with version information. ; Author ........: Kanashius ; Modified ......: ; Remarks .......: See __UVD_GetVersion for more information. ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func __UVD_FileGetVersion($sFile) Local $sCode = FileRead($sFile) If @error Then Return SetError(@error, @extended, 0) Local $arVersion = __UVD_GetVersion($sCode) If @error Then Return SetError(@error, @extended, -1) Return $arVersion EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: __UVD_GetVersion ; Description ...: Get the AutoIt Version as well as the UDF Version. ; Syntax ........: __UVD_GetVersion($sUdfCode) ; Parameters ....: $sUdfCode - the sourcecode of the udf ; Return values .: Array with version information. ; Author ........: Kanashius ; Modified ......: ; Remarks .......: @extended (1 - Only AutoIt Version found, 2 - Only UDF Version found, 3 - Both found) ; Resurns a 2D-Array with: ; [0][0] being the amount of version parts found for the AutoIt Version + 1 ; [0][1] If [0][0]>0 then this is the full autoit version as string ; [0][2] The first part of the autoit version (index 2-5 is a number) ; ... ; [0][6] The last part of the autoit version (last part is a/b/rc) ; [1][0] being the amount of version parts found for the UDF Version + 1 ; [1][1] If [1][0]>0 then this is the full udf version as string ; [1][2] The first part of the udf version (index 2-5 is a number) ; ... ; [1][6] The last part of the udf version (last part is a/b/rc) ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func __UVD_GetVersion($sUdfCode) Local $iExtended = 0 Local $arAutoItVersion = StringRegExp($sUdfCode, "(?m)(?s)^;\s*#INDEX#\s*=*.*?;\s*AutoIt\s*Version\s*\.*:\s((\d+)(?:\.(\d+)(?:\.(\d+)(?:\.(\d+))?)?)?(a|b|rc)?)\s*$", 1) If Not @error Then $iExtended = 1 Local $arUDFVersion = StringRegExp($sUdfCode, "(?m)(?s)^;\s*#INDEX#\s*=*.*?;\s*Version\s*\.*:\s((\d+)(?:\.(\d+)(?:\.(\d+))?)?(a|b|rc)?)\s*$", 1) If Not @error Then $iExtended += 2 Local $iVerNumbers = UBound($arAutoItVersion) If UBound($arUDFVersion)>$iVerNumbers Then $iVerNumbers = UBound($arUDFVersion) Local $arResult[2][$iVerNumbers+1] $arResult[0][0] = UBound($arAutoItVersion) For $i=0 to UBound($arAutoItVersion)-1 Step 1 $arResult[0][$i+1] = $arAutoItVersion[$i] Next $arResult[1][0] = UBound($arUDFVersion) For $i=0 to UBound($arUDFVersion)-1 Step 1 $arResult[1][$i+1] = $arUDFVersion[$i] Next Return SetExtended($iExtended, $arResult) EndFunc