Home
Manage Your Code
Snippet: SharedBuildFunctions.ps1 (PowerShell)
Title: SharedBuildFunctions.ps1 Language: PowerShell
Description: Re-usable functions for iterative dev and deployment. Views: 976
Author: Tony Bierman Date Added: 5/20/2008
Copy Code  
# ShareBuildFunctions.ps1
# Re-usable functions for iterative dev and deployment
# Copyright SharePoint Solutions 2008

#
# Functions
#
function Try {
    param
   	(
        [ScriptBlock]$Command = $(throw "The parameter -Command is required."),
        [ScriptBlock]$Catch   = { throw $_ },
        [ScriptBlock]$Finally = {}
    )
	
    & {
        $local:ErrorActionPreference = "SilentlyContinue"      
        trap {
            trap {
                & {
                    trap { throw $_ }
                    &$Finally
                }          
                throw $_
            }
            $_ | & { &$Catch }
        }  
        &$Command
    }
    & {
        trap { throw $_ }
        &$Finally
    }
}

function SolutionAdd {
	param ( [string]$FileName)
	
	Write-Output "`n------ Adding solution ${FileName} ------`n"
	&stsadm.exe -o addsolution -filename "${FileName}"
}

function SolutionDelete {
	param ( [string]$Name)
	
	Write-Output "`n------ Deleting solution ${Name} ------`n"
	&stsadm.exe -o deletesolution -name "${Name}"
}

function SolutionUpgrade {
	param ( [string]$FileName, [string]$Name)
	
	Write-Output "`n------ Upgrading solution ${FileName} ------`n"
	&stsadm.exe -o upgradesolution -filename "${FileName}" -name "${Name}" -allowgacdeployment
}


function SolutionDeploy {
	param ( [string]$Name, [string]$Url )
	
	Write-Output "`n------ Deploying solution ${Name} at ${Url} ------`n"
	&stsadm.exe -o deploysolution -name "${Name}" -local -url ${Url} -allowgacdeployment
}

function SolutionRetract {
	param ( [string]$Name, [string]$Url )
	
	Write-Output "`n------ Retracting solution ${Name} at ${Url} ------`n"
	&stsadm.exe -o retractsolution -name "${Name}" -local -url ${Url}
}

function FeatureActivateByName {
	param ( [string]$Name, [string]$Url )
	
	Write-Output "`n------ Activating feature ${Name} at ${Url} ------`n"
	&stsadm.exe -o activatefeature -name "${Name}" -url ${Url}
}

function FeatureDeactivateByName {
	param ( [string]$Name, [string]$Url )
	
	Write-Output "`n------ Deactivating feature ${Name} at ${Url} ------`n"
	&stsadm.exe -o deactivatefeature -name "${Name}" -url ${Url}
}

function FeatureInstallByName {
	param ( [string]$Name )
	
	Write-Output "`n------ Installing feature ${Name} ------`n"
	&stsadm.exe -o installfeature -name "${Name}"
}

function FeatureUninstallByName {
	param ( [string]$Name )
	
	Write-Output "`n------ Uninstalling feature ${Name} ------`n"
	&stsadm.exe -o uninstallfeature -name "${Name}"
}

function DeployArtifactsToTwelveHive {
    param ( [string]$ProjectFolder )
	
	Write-Output "`n------ Copying artifacts to 12 hive ------`n"
	&xcopy /Y /s /i $ProjectFolder\CONTROLTEMPLATES\* ${TwelveHive}\TEMPLATE\CONTROLTEMPLATES
	&xcopy /Y /s /i $ProjectFolder\LAYOUTS\* ${TwelveHive}\TEMPLATE\LAYOUTS
	&xcopy /Y /s /i $ProjectFolder\FEATURES\* ${TwelveHive}\TEMPLATE\FEATURES
	&xcopy /Y /s /i $ProjectFolder\IMAGES\* ${TwelveHive}\TEMPLATE\IMAGES
	&xcopy /Y /s /i $ProjectFolder\SiteTemplates\* ${TwelveHive}\TEMPLATE\SiteTemplates
}

function DeployResourceToTwelveHive {
    param ( [string]$ResourceFolder, [string]$ResourceFname )
	
	Write-Output "`n------ Copying resource ${ResourceFname} to twelve hive ------`n"
	Remove-Item -Force "${TwelveHive}\Resources\${ResourceFname}"
	Copy-Item "${ResourceFolder}\${ResourceFname}" "${TwelveHive}\Resources"
}

function DeployConfigResourceToTestWebAndTwelveHive {
    param ( [string]$ResourceFolder, [string]$ResourceFname, [string]$WebRoot )
	
	Write-Output "`n------ Copying resource ${ResourceFname} to test web and twelve hive ------`n"
	Remove-Item -Force "${WebRoot}\App_GlobalResources\${ResourceFname}"
	Copy-Item -Force "${ResourceFolder}\${ResourceFname}" "${WebRoot}\App_GlobalResources"
	
	Remove-Item -Force "${TwelveHive}\CONFIG\Resources\${ResourceFname}"
	Copy-Item "${ResourceFolder}\${ResourceFname}" "${TwelveHive}\CONFIG\Resources"
}

function RecycleApplicationPool {
	param ( [string]$AppPool = "DefaultAppPool" )

	Write-Output "`n------ Recycling application pool ------`n"
	&"${env:SystemRoot}\system32\inetsrv\AppCmd.exe" recycle apppool /apppool.name:"${AppPool}"
}

function DeployAssemblyToGac {
	param ( [string]$SourceFolder, [string]$Target, [string]$Extension = "dll" )
	
	Write-Output "`n------ Deploying ${Target}.${Extension} to GAC ------`n"
	&gacutil -u ${Target}
	&gacutil -i ${SourceFolder}\${Target}.$Extension
}

function BuildSolution {
	param ( [string]$Config, [string]$SolutionPath )
	
	Write-Output "`n------ Build started: Solution: ${SolutionPath}, Configuration: ${Config} ------`n"
	&devenv /Build $Config $SolutionPath
}

function BuildWsp {
	param ( [string]$WorkingFolder, [string]$Target, [string]$OutDir, [string]$Diamond = "package.ddf" )
	
	Write-Output "`n------ Building WSP ${Target}.wsp output to ${OutDir} ------`n"
	$Temp = $pwd
	cd $WorkingFolder
	&makecab /F "${WorkingFolder}\${Diamond}" /L "${OutDir}" /D CabinetNameTemplate=${Target}.wsp /D DiskDirectory1="${OutDir}"
	cd $Temp
}