Skip to content

Commit

Permalink
Add Windows to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gregcotten committed Nov 9, 2022
1 parent 1de3a3c commit 9d19a5e
Show file tree
Hide file tree
Showing 11 changed files with 2,179 additions and 129 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
26 changes: 22 additions & 4 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ on:
"Tests/**.js",
"Tests/package.json",
"Tests/package-lock.json",
".github/workflows/run_tests.yml",
]

jobs:
Expand Down Expand Up @@ -46,8 +47,9 @@ jobs:
needs: swift-format
strategy:
matrix:
os: [macos-12, ubuntu-latest]
node-version: ["18.x"]
os: [macos-12, ubuntu-latest, windows-latest]
node-version: ["19.0.1"]
node-headers-version: ["19.0.1"]
swift-version: ["5.7"]
runs-on: ${{ matrix.os }}
defaults:
Expand All @@ -57,15 +59,31 @@ jobs:
- uses: actions/checkout@v3
with:
ref: ${{ needs.swift-format.outputs.commitSHA }}
- name: Use Swift ${{ matrix.swift-version }}
- name: Install Swift ${{ matrix.swift-version }} on macOS/Linux
if: ${{ runner.os != 'Windows' }}
uses: swift-actions/setup-swift@v1
with:
swift-version: ${{ matrix.swift-version }}
- name: Use Node ${{ matrix.node-version }}
- name: Install Swift 5.7.1 on Windows
if: ${{ runner.os == 'Windows' }}
run: |
Install-Binary -Url "https://download.swift.org/swift-5.7.1-release/windows10/swift-5.7.1-RELEASE/swift-5.7.1-RELEASE-windows10.exe" -Name "installer.exe" -ArgumentList ("-q")
Add-Content $env:GITHUB_PATH "C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin"
Add-Content $env:GITHUB_PATH "C:\Program Files\swift\runtime-development\usr\bin"
Add-Content $env:GITHUB_PATH "C:\Program Files\swift\icu-69.1\usr\bin"
Add-Content $env:GITHUB_ENV "SDKROOT=C:\Library\Developer\Platforms\Windows.platform\Developer\SDKs\Windows.sdk"
- name: Install Node ${{ matrix.node-version }} on macOS/Linux
if: ${{ runner.os != 'Windows' }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: "npm"
cache-dependency-path: "Tests/package-lock.json"
- name: Install Node 19.0.0 on Windows
if: ${{ runner.os == 'Windows' }}
run: |
choco upgrade -y --force nodejs --version 19.0.0
choco upgrade -y --force nodejs.install --version 19.0.0
refreshenv
- name: Test
run: "npm install && npm test"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ DerivedData/
.swiftpm/config/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
node_headers/
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
},
"files.eol": "\n"
}
2 changes: 1 addition & 1 deletion Sources/Napoli/AnyValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public enum AnyValue: ValueConvertible, Codable {
case napi_undefined:
self = .undefined
default:
throw Error.unknownType(type.rawValue)
throw Error.unknownType(UInt32(type.rawValue))
}
}

Expand Down
5 changes: 3 additions & 2 deletions Sources/Napoli/ValueConvertible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,9 @@ protocol PrimitiveValueConvertible: ValueConvertible {

extension PrimitiveValueConvertible {
public init(_ env: napi_env, from: napi_value) throws {
self = Self.defaultValue
try Self.initWithValue(env, from, &self).throwIfError()
var result = Self.defaultValue
try Self.initWithValue(env, from, &result).throwIfError()
self = result
}

public func napiValue(_ env: napi_env) throws -> napi_value {
Expand Down
3 changes: 2 additions & 1 deletion Tests/.prettierrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"semi": false
"semi": false,
"endOfLine": "lf"
}
6 changes: 5 additions & 1 deletion Tests/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import PackageDescription

let dynamicLinkerSettings: [LinkerSetting] = [.unsafeFlags(["-Lnode_headers\\19.0.1\\x64", "-lnode"], .when(platforms: [.windows])),
.unsafeFlags(["-Xlinker", "-undefined", "-Xlinker", "dynamic_lookup"], .when(platforms: [.macOS, .linux]))]

let package = Package(
name: "NapoliTests",
platforms: [.macOS(.v10_15)],
Expand All @@ -16,6 +19,7 @@ let package = Package(
dependencies: [.product(name: "NAPIC", package: "Napoli")]),
.target(name: "NapoliTests",
dependencies: ["Trampoline",
.product(name: "Napoli", package: "Napoli")]),
.product(name: "Napoli", package: "Napoli")],
linkerSettings: dynamicLinkerSettings),
]
)
36 changes: 22 additions & 14 deletions Tests/bin/npm-postinstall.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
#!/usr/bin/env node

// check os
async function run() {
const process = require("process")
var os = require("os")
var platform = os.platform()

var swiftBuildCommand = ""
var moveLibraryCommand = ""
let nodeHeadersVersion = "19.0.1"
var downloadHeadersCommand = ""

const util = require("util")
const exec = util.promisify(require("node:child_process").exec)
var shell = ""

if (platform == "darwin" || platform == "linux") {
swiftBuildCommand =
"swift build -c release -Xlinker -undefined -Xlinker dynamic_lookup"
downloadHeadersCommand = `npx node-gyp install ${nodeHeadersVersion} --devdir=node_headers --ensure`
shell = "/bin/sh"
swiftBuildCommand = "swift build -c release"
var libExtension = platform == "darwin" ? ".dylib" : ".so"
moveLibraryCommand =
"mv .build/release/libNapoliTests" +
libExtension +
" .build/release/NapoliTests.node"
moveLibraryCommand = `mv .build/release/libNapoliTests${libExtension} .build/release/NapoliTests.node`
} else if (platform === "win32") {
shell = "C:\\Program Files\\PowerShell\\7\\pwsh.EXE"
downloadHeadersCommand = `npx.cmd node-gyp install ${nodeHeadersVersion} --arch=x64 --devdir=node_headers --ensure`
swiftBuildCommand = "swift.exe build -c release"
moveLibraryCommand =
"Move-Item -Force -Path .build\\release\\NapoliTests.dll -Destination .build\\release\\NapoliTests.node"
} else {
console.log("Unsupported platform: " + platform)
process.exit(1)
}

// run a shell script without exec
const util = require("util")
const exec = util.promisify(require("child_process").exec)

try {
await exec(swiftBuildCommand)
await exec(moveLibraryCommand)
console.log(`Downloading Node.js headers: \"${downloadHeadersCommand}\"`)
await exec(downloadHeadersCommand, { shell: shell })
console.log(`Build: \"${swiftBuildCommand}\"`)
await exec(swiftBuildCommand, { shell: shell })
console.log(`Renaming library to .node: \"${moveLibraryCommand}\"`)
await exec(moveLibraryCommand, { shell: shell })
} catch (e) {
console.error(e)
process.exit(1)
Expand Down
Loading

0 comments on commit 9d19a5e

Please sign in to comment.