Skip to content

Commit 0b606c4

Browse files
authored
Makefile and build scripts (#289)
- New Makefile with common commands - Split build.bat into multiple smaller Batch scripts (allow to be run individually or via make) - Makefile step to check Batch scripts - Fix compile tests in F# and VB unit tests
1 parent 1bb0cc8 commit 0b606c4

File tree

11 files changed

+242
-52
lines changed

11 files changed

+242
-52
lines changed

EasyPost.Tests.FSharp/FSharpCompileTest.fs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33

44
namespace EasyPost.Tests.FSharp
55

6-
open System
76
open EasyPost
87
open Microsoft.VisualStudio.TestTools.UnitTesting
98

109
[<TestClass>]
11-
type FSharpCompileTest () =
10+
type FSharpCompileTest() =
1211
[<TestMethod>]
1312
member this.TestCompile() =
13+
let address = new Address()
1414
// The assert doesn't really do anything, but as long as this test can run, then the code is compiling correctly.
15-
let result = Assert.ThrowsException<ClientNotConfigured>(fun() -> Console.Write(CarrierType.All()); new obj())
16-
Assert.IsNotNull(result)
15+
Assert.IsNotNull(address)

EasyPost.Tests.VB/VbCompileTest.vb

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,9 @@ Imports Microsoft.VisualStudio.TestTools.UnitTesting
66
Public Class VbCompileTest
77
<TestMethod>
88
Public Sub TestCompile()
9-
'API key is not set, so calling CarrierType.All() will produce an error. But if this runs, then the code compiled properly.
10-
Assert.ThrowsException(Of ClientNotConfigured)(Function() CarrierType.All())
11-
End Sub
12-
13-
<TestMethod>
14-
Public Sub TestAddress()
15-
Dim addressData As New Dictionary(Of String, Object)()
16-
17-
addressData.Add("name", "John Smith")
18-
addressData.Add("street1", "123 Main St")
19-
addressData.Add("city", "San Francisco")
20-
addressData.Add("state", "CA")
21-
addressData.Add("zip", "94107")
22-
addressData.Add("country", "US")
9+
Dim address = New Address()
2310

24-
'Without an API key, this will throw an error. But as long as it's a ClientNotConfigured exception, it's a success.
25-
Assert.ThrowsException(Of ClientNotConfigured)(Function() Address.Create(addressData))
11+
'Do not need to actually assert anything here. If it runs, it means it compiled successfully.
12+
Assert.IsNotNull(address)
2613
End Sub
2714
End Class

Makefile

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## help - Display help about make targets for this Makefile
2+
help:
3+
@cat Makefile | grep '^## ' --color=never | cut -c4- | sed -e "`printf 's/ - /\t- /;'`" | column -s "`printf '\t'`" -t
4+
5+
## release - Build, sign and package the project for distribution, signing with the provided certificate (Windows only)
6+
# @parameters:
7+
# cert= - The certificate to use for signing the built assets.
8+
# pass= - The password for the certificate.
9+
release:
10+
scripts\build_release_nuget.bat EasyPost ${cert} ${pass} EasyPost Release
11+
12+
## build-dev - Build the project in Debug mode
13+
build-dev:
14+
dotnet msbuild -property:Configuration="Debug" -target:Rebuild -restore
15+
16+
## build - Build the project in Release mode
17+
build:
18+
dotnet msbuild -property:Configuration="Release" -target:Rebuild -restore
19+
20+
## install-cert - Install the PFX certificate to your system (Windows only)
21+
# @parameters:
22+
# cert= - The certificate to use for signing the built assets.
23+
# pass= - The password for the certificate.
24+
install-cert:
25+
scripts\install_cert.bat ${cert} ${pass}
26+
27+
## sign - Sign all generated DLLs and NuGet packages with the provided certificate (Windows only)
28+
# @parameters:
29+
# cert= - The certificate to use for signing the built assets.
30+
# pass= - The password for the certificate.
31+
sign:
32+
install-cert cert=${cert} pass=${pass}
33+
scripts\sign_assemblies.bat ${cert} ${pass} EasyPost
34+
35+
## clean - Clean the project
36+
clean:
37+
dotnet clean
38+
39+
## restore - Restore the project
40+
restore:
41+
dotnet restore
42+
43+
## lint - Lint the project
44+
lint:
45+
dotnet format
46+
47+
## lint-check - Check lint issues in the project (does not make any changes)
48+
lint-check:
49+
dotnet format --verify-no-changes
50+
51+
## test - Test the project
52+
test:
53+
dotnet test
54+
55+
## lint-scripts - Lint and validate the Batch scripts (Windows only)
56+
lint-scripts:
57+
scripts\lint_scripts.bat
58+
59+
.PHONY: help release build-dev build install-cert sign clean restore lint lint-check test lint-scripts

scripts/build_project.bat

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
:: This script will restore and build a .NET project in a specified mode and platform.
2+
3+
:: Requirements:
4+
:: - dotnet is installed on the machine and is accessible everywhere (added to PATH) (might be in C:\Program Files\dotnet)
5+
6+
@ECHO OFF
7+
8+
:: Parse command line arguments
9+
SET buildMode=%1
10+
11+
:: Restore dependencies and build solution
12+
@ECHO:
13+
@ECHO Restoring and building project...
14+
@ECHO %buildMode%
15+
dotnet msbuild -property:Configuration="%buildMode%" -target:Rebuild -restore || GOTO :commandFailed
16+
17+
:commandFailed
18+
@ECHO Command failed.
19+
GOTO :exitWithError
20+
21+
:exitWithError
22+
@ECHO Exiting...
23+
EXIT /B 1

build.bat renamed to scripts/build_release_nuget.bat

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,54 +13,32 @@
1313
SET projectName=%1
1414
SET certFile=%2
1515
SET certPass=%3
16-
17-
:: Constants
18-
SET containerName=EasyPost
19-
SET buildMode="Release"
20-
SET buildPlatform="Any CPU"
16+
SET containerName=%4
17+
SET buildMode=%5
2118

2219
:: Delete old files
23-
@ECHO:
24-
@ECHO Cleaning old files...
25-
@RD /S /Q lib
26-
DEL /S /Q /F *.nupkg
20+
CALL "scripts\delete_old_assemblies.bat"
2721

2822
:: Install certificate (needed to automate signing later on)
29-
@ECHO:
30-
@ECHO (Re-)Installing certificate to system...
31-
sn -d "%containerName%"
32-
SnInstallPfx "%certFile%" "%certPass%" "%containerName%" || GOTO :commandFailed
23+
CALL "scripts\install_cert.bat" %certFile% %certPass% %containerName% || GOTO :commandFailed
3324

3425
:: Restore dependencies and build solution
35-
@ECHO:
36-
@ECHO Restoring and building project...
37-
dotnet msbuild -property:Configuration="%buildMode%" -property:Platform="%buildPlatform" -target:Rebuild -restore || GOTO :commandFailed
26+
CALL "scripts\build_project.bat" %buildMode% || GOTO :commandFailed
3827

3928
:: Sign the DLLs
40-
@ECHO:
41-
@ECHO Signing DLLs with certificate...
42-
FOR /R lib %%F IN (*.dll) DO (
43-
REM We need to run the DLLs through both sn.exe and signtool to get complete the signing process
44-
sn -Rca "%%F" "%containerName%" || GOTO :commandFailed
45-
signtool sign /f "%certFile%" /p "%certPass%" /v /tr http://timestamp.digicert.com?alg=sha256 /td SHA256 /fd SHA256 "%%F" || GOTO :commandFailed
46-
)
29+
CALL "scripts\sign_dlls.bat" %certFile% %certPass% %containerName% || GOTO :commandFailed
4730

4831
:: Package the DLLs in a NuGet package (will fail if DLLs are missing)
49-
@ECHO:
50-
@ECHO Generating NuGet package...
51-
nuget pack %projectName%.nuspec || GOTO :commandFailed
32+
CALL "scripts\pack_nuget.bat" %projectName% || GOTO :commandFailed
5233

5334
:: Sign the NuGet package
54-
@ECHO:
55-
@ECHO Signing NuGet package with certificate...
56-
:: Should only be one .nupkg file at this point, since we deleted the old ones
35+
CALL "scripts\sign_nuget.bat" %certFile% %certPass% || GOTO :commandFailed
5736
SET nugetFileName=
5837
FOR /R %%F IN (*.nupkg) DO (
5938
SET nugetFileName="%%F"
60-
nuget sign "%%F" -Timestamper http://timestamp.digicert.com -CertificatePath "%certFile%" -CertificatePassword "%certPass%" || GOTO :commandFailed
6139
)
6240
IF [%nugetFileName%]==[] (
63-
ECHO Could not find NuGet package to sign.
41+
ECHO Could not find NuGet package.
6442
GOTO :exitWithError
6543
)
6644

@@ -77,7 +55,7 @@ GOTO :eof
7755
GOTO :exitWithError
7856

7957
:commandFailed
80-
@ECHO Command failed.
58+
@ECHO Step failed.
8159
GOTO :exitWithError
8260

8361
:exitWithError

scripts/delete_old_assemblies.bat

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
:: This script will delete any DLLs and NuGet packages
2+
3+
@ECHO OFF
4+
5+
:: Delete old files
6+
@ECHO:
7+
@ECHO Cleaning old files...
8+
@RD /S /Q lib
9+
DEL /S /Q /F *.nupkg

scripts/install_cert.bat

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
:: This script will install a provided PFX certificate to the system.
2+
3+
:: Requirements:
4+
:: - dotnet is installed on the machine and is accessible everywhere (added to PATH) (might be in C:\Program Files\dotnet)
5+
:: - SnInstallPfx (https://github.com/honzajscz/SnInstallPfx) is installed on the machine and is accessible everywhere (added to PATH)
6+
7+
@ECHO OFF
8+
9+
:: Parse command line arguments
10+
SET certFile=%1
11+
SET certPass=%2
12+
SET containerName=%3
13+
14+
:: Install certificate
15+
@ECHO:
16+
@ECHO (Re-)Installing certificate to system...
17+
sn -d %containerName%
18+
SnInstallPfx %certFile% %certPass% %containerName% || GOTO :commandFailed
19+
20+
:commandFailed
21+
@ECHO Command failed.
22+
GOTO :exitWithError
23+
24+
:exitWithError
25+
@ECHO Exiting...
26+
EXIT /B 1

scripts/lint_scripts.bat

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
:: This script will install BatCodeCheck and run analysis on all Batch scripts in the "scripts" folder
2+
3+
4+
:: Requirements:
5+
:: - 7Zip is installed on the machine and is accessible everywhere (added to PATH)
6+
:: - Might need to run with elevated privileges
7+
8+
@ECHO OFF
9+
10+
:: Download BatCodeCheck
11+
curl https://www.robvanderwoude.com/files/batcodecheck.zip --output batcodecheck.zip || GOTO :commandFailed
12+
13+
:: Unzip BatCodeCheck
14+
7z x batcodecheck.zip -y || GOTO :commandFailed
15+
16+
:: Analyze all Batch files found in the scripts folder
17+
:: May complain about double-%, this is fine
18+
:: However, don't want to falsely throw an error, so this does not fail on error
19+
@ECHO:
20+
@ECHO Verifying Batch files...
21+
FOR /R scripts %%F IN (*.bat) DO (
22+
BatCodeCheck.exe "%%F" /LS
23+
)
24+
25+
:commandFailed
26+
@ECHO Command failed.
27+
GOTO :exitWithError
28+
29+
:exitWithError
30+
@ECHO Exiting...
31+
EXIT /B 1

scripts/pack_nuget.bat

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
:: This script will generate a NuGet package according the the project's .nuspec file.
2+
3+
:: Requirements:
4+
:: - NuGet is installed on the machine and is accessible everywhere (added to PATH)
5+
6+
@ECHO OFF
7+
8+
:: Generate a NuGet package (will fail if assemblies are missing)
9+
@ECHO:
10+
@ECHO Generating NuGet package...
11+
nuget pack %projectName%.nuspec || GOTO :commandFailed
12+
13+
:commandFailed
14+
@ECHO Command failed.
15+
GOTO :exitWithError
16+
17+
:exitWithError
18+
@ECHO Exiting...
19+
EXIT /B 1

scripts/sign_dlls.bat

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
:: This script will find and sign any DLLs with a provided PFX certificate
2+
3+
:: Requirements:
4+
:: - dotnet is installed on the machine and is accessible everywhere (added to PATH) (might be in C:\Program Files\dotnet)
5+
6+
@ECHO OFF
7+
8+
:: Parse command line arguments
9+
SET certFile=%1
10+
SET certPass=%2
11+
SET containerName=%3
12+
13+
:: Sign all DLLs found in the lib folder
14+
@ECHO:
15+
@ECHO Signing DLLs with certificate...
16+
FOR /R "lib" %%F IN (*.dll) DO (
17+
REM We need to run the DLLs through both sn.exe and signtool to get complete the signing process
18+
sn -Rca "%%F" %containerName% || GOTO :commandFailed
19+
signtool sign /f %certFile% /p %certPass% /v /tr http://timestamp.digicert.com?alg=sha256 /td SHA256 /fd SHA256 "%%F" || GOTO :commandFailed
20+
)
21+
22+
:commandFailed
23+
@ECHO Command failed.
24+
GOTO :exitWithError
25+
26+
:exitWithError
27+
@ECHO Exiting...
28+
EXIT /B 1

0 commit comments

Comments
 (0)