-
Notifications
You must be signed in to change notification settings - Fork 13
/
UpdateCrmWebResoucesCmdlet.cs
204 lines (187 loc) · 7.52 KB
/
UpdateCrmWebResoucesCmdlet.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using Cinteros.Crm.Utils.CI.Cmdlets.WebResources;
using Cinteros.Crm.Utils.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Xml;
namespace Cinteros.Crm.Utils.CI.Cmdlets
{
[Cmdlet(VerbsData.Update, "CrmWebResources")]
[OutputType(typeof(XmlDocument))]
public class UpdateCrmWebResoucesCmdlet : XrmCommandBase
{
#region Parameters
[Parameter(
Mandatory = true,
Position = 0,
HelpMessage = "Path to folder with wwebresources"
)]
[Alias("R")]
public string RootFolder { get; set; }
[Parameter(
Mandatory = true,
Position = 1,
HelpMessage = "Publisher prefix to add to the webresource path"
)]
[Alias("Pre")]
public string Prefix { get; set; }
[Parameter(
Position = 2,
HelpMessage = "File/folder include/exclude pattern"
)]
[Alias("P")]
public string Pattern { get; set; }
[Parameter(
Position = 2,
HelpMessage = "File path for include/exclude patterns"
)]
[Alias("PF")]
public string PatternFile { get; set; }
[Parameter(HelpMessage = "Set this to allow updating managed webresources")]
[Alias("UM")]
public bool UpdateManaged { get; set; } = false;
#endregion Parameters
protected override void ProcessRecord()
{
var files = GetWebResourcesFromDisk();
UpdateWebResources(files, Container);
}
private List<string> GetWebResourcesFromDisk()
{
var patterns = "**\\*.*";
var includepatterns = new List<string>();
var excludepatterns = new List<string>();
if (string.IsNullOrEmpty(Pattern) && !string.IsNullOrEmpty(PatternFile))
{
patterns = File.ReadAllText(PatternFile);
}
else
{
patterns = Pattern;
}
ExtractFilePatterns(includepatterns, excludepatterns, patterns);
var di = new DirectoryInfo(RootFolder);
var resourcefiles = new List<string>();
foreach (var pattern in includepatterns.Where(p => !string.IsNullOrWhiteSpace(p)))
{
var filepattern = pattern;
var option = SearchOption.TopDirectoryOnly;
if (pattern.StartsWith("**\\"))
{
option = SearchOption.AllDirectories;
filepattern = pattern.Substring(3);
}
WriteVerbose($"Including files matching \"{filepattern}\"");
try
{
var files = di.GetFiles(filepattern, option);
WriteObject($"Including {files.Count()} files matching \"{filepattern}\"");
resourcefiles.AddRange(files.Select(f => f.FullName));
}
catch (DirectoryNotFoundException e)
{
WriteWarning(e.Message);
}
}
foreach (var pattern in excludepatterns.Where(p => !string.IsNullOrWhiteSpace(p)))
{
var filepattern = pattern;
var option = SearchOption.TopDirectoryOnly;
if (pattern.StartsWith("**\\"))
{
option = SearchOption.AllDirectories;
filepattern = pattern.Substring(3);
}
WriteVerbose($"Excluding files matching \"{filepattern}\"");
try
{
var files = di.GetFiles(filepattern, option);
WriteObject($"Excluding {files.Count()} files matching \"{filepattern}\"");
resourcefiles.RemoveAll(file => (files.Select(f => f.FullName).Contains(file)));
}
catch (DirectoryNotFoundException e)
{
WriteWarning(e.Message);
}
}
return resourcefiles;
}
private static void ExtractFilePatterns(List<string> includepatterns, List<string> excludepatterns, string patterns)
{
var patternparts = patterns.Contains(";") ? patterns.Split(';').Select(p => p.Trim()) : patterns.Split('\n').Select(p => p.Trim());
foreach (var patternpart in patternparts)
{
if (patternpart.StartsWith("!"))
{
excludepatterns.Add(patternpart.Substring(1));
}
else
{
includepatterns.Add(patternpart);
}
}
}
private void UpdateWebResources(List<string> files, CintContainer container)
{
WriteObject(string.Format("Updating {0} webresources", files.Count));
var progress = new ProgressRecord(0, "UpdateWebResources", "Idle");
var fileno = 0;
var updatecount = 0;
foreach (var file in files)
{
fileno++;
string crmpath = GetCrmPath(file);
WriteVerbose(" " + crmpath);
progress.StatusDescription = $"Retrieving {crmpath}";
progress.PercentComplete = (fileno * 50) / files.Count;
WriteProgress(progress);
var wr = WebResourceManager.RetrieveWebResource(container, crmpath);
if (wr == null)
{
throw new ArgumentOutOfRangeException("file", crmpath, $"{crmpath} does not exist in target CRM. Make sure it is uploaded before updating.");
}
if (wr.Property("ismanaged", false))
{
if (!UpdateManaged)
{
throw new ArgumentOutOfRangeException("file", crmpath, $"{crmpath} is managed in target CRM. Use parameter UpdateManaged to allow this.");
}
else
{
WriteWarning("Updating managed webresource: " + crmpath);
}
}
fileno++;
progress.StatusDescription = $"Updating {crmpath}";
progress.PercentComplete = (fileno * 50) / files.Count;
WriteProgress(progress);
var filecontent = Convert.ToBase64String(File.ReadAllBytes(file));
if (filecontent != wr.Property("content", string.Empty))
{
var updatewr = wr.Clone(true);
updatewr.AddProperty("content", filecontent);
updatewr.Save();
WriteObject($"Updated {wr}");
updatecount++;
}
else
{
WriteVerbose($"No change: {wr}");
}
}
WriteObject($"Successfully updated {updatecount} webresources.");
}
private string GetCrmPath(string localfile)
{
if (!localfile.StartsWith(RootFolder, StringComparison.OrdinalIgnoreCase))
{
throw new FileLoadException("File not under root folder", localfile);
}
var crmpath = Prefix + (Prefix.EndsWith("_") ? "" : "_") + "/";
crmpath += localfile.Replace(RootFolder.EndsWith("\\") ? RootFolder : RootFolder + "\\", "").Replace("\\", "/");
return crmpath;
}
}
}