diff --git a/EasyPost.Tests.FSharp/FSharpCompileTest.fs b/EasyPost.Tests.FSharp/FSharpCompileTest.fs index 9a76f303a..93375b3e8 100644 --- a/EasyPost.Tests.FSharp/FSharpCompileTest.fs +++ b/EasyPost.Tests.FSharp/FSharpCompileTest.fs @@ -3,14 +3,13 @@ namespace EasyPost.Tests.FSharp -open System open EasyPost open Microsoft.VisualStudio.TestTools.UnitTesting [] -type FSharpCompileTest () = +type FSharpCompileTest() = [] member this.TestCompile() = + let address = new Address() // The assert doesn't really do anything, but as long as this test can run, then the code is compiling correctly. - let result = Assert.ThrowsException(fun() -> Console.Write(CarrierType.All()); new obj()) - Assert.IsNotNull(result) + Assert.IsNotNull(address) diff --git a/EasyPost.Tests.VB/VbCompileTest.vb b/EasyPost.Tests.VB/VbCompileTest.vb index 2f9db75f5..eb0dbf431 100644 --- a/EasyPost.Tests.VB/VbCompileTest.vb +++ b/EasyPost.Tests.VB/VbCompileTest.vb @@ -6,22 +6,9 @@ Imports Microsoft.VisualStudio.TestTools.UnitTesting Public Class VbCompileTest Public Sub TestCompile() - 'API key is not set, so calling CarrierType.All() will produce an error. But if this runs, then the code compiled properly. - Assert.ThrowsException(Of ClientNotConfigured)(Function() CarrierType.All()) - End Sub - - - Public Sub TestAddress() - Dim addressData As New Dictionary(Of String, Object)() - - addressData.Add("name", "John Smith") - addressData.Add("street1", "123 Main St") - addressData.Add("city", "San Francisco") - addressData.Add("state", "CA") - addressData.Add("zip", "94107") - addressData.Add("country", "US") + Dim address = New Address() - 'Without an API key, this will throw an error. But as long as it's a ClientNotConfigured exception, it's a success. - Assert.ThrowsException(Of ClientNotConfigured)(Function() Address.Create(addressData)) + 'Do not need to actually assert anything here. If it runs, it means it compiled successfully. + Assert.IsNotNull(address) End Sub End Class diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..5ce70cfbf --- /dev/null +++ b/Makefile @@ -0,0 +1,59 @@ +## help - Display help about make targets for this Makefile +help: + @cat Makefile | grep '^## ' --color=never | cut -c4- | sed -e "`printf 's/ - /\t- /;'`" | column -s "`printf '\t'`" -t + +## release - Build, sign and package the project for distribution, signing with the provided certificate (Windows only) +# @parameters: +# cert= - The certificate to use for signing the built assets. +# pass= - The password for the certificate. +release: + scripts\build_release_nuget.bat EasyPost ${cert} ${pass} EasyPost Release + +## build-dev - Build the project in Debug mode +build-dev: + dotnet msbuild -property:Configuration="Debug" -target:Rebuild -restore + +## build - Build the project in Release mode +build: + dotnet msbuild -property:Configuration="Release" -target:Rebuild -restore + +## install-cert - Install the PFX certificate to your system (Windows only) +# @parameters: +# cert= - The certificate to use for signing the built assets. +# pass= - The password for the certificate. +install-cert: + scripts\install_cert.bat ${cert} ${pass} + +## sign - Sign all generated DLLs and NuGet packages with the provided certificate (Windows only) +# @parameters: +# cert= - The certificate to use for signing the built assets. +# pass= - The password for the certificate. +sign: + install-cert cert=${cert} pass=${pass} + scripts\sign_assemblies.bat ${cert} ${pass} EasyPost + +## clean - Clean the project +clean: + dotnet clean + +## restore - Restore the project +restore: + dotnet restore + +## lint - Lint the project +lint: + dotnet format + +## lint-check - Check lint issues in the project (does not make any changes) +lint-check: + dotnet format --verify-no-changes + +## test - Test the project +test: + dotnet test + +## lint-scripts - Lint and validate the Batch scripts (Windows only) +lint-scripts: + scripts\lint_scripts.bat + +.PHONY: help release build-dev build install-cert sign clean restore lint lint-check test lint-scripts diff --git a/scripts/build_project.bat b/scripts/build_project.bat new file mode 100644 index 000000000..5c2b228da --- /dev/null +++ b/scripts/build_project.bat @@ -0,0 +1,23 @@ +:: This script will restore and build a .NET project in a specified mode and platform. + +:: Requirements: +:: - dotnet is installed on the machine and is accessible everywhere (added to PATH) (might be in C:\Program Files\dotnet) + +@ECHO OFF + +:: Parse command line arguments +SET buildMode=%1 + +:: Restore dependencies and build solution +@ECHO: +@ECHO Restoring and building project... +@ECHO %buildMode% +dotnet msbuild -property:Configuration="%buildMode%" -target:Rebuild -restore || GOTO :commandFailed + +:commandFailed +@ECHO Command failed. +GOTO :exitWithError + +:exitWithError +@ECHO Exiting... +EXIT /B 1 diff --git a/build.bat b/scripts/build_release_nuget.bat similarity index 51% rename from build.bat rename to scripts/build_release_nuget.bat index 1d04836cd..6405d40cc 100644 --- a/build.bat +++ b/scripts/build_release_nuget.bat @@ -13,54 +13,32 @@ SET projectName=%1 SET certFile=%2 SET certPass=%3 - -:: Constants -SET containerName=EasyPost -SET buildMode="Release" -SET buildPlatform="Any CPU" +SET containerName=%4 +SET buildMode=%5 :: Delete old files -@ECHO: -@ECHO Cleaning old files... -@RD /S /Q lib -DEL /S /Q /F *.nupkg +CALL "scripts\delete_old_assemblies.bat" :: Install certificate (needed to automate signing later on) -@ECHO: -@ECHO (Re-)Installing certificate to system... -sn -d "%containerName%" -SnInstallPfx "%certFile%" "%certPass%" "%containerName%" || GOTO :commandFailed +CALL "scripts\install_cert.bat" %certFile% %certPass% %containerName% || GOTO :commandFailed :: Restore dependencies and build solution -@ECHO: -@ECHO Restoring and building project... -dotnet msbuild -property:Configuration="%buildMode%" -property:Platform="%buildPlatform" -target:Rebuild -restore || GOTO :commandFailed +CALL "scripts\build_project.bat" %buildMode% || GOTO :commandFailed :: Sign the DLLs -@ECHO: -@ECHO Signing DLLs with certificate... -FOR /R lib %%F IN (*.dll) DO ( - REM We need to run the DLLs through both sn.exe and signtool to get complete the signing process - sn -Rca "%%F" "%containerName%" || GOTO :commandFailed - signtool sign /f "%certFile%" /p "%certPass%" /v /tr http://timestamp.digicert.com?alg=sha256 /td SHA256 /fd SHA256 "%%F" || GOTO :commandFailed -) +CALL "scripts\sign_dlls.bat" %certFile% %certPass% %containerName% || GOTO :commandFailed :: Package the DLLs in a NuGet package (will fail if DLLs are missing) -@ECHO: -@ECHO Generating NuGet package... -nuget pack %projectName%.nuspec || GOTO :commandFailed +CALL "scripts\pack_nuget.bat" %projectName% || GOTO :commandFailed :: Sign the NuGet package -@ECHO: -@ECHO Signing NuGet package with certificate... -:: Should only be one .nupkg file at this point, since we deleted the old ones +CALL "scripts\sign_nuget.bat" %certFile% %certPass% || GOTO :commandFailed SET nugetFileName= FOR /R %%F IN (*.nupkg) DO ( SET nugetFileName="%%F" - nuget sign "%%F" -Timestamper http://timestamp.digicert.com -CertificatePath "%certFile%" -CertificatePassword "%certPass%" || GOTO :commandFailed ) IF [%nugetFileName%]==[] ( - ECHO Could not find NuGet package to sign. + ECHO Could not find NuGet package. GOTO :exitWithError ) @@ -77,7 +55,7 @@ GOTO :eof GOTO :exitWithError :commandFailed -@ECHO Command failed. +@ECHO Step failed. GOTO :exitWithError :exitWithError diff --git a/scripts/delete_old_assemblies.bat b/scripts/delete_old_assemblies.bat new file mode 100644 index 000000000..7fec9654c --- /dev/null +++ b/scripts/delete_old_assemblies.bat @@ -0,0 +1,9 @@ +:: This script will delete any DLLs and NuGet packages + +@ECHO OFF + +:: Delete old files +@ECHO: +@ECHO Cleaning old files... +@RD /S /Q lib +DEL /S /Q /F *.nupkg diff --git a/scripts/install_cert.bat b/scripts/install_cert.bat new file mode 100644 index 000000000..8896db041 --- /dev/null +++ b/scripts/install_cert.bat @@ -0,0 +1,26 @@ +:: This script will install a provided PFX certificate to the system. + +:: Requirements: +:: - dotnet is installed on the machine and is accessible everywhere (added to PATH) (might be in C:\Program Files\dotnet) +:: - SnInstallPfx (https://github.com/honzajscz/SnInstallPfx) is installed on the machine and is accessible everywhere (added to PATH) + +@ECHO OFF + +:: Parse command line arguments +SET certFile=%1 +SET certPass=%2 +SET containerName=%3 + +:: Install certificate +@ECHO: +@ECHO (Re-)Installing certificate to system... +sn -d %containerName% +SnInstallPfx %certFile% %certPass% %containerName% || GOTO :commandFailed + +:commandFailed +@ECHO Command failed. +GOTO :exitWithError + +:exitWithError +@ECHO Exiting... +EXIT /B 1 diff --git a/scripts/lint_scripts.bat b/scripts/lint_scripts.bat new file mode 100644 index 000000000..ecd901e46 --- /dev/null +++ b/scripts/lint_scripts.bat @@ -0,0 +1,31 @@ +:: This script will install BatCodeCheck and run analysis on all Batch scripts in the "scripts" folder + + +:: Requirements: +:: - 7Zip is installed on the machine and is accessible everywhere (added to PATH) +:: - Might need to run with elevated privileges + +@ECHO OFF + +:: Download BatCodeCheck +curl https://www.robvanderwoude.com/files/batcodecheck.zip --output batcodecheck.zip || GOTO :commandFailed + +:: Unzip BatCodeCheck +7z x batcodecheck.zip -y || GOTO :commandFailed + +:: Analyze all Batch files found in the scripts folder +:: May complain about double-%, this is fine +:: However, don't want to falsely throw an error, so this does not fail on error +@ECHO: +@ECHO Verifying Batch files... +FOR /R scripts %%F IN (*.bat) DO ( + BatCodeCheck.exe "%%F" /LS +) + +:commandFailed +@ECHO Command failed. +GOTO :exitWithError + +:exitWithError +@ECHO Exiting... +EXIT /B 1 diff --git a/scripts/pack_nuget.bat b/scripts/pack_nuget.bat new file mode 100644 index 000000000..06687c14d --- /dev/null +++ b/scripts/pack_nuget.bat @@ -0,0 +1,19 @@ +:: This script will generate a NuGet package according the the project's .nuspec file. + +:: Requirements: +:: - NuGet is installed on the machine and is accessible everywhere (added to PATH) + +@ECHO OFF + +:: Generate a NuGet package (will fail if assemblies are missing) +@ECHO: +@ECHO Generating NuGet package... +nuget pack %projectName%.nuspec || GOTO :commandFailed + +:commandFailed +@ECHO Command failed. +GOTO :exitWithError + +:exitWithError +@ECHO Exiting... +EXIT /B 1 diff --git a/scripts/sign_dlls.bat b/scripts/sign_dlls.bat new file mode 100644 index 000000000..86e13d8b0 --- /dev/null +++ b/scripts/sign_dlls.bat @@ -0,0 +1,28 @@ +:: This script will find and sign any DLLs with a provided PFX certificate + +:: Requirements: +:: - dotnet is installed on the machine and is accessible everywhere (added to PATH) (might be in C:\Program Files\dotnet) + +@ECHO OFF + +:: Parse command line arguments +SET certFile=%1 +SET certPass=%2 +SET containerName=%3 + +:: Sign all DLLs found in the lib folder +@ECHO: +@ECHO Signing DLLs with certificate... +FOR /R "lib" %%F IN (*.dll) DO ( + REM We need to run the DLLs through both sn.exe and signtool to get complete the signing process + sn -Rca "%%F" %containerName% || GOTO :commandFailed + signtool sign /f %certFile% /p %certPass% /v /tr http://timestamp.digicert.com?alg=sha256 /td SHA256 /fd SHA256 "%%F" || GOTO :commandFailed +) + +:commandFailed +@ECHO Command failed. +GOTO :exitWithError + +:exitWithError +@ECHO Exiting... +EXIT /B 1 diff --git a/scripts/sign_nuget.bat b/scripts/sign_nuget.bat new file mode 100644 index 000000000..3a71d6067 --- /dev/null +++ b/scripts/sign_nuget.bat @@ -0,0 +1,31 @@ +:: This script will find and sign any NuGet packages with a provided PFX certificate + +:: Requirements: +:: - NuGet is installed on the machine and is accessible everywhere (added to PATH) + +@ECHO OFF + +:: Parse command line arguments +SET certFile=%1 +SET certPass=%2 + +@ECHO %certFile% +@ECHO %certPass% + +:: Sign all NuGet packages found +@ECHO: +@ECHO Signing NuGet package with %certFile%... +:: Should only be one .nupkg file at this point, since we deleted the old ones +SET nugetFileName= +FOR /R %%F IN (*.nupkg) DO ( + SET nugetFileName="%%F" + nuget sign "%%F" -Timestamper http://timestamp.digicert.com -CertificatePath "%certFile%" -CertificatePassword "%certPass%" || GOTO :commandFailed +) + +:commandFailed +@ECHO Command failed. +GOTO :exitWithError + +:exitWithError +@ECHO Exiting... +EXIT /B 1