Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Commit 8f1f21f

Browse files
committed
Initial Commit
0 parents  commit 8f1f21f

12 files changed

+247
-0
lines changed

.gitignore

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# General
2+
*.gem
3+
*.rbc
4+
/.config
5+
/coverage/
6+
/InstalledFiles
7+
/pkg/
8+
/spec/reports/
9+
/test/tmp/
10+
/test/version_tmp/
11+
/tmp/
12+
13+
# Documentation
14+
/.yardoc/
15+
/_yardoc/
16+
/doc/
17+
/rdoc/
18+
19+
# Environment
20+
/.bundle/
21+
/lib/bundler/man/
22+
23+
# Library
24+
Gemfile.lock
25+
.ruby-version
26+
.ruby-gemset
27+
28+
# RVM
29+
.rvmrc

Gemfile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source "https://rubygems.org"
2+
3+
gemspec

LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2015 Mattt Thompson (http://mattt.me/)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# xcodesnippet
2+
*A command-line interface for installing Xcode Snippets*
3+
4+
## Installation
5+
6+
```
7+
$ gem install xcodesnippet
8+
```
9+
10+
## Usage
11+
12+
#### Sourcefile.swift
13+
14+
```swift
15+
---
16+
title: "Hello, World!"
17+
summary: "Prints 'Hello World'"
18+
completion-scope: Function or Method
19+
---
20+
21+
println("Hello, World!")
22+
```
23+
24+
#### Terminal Command
25+
26+
```
27+
$ xcodesnippet install path/to/source.m
28+
```
29+
30+
---
31+
32+
## Creator
33+
34+
Mattt Thompson ([@mattt](https://twitter.com/mattt))
35+
36+
## License
37+
38+
xcodesnippet is released under an MIT license. See LICENSE for more information.

Rakefile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require "bundler/setup"
2+
3+
gemspec = eval(File.read("xcodesnippet.gemspec"))
4+
5+
task :build => "#{gemspec.full_name}.gem"
6+
7+
file "#{gemspec.full_name}.gem" => gemspec.files + ["xcodesnippet.gemspec"] do
8+
system "gem build xcodesnippet.gemspec"
9+
end

bin/xcodesnippet

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'commander/import'
4+
5+
$:.push File.expand_path("../../lib", __FILE__)
6+
require 'xcodesnippet'
7+
8+
HighLine.track_eof = false # Fix for built-in Ruby
9+
Signal.trap("INT") {} # Suppress backtrace when exiting command
10+
11+
program :version, XcodeSnippet::VERSION
12+
program :description, 'A command-line interface for installing Xcode Snippets'
13+
14+
program :help, 'Author', 'Mattt Thompson <m@mattt.me>'
15+
program :help, 'Website', 'https://github.com/mattt'
16+
program :help_formatter, :compact
17+
18+
default_command :help
19+
20+
require 'xcodesnippet/commands'

lib/xcodesnippet.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require 'xcodesnippet/snippet'
2+
require 'xcodesnippet/version'

lib/xcodesnippet/commands.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
$:.push File.expand_path('../', __FILE__)
2+
3+
require 'commands/install'

lib/xcodesnippet/commands/install.rb

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
require 'yaml'
2+
require 'yaml/front-matter'
3+
require 'fileutils'
4+
require 'securerandom'
5+
6+
USER_XCODE_SNIPPETS_DIRECTORY = "~/Library/Developer/Xcode/UserData/CodeSnippets/"
7+
8+
command :install do |c|
9+
c.syntax = 'codesnippet install [SNIPPET]'
10+
c.summary = 'Create and Install an Xcode Snippet'
11+
c.description = 'Creates and installs an Xcode .codesnippet file from an source file annotated with YAML front matter.'
12+
13+
c.example 'description', 'codesnippet install /path/to/code.source'
14+
15+
c.action do |args, options|
16+
say_error "Input file required" and abort unless @input_filepath = args.first
17+
say_error "Input file does not exist" and abort unless File.exist?(@input_filepath)
18+
19+
@content = File.read(@input_filepath)
20+
say_error "Input file is empty" and abort if @content.empty?
21+
22+
extract_front_matter!
23+
24+
@output_filepath = File.join(USER_XCODE_SNIPPETS_DIRECTORY, @snippet.identifier + ".codesnippet")
25+
begin
26+
FileUtils.mkdir_p(USER_XCODE_SNIPPETS_DIRECTORY)
27+
28+
File.open(@output_filepath, 'w') do |f|
29+
f.write @snippet.to_plist
30+
end
31+
rescue => error
32+
say_error "Error: #{error.message}" and abort
33+
end
34+
35+
say_ok "Installed Code Snippet"
36+
puts @output_filepath
37+
end
38+
end
39+
40+
private
41+
42+
def extract_front_matter!
43+
@snippet = XcodeSnippet::Snippet.new
44+
45+
front_matter, contents = YAML::FrontMatter.extract(@content)
46+
47+
if front_matter.empty?
48+
say_error "No YAML Front Matter Detected" and abort
49+
else
50+
@snippet.contents = contents.strip
51+
@snippet.completion_prefix = File.basename(@input_filepath, File.extname(@input_filepath))
52+
@snippet.language = case File.extname(@input_filepath)
53+
when ".swift" then "Xcode.SourceCodeLanguage.Swift"
54+
when ".m" then "Xcode.SourceCodeLanguage.Objective-C"
55+
when ".mm" then "Xcode.SourceCodeLanguage.Objective-C++"
56+
else ""
57+
end
58+
@snippet.title = front_matter["title"] || ""
59+
@snippet.summary = front_matter["summary"] || ""
60+
@snippet.completion_scopes = [front_matter["completion-scope"]] || front_matter["completion-scopes"] || "All"
61+
@snippet.identifier = SecureRandom.uuid().upcase
62+
@snippet.is_user_snippet = true
63+
@snippet.version = 0
64+
end
65+
end

lib/xcodesnippet/snippet.rb

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'plist'
2+
3+
module XcodeSnippet
4+
class Snippet
5+
attr_accessor :completion_prefix
6+
attr_accessor :completion_scopes
7+
attr_accessor :contents
8+
attr_accessor :identifier
9+
attr_accessor :is_user_snippet
10+
attr_accessor :language
11+
attr_accessor :summary
12+
attr_accessor :title
13+
attr_accessor :version
14+
15+
def to_plist
16+
{
17+
"IDECodeSnippetCompletionPrefix" => @completion_prefix,
18+
"IDECodeSnippetCompletionScopes" => @completion_scopes,
19+
"IDECodeSnippetContents" => @contents,
20+
"IDECodeSnippetIdentifier" => @identifier,
21+
"IDECodeSnippetLanguage" => @language,
22+
"IDECodeSnippetSummary" => @summary,
23+
"IDECodeSnippetTitle" => @title,
24+
"IDECodeSnippetUserSnippet" => @is_user_snippet,
25+
"IDECodeSnippetVersion" => @version
26+
}.to_plist
27+
end
28+
end
29+
end

lib/xcodesnippet/version.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module XcodeSnippet
2+
VERSION = "0.0.1"
3+
end

xcodesnippet.gemspec

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- encoding: utf-8 -*-
2+
$:.push File.expand_path("../lib", __FILE__)
3+
4+
require "xcodesnippet/version"
5+
6+
Gem::Specification.new do |s|
7+
s.name = "xcodesnippet"
8+
s.authors = ["Mattt Thompson"]
9+
s.email = "m@mattt.me"
10+
s.license = "MIT"
11+
s.homepage = "http://mattt.me"
12+
s.version = XcodeSnippet::VERSION
13+
s.platform = Gem::Platform::RUBY
14+
s.summary = "xcodesnippet"
15+
s.description = "A command-line interface for installing Xcode Snippets."
16+
17+
s.add_dependency "yaml-front-matter"
18+
s.add_dependency "plist"
19+
s.add_dependency "commander", "~> 4.1"
20+
21+
s.add_development_dependency "rake"
22+
23+
s.files = Dir["./**/*"].reject { |file| file =~ /\.\/(bin|log|pkg|script|spec|test|vendor)/ }
24+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
25+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
26+
s.require_paths = ["lib"]
27+
end

0 commit comments

Comments
 (0)