diff --git a/packages/generation/.npmignore b/packages/generation/.npmignore index b14bd3891..0b8f75b7a 100644 --- a/packages/generation/.npmignore +++ b/packages/generation/.npmignore @@ -13,3 +13,8 @@ checkstyle-result.xml types/**/*.d.ts.map lib/**/*.js.map coverage/ +tools/*/Makefile +tools/*/Brewfile +tools/*/Package.swift +tools/*/.build/ +tools/*/Sources/ diff --git a/packages/generation/src/typography.ts b/packages/generation/src/typography.ts index efb350df3..905a7f91e 100644 --- a/packages/generation/src/typography.ts +++ b/packages/generation/src/typography.ts @@ -23,9 +23,14 @@ export const locateFont = async ( } if (isMacOS()) { - const candidates = JSON.parse(await execAsync(`./MacFonts '${fontFamily}'`, { - cwd: join(__dirname, '..', 'tools', 'mac-fonts', 'bin'), - })) as GeneratedFont[]; + const candidates: GeneratedFont[] = []; + try { + candidates.push(...JSON.parse(await execAsync(`./MacFonts '${fontFamily}'`, { + cwd: join(__dirname, '..', 'tools', 'mac-fonts', 'bin'), + }))); + } catch (error) { + // Noop. For some reason, we were not able to run the MacFonts binary on this machine. + } if (!candidates.length) { return; diff --git a/packages/generation/test/typography.test.ts b/packages/generation/test/typography.test.ts index bfaed6348..7f4b7f3c8 100644 --- a/packages/generation/test/typography.test.ts +++ b/packages/generation/test/typography.test.ts @@ -15,6 +15,9 @@ describe('typography', () => { mockExec.mockResolvedValue(JSON.stringify([])); expect(await locateFont('', {})).toBeUndefined(); + mockExec.mockRejectedValueOnce(true); + expect(await locateFont('', {})).toBeUndefined(); + const nullFont = {name: '', style: '', path: ''}; mockExec.mockResolvedValue(JSON.stringify([nullFont])); expect(await locateFont('nil', {style: 'normal'})).toEqual(nullFont); diff --git a/packages/generation/tools/mac-fonts/.gitignore b/packages/generation/tools/mac-fonts/.gitignore new file mode 100644 index 000000000..02c087533 --- /dev/null +++ b/packages/generation/tools/mac-fonts/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +/.build +/Packages +/*.xcodeproj diff --git a/packages/generation/tools/mac-fonts/Brewfile b/packages/generation/tools/mac-fonts/Brewfile new file mode 100644 index 000000000..7606d8813 --- /dev/null +++ b/packages/generation/tools/mac-fonts/Brewfile @@ -0,0 +1 @@ +brew "upx" \ No newline at end of file diff --git a/packages/generation/tools/mac-fonts/Makefile b/packages/generation/tools/mac-fonts/Makefile index a0fe31901..8270018d6 100644 --- a/packages/generation/tools/mac-fonts/Makefile +++ b/packages/generation/tools/mac-fonts/Makefile @@ -1,18 +1,27 @@ MODULE_NAME = MacFonts ARCH = x86_64 ROOT_DIR = $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) +TARGET_DIR = $(ROOT_DIR)/.build/release OUTPUT_DIR = $(ROOT_DIR)/bin SRC_DIR = $(ROOT_DIR)/src SWIFT_FILES = $(wildcard $(SRC_DIR)/*.swift) +SWIFT_VERSION_CHECK := $(strip $(shell TOOLCHAINS=swift swift --version | grep -c 4.2)) + +all: clean build test + build: + if [ $(SWIFT_VERSION_CHECK) != 1 ]; then echo Invalid Swift compiler version; exit 1; fi + brew bundle mkdir -p $(OUTPUT_DIR) - swiftc $(SWIFT_FILES) -o $(OUTPUT_DIR)/$(MODULE_NAME) + TOOLCHAINS=swift swift build --static-swift-stdlib -c release -Xswiftc -Osize + cp $(TARGET_DIR)/$(MODULE_NAME) $(OUTPUT_DIR)/$(MODULE_NAME) + upx $(OUTPUT_DIR)/$(MODULE_NAME) clean: - rm -r $(OUTPUT_DIR) + rm -rf $(OUTPUT_DIR) + rm -rf $(TARGET_DIR) test: - make $(OUTPUT_DIR)/$(MODULE_NAME) Helvetica diff --git a/packages/generation/tools/mac-fonts/Package.swift b/packages/generation/tools/mac-fonts/Package.swift new file mode 100644 index 000000000..5e1e48089 --- /dev/null +++ b/packages/generation/tools/mac-fonts/Package.swift @@ -0,0 +1,13 @@ +// swift-tools-version:4.2 + +import PackageDescription + +let package = Package( + name: "MacFonts", + dependencies: [], + targets: [ + .target( + name: "MacFonts", + dependencies: []), + ] +) diff --git a/packages/generation/tools/mac-fonts/README.md b/packages/generation/tools/mac-fonts/README.md index 3cf706ddf..e70398667 100644 --- a/packages/generation/tools/mac-fonts/README.md +++ b/packages/generation/tools/mac-fonts/README.md @@ -1,3 +1,8 @@ # MacFonts A macOS-only utility for locating fonts installed on the user's local system. + +## Requirements + + - [Homebrew](https://brew.sh) + - [Swift 4.2](https://swift.org/download/) diff --git a/packages/generation/tools/mac-fonts/src/MacFonts.swift b/packages/generation/tools/mac-fonts/Sources/MacFonts/main.swift similarity index 100% rename from packages/generation/tools/mac-fonts/src/MacFonts.swift rename to packages/generation/tools/mac-fonts/Sources/MacFonts/main.swift diff --git a/packages/generation/tools/mac-fonts/bin/MacFonts b/packages/generation/tools/mac-fonts/bin/MacFonts index 35e3ac085..079e0cc04 100755 Binary files a/packages/generation/tools/mac-fonts/bin/MacFonts and b/packages/generation/tools/mac-fonts/bin/MacFonts differ