xcodesnippets 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/xcode_snippets.rb +1 -0
- data/lib/xcode_snippets/main.rb +28 -0
- data/lib/xcode_snippets/migrator.rb +42 -0
- data/lib/xcode_snippets/snippet.rb +19 -0
- data/lib/xcode_snippets/version.rb +11 -0
- data/spec/migrator_spec.rb +90 -0
- data/spec/spec_helper.rb +22 -0
- metadata +16 -13
data/lib/xcode_snippets.rb
CHANGED
data/lib/xcode_snippets/main.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'clamp/command'
|
2
|
+
require 'highline/import'
|
2
3
|
require 'xcode_snippets/snippet_manager'
|
4
|
+
require 'xcode_snippets/migrator'
|
3
5
|
|
4
6
|
module XcodeSnippets
|
5
7
|
class Main < Clamp::Command
|
@@ -36,6 +38,28 @@ module XcodeSnippets
|
|
36
38
|
end
|
37
39
|
end
|
38
40
|
|
41
|
+
subcommand "migrate", "Migrates existing Xcode snippets to a default bundle" do
|
42
|
+
option "--skip-confirm", :flag, "Skips confirmation before doing the migration"
|
43
|
+
|
44
|
+
def execute
|
45
|
+
snippets = migrator.migrate_snippets_from(XcodeSnippets.xcode_snippets_path, self)
|
46
|
+
migrator.clean_up
|
47
|
+
snippets
|
48
|
+
end
|
49
|
+
|
50
|
+
def migrator_should_proceed_with_migration?(migrator, snippets_to_migrate)
|
51
|
+
prompt = %Q{*********************************************************************
|
52
|
+
Warning: this will move #{snippets_to_migrate.count} code snippets under xcodesnippets control:
|
53
|
+
*********************************************************************
|
54
|
+
|
55
|
+
* #{snippets_to_migrate.map { |s| s.metadata.title}.join("\n* ")}
|
56
|
+
|
57
|
+
Continue? (y/n)
|
58
|
+
}
|
59
|
+
agree(prompt) unless skip_confirm?
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
39
63
|
private
|
40
64
|
|
41
65
|
def manager
|
@@ -45,6 +69,10 @@ module XcodeSnippets
|
|
45
69
|
def manifest
|
46
70
|
Manifest.load(XcodeSnippets.installation_path, XcodeSnippets.xcode_snippets_path)
|
47
71
|
end
|
72
|
+
|
73
|
+
def migrator
|
74
|
+
@migrator ||= XcodeSnippets::Migrator.new(manager)
|
75
|
+
end
|
48
76
|
|
49
77
|
end
|
50
78
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module XcodeSnippets
|
2
|
+
class Migrator
|
3
|
+
def initialize(snippet_manager)
|
4
|
+
@snippet_manager = snippet_manager
|
5
|
+
end
|
6
|
+
|
7
|
+
def migrate_snippets_from(path, confirmation_delegate = nil)
|
8
|
+
make_temp_dir(path)
|
9
|
+
snippet_files = Dir[File.join(path, "*.codesnippet")]
|
10
|
+
|
11
|
+
snippets = snippet_files.map do |snippet_path|
|
12
|
+
XcodeSnippets::Snippet.new(snippet_path)
|
13
|
+
end
|
14
|
+
|
15
|
+
if confirmation_delegate && confirmation_delegate.respond_to?(:migrator_should_proceed_with_migration?)
|
16
|
+
return unless confirmation_delegate.migrator_should_proceed_with_migration?(self, snippets)
|
17
|
+
end
|
18
|
+
|
19
|
+
temp_paths = snippets.map do |snippet|
|
20
|
+
File.join(@tmp_dir, "#{snippet.metadata.title}.codesnippet").tap do |temp_path|
|
21
|
+
FileUtils.mv(snippet.path, temp_path)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
@snippet_manager.install_snippets_from_paths(temp_paths)
|
26
|
+
end
|
27
|
+
|
28
|
+
def clean_up
|
29
|
+
FileUtils.rm_rf(@tmp_dir) if @tmp_dir
|
30
|
+
@tmp_dir = nil
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def make_temp_dir(path)
|
36
|
+
unless @tmp_dir
|
37
|
+
@tmp_dir = File.join(path, "migrator")
|
38
|
+
FileUtils.mkdir_p(@tmp_dir)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -30,6 +30,10 @@ module XcodeSnippets
|
|
30
30
|
File.basename(@path)
|
31
31
|
end
|
32
32
|
|
33
|
+
def metadata
|
34
|
+
@metadata ||= MetaData.from_file(path)
|
35
|
+
end
|
36
|
+
|
33
37
|
def key
|
34
38
|
@bundle ? "#{@bundle.name}/#{name}" : name
|
35
39
|
end
|
@@ -41,5 +45,20 @@ module XcodeSnippets
|
|
41
45
|
def symlinked?
|
42
46
|
symlink && File.exist?(symlink)
|
43
47
|
end
|
48
|
+
|
49
|
+
class MetaData
|
50
|
+
def initialize(data)
|
51
|
+
@data = data
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.from_file(path)
|
55
|
+
raise "Could not parse metadata in file #{path}" unless File.exist?(path)
|
56
|
+
new(Plist.parse_xml(path))
|
57
|
+
end
|
58
|
+
|
59
|
+
def title
|
60
|
+
@data["IDECodeSnippetTitle"]
|
61
|
+
end
|
62
|
+
end
|
44
63
|
end
|
45
64
|
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), *%w[spec_helper])
|
2
|
+
|
3
|
+
describe "Migrator" do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@manager = mock("manager")
|
7
|
+
@migrator = XcodeSnippets::Migrator.new(@manager)
|
8
|
+
create_example_xcode_snippets_directory
|
9
|
+
end
|
10
|
+
|
11
|
+
context "migrating existing snippets" do
|
12
|
+
|
13
|
+
it "copies each snippet to a temporary directory with a filename that reflects the snippet name" do
|
14
|
+
@manager.stub(:install_snippets_from_paths)
|
15
|
+
@migrator.migrate_snippets_from(XCODE_SNIPPET_PATH)
|
16
|
+
file_at(temporary_snippet_path_for("appledoc class.codesnippet")).should exist
|
17
|
+
end
|
18
|
+
|
19
|
+
it "removes the original snippet" do
|
20
|
+
@manager.stub(:install_snippets_from_paths)
|
21
|
+
@migrator.migrate_snippets_from(XCODE_SNIPPET_PATH)
|
22
|
+
file_at(xcode_snippet_path).should_not exist
|
23
|
+
end
|
24
|
+
|
25
|
+
it "installs each snippet from it's temporary location" do
|
26
|
+
@manager.should_receive(:install_snippets_from_paths).with([temporary_snippet_path_for("appledoc class.codesnippet")])
|
27
|
+
@migrator.migrate_snippets_from(XCODE_SNIPPET_PATH)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "returns the created snippets" do
|
31
|
+
@manager.stub(:install_snippets_from_paths).and_return(["snippet"])
|
32
|
+
@migrator.migrate_snippets_from(XCODE_SNIPPET_PATH).should == ["snippet"]
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
context "migrating with a confirmation delegate" do
|
38
|
+
|
39
|
+
before do
|
40
|
+
@delegate = stub("confirmation-delegate")
|
41
|
+
end
|
42
|
+
|
43
|
+
it "migrates the snippets if the confirmation delegate returns true" do
|
44
|
+
@delegate.stub(:migrator_should_proceed_with_migration?).and_return(true)
|
45
|
+
@manager.should_receive(:install_snippets_from_paths)
|
46
|
+
@migrator.migrate_snippets_from(XCODE_SNIPPET_PATH, @delegate)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "migrates the snippets if the confirmation delegate returns false" do
|
50
|
+
@delegate.stub(:migrator_should_proceed_with_migration?).and_return(false)
|
51
|
+
@manager.should_receive(:install_snippets_from_paths).never
|
52
|
+
@migrator.migrate_snippets_from(XCODE_SNIPPET_PATH, @delegate)
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
context "cleaning up after migration" do
|
58
|
+
|
59
|
+
before do
|
60
|
+
@manager.stub(:install_snippets_from_paths)
|
61
|
+
@migrator.migrate_snippets_from(XCODE_SNIPPET_PATH)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "removes the temporary migrator directory" do
|
65
|
+
@migrator.clean_up
|
66
|
+
directory(File.join(XCODE_SNIPPET_PATH, "migrator")).should_not exist
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def create_example_xcode_snippets_directory
|
74
|
+
FileUtils.cp(example_snippet_path, xcode_snippet_path)
|
75
|
+
end
|
76
|
+
|
77
|
+
def temporary_snippet_path_for(name)
|
78
|
+
File.join(XCODE_SNIPPET_PATH, "migrator", name)
|
79
|
+
end
|
80
|
+
|
81
|
+
def example_snippet_path
|
82
|
+
File.join(FIXTURES_PATH, "example.codesnippet")
|
83
|
+
end
|
84
|
+
|
85
|
+
def xcode_snippet_path
|
86
|
+
@guid ||= UUIDTools::UUID.timestamp_create
|
87
|
+
File.join(XCODE_SNIPPET_PATH, "#{@guid}.codesnippet")
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -25,6 +25,28 @@ class FakeUUIDGenerator
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
+
class FileQuery
|
29
|
+
def initialize(path)
|
30
|
+
@path = path
|
31
|
+
end
|
32
|
+
|
33
|
+
def exist?
|
34
|
+
File.exist?(@path)
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_s
|
38
|
+
"<file at #{@path}>"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def file_at(path)
|
43
|
+
FileQuery.new(path)
|
44
|
+
end
|
45
|
+
|
46
|
+
def directory(path)
|
47
|
+
FileQuery.new(path)
|
48
|
+
end
|
49
|
+
|
28
50
|
def setup_testing_environment!
|
29
51
|
[SNIPPETS_PATH, XCODE_SNIPPET_PATH].each do |dir|
|
30
52
|
FileUtils.rm_rf(dir) && FileUtils.mkdir_p(dir)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xcodesnippets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -14,7 +14,7 @@ default_executable:
|
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: clamp
|
17
|
-
requirement: &
|
17
|
+
requirement: &2156053140 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ~>
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: 0.2.1
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *2156053140
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: uuidtools
|
28
|
-
requirement: &
|
28
|
+
requirement: &2156045060 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ~>
|
@@ -33,10 +33,10 @@ dependencies:
|
|
33
33
|
version: 2.1.2
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *2156045060
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: plist
|
39
|
-
requirement: &
|
39
|
+
requirement: &2156044380 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ~>
|
@@ -44,10 +44,10 @@ dependencies:
|
|
44
44
|
version: 3.1.0
|
45
45
|
type: :runtime
|
46
46
|
prerelease: false
|
47
|
-
version_requirements: *
|
47
|
+
version_requirements: *2156044380
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
name: rspec
|
50
|
-
requirement: &
|
50
|
+
requirement: &2156043980 !ruby/object:Gem::Requirement
|
51
51
|
none: false
|
52
52
|
requirements:
|
53
53
|
- - ! '>='
|
@@ -55,10 +55,10 @@ dependencies:
|
|
55
55
|
version: '0'
|
56
56
|
type: :development
|
57
57
|
prerelease: false
|
58
|
-
version_requirements: *
|
58
|
+
version_requirements: *2156043980
|
59
59
|
- !ruby/object:Gem::Dependency
|
60
60
|
name: cucumber
|
61
|
-
requirement: &
|
61
|
+
requirement: &2156043300 !ruby/object:Gem::Requirement
|
62
62
|
none: false
|
63
63
|
requirements:
|
64
64
|
- - ! '>='
|
@@ -66,10 +66,10 @@ dependencies:
|
|
66
66
|
version: '0'
|
67
67
|
type: :development
|
68
68
|
prerelease: false
|
69
|
-
version_requirements: *
|
69
|
+
version_requirements: *2156043300
|
70
70
|
- !ruby/object:Gem::Dependency
|
71
71
|
name: ruby-debug19
|
72
|
-
requirement: &
|
72
|
+
requirement: &2156042600 !ruby/object:Gem::Requirement
|
73
73
|
none: false
|
74
74
|
requirements:
|
75
75
|
- - ! '>='
|
@@ -77,7 +77,7 @@ dependencies:
|
|
77
77
|
version: '0'
|
78
78
|
type: :development
|
79
79
|
prerelease: false
|
80
|
-
version_requirements: *
|
80
|
+
version_requirements: *2156042600
|
81
81
|
description:
|
82
82
|
email: luke@lukeredpath.co.uk
|
83
83
|
executables:
|
@@ -90,13 +90,16 @@ files:
|
|
90
90
|
- README.md
|
91
91
|
- bin/xcodesnippets
|
92
92
|
- spec/bundle_spec.rb
|
93
|
+
- spec/migrator_spec.rb
|
93
94
|
- spec/snippet_manager_spec.rb
|
94
95
|
- spec/spec_helper.rb
|
95
96
|
- lib/xcode_snippets/bundle.rb
|
96
97
|
- lib/xcode_snippets/main.rb
|
97
98
|
- lib/xcode_snippets/manifest.rb
|
99
|
+
- lib/xcode_snippets/migrator.rb
|
98
100
|
- lib/xcode_snippets/snippet.rb
|
99
101
|
- lib/xcode_snippets/snippet_manager.rb
|
102
|
+
- lib/xcode_snippets/version.rb
|
100
103
|
- lib/xcode_snippets.rb
|
101
104
|
has_rdoc: true
|
102
105
|
homepage: http://lukeredpath.co.uk
|