Integrate Vale with Xcode

Jeroen LeenartsJeroen Leenarts
3 min read

2023-04-10 22:55:00

This is an excerpt from an article about Vale I wrote for the Stream Blog.

To integrate Vale with Xcode we need to do some more work. There is not an Xcode plugin available at the moment allowing easy integration with Vale.But fortunately, the Vale tool itself is very flexible. Here’s how to integrate:

  1. After creating your Vale configuration, make sure it works on the commandline.

  2. This guide assumes you created your Vale styles configuration in the .styles folder of your repository containing your Xcode project.

  3. In the directory called .styles, create a file names: xcode-line.tmpl. The contents of this file should be:

    {{range .Files}} {{- $path := .Path -}} {{- range .Alerts -}} {{$path}}:{{.Line}}:{{index .Span 0}}: {{.Severity}}: {{.Check}}:{{.Message}} {{end -}} {{end -}}

  4. Next, make sure the directory containing your markdown files is a part of the Xcode project. Make sure it is not a part of any build target. Otherwise the directory will be included in any bundle output your project build might create.

  5. Create a script file called vale-linting.sh in the directory bin at the root of your project. a. Change the directory in your terminal at the root of your project. b. Run mkdir bin if the bin directory does not exist yet. c. Next, run touch bin/vale-linting.sh. d. Make sure your new script is executable by running: chmod +xu bin/vale-linting.sh. e. Edit the vale-linting.sh file with your text editor of choice and make sure the contents of the file are:

    if ! which vale >/dev/null; then if ! which brew >/dev/null; then # If it's not found, check the Homebrew location and update PATH when needed if test -e "/usr/local/bin/brew"; then # Default Homebrew location (pre M1 era) PATH="${PATH}:/usr/local/bin" elif test -e "/opt/homebrew/bin/brew"; then # Default Homebrew location (M1 era) PATH="${PATH}:/opt/homebrew/bin" else echo "warning: Homebrew not found at default location" fi fi fi if which vale >/dev/null; then vale --output=./.styles/xcode-line.tmpl docusaurus else echo "warning: Vale not installed, download from https://vale.sh" fi

    f. Finally, add a custom build script step to your build target.

    1. Click on the project in the navigator on the left side of the Xcode screen.

    2. In the target list, choose your app target, or whatever other target you want to add the Vale linting on.

    3. Select “Build phases” from the horizontal target specific tab bar.

    4. Click on the “+” icon and in the popup menu that appears select “New Run Script Phase”

    5. A new section will appear in the list of Build Phases.

    6. On your new Script build phase, give it a meaningful name by double clicking on the build phase’s title.

    7. Next, make sure the checkbox with “Based on dependency analysis” is disabled.

    8. Finally, in the script section, add the line ./bin/vale-lint.sh.

Stream’s Swift Chat SDK has a working setup based on the above.

0
Subscribe to my newsletter

Read articles from Jeroen Leenarts directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jeroen Leenarts
Jeroen Leenarts