using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using SourceCode.ProjectSystem;
using SourceCode.Workflow.Authoring;
using SourceCode.Framework.Deployment;
using System.IO;
using SourceCode.EnvironmentSettings.Client;
namespace CreateK2DeploymentPackage
{
public class DeployPackageCreator : Task
{
private string folderName;
private string kprxLocation;
private string outputFolder;
private string selectedEnvironment;
private string k2ConnectionString;
public string K2ConnectionString
{
get { return k2ConnectionString; }
set { k2ConnectionString = value; }
}
public string SelectedEnvironment
{
get { return selectedEnvironment; }
set { selectedEnvironment = value; }
}
public string OutputFolder
{
get { return outputFolder; }
set { outputFolder = value; }
}
public string KPRXLocation
{
get { return kprxLocation; }
set { kprxLocation = value; }
}
public string FolderName
{
get { return folderName; }
set { folderName = value; }
}
public override bool Execute()
{
try
{
this.SavePackage(this.FolderName, this.KPRXLocation, this.OutputFolder);
}
catch (Exception ex)
{
//Log.LogError(ex.Message);
throw ex;
return false;
}
return true;
}
private void SavePackage(string folderName, string kprxLocation, string outputFolder)
{
//Create a project to use for deployment, the project is the folder/solution
string tmpPath = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetTempFileName()));
Project project = new Project(folderName, tmpPath);
//Add the process as a projectfile.
Process processToDeploy = Process.Load(kprxLocation);
// Log load problems.
foreach (SourceCode.Framework.ObjectLoadException loadExceptions in processToDeploy.LoadErrors)
{
//Log.LogWarning("Load exception: {0}", loadExceptions.Message);
}
// Add process to the project.
AddProcess(project, processToDeploy);
//Do a test-compile
if (!TestCompile(project))
{
throw new Exception("First compile test; Project did not compile.");
}
// create a deployment package
DeploymentPackage package = project.CreateDeploymentPackage();
// Add environment stuff
AddEnvironment(package, this.SelectedEnvironment);
//Set other connections. The K2 deployment package uses a reference. This does the same but does not use the reference!
package.SmartObjectConnectionString = this.K2ConnectionString;
package.WorkflowManagementConnectionString = this.K2ConnectionString;
//Do a test-compile
if (!TestCompile(project))
{
throw new Exception("Second compile test; Project did not compile.");
}
//Finaly, save the deployment package
package.Save(outputFolder, folderName);
}
///
///
///
///
///
///TODO: Method needs expanding to output the errors (etc..)
private bool TestCompile(Project project)
{
project.Environment.AuthoringMode = SourceCode.Framework.AuthoringMode.CodeOnly;
Log.LogMessage("Project.environment: {0}", project.Environment.Name);
DeploymentResults compileResult = project.Compile();
if (!compileResult.Successful)
{
foreach (System.CodeDom.Compiler.CompilerError error in compileResult.Errors)
{
string errString = string.Format("Error compiling: {0} - {1}", error.ErrorNumber, error.ErrorText);
//Log.LogWarning(errString);
Console.WriteLine(errString);
}
}
return compileResult.Successful;
}
private void AddProcess(Project project, Process process)
{
// Create the ProjectFile
ProjectFile file = (ProjectFile)project.CreateNewFile();
// Set information on the file.
file.FileName = process.FileName;
file.Content = process;
// Add the file to the project
project.Files.Add(file);
// Save the project to the temp location.
project.SaveAll();
}
private void AddEnvironment(DeploymentPackage package, string SelectedEnvironment)
{
// Since there's no documentation on connecting to the environment server. This seems to work....
EnvironmentSettingsManager envManager = new EnvironmentSettingsManager(true);
envManager.ConnectToServer(this.K2ConnectionString);
envManager.InitializeSettingsManager(true);
envManager.Refresh();
// Add environments + environment properties.
foreach (EnvironmentTemplate envTemp in envManager.EnvironmentTemplates)
{
foreach (EnvironmentInstance envInst in envTemp.Environments)
{
//Add an environment to the package.
DeploymentEnvironment env = package.AddEnvironment(envInst.EnvironmentName);
foreach (EnvironmentField field in envInst.EnvironmentFields)
{
env.Properties[field.FieldName] = field.Value;
}
// Make sure the environment we select actually exists.
if (envInst.EnvironmentName == SelectedEnvironment)
{
package.SelectedEnvironment = envInst.EnvironmentName;
}
}
}
//Cleanup
envManager.Disconnect();
envManager = null;
//Final check of the selected environment
if (package.SelectedEnvironment == null || package.SelectedEnvironment == string.Empty)
{
throw new Exception(string.Format("Could not find the environment you wanted to select ({0})", SelectedEnvironment));
}
}
}
}