sbsv2/raptor/util/install-windows/raptorinstallerutils.nsh
changeset 0 044383f39525
child 3 e1eecf4d390d
equal deleted inserted replaced
-1:000000000000 0:044383f39525
       
     1 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 # All rights reserved.
       
     3 # This component and the accompanying materials are made available
       
     4 # under the terms of the License "Eclipse Public License v1.0"
       
     5 # which accompanies this distribution, and is available
       
     6 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 #
       
     8 # Initial Contributors:
       
     9 # Nokia Corporation - initial contribution.
       
    10 #
       
    11 # Contributors:
       
    12 #
       
    13 # Description: 
       
    14 # Raptor installer header file
       
    15 
       
    16 !include "WordFunc.nsh"
       
    17 
       
    18 # Time macros
       
    19 !macro DefineDateStamp
       
    20 	${time::GetLocalTime} $RESULT
       
    21 	${time::TimeString} "$RESULT" $0 $1 $2 $3 $4 $5
       
    22 	!define DATE_STAMP "$2-$1-$0-$3-$4-$5"
       
    23 !macroend
       
    24 
       
    25 # Env var manipulation macros
       
    26 
       
    27 # Macro to refresh the computer's environment by sending Windows the
       
    28 # WM_WININICHANGE message so that it re-reads the environment changes
       
    29 # the installer has made.
       
    30 !macro RefreshEnv
       
    31 	DetailPrint "Refreshing your computer's environment..."
       
    32 	SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" $RESULT /TIMEOUT=5000
       
    33 	DetailPrint "Done."
       
    34 !macroend
       
    35 
       
    36 # Sets ${RESULT} to value of user env var named ${VARNAME}
       
    37 !macro ReadUsrEnvVar VARNAME RESULT
       
    38 	ReadRegStr ${RESULT} HKCU "Environment" ${VARNAME}
       
    39 !macroend
       
    40 
       
    41 # Sets ${RESULT} to value of system env var named ${VARNAME}
       
    42 !macro ReadSysEnvVar VARNAME RESULT
       
    43 	ReadRegStr ${RESULT} HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" ${VARNAME}
       
    44 !macroend
       
    45 
       
    46 # Read the env var from the appropriate place
       
    47 !macro ReadEnvVar VARNAME RESULT
       
    48 	${If} $USERONLYINSTALL_STATE == ${BST_CHECKED}
       
    49 		# User env var
       
    50 		!insertmacro ReadUsrEnvVar ${VARNAME} ${RESULT}
       
    51 	${ElseIf} $ALLUSERSINSTALL_STATE == ${BST_CHECKED}
       
    52 		# System env var
       
    53 		!insertmacro ReadSysEnvVar ${VARNAME} ${RESULT}
       
    54 	${Else}
       
    55 		# Something has gone wrong!
       
    56 		MessageBox MB_OK|MB_ICONSTOP "Failed to determine installation type (Current User or All Users)."
       
    57 	${EndIf}
       
    58 !macroend
       
    59 
       
    60 # Read the user Path
       
    61 !macro ReadUsrPath OUTPUT
       
    62 	# Reset error flag
       
    63 	ClearErrors
       
    64     !insertmacro ReadUsrEnvVar "Path" ${OUTPUT}
       
    65     
       
    66 	${If} ${Errors}
       
    67 		DetailPrint "User has no Path variable."
       
    68 		StrCpy "${OUTPUT}" ""
       
    69 	${EndIf}
       
    70 !macroend
       
    71 
       
    72 # Read the user Path
       
    73 !macro ReadSysPath OUTPUT
       
    74 	# Reset error flag
       
    75 	ClearErrors
       
    76 	!insertmacro ReadSysEnvVar "Path" ${OUTPUT}
       
    77 !macroend
       
    78 
       
    79 # Read the Path (installer only).
       
    80 !macro ReadPath OUTPUT
       
    81 ${If} $USERONLYINSTALL_STATE == ${BST_CHECKED}
       
    82 	# User env var
       
    83 	!insertmacro ReadUsrPath ${OUTPUT}
       
    84 ${ElseIf} $ALLUSERSINSTALL_STATE == ${BST_CHECKED}
       
    85 	# System env var
       
    86 	!insertmacro ReadSysPath ${OUTPUT}
       
    87 ${Else}
       
    88 	# Something has gone wrong!
       
    89 	MessageBox MB_OK|MB_ICONSTOP "Failed to determine installation type (Current User or All Users)."
       
    90 ${EndIf}
       
    91 !macroend
       
    92 
       
    93 # Writes a string user environment variable to the Registry
       
    94 # DO NOT USE FOR WRITING THE PATH ENVIRONMENT VARIABLE. USE THE BELOW MARCOS!
       
    95 !macro WriteUsrEnvVar VARNAME VALUE
       
    96 	WriteRegStr HKCU "Environment" ${VARNAME} ${VALUE}
       
    97 !macroend
       
    98 
       
    99 # Writes a string system environment variable to the Registry
       
   100 # DO NOT USE FOR WRITING THE PATH ENVIRONMENT VARIABLE. USE THE BELOW MARCOS!
       
   101 !macro WriteSysEnvVar VARNAME VALUE
       
   102 	WriteRegStr HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" ${VARNAME} ${VALUE}
       
   103 !macroend
       
   104 
       
   105 # Use the following for PATH env var that can expand variables it contains, e.g.
       
   106 # Something like 
       
   107 # %SBS_HOME%;C:\Windows...
       
   108 # should be written to the registry
       
   109 # SBS_HOME must NOT be an "expandable string"; in fact expandable strings don't work recursively
       
   110 
       
   111 # Writes an expandable string user environment variable to the Registry; mostly used for PATH
       
   112 !macro WriteUsrEnvVarExp VARNAME VALUE
       
   113 	WriteRegExpandStr HKCU "Environment" ${VARNAME} ${VALUE}
       
   114 !macroend
       
   115 
       
   116 # Writes an expandable string system environment variable to the Registry; mostly used for PATH
       
   117 !macro WriteSysEnvVarExp VARNAME VALUE
       
   118 	WriteRegExpandStr HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" ${VARNAME} ${VALUE}
       
   119 !macroend
       
   120 
       
   121 # Deletes a user environment variable from the Registry
       
   122 !macro RmUsrEnvVar VARNAME
       
   123 	DeleteRegValue HKCU "Environment" ${VARNAME}
       
   124 !macroend
       
   125 
       
   126 # Deletes a system environment variable from the Registry
       
   127 !macro RmSysEnvVar VARNAME
       
   128 	DeleteRegValue HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" ${VARNAME}
       
   129 !macroend
       
   130 
       
   131 # Push env var name, value of env var, and either "" (for normal string env var) 
       
   132 # or "exp" (for expandable env var) onto stack before calling this function
       
   133 # in this order
       
   134 Function WriteEnvVar
       
   135 	pop $2 # Expandable string or not?
       
   136 	pop $1 # Env var value
       
   137 	pop $0 # Env var name
       
   138 		
       
   139 	DetailPrint "Going to write evn var $0, with value $1, expandable: $2."
       
   140 	
       
   141 	# Reset error flag
       
   142 	ClearErrors
       
   143 	
       
   144 	${If} $2 == "exp" # Expandable string env var
       
   145 		# Write the env var to the appropriate place
       
   146 		${If} $USERONLYINSTALL_STATE == ${BST_CHECKED}
       
   147 			DetailPrint "DEBUG $$0 $$1 = $0 $1"
       
   148 			# User env var
       
   149 			!insertmacro WriteUsrEnvVarExp $0 $1
       
   150 		${ElseIf} $ALLUSERSINSTALL_STATE == ${BST_CHECKED}
       
   151 			DetailPrint "DEBUG $$0 $$1 = $0 $1"
       
   152 			# System env var
       
   153 			!insertmacro WriteSysEnvVarExp $0 $1
       
   154 		${Else}
       
   155 			# Something has gone wrong!
       
   156 			MessageBox MB_OK|MB_ICONSTOP "Failed to determine installation type (Current User or All Users)."
       
   157 		${EndIf}
       
   158 	${Else} # Normal string env var
       
   159 		# Write the env var to the appropriate place
       
   160 		${If} $USERONLYINSTALL_STATE == ${BST_CHECKED}
       
   161 			DetailPrint "DEBUG $$0 $$1 = $0 $1"
       
   162 			# User env var
       
   163 			!insertmacro WriteUsrEnvVar $0 $1
       
   164 		${ElseIf} $ALLUSERSINSTALL_STATE == ${BST_CHECKED}
       
   165 			DetailPrint "DEBUG $$0 $$1 = $0 $1"
       
   166 			# System env var
       
   167 			!insertmacro WriteSysEnvVar $0 $1
       
   168 		${Else}
       
   169 			# Something has gone wrong!
       
   170 			MessageBox MB_OK|MB_ICONSTOP "Failed to determine installation type (Current User or All Users)."
       
   171 		${EndIf}
       
   172 	${EndIf}
       
   173 FunctionEnd
       
   174 
       
   175 # Prepend the PATH env var with the given string. User/system path is determined using
       
   176 # other function.
       
   177 Function PrependToPath
       
   178 	pop $0 # String to prepend to PATH
       
   179 		
       
   180 	DetailPrint "Going to prepend PATH with $0."
       
   181 	
       
   182 	# Reset error flag
       
   183 	ClearErrors
       
   184 	
       
   185 	# Read Path
       
   186 	!insertmacro ReadPath $RESULT
       
   187 	
       
   188 	${Unless} ${Errors} # If no errors
       
   189 		${REQuoteMeta} $9 $0 # $9 now contains the meta-quoted version of $0
       
   190 		${If} $RESULT !~ $9 # If Path doesn't contain string to add
       
   191 			StrLen $RESULT2 "$0;$RESULT"
       
   192 			# Warn is Path might be "too" long for the Windows registry.
       
   193 			${If} $RESULT2 > 1023
       
   194 				DetailPrint "Note: adding %SBS_HOME%\bin; to the start of your Path..."
       
   195 				DetailPrint "... will result in a string longer than 1023 characters..."
       
   196 				DetailPrint "... being written to your registry. Certain versions of Windows..."
       
   197 				DetailPrint "... cannot handle a string that long in the registry. The installer..."
       
   198 				DetailPrint "... will continue writing to the registry. However, a back up of..."
       
   199 				DetailPrint "... your full environment has been created in your installation directory ..."
       
   200 				DetailPrint "... should anything go wrong which can be used to restore your previous Path."
       
   201 			${EndIf}
       
   202 			
       
   203 			Push "Path" # Third on stack
       
   204 			Push "$0;$RESULT" # Second on stack
       
   205 			Push "exp" # First on stack
       
   206 			# Write expandable string to registry
       
   207 			call WriteEnvVar
       
   208 		${EndIf}
       
   209 	${Else}
       
   210 		DetailPrint "Error: failed to read Path environment variable."
       
   211 	${EndUnless}
       
   212 FunctionEnd
       
   213 
       
   214 # Remove the string STR from the string PATH.
       
   215 !macro RemoveFromPathString PATH STR
       
   216 	DetailPrint "Going to remove ${STR} from ${PATH}."
       
   217 	${WordReplace} "${PATH}" "${STR}" "" "+" $RESULT2
       
   218 	DetailPrint "Debug: Replaced ${STR} in RESULT2 = [$RESULT2]"
       
   219 	StrCpy ${PATH} "$RESULT2"
       
   220 	
       
   221 	${WordReplace} "${PATH}" ";;" ";" "+" $RESULT2
       
   222 	DetailPrint "Debug: Replaced ;; in RESULT2 = [$RESULT2]"
       
   223 	StrCpy ${PATH} $RESULT2
       
   224 !macroend
       
   225 
       
   226 ################### Miscellaneous utilities
       
   227 # WriteFile - writes a file with given contents
       
   228 # FILENAME - full path to file (all directories in path must exist)
       
   229 # CONTENTS - string to write to the file.
       
   230 !macro WriteFile FILENAME CONTENTS
       
   231 	DetailPrint "Creating batch file for setting Raptor's environment..."
       
   232 	ClearErrors
       
   233 	FileOpen $0 ${FILENAME} w
       
   234 	${Unless} ${Errors}
       
   235 		FileWrite $0 "${CONTENTS}"
       
   236 		FileClose $0
       
   237 		DetailPrint "Done."		
       
   238 	${Else}
       
   239 		DetailPrint "Error: failed to write RaptorEnv.bat."
       
   240 	${EndUnless}
       
   241 !macroend
       
   242 
       
   243 ################################################ End ################################################