-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.cake
219 lines (179 loc) · 7.45 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
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
#addin nuget:?package=Cake.Npm&version=0.17.0
#addin nuget:?package=Cake.FileHelpers&version=3.2.1
#addin nuget:?package=Cake.Json&version=4.0.0
#addin nuget:?package=SharpZipLib&version=1.2.0
#addin nuget:?package=Cake.Compression&version=0.2.4
#load "scripts/utils.cake"
using System.Xml.Linq;
using System.Xml.XPath;
using System.Text.RegularExpressions;
using SysIO = System.IO;
//---------------------------------------------------------------------------------------
// Global parameters
//=======================================================================================
var Target = Argument("target", "Default");
var Clean = HasArgument("clean");
var AppName = "ColorTemperature";
// Confiuration parameters.
var Configuration = Argument("configuration", "Debug");
var IsReleaseBuild = string.Compare(Configuration, "Release", false) == 0;
var SignApp = IsReleaseBuild;
var UpdateBuildNumber = IsReleaseBuild;
var PublishPackage = IsReleaseBuild;
// Source directories
var AppSrcDir = "./app";
var TsSrcDir = AppSrcDir + "/ts";
var PackgeSrcDir = "./pkg";
var InitJsonFilename = PackgeSrcDir + "/init.json";
var ManifestFilename = AppSrcDir + "/CSXS/manifest.xml";
// Build directories
var BuildDir = "./build/" + Configuration;
var CompiledAppDir = BuildDir + "/app";
var PackageDir = BuildDir + "/pkg";
var SignedAppDir = PackageDir + "/html";
var SignedAppZxp = BuildDir + "/" + AppName + ".zxp";
//
var DistDir = "./dist";
// Build configuration which loaded from build config file.
class BuildConfigInfo
{
public string ZxpSignCmd;
public string CertFile;
public string PassFile;
public string PackageVersion;
}
var BuildConfigFile = "./build-config.json";
BuildConfigInfo BuildConfig;
Version BuildVersion;
//---------------------------------------------------------------------------------------
// Setup
//=======================================================================================
var TsCompiler = "";
Setup(context => {
TsCompiler = Context.Tools.Resolve("tsc.exe").FullPath;
BuildConfig = DeserializeJsonFromFile<BuildConfigInfo>(BuildConfigFile);
// Update build number
if (UpdateBuildNumber)
{
// Update build number value in build config file.
var curVer = new Version(BuildConfig.PackageVersion);
var now = DateTime.UtcNow;
var build = (now.Year % 100) * 10000 + now.Month * 100 + now.Day;
var newVer = new Version(curVer.Major, curVer.Minor, build, curVer.Revision + 1);
BuildConfig.PackageVersion = newVer.ToString();
SerializeJsonToFile<BuildConfigInfo>(BuildConfigFile, BuildConfig);
// Update build number value in manifest.xml
var doc = XDocument.Load(NormalizePath(ManifestFilename));
doc.XPathSelectElement("//ExtensionList/Extension").Attribute("Version").Value = newVer.ToString();
doc.Save(NormalizePath(ManifestFilename));
// Update build number value in pkg/init.json
var lines = new List<string>();
foreach (var line in SysIO.File.ReadLines(NormalizePath(InitJsonFilename)))
{
lines.Add(Regex.Replace(line, "(PRODUCT_VERSION:\\s*\")([0-9.]+)(\")",
(m) => m.Groups[1].Value + newVer + m.Groups[3].Value));
}
SysIO.File.WriteAllLines(NormalizePath(InitJsonFilename), lines);
}
BuildVersion = new Version(BuildConfig.PackageVersion);
Information("Packge Version: {0}", BuildConfig.PackageVersion);
});
//---------------------------------------------------------------------------------------
//=======================================================================================
Task("Clean")
.Does(() => {
Information("Clean directory {0}", BuildDir);
CleanDirectories(BuildDir);
});
//---------------------------------------------------------------------------------------
// Compile App
//=======================================================================================
ItemCollection compiledAppItems;
Task("Compile")
.IsDependentOn("CompileTypeScript")
.IsDependentOn("PlaceAppFiles")
.Does(() => {
compiledAppItems = CreateItemCollection(CompiledAppDir, "/**/*", SignedAppDir);
});
// Compile typescript
var tsItems = CreateItemCollection(
AppSrcDir + "/ts", "/**/*.ts",
CompiledAppDir + "/js", ".js",
(p, f) => p.EndsWith(".d.ts")? null: f);
Task("CompileTypeScript")
.WithCriteria(() => tsItems.IsStaled())
.Does(() => {
EnsureDirectoryExists(CompiledAppDir + "/js");
RunProcess(TsCompiler,
"--p", TsSrcDir + "/tsconfig-" + Configuration + ".json",
"-outDir", CompiledAppDir + "/js");
});
// Place App items
var appItems = CreateItemCollection();
appItems.Include(AppSrcDir, "/**/*", CompiledAppDir, null, (p, f) => (p.Contains("/ts/"))? null : f);
appItems.Include(AppSrcDir + "/ts", "/**/*.js", CompiledAppDir + "/js");
Task("PlaceAppFiles")
.WithCriteria(() => appItems.IsStaled())
.Does(() => appItems.CopyFromSourceToTarget());
//---------------------------------------------------------------------------------------
// Package creation tasks
//=======================================================================================
var installerSrcFilename = "install Color Temperature.jsx";
var packageItems = CreateItemCollection(PackgeSrcDir, "/**/*", PackageDir, null,
(p, f) => p.EndsWith(installerSrcFilename)? null : f);
Task("PlacePackage")
.WithCriteria(() => {
#break
var installerFilename = string.Format("Install Color Temperature.{0}.{1}.jsx",
BuildVersion.Major, BuildVersion.Minor);
packageItems.Include(PackgeSrcDir, "/**/*", PackageDir, null,
(p, f) => p.EndsWith(installerSrcFilename)? installerFilename : null);
return packageItems.IsStaled();
})
.Does(() => packageItems.CopyFromSourceToTarget());
Task("Sign")
.IsDependentOn("PlacePackage")
.WithCriteria(SignApp)
.WithCriteria(() => compiledAppItems.IsStaled())
.Does(() => {
// Create a ZXP file.
Information("Signing App at {0}", CompiledAppDir);
EnsureDeleteFile(SignedAppZxp);
RunProcess(BuildConfig.ZxpSignCmd, "-sign",
NormalizePath(CompiledAppDir),
NormalizePath(SignedAppZxp),
NormalizePath(BuildConfig.CertFile),
FileReadText(BuildConfig.PassFile),
"-tsa http://time.certum.pl");
// Extract to signed-app directory.
CleanDirectory(SignedAppDir);
ZipUncompress(SignedAppZxp, SignedAppDir);
});
ItemCollection publishSrcItems;
ItemCollection publishTgtItems;
Task("PrepareForPublish")
.IsDependentOn("Sign")
.WithCriteria(PublishPackage)
.Does(() => {
publishSrcItems = CreateItemCollection(PackageDir, "/**/*");
publishTgtItems = CreateItemCollection();
var ver = BuildConfig.PackageVersion.ToString().Replace(".", "_");
publishTgtItems.Include(string.Format("{0}/{1}_{2}.zip", DistDir, AppName, ver));
});
Task("Publish")
.IsDependentOn("PrepareForPublish")
.WithCriteria(PublishPackage)
.WithCriteria(() => IsStaled(publishSrcItems, publishTgtItems))
.Does(() => {
ZipCompress(PackageDir, publishTgtItems.First().Path);
});
Task("Deploy")
.Does(() => {
});
Task("Default")
.IsDependentOn("Clean")
.IsDependentOn("Compile")
.IsDependentOn("Publish")
.Does(() => {
});
RunTarget(Target);