forked from Sitecore/Habitat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
524 lines (478 loc) · 18.7 KB
/
gulpfile.js
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/// <binding />
var gulp = require("gulp");
var msbuild = require("gulp-msbuild");
var debug = require("gulp-debug");
var foreach = require("gulp-foreach");
var rename = require("gulp-rename");
var newer = require("gulp-newer");
var util = require("gulp-util");
var runSequence = require("run-sequence");
var fs = require("fs");
var yargs = require("yargs").argv;
var unicorn = require("./scripts/unicorn.js");
var habitat = require("./scripts/habitat.js");
var helix = require("./scripts/helix.js");
var path = require("path");
var rimrafDir = require("rimraf");
var rimraf = require("gulp-rimraf");
var xmlpoke = require("xmlpoke");
var config;
if (fs.existsSync("./gulp-config.js.user")) {
config = require("./gulp-config.js.user")();
} else {
config = require("./gulp-config.js")();
}
var buildConfigurationCsproj = "BuildConfiguration.csproj";
module.exports.config = config;
helix.header("The Habitat source code, tools and processes are examples of Sitecore Helix.",
"Habitat is not supported by Sitecore and should be used at your own risk.");
gulp.task("default",
function(callback) {
config.runCleanBuilds = true;
return runSequence(
"Publish-All-Projects",
"Apply-Xml-Transform",
"Publish-Transforms",
"Sync-Unicorn",
callback);
});
gulp.task("deploy",
function(callback) {
config.runCleanBuilds = true;
return runSequence(
"Publish-All-Projects",
"Apply-Xml-Transform",
"Publish-Transforms",
callback);
});
/*****************************
Initial setup
*****************************/
gulp.task("References-Local",
function(callback) {
var buildConfig = `<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<LocalReferences>true</LocalReferences>
<SitecorePath>${config.websiteRoot}</SitecorePath>
</PropertyGroup>
</Project>`;
return fs.writeFile(buildConfigurationCsproj, buildConfig, callback);
});
gulp.task("References-Nuget",
function(callback) {
return fs.exists(buildConfigurationCsproj, function(exists) {
if (!exists) {
return;
}
return fs.unlink(buildConfigurationCsproj, callback);
});
});
gulp.task("Publish-All-Projects",
function(callback) {
return runSequence(
"Build-Solution",
"Publish-Foundation-Projects",
"Publish-Feature-Projects",
"Publish-Project-Projects",
callback);
});
gulp.task("Apply-Xml-Transform",
function() {
var layerPathFilters = [
"./src/Foundation/**/*.xdt", "./src/Feature/**/*.xdt", "./src/Project/**/*.xdt",
"!./src/**/obj/**/*.xdt", "!./src/**/bin/**/*.xdt"
];
return gulp.src(layerPathFilters)
.pipe(foreach(function(stream, file) {
var fileToTransform = file.path.replace(/.+code\\(.+)\.xdt/, "$1");
util.log("Applying configuration transform: " + file.path);
return gulp.src("./scripts/applytransform.targets")
.pipe(msbuild({
targets: ["ApplyTransform"],
configuration: config.buildConfiguration,
logCommand: false,
verbosity: config.buildVerbosity,
stdout: true,
errorOnFail: true,
maxcpucount: config.buildMaxCpuCount,
nodeReuse: false,
toolsVersion: config.buildToolsVersion,
properties: {
Platform: config.buildPlatform,
WebConfigToTransform: config.websiteRoot,
TransformFile: file.path,
FileToTransform: fileToTransform
}
}));
}));
});
gulp.task("Publish-Transforms",
function() {
return gulp.src("./src/**/code/**/*.xdt")
.pipe(gulp.dest(config.websiteRoot + "/temp/transforms"));
});
gulp.task("Sync-Unicorn",
function(callback) {
var options = {};
options.siteHostName = habitat.getSiteUrl();
options.authenticationConfigFile = config.websiteRoot + "/App_config/Include/Unicorn.SharedSecret.config";
unicorn(function() { return callback() }, options);
});
/*****************************
Copy assemblies to all local projects
*****************************/
gulp.task("Copy-Local-Assemblies",
function() {
console.log("Copying site assemblies to all local projects");
var files = config.sitecoreLibraries + "/**/*";
var root = "./src";
var projects = root + "/**/code/bin";
return gulp.src(projects, { base: root })
.pipe(foreach(function(stream, file) {
console.log("copying to " + file.path);
gulp.src(files)
.pipe(gulp.dest(file.path));
return stream;
}));
});
/*****************************
Publish
*****************************/
var publishStream = function(stream, dest) {
var targets = ["Build"];
return stream
.pipe(debug({ title: "Building project:" }))
.pipe(msbuild({
targets: targets,
configuration: config.buildConfiguration,
logCommand: false,
verbosity: config.buildVerbosity,
stdout: true,
errorOnFail: true,
maxcpucount: config.buildMaxCpuCount,
nodeReuse: false,
toolsVersion: config.buildToolsVersion,
properties: {
Platform: config.publishPlatform,
DeployOnBuild: "true",
DeployDefaultTarget: "WebPublish",
WebPublishMethod: "FileSystem",
BuildProjectReferences: "false",
DeleteExistingFiles: "false",
publishUrl: dest
}
}));
};
var publishProject = function(location, dest) {
dest = dest || config.websiteRoot;
console.log("publish to " + dest + " folder");
return gulp.src(["./src/" + location + "/code/*.csproj"])
.pipe(foreach(function(stream, file) {
return publishStream(stream, dest);
}));
};
var publishProjects = function(location, dest) {
dest = dest || config.websiteRoot;
console.log("publish to " + dest + " folder");
return gulp.src([location + "/**/code/*.csproj"])
.pipe(foreach(function(stream, file) {
return publishStream(stream, dest);
}));
};
gulp.task("Build-Solution",
function() {
var targets = ["Build"];
if (config.runCleanBuilds) {
targets = ["Clean", "Build"];
}
var solution = "./" + config.solutionName + ".sln";
return gulp.src(solution)
.pipe(msbuild({
targets: targets,
configuration: config.buildConfiguration,
logCommand: false,
verbosity: config.buildVerbosity,
stdout: true,
errorOnFail: true,
maxcpucount: config.buildMaxCpuCount,
nodeReuse: false,
toolsVersion: config.buildToolsVersion,
properties: {
Platform: config.buildPlatform
},
customArgs: [ "/restore" ]
}));
});
gulp.task("Publish-Foundation-Projects",
function() {
return publishProjects("./src/Foundation");
});
gulp.task("Publish-Feature-Projects",
function() {
return publishProjects("./src/Feature");
});
gulp.task("Publish-Project-Projects",
function() {
return publishProjects("./src/Project");
});
gulp.task("Publish-Project",
function() {
if (yargs && yargs.m && typeof(yargs.m) == "string") {
return publishProject(yargs.m);
} else {
throw "\n\n------\n USAGE: -m Layer/Module \n------\n\n";
}
});
gulp.task("Publish-Assemblies",
function() {
var root = "./src";
var binFiles = root + "/**/code/**/bin/Sitecore.{Feature,Foundation,Habitat}.*.{dll,pdb}";
var destination = config.websiteRoot + "/bin/";
return gulp.src(binFiles, { base: root })
.pipe(rename({ dirname: "" }))
.pipe(newer(destination))
.pipe(debug({ title: "Copying " }))
.pipe(gulp.dest(destination));
});
gulp.task("Publish-All-Views",
function() {
var root = "./src";
var roots = [root + "/**/Views", "!" + root + "/**/obj/**/Views"];
var files = "/**/*.cshtml";
var destination = config.websiteRoot + "\\Views";
return gulp.src(roots, { base: root }).pipe(
foreach(function(stream, file) {
console.log("Publishing from " + file.path);
gulp.src(file.path + files, { base: file.path })
.pipe(newer(destination))
.pipe(debug({ title: "Copying " }))
.pipe(gulp.dest(destination));
return stream;
})
);
});
gulp.task("Publish-All-Configs",
function() {
var root = "./src";
var roots = [root + "/**/App_Config", "!" + root + "/**/tests/App_Config", "!" + root + "/**/obj/**/App_Config"];
var files = "/**/*.config";
var destination = config.websiteRoot + "\\App_Config";
return gulp.src(roots, { base: root }).pipe(
foreach(function(stream, file) {
console.log("Publishing from " + file.path);
gulp.src(file.path + files, { base: file.path })
.pipe(newer(destination))
.pipe(debug({ title: "Copying " }))
.pipe(gulp.dest(destination));
return stream;
})
);
});
/*****************************
Watchers
*****************************/
gulp.task("Auto-Publish-Css",
function() {
var root = "./src";
var roots = [root + "/**/styles", "!" + root + "/**/obj/**/styles"];
var files = "/**/*.css";
var destination = config.websiteRoot + "\\styles";
gulp.src(roots, { base: root }).pipe(
foreach(function(stream, rootFolder) {
gulp.watch(rootFolder.path + files,
function(event) {
if (event.type === "changed") {
console.log("publish this file " + event.path);
gulp.src(event.path, { base: rootFolder.path }).pipe(gulp.dest(destination));
}
console.log("published " + event.path);
});
return stream;
})
);
});
gulp.task("Auto-Publish-Views",
function() {
var root = "./src";
var roots = [root + "/**/Views", "!" + root + "/**/obj/**/Views"];
var files = "/**/*.cshtml";
var destination = config.websiteRoot + "\\Views";
return gulp.src(roots, { base: root }).pipe(
foreach(function(stream, rootFolder) {
gulp.watch(rootFolder.path + files,
function(event) {
if (event.type === "changed") {
console.log("publish this file " + event.path);
gulp.src(event.path, { base: rootFolder.path }).pipe(gulp.dest(destination));
}
console.log("published " + event.path);
});
return stream;
})
);
});
gulp.task("Auto-Publish-Assemblies",
function() {
var root = "./src";
var roots = [root + "/**/code/**/bin"];
var files = "/**/Sitecore.{Feature,Foundation,Habitat}.*.{dll,pdb}";;
var destination = config.websiteRoot + "/bin/";
return gulp.src(roots, { base: root }).pipe(
foreach(function(stream, rootFolder) {
gulp.watch(rootFolder.path + files,
function(event) {
if (event.type === "changed") {
console.log("publish this file " + event.path);
gulp.src(event.path, { base: rootFolder.path }).pipe(gulp.dest(destination));
}
console.log("published " + event.path);
});
return stream;
})
);
});
/*****************************
Package
*****************************/
var websiteRootBackup = config.websiteRoot;
var packageXmlPath = config.packagePath + "\\" + config.solutionName + ".xml";
/* create base package configuration */
gulp.task("Package-Create-Package-Xml",
function(callback) {
return gulp.src(config.packageXmlBasePath).pipe(rename(config.solutionName + ".xml")).pipe(gulp.dest(config.packagePath));
});
/* publish files to temp location */
gulp.task("Package-Publish",
function(callback) {
config.websiteRoot = path.resolve("./temp");
config.buildConfiguration = "Release";
fs.mkdirSync(config.websiteRoot);
runSequence(
"Build-Solution",
"Publish-Foundation-Projects",
"Publish-Feature-Projects",
"Publish-Project-Projects",
"Publish-Transforms",
callback);
});
/* Remove unwanted files */
gulp.task("Package-Prepare-Package-Files",
function(callback) {
var excludeList = [
config.websiteRoot + "\\bin\\{Sitecore,Lucene,Newtonsoft,System,Microsoft.Web.Infrastructure}*dll",
config.websiteRoot + "\\compilerconfig.json.defaults",
config.websiteRoot + "\\packages.config",
config.websiteRoot + "\\App_Config\\Include\\{Feature,Foundation,Project}\\*Serialization.config",
config.websiteRoot + "\\App_Config\\Include\\{Feature,Foundation,Project}\\z.*DevSettings.config",
"!" + config.websiteRoot + "\\bin\\Sitecore.Support*dll",
"!" + config.websiteRoot + "\\bin\\Sitecore.{Feature,Foundation,Habitat,Demo,Common}*dll"
];
console.log(excludeList);
return gulp.src(excludeList, { read: false }).pipe(rimraf({ force: true }));
});
/* Add files to package definition */
gulp.task("Package-Enumerate-Files",
function() {
var packageFiles = [];
config.websiteRoot = websiteRootBackup;
return gulp.src(path.resolve("./temp") + "/**/*.*", { base: "temp", read: false })
.pipe(foreach(function(stream, file) {
var item = "/" + file.relative.replace(/\\/g, "/");
console.log("Added to the package:" + item);
packageFiles.push(item);
return stream;
})).pipe(util.buffer(function() {
xmlpoke(packageXmlPath,
function(xml) {
for (var idx in packageFiles) {
xml.add("project/Sources/xfiles/Entries/x-item", packageFiles[idx]);
}
});
}));
});
/* Add items to package definition */
gulp.task("Package-Enumerate-Items",
function() {
var itemPaths = [];
var allowedPatterns = [
"./src/**/serialization/**/*.yml",
"!./src/**/serialization/Roles/**/*.yml",
"!./src/**/serialization/Users/**/*.yml"
];
return gulp.src(allowedPatterns)
.pipe(foreach(function(stream, file) {
console.log(file);
var itemPath = unicorn.getFullItemPath(file);
itemPaths.push(itemPath);
return stream;
})).pipe(util.buffer(function() {
xmlpoke(packageXmlPath,
function(xml) {
for (var idx in itemPaths) {
xml.add("project/Sources/xitems/Entries/x-item", itemPaths[idx]);
}
});
}));
});
/* Add users to package definition */
gulp.task("Package-Enumerate-Users",
function() {
var users = [];
return gulp.src("./src/**/serialization/Users/**/*.yml")
.pipe(foreach(function(stream, file) {
console.log(file);
var fileContent = file.contents.toString();
var userName = unicorn.getUserPath(file);
users.push(userName);
return stream;
})).pipe(util.buffer(function() {
xmlpoke(packageXmlPath,
function(xml) {
for (var idx in users) {
xml.add("project/Sources/accounts/Entries/x-item", users[idx]);
}
});
}));
});
/* Add roles to package definition */
gulp.task("Package-Enumerate-Roles",
function() {
var roles = [];
return gulp.src("./src/**/serialization/Roles/**/*.yml")
.pipe(foreach(function(stream, file) {
console.log(file);
var fileContent = file.contents.toString();
var roleName = unicorn.getRolePath(file);
roles.push(roleName);
return stream;
})).pipe(util.buffer(function() {
xmlpoke(packageXmlPath,
function(xml) {
for (var idx in roles) {
xml.add("project/Sources/accounts/Entries/x-item", roles[idx]);
}
});
}));
});
/* Remove temp files */
gulp.task("Package-Clean",
function(callback) {
rimrafDir.sync(path.resolve("./temp"));
callback();
});
/* Main task, generate package.xml */
gulp.task("Package-Generate",
function(callback) {
runSequence(
"Package-Clean",
"Package-Create-Package-Xml",
"Package-Publish",
"Package-Prepare-Package-Files",
"Package-Enumerate-Files",
"Package-Enumerate-Items",
"Package-Enumerate-Users",
"Package-Enumerate-Roles",
"Package-Clean",
callback);
});