Skip to content

Commit

Permalink
Added Cmdlet to Pack Solution using solutionpackager.exe
Browse files Browse the repository at this point in the history
  • Loading branch information
WaelHamze committed Jan 28, 2019
1 parent 6302e68 commit 381c129
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"PackageType": "Both",
"IncludeVersionInName": true,
"TreamWarningsAsErrors": false,
"IncrementReleaseVersion": false,
"Version": ""
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ string logsDirectory

Logger.LogVerbose("zipFile: {0}", zipFilePath);

string log = $"{logsDirectory}\\PackagerLog_{zipFile.Replace(".zip", "")}_{DateTime.Now.ToString("yyyy_MM_dd__HH_mm")}.txt";
string log = string.Empty;

if (!string.IsNullOrEmpty(logsDirectory))
{
log = $"{logsDirectory}\\PackagerLog_{zipFile.Replace(".zip", "")}_{DateTime.Now.ToString("yyyy_MM_dd__HH_mm")}.txt";
}

Logger.LogVerbose("log: {0}", log);

Expand Down Expand Up @@ -130,11 +135,6 @@ public List<bool> PackSolutions(
throw new Exception(string.Format("Config file couldn't be found at {0}", configFilePath));
}

if (!Directory.Exists(logsDirectory))
{
throw new Exception(string.Format("logsDirectory couldn't be found at {0}", logsDirectory));
}

SolutionPackConfig config =
Serializers.ParseJson<SolutionPackConfig>(configFilePath);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using System;
using System.Collections.Generic;
using System.Management.Automation;
using Microsoft.Crm.Sdk.Messages;
using Xrm.Framework.CI.Common;

namespace Xrm.Framework.CI.PowerShell.Cmdlets
{
/// <summary>
/// <para type="synopsis">Invokes a WhoAmIRequest</para>
/// <para type="description">This cmdlet can be used to test your connectivity to CRM by calling
/// WhoAmIRequest and returning a WhoAmIResponse object.
/// </para>
/// </summary>
/// <example>
/// <code>C:\PS>Export-XrmSolution -ConnectionString "" -EntityName "account"</code>
/// <para>Exports the "" managed solution to "" location</para>
/// </example>
[Cmdlet(VerbsData.Compress, "XrmSolution")]
[OutputType(typeof(WhoAmIResponse))]
public class CompressXrmSolution : CommandBase
{
#region Parameters

/// <summary>
/// <para type="description">The absolute path to the solutionpackager.exe</para>
/// </summary>
[Parameter(Mandatory = true)]
public string SolutionPackagerPath { get; set; }

/// <para type="description">The package type used by the solution packager</para>
/// </summary>
[Parameter(Mandatory = true)]
public string PackageType { get; set; }


/// <para type="description">The folder containing the unpacked customizations</para>
/// </summary>
[Parameter(Mandatory = true)]
public string Folder { get; set; }

/// <summary>
/// <para type="description">The mapping file used by the solution packager</para>
/// </summary>
[Parameter(Mandatory = false)]
public string MappingFile { get; set; }

/// <summary>
/// <para type="description">The new version of the solution to set in solution.xml</para>
/// </summary>
[Parameter(Mandatory = false)]
public string Version { get; set; }

/// <summary>
/// <para type="description">Set to true to include the version number in the generated solution file</para>
/// </summary>
[Parameter(Mandatory = false)]
public bool IncludeVersionInName { get; set; }


/// <summary>
/// <para type="description">Set to true to update solution release version</para>
/// </summary>
[Parameter(Mandatory = false)]
public bool IncrementReleaseVersion { get; set; }

/// <summary>
/// <para type="description">Set to true to cause operation to fail if warnings are encountered</para>
/// </summary>
[Parameter(Mandatory = false)]
public bool TreatWarningsAsErrors { get; set; }

/// <summary>
/// <para type="description">The absolute path to the location of the packed solutions</para>
/// </summary>
[Parameter(Mandatory = true)]
public string OutputFolder { get; set; }

/// <summary>
/// <para type="description">The directory where the pack logs should be placed.</para>
/// </summary>
[Parameter(Mandatory = false)]
public string LogsDirectory { get; set; }

#endregion

#region Process Record

protected override void ProcessRecord()
{
base.ProcessRecord();

Logger.LogInformation("Packing Solution from Path: {0}", Folder);

SolutionPackagerManager packagerManager = new SolutionPackagerManager(Logger);

SolutionPackager_PackageType packageType;
if (!Enum.TryParse<SolutionPackager_PackageType>(PackageType, out packageType))
{
throw new Exception($"{PackageType} is not valid");
}

bool result = packagerManager.PackSolution(
SolutionPackagerPath,
OutputFolder,
Folder,
packageType,
IncludeVersionInName,
MappingFile,
TreatWarningsAsErrors,
IncrementReleaseVersion,
Version,
LogsDirectory);

if (!result)
{
throw new System.Exception("Packing Solution failed. Check logs for more information");
}

Logger.LogInformation("Packing Solution Completed");
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Xrm.Framework.CI.PowerShell.Cmdlets
/// </example>
[Cmdlet(VerbsData.Compress, "XrmSolutions")]
[OutputType(typeof(WhoAmIResponse))]
public class CompressXrmSolutionUsingConfig : CommandBase
public class CompressXrmSolutionsUsingConfig : CommandBase
{
#region Parameters

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
<Compile Include="AddXrmSolutionPatch.cs" />
<Compile Include="Common\ReflectionLoader.cs" />
<Compile Include="Common\XrmSolutionInfo.cs" />
<Compile Include="CompressXrmSolution.cs" />
<Compile Include="CopyXrmSolutionComponentsCommand.cs" />
<Compile Include="ExportXrmAccessTeamTemplatesCommand.cs" />
<Compile Include="ExportXrmSolutionsCommand.cs" />
Expand Down Expand Up @@ -142,7 +143,7 @@
<Compile Include="RemoveXrmEntityAttribute.cs" />
<Compile Include="RemoveXrmEntityCommand.cs" />
<Compile Include="RemoveXrmRecordCommand.cs" />
<Compile Include="CompressXrmSolutionUsingConfig.cs" />
<Compile Include="CompressXrmSolutionsUsingConfig.cs" />
<Compile Include="ShowXrmLogging.cs" />
<Compile Include="RemoveXrmSolutionComponentsCommand.cs" />
<Compile Include="SelectXrmWhoAmICommand.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ param(
)

$ErrorActionPreference = "Stop"
$InformationPreference = "Continue"

Write-Verbose 'Entering ExportSolutionsUsingConfig.ps1'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ param(
[bool]$UpdateVersion,
[string]$RequiredVersion,
[bool]$IncludeVersionInSolutionFile,
[bool]$IncrementReleaseVersion,
[string]$OutputPath,
[bool]$TreatPackWarningsAsErrors,
[string]$CoreToolsPath
[string]$CoreToolsPath,
[string]$LogsDirectory
)

$ErrorActionPreference = "Stop"
$InformationPreference = "Continue"

Write-Verbose 'Entering PackSolution.ps1' -Verbose

Expand All @@ -24,9 +27,11 @@ Write-Verbose "PackageType = $PackageType"
Write-Verbose "UpdateVersion = $UpdateVersion"
Write-Verbose "RequiredVersion = $RequiredVersion"
Write-Verbose "IncludeVersionInSolutionFile = $IncludeVersionInSolutionFile"
Write-Verbose "IncrementReleaseVersion = $IncrementReleaseVersion"
Write-Verbose "OutputPath = $OutputPath"
Write-Verbose "TreatPackWarningsAsErrors = $TreatPackWarningsAsErrors"
Write-Verbose "CoreToolsPath = $CoreToolsPath"
Write-Verbose "LogsDirectory = $LogsDirectory"

#Script Location
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
Expand All @@ -38,84 +43,35 @@ Write-Verbose "Importing CIToolkit: $xrmCIToolkit"
Import-Module $xrmCIToolkit
Write-Verbose "Imported CIToolkit"

if ($UpdateVersion)
{
Write-Verbose "Setting Solution Version in File to: $RequiredVersion"

$SolutionXmlFile = "$UnpackedFilesFolder\Other\Solution.xml"

Write-Verbose "Setting $SolutionXmlFile to IsReadyOnly = false"

Set-ItemProperty $SolutionXmlFile -name IsReadOnly -value $false

Write-Verbose "Setting Solution Version in File to: $RequiredVersion"

Set-XrmSolutionVersionInFolder -SolutionFilesFolderPath $UnpackedFilesFolder -Version $RequiredVersion

Write-Host "$SolutionXmlFile updated with $RequiredVersion"
}

$solutionInfo = Get-XrmSolutionInfoFromFolder -SolutionFilesFolderPath $UnpackedFilesFolder
$packSolutionName = $solutionInfo.UniqueName
$packSolutionVersion = $solutionInfo.Version

Write-Host "Packing Solution = " $packSolutionName ", Version = " $packSolutionVersion

$packStringBuilder = $packSolutionName
if ($IncludeVersionInSolutionFile)
{
$packStringBuilder = $packStringBuilder + "_" + $packSolutionVersion.replace(".", "_")
}
$packManagedFile = $packStringBuilder + "_managed.zip"
$packUnmanagedFile = $packStringBuilder + ".zip"

if ($PackageType -eq 'Managed')
{
$targetFile = $OutputPath + "\" + $packManagedFile
}
else
{
$targetFile = $OutputPath + "\" + $packUnmanagedFile
}

$SolutionPackagerFile = $scriptPath + "\SolutionPackager.exe"
if ($CoreToolsPath)
{
$SolutionPackagerFile = $CoreToolsPath + "\SolutionPackager.exe"
}

if ($MappingFile)
{
$packOutput = & "$SolutionPackagerFile" /action:Pack /zipfile:"$targetFile" /folder:"$UnpackedFilesFolder" /packagetype:$PackageType /map:"$MappingFile"
$PackParams = @{
SolutionPackagerPath = $SolutionPackagerFile
PackageType = $PackageType
Folder = $UnpackedFilesFolder
IncludeVersionInName = $IncludeVersionInSolutionFile
IncrementReleaseVersion = $IncrementReleaseVersion
TreatWarningsAsErrors = $TreatPackWarningsAsErrors
OutputFolder = $OutputPath
}
else

if ($MappingFile)
{
$packOutput = & "$SolutionPackagerFile" /action:Pack /zipfile:"$targetFile" /folder:"$UnpackedFilesFolder" /packagetype:$PackageType
$PackParams.MappingFile = $MappingFile
}

Write-Output $packOutput

if ($lastexitcode -ne 0)
if ($LogsDirectory)
{
throw "Solution Pack operation failed with exit code: $lastexitcode"
$PackParams.LogsDirectory = $LogsDirectory
}
else
if ($RequiredVersion)
{
if (($packOutput -ne $null) -and ($packOutput -like "*warnings encountered*"))
{
if ($TreatPackWarningsAsErrors)
{
throw "Solution Packager encountered warnings. Check the output."
}
else
{
Write-Warning "Solution Packager encountered warnings. Check the output."
}
}
else
{
Write-Host "Solution Pack Completed Successfully"
}
$PackParams.Version = $RequiredVersion
}

Compress-XrmSolution @PackParams

Write-Verbose 'Leaving PackSolution.ps1'
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#
# UpdateSolutionVersionInFolder.ps1
#

param(
[string]$unpackedFilesFolder,
[string]$VersionNumber
)

$ErrorActionPreference = "Stop"

Write-Verbose 'Entering UpdateSolutionVersionInFolder.ps1'

#Parameters
Write-Verbose "unpackedFilesFolder = $unpackedFilesFolder"
Write-Verbose "VersionNumber = $VersionNumber"

#Script Location
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
Write-Verbose "Script Path: $scriptPath"

#Load XrmCIFramework
$xrmCIToolkit = $scriptPath + "\Xrm.Framework.CI.PowerShell.Cmdlets.dll"
Write-Verbose "Importing CIToolkit: $xrmCIToolkit"
Import-Module $xrmCIToolkit
Write-Verbose "Imported CIToolkit"

Write-Verbose "Setting Solution Version in File to: $VersionNumber"

$SolutionXmlFile = "$UnpackedFilesFolder\Other\Solution.xml"

Write-Verbose "Setting $SolutionXmlFile to IsReadyOnly = false"

Set-ItemProperty $SolutionXmlFile -name IsReadOnly -value $false

Set-XrmSolutionVersionInFolder -SolutionFilesFolderPath $UnpackedFilesFolder -Version $VersionNumber

Write-Host "$SolutionXmlFile updated with $VersionNumber"

Write-Verbose 'Leaving UpdateSolutionVersionInFolder.ps1'
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<Compile Include="UpdateConfigurationRecords.ps1" />
<Compile Include="UpdateSecureConfiguration.ps1" />
<Compile Include="UpdateSolutionDescriptionInCRM.ps1" />
<Compile Include="UpdateSolutionVersionInFolder.ps1" />
<Compile Include="UpdateSolutionVersionInCRM.ps1" />
<Compile Include="UpdateWebResource.ps1" />
<Compile Include="UpdateWebResourcesJsonMapping.ps1" />
Expand Down

0 comments on commit 381c129

Please sign in to comment.