-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuild.cake
115 lines (92 loc) · 3.15 KB
/
build.cake
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
var configuration = Argument("configuration", string.Empty);
var target = Argument("target", "default");
var configurationParts = configuration.Split('/', StringSplitOptions.RemoveEmptyEntries);
var cookbook = configurationParts.ElementAtOrDefault(0) ?? Argument<string>("cookbook");
var instance = configurationParts.ElementAtOrDefault(1) ?? Argument("instance", string.Empty);
var driver = configurationParts.ElementAtOrDefault(2) ?? Argument("driver", "vagrant");
var cookbookDirectory = Directory($"samples/cookbooks/{cookbook}");
var artifactsDirectory = Directory($"artifacts");
Task("commit-init")
.Does(() => {
Chef("--version");
});
Task("commit-lint")
.Does(() => {
Chef("exec", "cookstyle");
});
Task("commit-build")
.Does(() => {
var policyfiles = GetFiles($"{cookbookDirectory}/Policyfile*.rb");
foreach (var policyfile in policyfiles) {
var policyfileName = policyfile.GetFilename().ToString();
Chef("install", policyfileName);
Chef("update", policyfileName, "--attributes");
Chef("export", policyfileName, MakeAbsolute(artifactsDirectory).ToString(), "--force", "--archive");
foreach (var archive in GetFiles($"{artifactsDirectory}/*{cookbook.Replace('-', '_')}*-*.tgz")) {
var archiveTarget = $"{artifactsDirectory}/{string.Join('-', archive.GetFilename().ToString().Split('-').SkipLast(1))}.tgz";
if (FileExists(archiveTarget)) {
DeleteFile(archiveTarget);
}
MoveFile(archive, $"{archiveTarget}");
}
}
});
Task("commit-test")
.Does(() => {
Chef("exec", "rspec");
});
Task("commit-clean")
.Does(() => {
CleanDirectory(artifactsDirectory);
});
Task("commit")
.IsDependentOn("commit-init")
.IsDependentOn("commit-lint")
.IsDependentOn("commit-build")
.IsDependentOn("commit-test");
Task("acceptance-init")
.Does(() => {
Kitchen("--version");
Kitchen("list", instance);
});
Task("acceptance-test")
.Does(() => {
try {
Kitchen("test", instance);
} catch (Exception) {
Kitchen("create", instance);
throw;
}
});
Task("acceptance-clean")
.Does(() => {
Kitchen("destroy", instance);
});
Task("acceptance")
.IsDependentOn("acceptance-init")
.IsDependentOn("acceptance-test");
Task("default")
.IsDependentOn("commit");
RunTarget(target);
void Chef(params string[] arguments) {
var result = StartProcess("chef", new ProcessSettings {
Arguments = ProcessArgumentBuilder.FromStrings(arguments),
WorkingDirectory = cookbookDirectory
});
if (result != 0) {
throw new Exception($"Chef failed with code {result} .");
}
}
void Kitchen(params string[] arguments) {
var executable = IsRunningOnWindows() ? @"C:\opscode\chef-workstation\bin\kitchen.bat" : "kitchen";
var result = StartProcess(executable, new ProcessSettings {
Arguments = ProcessArgumentBuilder.FromStrings(arguments),
WorkingDirectory = cookbookDirectory,
EnvironmentVariables = new Dictionary<string, string> {
{ "KITCHEN_GLOBAL_YAML", $"../../../src/kitchen.{driver}.yml" }
},
});
if (result != 0) {
throw new Exception($"Kitchen failed with code {result} .");
}
}