Tool for auto-updating your external references in K2Studio

  • 19 July 2007
  • 1 reply
  • 30 views

Badge +3

Our projects use external references/dlls liberally.  We find it much easier to manage and develop the bulk of the code in Visual Studio.  One of the consequences of taking this approach is that whenever your external DLLs' are changed, you need to manually go into k2 studio and update each reference to get the workflow to use the latest codebase.



This tool basically uses the K2Studio Automation library from K2 and uses it to auto-update the external references.

 

The syntax for using this tool would be:

K2DeploymentUtil
/ksnpath solutionpath
/k2server serverName
/port portnumber
/kpj k2projectname
/kpr k2processname
/binpath binpath
[/savechanges]

For example:

For Example: /ksnpath "C:myProjectK2ServicesProcess.ksn" /k2server localhost /port 5252 /kpj CommonProcesses /kpr "Bidding Process" /binpath "c:k2Assembly" /savechanges



using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.IO;

namespace K2DeploymentUtil
{
    /// <summary>
    /// Automates the build and deployment of a K2 Process.
    ///
    /// Also automates configuring the external references to the K2 Process.
    /// </summary>
    class Program
    {
        static bool IsExternalRef(System.IO.FileInfo[] dllFileNames, string k2RefName)
        {
            foreach (System.IO.FileInfo fInfo in dllFileNames)
            {
                string dllName = fInfo.Name;
                if (dllName == k2RefName)
                    return true;
            }
            return false;
        }

        static StringDictionary ParseArgs(string[] args)
        {
            StringDictionary sd = new StringDictionary();
            for (int i = 0; i < args.Length; i++)
            {
                string sArg = argsIdea;
                if (sArg.StartsWith("/") || sArg.StartsWith("-"))
                {
                    if(sArg.Length < 2)
                    {
                        Console.WriteLine("Invalid argument {0}", sArg);
                        return null;
                    }

                    //add arg key to stringdictionary
                    string key = sArg.Substring(1).ToLower();
                    sd.Add(key, string.Empty);

                    if (i + 1 < args.Length)
                    {
                        //there may be a value for the arg key, check for it
                        string nextArg = args[i + 1];
                        if (nextArg.StartsWith("/") || nextArg.StartsWith("-"))
                        {
                            continue;
                        }
                        else
                        {
                            sd[key] = nextArg.ToLower();
                        }
                    }
                }
            }
            return sd;
        }



        static int Main(string[] args)
        {
            //Default instructions when no args present
            if (args.Length == 0 || args[0] == "/?" || args[0] == "-?" || args[0] == "?" || args[0] == "help")
            {
                Console.WriteLine("Sets the updated external reference dlls and deploys a K2 Workflow project to a server");
                Console.WriteLine("
K2DeploymentUtil /ksnpath solutionpath /k2server serverName /port portnumber /kpj k2projectname /kpr k2processname /binpath binpath [/savechanges]

");

                Console.WriteLine("For Example: /ksnpath "C:projectK2WellServicesProcess.ksn"" /k2server localhost /port 5252 /kpj CommonProcesses /kpr "Cementing Bidding Process" /binpath "c:k2Assembly"" /savechanges");
                return 1;
            }

            ArrayList switchesList = new ArrayList(new string[] { "ksnpath", "k2server", "port", "kpj", "kpr", "binpath", "savechanges" });


            StringDictionary argsKeyValDictionary = ParseArgs(args);
            bool isArgsValid = true;
            foreach (string key in argsKeyValDictionary.Keys)
            {
                if (false == switchesList.Contains(key.ToLower()))
                {
                    Console.WriteLine("The command line argument: {0} is not recognized", key);
                    isArgsValid = false;
                }
            }
            if (false == isArgsValid) return 1;

            Console.WriteLine("Running with arguments:
");
            foreach (string key in argsKeyValDictionary.Keys)
            {
                Console.WriteLine("{0} - {1}", new string[] { key, argsKeyValDictionary[key] });
            }




            string k2SolutionFileName = argsKeyValDictionary["ksnpath"];
            string k2ServerName = argsKeyValDictionary["k2server"];
            string k2Port = argsKeyValDictionary["port"];
            string projectToDeployName = argsKeyValDictionary["kpj"];
            string processToDeployName = argsKeyValDictionary["kpr"];
            string externalDllsPath = argsKeyValDictionary["binpath"];
            bool saveSolution = argsKeyValDictionary.ContainsKey("savechanges");
           

            K2Studio.ApplicationClass app = null;
            try
            {
                app = new K2Studio.ApplicationClass();
                app.Solution.Open(k2SolutionFileName);
                K2Studio.Project proj =
                    app.Solution.Projects.Item(projectToDeployName);

                string tempExportServerName = k2ServerName + DateTime.Now.ToFileTime();

                K2Studio.ExportServer exportSvr = proj.ExportServers.Add(tempExportServerName);

                exportSvr.WindowsAuthentication = true;

                K2Studio.Process proc =
                    proj.ProcessFolder.Processes.Item(processToDeployName);

               
                ///update the external references
                System.IO.DirectoryInfo dInfo = new System.IO.DirectoryInfo(externalDllsPath);
                if (false == dInfo.Exists) throw new Exception(String.Format("The directory {0} doesn't exist", externalDllsPath));
                FileInfo[] dllFiles = dInfo.GetFiles("*.dll");


                ArrayList refsToReAddCollection = new ArrayList();
                for (int i = proj.References.Count - 1; i>=0 ; i--)
                {
                    K2Studio.Reference externalRef = proj.References.Item(i);
                    string refName = externalRef.Name;
                    bool isGAC = externalRef.GlobalAssemblyCache;
                    string fullName = externalRef.FullName;

                    //check if this is in the config specified assemblies
                    if (IsExternalRef(dllFiles, refName))
                    {
                        proj.References.Remove(i);
                        Console.WriteLine("Removing DLL Reference {0}", refName);
                        refsToReAddCollection.Add(externalRef);
                    }
                }

                Console.WriteLine("Adding References back:");
                foreach (K2Studio.Reference refToAdd in refsToReAddCollection)
                {
                    string fileName = dInfo.GetFiles(refToAdd.Name)[0].FullName;
                    proj.References.Add(refToAdd.Name, fileName, false);
                    Console.WriteLine("{0}",fileName);
                }

                if (saveSolution)
                {
                    proj.Save();
                }

                ///run the export function
                string exportResults = "";
                proc.Export(exportSvr.Name, ref exportResults);
               
                Console.WriteLine(exportResults);

                if (false == exportResults.Contains("Exported successfully"))
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return 1; //error
            }
            finally
            {
                if (app != null) app.Solution.Close(saveSolution);
                System.Threading.Thread.Sleep(5 * 1000);
            }
        }
    }
}

 


1 reply

Badge +9
Nice job.  This look great.

Reply