<commit_before>#!/bin/bash

set -e

dev_tools_dir=`xcode-select -print-path`
tmp_dir="/tmp"
build_dir="$tmp_dir/screen_shooter"
bundle_dir="$build_dir/app.app"
trace_results_dir="$build_dir/traces"
destination="$1"

main() {
  xcodebuild_command clean build TARGETED_DEVICE_FAMILY=1

  shoot "iPhone (Retina 3.5-inch)" "en"
  shoot "iPhone (Retina 3.5-inch)" "fr"
  shoot "iPhone (Retina 3.5-inch)" "ja"
  shoot "iPhone (Retina 4-inch)" "en"
  shoot "iPhone (Retina 4-inch)" "fr"
  shoot "iPhone (Retina 4-inch)" "ja"

  xcodebuild_command build TARGETED_DEVICE_FAMILY=2

  shoot "iPad (Retina)" "en"
  shoot "iPad (Retina)" "fr"
  shoot "iPad (Retina)" "ja"

  close_sim
}

shoot() {
  clean_trace_results_dir
  choose_sim_device "$1"
  choose_sim_language "$2"
  take_screenshots
  copy_screenshots
}

# TODO: provide instructions on modifying this for specific projects
xcodebuild_command() {
  xcrun xcodebuild -sdk iphonesimulator \
    CONFIGURATION_BUILD_DIR=$build_dir \
    PRODUCT_NAME=app \
    $*
}

clean_trace_results_dir() {
  rm -rf "$trace_results_dir"
  mkdir -p "$trace_results_dir"
}

choose_sim_language() {
  echo "Localizing for $1"

  pref_file=`ls ~/Library/Application\ Support/iPhone\ Simulator/6.0/Library/Preferences/.GlobalPreferences.plist`
  close_sim
  /usr/libexec/PlistBuddy "$pref_file" -c "Delete :AppleLanguages"
  /usr/libexec/PlistBuddy "$pref_file" -c "Add :AppleLanguages array"
  /usr/libexec/PlistBuddy "$pref_file" -c "Add :AppleLanguages:0 string '$1'"
}

choose_sim_device() {
  # This is just a prettier function name around the Applescript
  bin/choose_sim_device "$*"
}

close_sim() {
  osascript -e 'tell application "iPhone Simulator" to quit'
}

take_screenshots() {
  tracetemplate="$dev_tools_dir/../Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate"

  # Check out the `unix_instruments` script to see why we need it
  ./bin/unix_instruments \
    -D "$trace_results_dir/trace" \
    -t "$tracetemplate" \
    $bundle_dir \
    -e UIARESULTSPATH "$trace_results_dir" \
    -e UIASCRIPT screenshots.js \
    $*
}

copy_screenshots() {
  mkdir -p $destination
  cp $trace_results_dir/Run\ 1/*.png $destination
}

main
<commit_msg>Added some comments for more details<commit_after>#!/bin/bash

set -e

main() {
  xcode clean build TARGETED_DEVICE_FAMILY=1

  shoot "iPhone (Retina 3.5-inch)" "en"
  shoot "iPhone (Retina 3.5-inch)" "fr"
  shoot "iPhone (Retina 3.5-inch)" "ja"

  shoot "iPhone (Retina 4-inch)" "en"
  shoot "iPhone (Retina 4-inch)" "fr"
  shoot "iPhone (Retina 4-inch)" "ja"

  # We to build again with the iPad device family because otherwise Instruments
  # will build and run for iPhone even though the simulator says otherwise.
  xcode build TARGETED_DEVICE_FAMILY=2

  shoot "iPad (Retina)" "en"
  shoot "iPad (Retina)" "fr"
  shoot "iPad (Retina)" "ja"

  close_sim
}

# Global variables to keep track of where everything goes
dev_tools_dir=`xcode-select -print-path`
tmp_dir="/tmp"
build_dir="$tmp_dir/screen_shooter"
bundle_dir="$build_dir/app.app"
trace_results_dir="$build_dir/traces"
destination="$1"

shoot() {
  # Takes the sim device type and a language code, runs the screenshot script,
  # and then copies over the screenshots to the destination

  clean_trace_results_dir
  bin/choose_sim_device "$1"
  choose_sim_language "$2"
  take_screenshots
  copy_screenshots
}

xcode() {
  # A wrapper around `xcodebuild` that tells it to build the app in the temp
  # directory. If your app uses workspaces or special schemes, you'll need to
  # specify them here.
  #
  # Use `man xcodebuild` for more information on how to build your project.

  xcodebuild -sdk iphonesimulator \
    CONFIGURATION_BUILD_DIR=$build_dir \
    PRODUCT_NAME=app \
    $*
}

clean_trace_results_dir() {
  # Removes the trace results directory. We need to do this because Instruments
  # keeps appending new trace runs and it's simpler for us to always assume
  # there's just one run recorded where we look for screenshots.

  rm -rf "$trace_results_dir"
  mkdir -p "$trace_results_dir"
}

choose_sim_language() {
  # Alters the global preference file for every simulator type and moves the
  # chosen language identifier to the top.

  echo "Localizing for $1"

  pref_file=`ls ~/Library/Application\ Support/iPhone\ Simulator/6.0/Library/Preferences/.GlobalPreferences.plist`

  close_sim

  /usr/libexec/PlistBuddy "$pref_file" -c "Delete :AppleLanguages"
  /usr/libexec/PlistBuddy "$pref_file" -c "Add :AppleLanguages array"
  /usr/libexec/PlistBuddy "$pref_file" -c "Add :AppleLanguages:0 string '$1'"
}

close_sim() {
  # Closes the simulator. We need to do this after altering the languages and
  # when we want to clean up at the end.

  osascript -e 'tell application "iPhone Simulator" to quit'
}

take_screenshots() {
  # Runs the UI Automation JavaScript file that actually takes the screenshots.

  tracetemplate="$dev_tools_dir/../Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate"

  # Check out the `unix_instruments` script to see why we need this wrapper.
  bin/unix_instruments \
    -D "$trace_results_dir/trace" \
    -t "$tracetemplate" \
    $bundle_dir \
    -e UIARESULTSPATH "$trace_results_dir" \
    -e UIASCRIPT screenshots.js \
    $*
}

copy_screenshots() {
  # Since we're always clearing out the trace results before every run, we can
  # assume that any screenshots were saved in the "Run 1" directory. Copy them
  # to the destination!

  mkdir -p $destination
  cp $trace_results_dir/Run\ 1/*.png $destination
}

main

{'fraction_non_alphanumeric': 0.10707964601769912, 'fraction_numerical': 0.007256637168141593, 'mean_word_length': 4.141947224749773, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5069038', 'n_tokens_mistral': 2050, 'n_tokens_neox': 1915, 'n_words': 545}