protected override void ExecuteInternal(string filePath, OrganizationServiceContext ctx, bool backupFiles) { _trace.WriteLine("Searching for plugin classes in '{0}'", filePath); var targetFolder = new DirectoryInfo(filePath); var matches = DirectoryEx.Search(filePath, "*.cs", null); if (matches == null) { return; } var pluginRegistration = new PluginRegistraton(_service, ctx, _trace); int codeFilesUpdated = 0; foreach (var codeFile in matches) { try { // Find if it contains any IPlugin files CodeParser parser = new CodeParser(new Uri(codeFile)); if (parser.PluginCount > 0) { // Backup if (backupFiles) { File.WriteAllText(parser.FilePath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".bak", parser.Code); } foreach (var pluginType in parser.ClassNames) { // Remove existing attributes parser.RemoveExistingAttributes(); if (parser.IsPlugin(pluginType)) { AddPluginAttributes(ctx, parser, pluginType); } else if (parser.IsWorkflowActivity(pluginType)) { AddWorkflowActivityAttributes(ctx, parser, pluginType); } else { _trace.WriteLine("Cannot find Plugin Type Registration {0}", pluginType); } } // Update File.WriteAllText(parser.FilePath, parser.Code); codeFilesUpdated++; } } catch (ReflectionTypeLoadException ex) { throw new Exception(ex.LoaderExceptions.First().Message); } } _trace.WriteLine("{0} plugins decorated with deployment attributes!", codeFilesUpdated); }
private string GetPackagerFolder() { // locate the CrmSvcUtil package folder var targetfolder = DirectoryEx.GetApplicationDirectory(); // If we are running in VS, then move up past bin/Debug if (targetfolder.Contains(@"bin\Debug") || targetfolder.Contains(@"bin\Release")) { targetfolder += @"\.."; } // move from spkl.v.v.v.\tools - back to packages folder var binPath = DirectoryEx.Search(targetfolder + @"\..\..", "SolutionPackager.exe"); _trace.WriteLine("Target {0}", targetfolder); if (string.IsNullOrEmpty(binPath)) { throw new SparkleTaskException(SparkleTaskException.ExceptionTypes.UTILSNOTFOUND, String.Format("Cannot locate SolutionPackager at '{0}' - run Install-Package Microsoft.CrmSdk.CoreTools", binPath)); } return(binPath); }
private void CreateEarlyBoundTypes(OrganizationServiceContext ctx, ConfigFile config) { // locate the CrmSvcUtil package folder var targetfolder = DirectoryEx.GetApplicationDirectory(); // move from spkl.v.v.v.\tools - back to packages folder var crmsvcutilPath = DirectoryEx.Search(targetfolder + @"\..\..", "crmsvcutil.exe"); _trace.WriteLine("Target {0}", targetfolder); var crmsvcutilFolder = new FileInfo(crmsvcutilPath).DirectoryName; if (string.IsNullOrEmpty(crmsvcutilPath)) { throw new SparkleTaskException(SparkleTaskException.ExceptionTypes.UTILSNOTFOUND, String.Format("Cannot locate CrmSvcUtil at '{0}' - run Install-Package Microsoft.CrmSdk.CoreTools", crmsvcutilPath)); } // Copy the filtering assembly FileInfo filteringAssemblyPath = new FileInfo(Path.Combine(targetfolder, "spkl.CrmSvcUtilExtensions.dll")); if (!filteringAssemblyPath.Exists) { throw new SparkleTaskException(SparkleTaskException.ExceptionTypes.UTILSNOTFOUND, String.Format("Cannot locate spkl.CrmSvcUtilExtensions.dll at '{0}' ", crmsvcutilPath)); } File.Copy(filteringAssemblyPath.FullName, Path.Combine(crmsvcutilFolder, "spkl.CrmSvcUtilExtensions.dll"), true); var earlyBoundTypeConfigs = config.GetEarlyBoundConfig(this.Profile); foreach (var earlyboundconfig in earlyBoundTypeConfigs) { // Create config and copy to the CrmSvcUtil folder string configXml = String.Format(@"<?xml version=""1.0"" encoding=""utf-8"" ?> <configuration> <entities>{0}</entities> <messages>{1}</messages> <picklistEnums>{2}</picklistEnums> <stateEnums>{3}</stateEnums> </configuration>", earlyboundconfig.entities, earlyboundconfig.actions, earlyboundconfig.generateOptionsetEnums.ToString().ToLower(), earlyboundconfig.generateStateEnums.ToString().ToLower()); // Copy the filtering assembly to the CrmSvcUtil folder File.WriteAllText(Path.Combine(crmsvcutilFolder, "spkl.crmsvcutil.config"), configXml); // Run CrmSvcUtil string parameters = String.Format(@"/connstr:""{0}"" /out:""{1}"" /namespace:""{2}"" /serviceContextName:""{3}"" /GenerateActions:""{4}"" /codewriterfilter:""spkl.CrmSvcUtilExtensions.FilteringService,spkl.CrmSvcUtilExtensions"" /codewritermessagefilter:""spkl.CrmSvcUtilExtensions.MessageFilteringService,spkl.CrmSvcUtilExtensions"" /metadataproviderqueryservice:""spkl.CrmSvcUtilExtensions.MetadataProviderQueryService,spkl.CrmSvcUtilExtensions""", this.ConectionString, Path.Combine(this._folder, earlyboundconfig.filename), earlyboundconfig.classNamespace, earlyboundconfig.serviceContextName, !String.IsNullOrEmpty(earlyboundconfig.actions)); ProcessStartInfo procStart = new ProcessStartInfo(crmsvcutilPath, parameters) { WorkingDirectory = crmsvcutilFolder, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true, UseShellExecute = false, WindowStyle = ProcessWindowStyle.Hidden }; _trace.WriteLine("Running {0} {1}", crmsvcutilPath, parameters); Process proc = null; try { proc = Process.Start(procStart); proc.OutputDataReceived += Proc_OutputDataReceived; proc.ErrorDataReceived += Proc_OutputDataReceived; proc.BeginOutputReadLine(); proc.BeginErrorReadLine(); proc.WaitForExit(20 * 60 * 60 * 1000); proc.CancelOutputRead(); proc.CancelErrorRead(); } catch (Exception ex) { throw; } finally { proc.Close(); } } }
protected override void ExecuteInternal(string filePath, OrganizationServiceContext ctx) { _trace.WriteLine("Searching for plugin classes in '{0}'", filePath); var targetFolder = new DirectoryInfo(filePath); var matches = DirectoryEx.Search(filePath, "*.cs", null); if (matches == null) { return; } var pluginRegistration = new PluginRegistraton(_service, ctx, _trace); int codeFilesUpdated = 0; foreach (var codeFile in matches) { try { // Find if it contains any IPlugin files CodeParser parser = new CodeParser(new Uri(codeFile)); if (parser.PluginCount > 0) { // Backup File.WriteAllText(parser.FilePath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".bak", parser.Code); foreach (var pluginType in parser.ClassNames) { // Remove existing attributes parser.RemoveExistingAttributes(); if (parser.IsPlugin(pluginType)) { AddPluginAttributes(ctx, parser, pluginType); } else if (parser.IsWorkflowActivity(pluginType)) { AddWorkflowActivityAttributes(ctx, parser, pluginType); } else { _trace.WriteLine("Cannot find Plugin Type Registration {0}", pluginType); } } // Update File.WriteAllText(parser.FilePath, parser.Code); codeFilesUpdated++; } } catch (ReflectionTypeLoadException ex) { throw new Exception(ex.LoaderExceptions.First().Message); } } _trace.WriteLine("{0} plugins decorated with deployment attributes!", codeFilesUpdated); //TODO: remove backup files //// Create a spkl.json file here //var files = ConfigFile.FindConfig(filePath,false); //var file = files[0]; //if (file.plugins == null) //{ // file.plugins = new List<PluginDeployConfig>(); //} //if (file.plugins.Where(a=>a.assemblypath == @"bin\Debug").FirstOrDefault()==null) //{ // file.plugins.Add(new PluginDeployConfig() // { // assemblypath = @"bin\Debug" // }); //} //file.filePath = filePath; //file.Save(); }
protected override void ExecuteInternal(string filePath, OrganizationServiceContext ctx) { _trace.WriteLine("Searching for classes in '{0}'", filePath); var targetFolder = new DirectoryInfo(filePath); var matches = DirectoryEx.Search(filePath, "*.cs", null); if (matches == null) { return; } var pluginRegistration = new PluginRegistraton(_service, ctx, _trace); int codeFilesUpdated = 0; // Create a spkl.json file here (or load an existing one) var files = ConfigFile.FindConfig(filePath, false); var file = files[0]; foreach (var codeFile in matches) { try { string customClassRegex = null; // Support for custom base class regex var profile = file.GetPluginsConfig(this.Profile); if (profile != null && profile.Length > 0 && !String.IsNullOrEmpty(profile[0].classRegex)) { customClassRegex = profile[0].classRegex; } // Find if it contains any plugin/workflow classes CodeParser parser = new CodeParser(new Uri(codeFile), customClassRegex); if (parser.PluginCount > 0) { // Backup File.WriteAllText(parser.FilePath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".bak", parser.Code); foreach (var pluginType in parser.ClassNames) { // Remove existing attributes parser.RemoveExistingAttributes(); if (parser.IsPlugin(pluginType)) { AddPluginAttributes(ctx, parser, pluginType); } else if (parser.IsWorkflowActivity(pluginType)) { AddWorkflowActivityAttributes(ctx, parser, pluginType); } else { _trace.WriteLine("Cannot find Type Registration {0}", pluginType); } } // Update File.WriteAllText(parser.FilePath, parser.Code); codeFilesUpdated++; } } catch (ReflectionTypeLoadException ex) { throw new Exception(ex.LoaderExceptions.First().Message); } } _trace.WriteLine("{0} classes decorated with deployment attributes!", codeFilesUpdated); if (file.plugins == null) { file.plugins = new List <PluginDeployConfig>(); } if (file.plugins.Where(a => a.assemblypath == @"bin\Debug").FirstOrDefault() == null) { file.plugins.Add(new PluginDeployConfig() { assemblypath = @"bin\Debug" }); } file.filePath = filePath; file.Save(); }
protected override void ExecuteInternal(string filePath, OrganizationServiceContext ctx) { _trace.WriteLine("Searching for webresources in '{0}'", filePath); ConfigFile config = null; try { var configs = ConfigFile.FindConfig(filePath); config = configs[0]; } catch { config = new ConfigFile() { filePath = filePath, }; } if (config.webresources == null || config.webresources.Count == 0) { // Add a webresource seciton config.webresources = new List <WebresourceDeployConfig> { new WebresourceDeployConfig { files = new List <WebResourceFile>() } }; } var newWebResources = new List <WebResourceFile>(); var files = config.webresources.Where(a => a.files != null).SelectMany(a => a.files); Dictionary <string, WebResourceFile> existingWebResources = new Dictionary <string, WebResourceFile>(); foreach (var file in files) { string key = file.uniquename.ToLower(); if (!existingWebResources.ContainsKey(key)) { existingWebResources[key] = file; } else { var duplicate = new SparkleTaskException(SparkleTaskException.ExceptionTypes.DUPLICATE_FILE, String.Format("Duplicate file in webresource config '{0}'. Config at '{1}'", file.file, filePath)); throw duplicate; } } var webresources = DirectoryEx.Search(filePath, "*.js|*.htm|*.css|*.xap|*.png|*.jpeg|*.jpg|*.gif|*.ico|*.xml", null); // check there is a prefix supplied if (string.IsNullOrWhiteSpace(Prefix)) { throw new SparkleTaskException(SparkleTaskException.ExceptionTypes.MISSING_PREFIX, "Please supply the prefix for your webresources e.g. /p:new"); } // Get a list of all webresources! var matchList = ctx.GetWebresources().ToDictionary(w => w.Name.ToLower().Replace(Prefix + (Prefix.EndsWith("_") ? "" : "_"), "")); var webresourceConfig = config.GetWebresourceConfig(null).FirstOrDefault(); if (webresourceConfig == null) { throw new Exception("Cannot find webresource section in spkl.json"); } string rootPath = Path.Combine(config.filePath, webresourceConfig.root != null ? webresourceConfig.root : ""); foreach (var filename in webresources) { _trace.WriteLine("Found '{0}'", filename); var file = filename.Replace("\\", "/").ToLower(); // Find if there are any matches var match = matchList.Keys.Where(w => file.Contains(w)).FirstOrDefault(); if (match != null) { _trace.WriteLine(String.Format("Found webresource {0}", match)); var webresource = matchList[match]; var webresourcePath = filename.Replace(rootPath, "").TrimStart('\\').TrimStart('/'); // is it already in the config file var existingMatch = existingWebResources.Keys.Where(w => webresource.Name.Contains(w)).FirstOrDefault(); if (existingMatch != null) { continue; } var webresourceFile = new WebResourceFile { uniquename = webresource.Name, file = webresourcePath, displayname = "" + webresource.DisplayName, description = "" + webresource.Description }; // If the displayname is the same as the uniquename then we only need the unique name if (webresourceFile.displayname == webresourceFile.uniquename) { webresourceFile.displayname = null; } newWebResources.Add(webresourceFile); } } if (webresourceConfig.files == null) { webresourceConfig.files = new List <WebResourceFile>(); } webresourceConfig.files.AddRange(newWebResources); config.Save(); }