Last active
September 9, 2018 15:10
-
-
Save ilyapuchka/4c95cbd8f0206a55d724e1271acc70cb to your computer and use it in GitHub Desktop.
Generate Xcode snippets from XCTest_Gherkin step definitions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Usage: | |
# chmod 755 create_xcode_snippets.rb | |
# ./create_xcode_snippets.rb --project_path "path-to-project-file.xcodeproj" | |
require 'xcodeproj' | |
$project_path = File.expand_path(ARGV.find.with_index { |item, index| ARGV[index - 1] == "--project_path" }) | |
$snippets_path = File.expand_path("~/Library/Developer/Xcode/UserData/CodeSnippets/") | |
$snippet_prefix = "STEP-" | |
def clear_snippets | |
Dir.new($snippets_path).each do |file| | |
File.delete(File.join($snippets_path, file)) if file.start_with?($snippet_prefix) | |
end | |
end | |
def parse_steps | |
project = Xcodeproj::Project.open($project_path) | |
project.targets.each do |target| | |
step_files = target.source_build_phase.files.to_a.map do |pbx_build_file| | |
pbx_build_file.file_ref.real_path.to_s | |
end.select do |path| | |
path.end_with?("Steps.swift") | |
end.select do |path| | |
File.exists?(path) | |
end | |
step_files.each do |path| | |
File.open(path).each do |line| | |
line.scan(/\s*step\("(.+)"\)[\s\n]+{/).each do |match| | |
uuid = `uuidgen`.strip | |
yield(match[0], uuid) | |
end | |
end | |
end | |
end | |
end | |
def generate_snippet(expr, uuid) | |
%Q( | |
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<dict> | |
<key>IDECodeSnippetCompletionPrefix</key> | |
<string>step #{expr}</string> | |
<key>IDECodeSnippetCompletionScopes</key> | |
<array> | |
<string>CodeExpression</string> | |
</array> | |
<key>IDECodeSnippetContents</key> | |
<string>"#{expr}"</string> | |
<key>IDECodeSnippetIdentifier</key> | |
<string>STEP-#{uuid}</string> | |
<key>IDECodeSnippetLanguage</key> | |
<string>Xcode.SourceCodeLanguage.Swift</string> | |
<key>IDECodeSnippetTitle</key> | |
<string>#{expr}</string> | |
<key>IDECodeSnippetUserSnippet</key> | |
<true/> | |
<key>IDECodeSnippetVersion</key> | |
<integer>2</integer> | |
</dict> | |
</plist> | |
) | |
end | |
clear_snippets | |
parse_steps do |step, uuid| | |
snippet = generate_snippet(step, uuid) | |
File.open(File.join($snippets_path, "#{$snippet_prefix}#{uuid}.codesnippet"), "w") { |f| f.write(snippet) } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment