Skip to content

Commit

Permalink
fix(generation): increased compatibility and robustness for font loca…
Browse files Browse the repository at this point in the history
…tion (#144)
  • Loading branch information
gumptious committed Nov 14, 2019
1 parent 578ea72 commit cae86f3
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 6 deletions.
5 changes: 5 additions & 0 deletions packages/generation/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
11 changes: 8 additions & 3 deletions packages/generation/src/typography.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions packages/generation/test/typography.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions packages/generation/tools/mac-fonts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
1 change: 1 addition & 0 deletions packages/generation/tools/mac-fonts/Brewfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
brew "upx"
15 changes: 12 additions & 3 deletions packages/generation/tools/mac-fonts/Makefile
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions packages/generation/tools/mac-fonts/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// swift-tools-version:4.2

import PackageDescription

let package = Package(
name: "MacFonts",
dependencies: [],
targets: [
.target(
name: "MacFonts",
dependencies: []),
]
)
5 changes: 5 additions & 0 deletions packages/generation/tools/mac-fonts/README.md
Original file line number Diff line number Diff line change
@@ -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/)
Binary file modified packages/generation/tools/mac-fonts/bin/MacFonts
Binary file not shown.

0 comments on commit cae86f3

Please sign in to comment.