Skip to main content
Bounty Ended with 50 reputation awarded by Kasper
added 2415 characters in body
Source Link
Szymon Kuzniak
  • 2.7k
  • 17
  • 30

A good starting point would be this piece of code:

# Let's define layers here
$layers = @( "Foundation", "Feature", "Project" )
# These folders we want to copy to our target
$foldersToCopy = @( "Views", "bin", "App_Config" )

# This is the default task which will run if no tasks are specified
task default -depends CreateReleasePackage

# This is the main task of the scrip - it creates release package
# It relies on Clear to make sure all the garbage from previous run is out
task CreateReleasePackage -Depends Clear,Prepare,BuildSolution {
    $layers |% {
        Process-Layer $_
    }
}

# Cleanup task to remove output of the previous run
task Clear {
    if (Test-Path "../target") {
        Remove-Item "../target" -recurse
    }
}

# Prepare the folders to simplify copying code
task Prepare {
    New-Item -type Directory "../target"
    $foldersToCopy |% {
        New-Item -type Directory "../target/$_"
    }
}

# If the solution is not built there is a lot of stuff missing
task BuildSolution {
    Exec { msbuild "../Habitat.sln" }
}

# Function to process Layer, ie. iterate over it's directories
function Process-Layer {
    param( [string]$layer )
    Write-Host "Creating release package for layer: $layer"
    Get-ChildItem ../src/$layer |% {
        Process-Module $layer $_.Name
    }
    
}

# This function does all the job - it collects the interesting data from the project and copy it to common folder
function Process-Module {
    param( [string]$layer, [string]$moduleName )

    $foldersToCopy |% {
        $folderName = $_
        $folder = "../src/$layer/$moduleName/code/$folderName"
        # Better check if the folder exists, some modules might not have views or configs
        if (Test-Path $folder) {
            Write-Host "Copying $folder to ../target/$folderName"

            copy-item "$folder/*" "../target/$folderName" -force -recurse -Exclude "Debug"

        }
    }
}

Now all you have to do is to call a following script from a folder where you have your psake directory:

Import-Module ./psake/psake.psm1

Invoke-Psake ./build.ps1

Note that this is build agains Habitat project and it might be slightly different depending on how your solution is organized


And since you already mentioned powershell it is worth mentioning, that using Sitecore Powershell Extensions you can also automate creation of a Sitecore packages.

# Create package
   $package = new-package "Sitecore PowerShell Extensions";

# Set package metadata
   $package.Sources.Clear();

   $package.Metadata.Author = "John Doe";
   $package.Metadata.Publisher = "ACME Ltd";
   $package.Metadata.Version = "1.0";
   $package.Metadata.Readme = 'Information about your package'

# Add contnet/home to the package
    $source = Get-Item 'master:\content\home' | New-ItemSource -Name 'Home Page' -InstallMode Overwrite
    $package.Sources.Add($source);

# Save package
   Export-Package -Project $package -Path "$($package.Name)-$($package.Metadata.Version).zip" -Zip

# Offer the user to download the package
   Download-File "$SitecorePackageFolder\$($package.Name)-$($package.Metadata.Version).zip" 
# Create package
   $package = new-package "Sitecore PowerShell Extensions";

# Set package metadata
   $package.Sources.Clear();

   $package.Metadata.Author = "John Doe";
   $package.Metadata.Publisher = "ACME Ltd";
   $package.Metadata.Version = "1.0";
   $package.Metadata.Readme = 'Information about your package'

# Add contnet/home to the package
    $source = Get-Item 'master:\content\home' | New-ItemSource -Name 'Home Page' -InstallMode Overwrite
    $package.Sources.Add($source);

# Save package
   Export-Package -Project $package -Path "$($package.Name)-$($package.Metadata.Version).zip" -Zip

# Offer the user to download the package
   Download-File "$SitecorePackageFolder\$($package.Name)-$($package.Metadata.Version).zip" 

And since you already mentioned powershell it is worth mentioning, that using Sitecore Powershell Extensions you can also automate creation of a Sitecore packages.

# Create package
   $package = new-package "Sitecore PowerShell Extensions";

# Set package metadata
   $package.Sources.Clear();

   $package.Metadata.Author = "John Doe";
   $package.Metadata.Publisher = "ACME Ltd";
   $package.Metadata.Version = "1.0";
   $package.Metadata.Readme = 'Information about your package'

# Add contnet/home to the package
    $source = Get-Item 'master:\content\home' | New-ItemSource -Name 'Home Page' -InstallMode Overwrite
    $package.Sources.Add($source);

# Save package
   Export-Package -Project $package -Path "$($package.Name)-$($package.Metadata.Version).zip" -Zip

# Offer the user to download the package
   Download-File "$SitecorePackageFolder\$($package.Name)-$($package.Metadata.Version).zip" 

A good starting point would be this piece of code:

# Let's define layers here
$layers = @( "Foundation", "Feature", "Project" )
# These folders we want to copy to our target
$foldersToCopy = @( "Views", "bin", "App_Config" )

# This is the default task which will run if no tasks are specified
task default -depends CreateReleasePackage

# This is the main task of the scrip - it creates release package
# It relies on Clear to make sure all the garbage from previous run is out
task CreateReleasePackage -Depends Clear,Prepare,BuildSolution {
    $layers |% {
        Process-Layer $_
    }
}

# Cleanup task to remove output of the previous run
task Clear {
    if (Test-Path "../target") {
        Remove-Item "../target" -recurse
    }
}

# Prepare the folders to simplify copying code
task Prepare {
    New-Item -type Directory "../target"
    $foldersToCopy |% {
        New-Item -type Directory "../target/$_"
    }
}

# If the solution is not built there is a lot of stuff missing
task BuildSolution {
    Exec { msbuild "../Habitat.sln" }
}

# Function to process Layer, ie. iterate over it's directories
function Process-Layer {
    param( [string]$layer )
    Write-Host "Creating release package for layer: $layer"
    Get-ChildItem ../src/$layer |% {
        Process-Module $layer $_.Name
    }
    
}

# This function does all the job - it collects the interesting data from the project and copy it to common folder
function Process-Module {
    param( [string]$layer, [string]$moduleName )

    $foldersToCopy |% {
        $folderName = $_
        $folder = "../src/$layer/$moduleName/code/$folderName"
        # Better check if the folder exists, some modules might not have views or configs
        if (Test-Path $folder) {
            Write-Host "Copying $folder to ../target/$folderName"

            copy-item "$folder/*" "../target/$folderName" -force -recurse -Exclude "Debug"

        }
    }
}

Now all you have to do is to call a following script from a folder where you have your psake directory:

Import-Module ./psake/psake.psm1

Invoke-Psake ./build.ps1

Note that this is build agains Habitat project and it might be slightly different depending on how your solution is organized


And since you already mentioned powershell it is worth mentioning, that using Sitecore Powershell Extensions you can also automate creation of a Sitecore packages.

# Create package
   $package = new-package "Sitecore PowerShell Extensions";

# Set package metadata
   $package.Sources.Clear();

   $package.Metadata.Author = "John Doe";
   $package.Metadata.Publisher = "ACME Ltd";
   $package.Metadata.Version = "1.0";
   $package.Metadata.Readme = 'Information about your package'

# Add contnet/home to the package
    $source = Get-Item 'master:\content\home' | New-ItemSource -Name 'Home Page' -InstallMode Overwrite
    $package.Sources.Add($source);

# Save package
   Export-Package -Project $package -Path "$($package.Name)-$($package.Metadata.Version).zip" -Zip

# Offer the user to download the package
   Download-File "$SitecorePackageFolder\$($package.Name)-$($package.Metadata.Version).zip" 
Source Link
Szymon Kuzniak
  • 2.7k
  • 17
  • 30

You might want to consider psake. It is a task based build system written in Powershell.

And since you already mentioned powershell it is worth mentioning, that using Sitecore Powershell Extensions you can also automate creation of a Sitecore packages.

You can do this using create-package cmdlet

# Create package
   $package = new-package "Sitecore PowerShell Extensions";

# Set package metadata
   $package.Sources.Clear();

   $package.Metadata.Author = "John Doe";
   $package.Metadata.Publisher = "ACME Ltd";
   $package.Metadata.Version = "1.0";
   $package.Metadata.Readme = 'Information about your package'

# Add contnet/home to the package
    $source = Get-Item 'master:\content\home' | New-ItemSource -Name 'Home Page' -InstallMode Overwrite
    $package.Sources.Add($source);

# Save package
   Export-Package -Project $package -Path "$($package.Name)-$($package.Metadata.Version).zip" -Zip

# Offer the user to download the package
   Download-File "$SitecorePackageFolder\$($package.Name)-$($package.Metadata.Version).zip"