-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.cake
381 lines (322 loc) · 12.5 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
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
using System.Text.RegularExpressions;
const string buildTarget = "build";
const string buildReleaseTarget = "build_release";
const string makeDistTarget = "make_dist";
const string nugetPackTarget = "nuget_pack";
const string buildMsiTarget = "build_msi";
const string chocoPackTarget = "choco_pack";
const string wixVersion = "3.14";
string target = Argument( "target", buildTarget );
bool noBuild = Argument<bool>( "no_build", false );
FilePath sln = new FilePath( "./SshRunAs.sln" );
DirectoryPath distFolder = MakeAbsolute( new DirectoryPath( "./dist" ) );
DirectoryPath installFolder = MakeAbsolute( new DirectoryPath( "./Install" ) );
DirectoryPath wixDir = installFolder.Combine( Directory( "WiX" ) );
DirectoryPath msiWorkDir = wixDir.Combine( Directory( "obj" ) );
DirectoryPath msiOutputDir = wixDir.Combine( Directory( "bin" ) );
FilePath msiPath = msiOutputDir.CombineWithFilePath( "SshRunAs.msi" );
FilePath msiShaFile = File( msiPath.ToString() + ".sha256" );
// This is the version of this software,
// update before making a new release.
const string version = "4.0.1";
DotNetMSBuildSettings msBuildSettings = new DotNetMSBuildSettings();
// Sets the assembly version.
msBuildSettings.WithProperty( "Version", version )
.WithProperty( "AssemblyVersion", version )
.SetMaxCpuCount( System.Environment.ProcessorCount )
.WithProperty( "FileVersion", version );
Task( buildTarget )
.Does(
() =>
{
Build( "Debug" );
}
).Description( "Builds the Debug target." );
Task( buildReleaseTarget )
.Does(
() =>
{
Build( "Release" );
}
).Description( "Builds with the Release Configuration." );
void Build( string config )
{
msBuildSettings.SetConfiguration( config );
var settings = new DotNetBuildSettings
{
MSBuildSettings = msBuildSettings
};
DotNetBuild( sln.ToString(), settings );
}
Task( makeDistTarget )
.Does(
() =>
{
EnsureDirectoryExists( distFolder );
CleanDirectory( distFolder );
var settings = new DotNetPublishSettings
{
OutputDirectory = distFolder,
Configuration = "Release",
SelfContained = true,
Runtime = "win-x64",
MSBuildSettings = msBuildSettings
};
DotNetPublish( "./SshRunAs/SshRunAs.csproj", settings );
CopyFile( "./LICENSE_1_0.txt", System.IO.Path.Combine( distFolder.ToString(), "License.txt" ) );
CopyFileToDirectory( "./Credits.md", distFolder );
CopyFileToDirectory( "./Readme.md", distFolder );
}
).Description( "Moves the files into directory so it can be distributed." ).
IsDependentOn( buildReleaseTarget );
Task( nugetPackTarget )
.Does(
() =>
{
var files = new List<NuSpecContent>(
GetFiles( System.IO.Path.Combine( distFolder.ToString(), "*.dll" ) )
.Select( file => new NuSpecContent { Source = file.ToString(), Target = "tools" } )
);
files.AddRange(
GetFiles( System.IO.Path.Combine( distFolder.ToString(), "*.pdb" ) )
.Select( file => new NuSpecContent { Source = file.ToString(), Target = "tools" } )
);
files.AddRange(
GetFiles( System.IO.Path.Combine( distFolder.ToString(), "*.exe" ) )
.Select( file => new NuSpecContent { Source = file.ToString(), Target = "tools" } )
);
files.Add(
new NuSpecContent
{
Source = System.IO.Path.Combine( distFolder.ToString(), "License.txt" ),
Target = "License.txt"
}
);
files.Add(
new NuSpecContent
{
Source = System.IO.Path.Combine( distFolder.ToString(), "Readme.md" ),
Target = "Readme.md"
}
);
files.Add(
new NuSpecContent
{
Source = System.IO.Path.Combine( distFolder.ToString(), "Credits.md" ),
Target = "Credits.md"
}
);
files.Add(
new NuSpecContent
{
Source = new FilePath( "../Assets/icon.png" ).ToString(),
Target = "icon.png"
}
);
var settings = new NuGetPackSettings
{
Id = "SshRunAs-Win-x64",
Version = version,
Title = "SshRunAs",
Authors = new string[] { "Seth Hendrick" },
Description = "Allows one to run commands via SSH automatically while specifying a username/password.",
Summary = "Allows one to run commands via SSH automatically while specifying a username/password.",
License = new NuSpecLicense
{
Type = "expression",
Value = "BSL-1.0"
},
ProjectUrl = new Uri( "https://github.com/xforever1313/SshRunAs" ),
RequireLicenseAcceptance = false,
Repository = new NuGetRepository
{
Type = "git",
Url = "https://github.com/xforever1313/SshRunAs.git"
},
Copyright = "Copyright (c) Seth Hendrick",
Tags = new string[] { "sshrunas", "ssh", "runas", "password", "sshpass", "windows", "xforever1313" },
Icon = "icon.png",
BasePath = distFolder,
OutputDirectory = distFolder,
Symbols = false,
NoPackageAnalysis = false,
Files = files
};
NuGetPack( settings );
}
).Description( "Builds the nuget package." )
.IsDependentOn( makeDistTarget );
Task( buildMsiTarget ).
Does(
() =>
{
EnsureDirectoryExists( msiWorkDir );
CleanDirectory( msiWorkDir );
FilePath wxsFile = msiWorkDir.CombineWithFilePath( "SshRunAs.wxs" );
// -------- Heat --------
Information( "Starting Heat" );
var heatSettings = new HeatSettings
{
ComponentGroupName = "SshRunAs", // -cg
Platform = "x64",
GenerateGuid = true, // -gg
SuppressFragments = true, // -sfrag
SuppressRegistry = true, // -sreg
SuppressVb6Com = true, // -svb6
Template = WiXTemplateType.Product,// -template product
Transform = wixDir.CombineWithFilePath( File( "SshRunAs.xslt" ) ).ToString(),
ToolPath = $@"C:\Program Files (x86)\WiX Toolset v{wixVersion}\bin\heat.exe"
};
WiXHeat(
distFolder,
wxsFile,
WiXHarvestType.Dir,
heatSettings
);
// -------- Candle --------
Information( "Starting Candle" );
var candleSettings = new CandleSettings
{
WorkingDirectory = msiWorkDir.ToString(),
ToolPath = $@"C:\Program Files (x86)\WiX Toolset v{wixVersion}\bin\candle.exe"
};
WiXCandle( wxsFile.ToString(), candleSettings );
// -------- Light --------
Information( "Starting Light" );
var lightSettings = new LightSettings
{
RawArguments = $"-ext WixUIExtension -cultures:en-us -b {distFolder.ToString()}",
OutputFile = msiPath,
ToolPath = $@"C:\Program Files (x86)\WiX Toolset v{wixVersion}\bin\light.exe"
};
FilePath wixObjFile = msiWorkDir.CombineWithFilePath( $"SshRunAs.wixobj" );
WiXLight( wixObjFile.ToString(), lightSettings );
// Create file hash of .MSI file.
FileHash hash = CalculateFileHash(
lightSettings.OutputFile,
HashAlgorithm.SHA256
);
string hashStr = hash.ToHex();
System.IO.File.WriteAllText(
msiShaFile.ToString(),
hashStr
);
Information( "Hash for " + lightSettings.OutputFile.GetFilename() + ": " + hashStr );
}
).Description( "Builds the Windows MSI. This requires WiX to be installed" )
.IsDependentOn( makeDistTarget );
var chocoTask = Task( chocoPackTarget )
.Does(
() =>
{
DirectoryPath chocoDir = installFolder.Combine( "Chocolatey" );
DirectoryPath outputDirectory = chocoDir.Combine( "bin" );
DirectoryPath workingDirectory = chocoDir.Combine( "obj" );
DirectoryPath toolsDirectory = workingDirectory.Combine( "tools" );
FilePath installScript = toolsDirectory.CombineWithFilePath( "chocolateyinstall.ps1" );
EnsureDirectoryExists( outputDirectory );
CleanDirectory( outputDirectory );
EnsureDirectoryExists( workingDirectory );
CleanDirectory( workingDirectory );
EnsureDirectoryExists( toolsDirectory );
// First, need to set the checksum of the MSI file.
string checksum = System.IO.File.ReadAllText( msiShaFile.ToString() ).Trim();
// Second, need to create the install.ps1 & uninstall/ps1 files.
string installPs1 =
$@"
$ErrorActionPreference = 'Stop';
$toolsDir = ""$(Split-Path -parent $MyInvocation.MyCommand.Definition)""
$fileLocation = Join-Path $toolsDir 'SshRunAs.msi'
$packageArgs = @{{
packageName = $env:ChocolateyPackageName
unzipLocation = $toolsDir
fileType = 'msi'
file = $fileLocation
softwareName = 'SshRunAs*'
checksum = '{checksum}'
checksumType= 'sha256'
# MSI
silentArgs = ""/qn /norestart /l*v `""$($env:TEMP)\$($packageName).$($env:chocolateyPackageVersion).MsiInstall.log`""""
validExitCodes= @(0, 3010, 1641)
}}
Install-ChocolateyPackage @packageArgs
";
System.IO.File.WriteAllText( installScript.ToString(), installPs1 );
CopyFileToDirectory(
msiPath,
toolsDirectory
);
List<ChocolateyNuSpecContent> files = new List<ChocolateyNuSpecContent>();
foreach( FilePath file in GetFiles( toolsDirectory.Combine( "*" ).ToString() ) )
{
files.Add(
new ChocolateyNuSpecContent
{
Source = file.ToString(),
Target = "tools"
}
);
}
files.Add(
new ChocolateyNuSpecContent
{
Source = File( "LICENSE_1_0.txt" ).ToString(),
Target = "License.txt"
}
);
files.Add(
new ChocolateyNuSpecContent
{
Source = chocoDir.CombineWithFilePath( File( "VERIFICATION.txt" ) ).ToString(),
Target = "VERIFICATION.txt"
}
);
string[] readmeContents = System.IO.File.ReadAllLines( "Readme.md" );
var description = new StringBuilder();
foreach( string line in readmeContents )
{
if( line.StartsWith( "## Usage" ) )
{
break;
}
description.AppendLine( line );
}
// With our checksum set, pack it!
var settings = new ChocolateyPackSettings
{
// Package Specific Section
Id = "sshrunas",
Version = version,
PackageSourceUrl = new Uri( "https://github.com/xforever1313/SshRunAs" ),
Owners = new string[] { "Seth Hendrick" },
// Software Specific Section
Title = "SshRunAs (Install)",
Authors = new string[] { "Seth Hendrick" },
ProjectUrl = new Uri( "https://github.com/xforever1313/SshRunAs" ),
Copyright = "Copyright © Seth Hendrick 2019-2024",
LicenseUrl = new Uri( "https://raw.githubusercontent.com/xforever1313/SshRunAs/master/LICENSE_1_0.txt" ),
RequireLicenseAcceptance = false,
ProjectSourceUrl = new Uri( "https://github.com/xforever1313/SshRunAs" ),
BugTrackerUrl = new Uri( "https://github.com/xforever1313/SshRunAs/issues" ),
Tags = new string[] { "sshrunas", "ssh", "runas", "password", "sshpass", "windows", "admin" },
Summary = "Run a process via SSH and a user can pass in a username/password.",
Description = description.ToString(),
Files = files,
IconUrl = new Uri( "https://rawcdn.githack.com/xforever1313/SshRunAs/b92587025b0ce210a7de335d931960073496de36/Assets/icon.png" ),
// Cake-Related Section
OutputDirectory = outputDirectory,
WorkingDirectory = workingDirectory
};
ChocolateyPack(
settings
);
}
).Description( "Creates the Chocolatey Package" );
if( noBuild == false )
{
chocoTask.IsDependentOn( buildMsiTarget );
}
Task( "all" )
.IsDependentOn( chocoPackTarget )
.IsDependentOn( nugetPackTarget );
RunTarget( target );