Two installations in one installer: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Updated author links.)
m (Added category links.)
Line 230: Line 230:


-Stu
-Stu
[[{{ns:14}}:Code Examples]]

Revision as of 22:19, 30 April 2005

Author: Afrow UK (talk, contrib)


Description

This script shows how we can install two programs with one installer, where each program has its own Directory and InstFiles page. Obviously we still have the single Components page though, as if we chose to have two then we'd might as well make two bloody installer exe's!

The Script

Here's the code. You can compile this script straight off. You should put some files in the Sections first though, otherwise you can't really see it 'working'...

Name "Multiple InstFiles"
OutFile "multi-install.exe"
 
!include MUI.nsh
!include Sections.nsh
 
##===========================================================================
## Modern UI Pages
##===========================================================================
 
!insertmacro MUI_PAGE_WELCOME
 
!define MUI_PAGE_CUSTOMFUNCTION_PRE SelectFilesBCheck
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE ComponentsLeave
!insertmacro MUI_PAGE_COMPONENTS
 
## This is the title on the first Directory page
!define MUI_DIRECTORYPAGE_TEXT_TOP "Setup will install Program #1 in the \
following folder..."
 
!define MUI_PAGE_CUSTOMFUNCTION_PRE SelectFilesA
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
 
## This is the title on the second Directory page
!define MUI_DIRECTORYPAGE_TEXT_TOP "Setup will install Program #2 in the \
following folder..."
 
!define MUI_PAGE_CUSTOMFUNCTION_PRE SelectFilesB
!insertmacro MUI_PAGE_DIRECTORY
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE DeleteSectionsINI
!insertmacro MUI_PAGE_INSTFILES
 
!insertmacro MUI_PAGE_FINISH
 
!insertmacro MUI_LANGUAGE "English"
 
##===========================================================================
## Install dir's
##===========================================================================
 
!define PROG1_InstDir "C:\PROG1"
!define PROG2_InstDir "C:\PROG2"
 
##===========================================================================
## Start sections
##===========================================================================
 
## Sections Group 1
SectionGroup "Program #1" PROG1
 
Section "Main" SEC1
 SectionIn RO
 ## Main files to install here
 
messagebox mb_ok sec1
 
SectionEnd
 
Section "Other" SEC2
 ## Other files to install here
 
messagebox mb_ok sec2
 
SectionEnd
 
SectionGroupEnd
 
## Sections Group 2
SectionGroup "Program #2" PROG2
 
Section "Main" SEC3
 ## Main files to install here
 
messagebox mb_ok sec3
 
SectionEnd
 
Section "Other" SEC4
 ## Other files to install here
 
messagebox mb_ok sec4
 
SectionEnd
 
## This must be the last section
Section -dummy SEC_LAST
SectionEnd
 
SectionGroupEnd
 
 
##===========================================================================
## Please don't modify below here unless you're a NSIS 'wiz-kid'
##===========================================================================
 
## Create $PLUGINSDIR
Function .onInit
 InitPluginsDir
FunctionEnd
 
## If user goes back to this page from 1st Directory page
## we need to put the sections back to how they were before
Var IfBack
Var UseProg2
Function SelectFilesBCheck
 StrCmp $IfBack 1 0 NoCheck
  Call ResetFilesB
 NoCheck:
 
FunctionEnd
 
## Check if PROG2 needs to be installed!
Function ComponentsLeave
Push $R0
 SectionGetFlags ${PROG2} $R0
 IntOp $UseProg2 $R0 & ${SF_SELECTED}
Pop $R0
FunctionEnd
 
## Here we are selecting first sections to install
## by unselecting all the others!
Function SelectFilesA
 
 # If user clicks Back now, we will know to reselect Group 2's sections for
 # Components page
 StrCpy $IfBack 1
 
 # We need to save the state of the Group 2 Sections
 # for the next InstFiles page
 Push $R0
 Push $R1
 
 StrCpy $R0 ${PROG2} # Group 2 start
 
  Loop:
   IntOp $R0 $R0 + 1
   SectionGetFlags $R0 $R1				    # Get section flags
    WriteINIStr "$PLUGINSDIR\sections.ini" Sections $R0 $R1 # Save state
    !insertmacro UnselectSection $R0			    # Then unselect it
    StrCmp $R0 ${Sec_LAST} 0 Loop
 
 # Set current $INSTDIR to PROG1_InstDir define
 StrCpy $INSTDIR "${PROG1_InstDir}"
 
 Pop $R1
 Pop $R0
 
FunctionEnd
 
## Here we need to unselect all Group 1 sections
## and then re-select those in Group 2 (that the user had selected on
## Components page)
Function SelectFilesB
 
 Push $R0
 Push $R1
 
 StrCpy $R0 1        # Group 1 start
 StrCpy $R1 ${PROG2} # Group 1 finish
 
  Loop1:
   !insertmacro UnselectSection $R0			  # Unselect section
   StrCmp $R0 $R1 Done1
    IntOp $R0 $R0 + 1
    Goto Loop1						  # Go to next section(s)
  Done1:
 
 Call ResetFilesB
 
 # Don't install prog 2?
 StrCmp $UseProg2 1 +2
  Abort
 
 # Set current $INSTDIR to PROG2_InstDir define
 StrCpy $INSTDIR "${PROG2_InstDir}"
 
 Pop $R1
 Pop $R0
 
FunctionEnd
 
## This will set all sections to how they were on the components page
## originally
Function ResetFilesB
 
 Push $R0
 Push $R1
 
 StrCpy $R0 ${PROG2}    # Group 2 starts at SEC3!
 
  Loop:
   IntOp $R0 $R0 + 1
   ReadINIStr "$R1" "$PLUGINSDIR\sections.ini" Sections $R0 # Get sec flags
    SectionSetFlags $R0 $R1				  # Re-set flags for this sec
    StrCmp $R0 ${Sec_LAST} 0 Loop
 
 Pop $R1
 Pop $R0
FunctionEnd
 
## Here we are deleting the temp INI file at the end of installation
Function DeleteSectionsINI
 Delete "$PLUGINSDIR\Sections.ini"
 FlushINI "$PLUGINSDIR\Sections.ini"
FunctionEnd
 
Function .onSelChange
Push $R0
 SectionGetFlags ${SEC4} $R0
 IntOp $R0 $R0 & ${SF_SELECTED}
 StrCmp $R0 ${SF_SELECTED} 0 NoSelectMain
  !insertmacro SelectSection ${SEC3}
 NoSelectMain:
Pop $R0
FunctionEnd

If you need some help, feel free to PM me (Afrow UK) via the NSIS forums.

-Stu