-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerator.cs
More file actions
385 lines (359 loc) · 18.3 KB
/
Generator.cs
File metadata and controls
385 lines (359 loc) · 18.3 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
namespace GitRepoGenerator
{
class Generator
{
private const string HelpMessage =
"\nUsage: GitRepoGenerator --url=<API_Url_Location> --existing=<Existing_Projects_File> [--disabled] [--ignore=<Project_Name>] [--child=<Child_Project>] [--msi_out=<TargetDir>]" +
"\n\n--url=\t\tThe full url for the GitHub API call.\n" +
"--existing=\tInput text file listing existing Jenkins projects, 1 project per line.\n" +
"--disabled\tOptional parameter. Will generate Jenkins project files already set to 'Disabled'.\n"+
"--ignore=\tOptional parameter. <Project_Name> will be removed from <Existing_Projects_File> before processing.\n"+
"--child=\tOptional parameter. Trigger a build of <Child_Project> after any successful build of this project.\n"+
"--msi_out=\tOptional parameter. This will copy any .msi files from the workspace to <TargetDir> after a successful build.\n";
private static string child = null;
private static string msi_out = null;
/*
*
*
* Only input is a string url to the GitHub Rest API.
* Writes Jenkins project xml files to current directory.
* Writes to standard output a list of all repos with project files.
*/
static int Main(string[] args)
{
bool EnableProjects = true;
args.Switches();
if (args.SwitchValue("help") != null)
{
Console.WriteLine(HelpMessage);
return -1;
}
// Check for --disabled switch
if (args.SwitchValue("disabled") != null)
EnableProjects = false;
List<string> ignore = new List<string>(args.Switches()["ignore"]);
// Load the list of existing projects...
List<string> existing;
if (args.SwitchValue("existing") != null)
try
{
//FileStream file = new FileStream(,FileMode.Open,FileAccess.Read);
StreamReader IN = new StreamReader(args.SwitchValue("existing"));
List<string> init = new List<string>();
while (!IN.EndOfStream)
{
init.Add(IN.ReadLine());
}
existing = new List<string>(init.Distinct());
foreach (string s in ignore)
{
existing.Remove(s); //remove the project to be ignored
}
}
catch (FileNotFoundException e)
{
Console.WriteLine(HelpMessage);
return -2;
}
else
{
Console.WriteLine(HelpMessage);
return -2;
}
child = args.SwitchValue("child");
msi_out = args.SwitchValue("msi_out");
// Load the repo list...
List<JObject> jsons = new List<JObject>();
if (args.SwitchValue("url") != null)
try
{
string input = new System.Net.WebClient().DownloadString(args.SwitchValue("url"));
List<string> set = new List<string>();
StringReader reader = new StringReader(input);
char c = (char)reader.Read(); // I expect this to be '['
string s = String.Empty;
int depth = 0;
c = (char)reader.Read(); // I need this to not be the initial '['
while (c != ']' || depth > 0) // escape when we hit the end
{
switch (c)
{
case '{':
depth += 1;
goto default;
case '}':
depth -= 1;
goto default;
case ',':
if (depth == 0)
{
jsons.Add(JObject.Parse(s));
s = String.Empty;
}
else
{
goto default;
}
break;
default:
s += c;
break;
}
c = (char)reader.Read();
}
jsons.Add(JObject.Parse(s));
}
catch (Exception e)
{
Console.WriteLine(HelpMessage);
return -3;
}
else
{
Console.WriteLine(HelpMessage);
return -3;
}
// Do actual work...
Dictionary<string,JObject> repos = new Dictionary<string, JObject>();
foreach (JObject R in jsons)
{
try
{
repos.Add((string) R["name"], R);
}
catch (Exception e)
{
}
}
// Compare existing with new list
List<string> stillExisting = new List<string>();
List<string> newRepos = new List<string>();
foreach (KeyValuePair<string, JObject> repo in repos)
{
if (existing.Contains(repo.Key))
{
stillExisting.Add(repo.Key);
existing.Remove(repo.Key);
}
else
{
if (Generate(repo.Value, EnableProjects))
newRepos.Add(repo.Key);
else
{
Console.Error.WriteLine("Error generating project file for repo: "+repo.Key);
}
}
}
//// Generate text files with repo/file names
//List of projects to disable
StreamWriter disable = new StreamWriter("disable.txt", false);
foreach (string s in existing)
{
disable.WriteLine(s);
}
disable.Close();
//List of active/existing repos
StreamWriter enable = new StreamWriter("enable.txt", false);
foreach (string s in stillExisting)
{
enable.WriteLine(s);
}
enable.Close();
//List of new repo project files
StreamWriter newProjects = new StreamWriter("new.txt", false);
foreach (string s in newRepos)
{
newProjects.WriteLine(s);
}
newProjects.Close();
// If we've made it this far, exit with success.
return 0;
}
private static bool Generate(JObject repo, bool enabled = true)
{
try
{
XmlWriterSettings init = new XmlWriterSettings();
init.NewLineChars = "\n";
init.Indent = true;
XmlWriter xml = XmlWriter.Create(((string) repo["name"] + ".config.xml"), init);
xml.WriteStartElement("project");
xml.WriteStartElement("actions");
xml.WriteEndElement(); //empty actions
xml.WriteElementString("description","Auto-Build project for github repository at "+(string)repo["html_url"]);
xml.WriteElementString("keepDependencies","false");
xml.WriteStartElement("properties");
xml.WriteStartElement("com.coravy.hudson.plugins.github.GithubProjectProperty");
xml.WriteElementString("projectUrl", (string)repo["html_url"]);
xml.WriteEndElement(); //end GithubProjectProperty
xml.WriteEndElement(); //end properties
xml.WriteStartElement("scm");
xml.WriteAttributeString("class","hudson.plugins.git.GitSCM");
xml.WriteElementString("configVersion","2");
xml.WriteStartElement("userRemoteConfigs");
xml.WriteStartElement("hudson.plugins.git.UserRemoteConfig");
xml.WriteStartElement("name");
xml.WriteFullEndElement();
xml.WriteStartElement("refspec");
xml.WriteFullEndElement();
xml.WriteElementString("url", (string)repo["git_url"]);
xml.WriteEndElement(); //end hudson...UserRemoteConfig
xml.WriteEndElement(); //end userRemoteConfigs
xml.WriteStartElement("branches");
xml.WriteStartElement("hudson.plugins.git.BranchSpec");
xml.WriteElementString("name","**");
xml.WriteEndElement(); //end hudson...BranchSpec
xml.WriteEndElement(); //end branches
xml.WriteElementString("disableSubmodules","false");
xml.WriteElementString("recursiveSubmodules","true");
xml.WriteElementString("doGenerateSubmoduleConfigurations","false");
xml.WriteElementString("authorOrCommitter","false");
xml.WriteElementString("clean","false");
xml.WriteElementString("wipeOutWorkspace","true");
xml.WriteElementString("pruneBranches","false");
xml.WriteElementString("remotePoll","false");
xml.WriteStartElement("buildChooser");
xml.WriteAttributeString("class","hudson.plugins.git.util.DefaultBuildChooser");
xml.WriteEndElement(); //end buildChooser
xml.WriteElementString("gitTool","Default");
xml.WriteStartElement("submoduleCfg");
xml.WriteAttributeString("class", "list");
xml.WriteEndElement(); //end submoduleCfg
xml.WriteStartElement("relativeTargetDir");
xml.WriteFullEndElement();
xml.WriteStartElement("reference");
xml.WriteFullEndElement();
xml.WriteStartElement("excludedRegions");
xml.WriteFullEndElement();
xml.WriteStartElement("excludedUsers");
xml.WriteFullEndElement();
xml.WriteStartElement("gitConfigName");
xml.WriteFullEndElement();
xml.WriteStartElement("gitConfigEmail");
xml.WriteFullEndElement();
xml.WriteElementString("skipTag", "false");
xml.WriteStartElement("scmName");
xml.WriteFullEndElement();
xml.WriteEndElement(); //end scm
xml.WriteElementString("quietPeriod","30");
xml.WriteElementString("canRoam", "true");
xml.WriteElementString("disabled",enabled?"true":"false");
xml.WriteElementString("blockBuildWhenDownstreamBuilding","false");
xml.WriteElementString("blockBuildWhenUpstreamBuilding", "false");
xml.WriteStartElement("triggers");
xml.WriteAttributeString("class","vector");
xml.WriteStartElement("hudson.triggers.SCMTrigger");
xml.WriteElementString("spec",@"0 * * * *
20 * * * *
40 * * * *");
xml.WriteEndElement(); //end SCMTrigger
xml.WriteEndElement(); //end triggers
xml.WriteElementString("concurrentBuild","false");
xml.WriteStartElement("builders");
xml.WriteStartElement("hudson.tasks.BatchFile");
xml.WriteElementString("command", @"setlocal ENABLEDELAYEDEXPANSION
git diff HEAD HEAD~1 COPKG\version.inc > COPKG\tmp_diff.txt
set HAS_DIFF=false
if %ERRORLEVEL% == 0 (
for /F ""tokens=*"" %%A in ('type COPKG\tmp_diff.txt') do set HAS_DIFF=true
if ""!HAS_DIFF!"" == ""false"" goto :eof
)
ptk test && ptk package
");
xml.WriteEndElement(); //end BatchFile
xml.WriteStartElement("org.jenkinsci.plugins.conditionalbuildstep.singlestep.SingleConditionalBuilder");
xml.WriteStartElement("condition");
xml.WriteAttributeString("class", "org.jenkins_ci.plugins.run_condition.core.StatusCondition");
xml.WriteStartElement("worstResult");
xml.WriteElementString("name","ABORTED");
xml.WriteElementString("ordinal","4");
xml.WriteElementString("color", "ABORTED");
xml.WriteEndElement(); //end worstResult
xml.WriteStartElement("bestResult");
xml.WriteElementString("name", "SUCCESS");
xml.WriteElementString("ordinal", "0");
xml.WriteElementString("color", "BLUE");
xml.WriteEndElement(); //end bestResult
xml.WriteEndElement(); //end condition
xml.WriteStartElement("buildStep");
xml.WriteAttributeString("class","hudson.tasks.BatchFile");
xml.WriteElementString("command", @"REM This clears a block on deleting the workspace before/after a build.
setlocal ENABLEDELAYEDEXPANSION
set iter=1
for /F ""tokens=3,6,* delims=: ""; %%A in ('C:\utl\SysInternals\handle.exe " + repo["name"] + @"\workspace\.git') do (
if !iter! lss 3 (
set /a iter+=1
) else (
C:\utl\sysinternals\handle.exe /accepteula -c %%B -y -p %%A
)
)
for /F ""tokens=3,6,* delims=: "" %%A in ('C:\utl\SysInternals\handle.exe " + repo["name"] + @"\workspace\COPKG') do (
if !iter! lss 3 (
set /a iter+=1
) else (
C:\utl\sysinternals\handle.exe /accepteula -c %%B -y -p %%A
)
)
endlocal");
xml.WriteEndElement(); //end buildStep
xml.WriteStartElement("runner");
xml.WriteAttributeString("class", "org.jenkins_ci.plugins.run_condition.BuildStepRunner$RunUnstable");
xml.WriteEndElement();
xml.WriteEndElement(); //end SingleConditionalBuilder
xml.WriteEndElement(); //end builders
xml.WriteStartElement("publishers");
xml.WriteStartElement("hudson.tasks.ArtifactArchiver");
xml.WriteElementString("artifacts",@"COPKG\**.msi");
xml.WriteElementString("latestOnly","false");
xml.WriteEndElement(); //end ArtifactArchiver
if (child != null)
{
xml.WriteStartElement("hudson.tasks.BuildTrigger");
xml.WriteElementString("childProjects",child);
xml.WriteStartElement("threshold");
xml.WriteElementString("name","SUCCESS");
xml.WriteElementString("ordinal","0");
xml.WriteElementString("color","BLUE");
xml.WriteEndElement(); //end threshold
xml.WriteEndElement(); //end BuildTrigger
}
if (msi_out != null)
{
xml.WriteStartElement("org.jenkinsci.plugins.artifactdeployer.ArtifactDeployerPublisher");
xml.WriteStartElement("entries");
xml.WriteStartElement("org.jenkinsci.plugins.artifactdeployer.ArtifactDeployerEntry");
xml.WriteElementString("includes",@"COPKG\**.msi");
xml.WriteElementString("remote",msi_out);
xml.WriteElementString("flatten","true");
xml.WriteElementString("deleteRemote","false");
xml.WriteElementString("deleteRemoteArticacts","false");
xml.WriteElementString("deleteRemoteArtifactsByScript","false");
xml.WriteEndElement(); // end ArtifactDeployerEntry
xml.WriteEndElement(); //end entries
xml.WriteEndElement(); //end ArtifactDeployerPublisher
}
xml.WriteEndElement(); //end publishers
xml.WriteStartElement("buildWrappers");
xml.WriteEndElement();
xml.WriteEndElement(); //end project
//close and write the xml file
xml.Close();
return true;
}
catch(Exception e)
{
Console.Error.WriteLine(e.Message);
Console.Error.WriteLine(e.StackTrace);
}
return false;
}
}
}