gist_directory 0.0.3 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -5
- data/bin/gist_directory +22 -13
- data/lib/gist_directory.rb +103 -47
- data/spec/gist_directory_spec.rb +14 -14
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0080a71489798fc02f6e2a875949d6b490dc5ab9
|
4
|
+
data.tar.gz: f2216c59729f4a82451b568214bc78a1299ac892
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d348e279867f93f0911e24fcac2a8526e9bafcc6946f91acad84180a037b16bed1cb8d414d0d775e468127a7368adedaf98b953272498e09833dc6b3a9ed935
|
7
|
+
data.tar.gz: b362c710f3887e208150d6fb9782aef57452ad0c01161c425bd0c243b4c98162e9dcb2ffe016b43bc03c4f7c04f75da227e6c02596d561139fdc1ce9592e8c54
|
data/README.md
CHANGED
@@ -1,18 +1,29 @@
|
|
1
1
|
# gist_directory
|
2
2
|
|
3
|
-
Create collections of public gists in a local directory
|
3
|
+
Create and maintain collections of public gists in a local directory
|
4
|
+
|
5
|
+
# Dependencies
|
6
|
+
|
7
|
+
```
|
8
|
+
npm install gulp-octodown-livereload
|
9
|
+
```
|
4
10
|
|
5
11
|
# Usage
|
6
12
|
|
7
|
-
Create a gist called `
|
13
|
+
Create a gist called `bacon.md` in a directory called `food`
|
8
14
|
under your home directory:
|
9
15
|
|
10
16
|
```shell
|
11
|
-
gist_directory create ~/food
|
17
|
+
gist_directory create ~/food/bacon.md
|
12
18
|
```
|
13
19
|
|
14
|
-
|
20
|
+
Edit in your favourite editor (creating if necessary):
|
15
21
|
|
16
22
|
```shell
|
17
|
-
gist_directory
|
23
|
+
gist_directory edit ~/food/bacon.md
|
18
24
|
```
|
25
|
+
|
26
|
+
View compiled Markdown in your browser (ith livereload):
|
27
|
+
|
28
|
+
```shell
|
29
|
+
gist_directory view ~/food/bacon.md
|
data/bin/gist_directory
CHANGED
@@ -6,15 +6,17 @@ require "gist_directory"
|
|
6
6
|
|
7
7
|
def usage
|
8
8
|
puts <<-EOT
|
9
|
-
#{$0} create
|
9
|
+
#{$0} create|edit|view [FILE_PATH]"
|
10
10
|
|
11
|
-
|
12
|
-
|
11
|
+
create:
|
12
|
+
* if the directory already contains a Gist, add the file
|
13
|
+
* otherwise, creates a new Gist containing the file
|
13
14
|
|
14
|
-
|
15
|
+
edit:
|
16
|
+
* opens the file for editing
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
+
view (Markdown only):
|
19
|
+
* opens the converted file in your browser and watches for changes.
|
18
20
|
EOT
|
19
21
|
end
|
20
22
|
|
@@ -24,10 +26,17 @@ if ARGV.size < 2
|
|
24
26
|
end
|
25
27
|
|
26
28
|
command = ARGV[0]
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
29
|
+
filename = ARGV[1]
|
30
|
+
|
31
|
+
gist = GistDirectory.new(filename: filename)
|
32
|
+
|
33
|
+
case command
|
34
|
+
when "create"
|
35
|
+
gist.create
|
36
|
+
when "edit"
|
37
|
+
gist.edit
|
38
|
+
when "view"
|
39
|
+
gist.view
|
40
|
+
else
|
41
|
+
raise "Unknown command '#{command}'"
|
42
|
+
end
|
data/lib/gist_directory.rb
CHANGED
@@ -1,50 +1,111 @@
|
|
1
1
|
require "gist"
|
2
2
|
require "git"
|
3
3
|
|
4
|
+
class GistFile
|
5
|
+
attr_reader :pathname
|
6
|
+
|
7
|
+
def initialize(pathname)
|
8
|
+
@pathname = File.expand_path(pathname)
|
9
|
+
end
|
10
|
+
|
11
|
+
def create
|
12
|
+
if ! File.directory?(path)
|
13
|
+
create_gist
|
14
|
+
else
|
15
|
+
create_file_if_missing
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def edit
|
20
|
+
create
|
21
|
+
raise "Please set the EDITOR environment variable" if ! ENV.include?("EDITOR")
|
22
|
+
|
23
|
+
exec "$SHELL $EDITOR '#{pathname}'"
|
24
|
+
end
|
25
|
+
|
26
|
+
def view
|
27
|
+
exec "$SHELL gulp-octodown-livereload '#{pathname}'"
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def create_gist
|
33
|
+
result = Gist.gist(content_for_empty_file, filename: basename, public: true)
|
34
|
+
gist_hash = result["id"]
|
35
|
+
|
36
|
+
Git.clone("git@gist.github.com:#{gist_hash}", path)
|
37
|
+
end
|
38
|
+
|
39
|
+
def create_file_if_missing
|
40
|
+
raise "The file #{pathname} already exists" if exists?
|
41
|
+
create_empty_file pathname
|
42
|
+
add
|
43
|
+
end
|
44
|
+
|
45
|
+
def create_empty_file(pathname)
|
46
|
+
File.open(pathname, "w") { |f| f.puts content_for_empty_file }
|
47
|
+
end
|
48
|
+
|
49
|
+
def add
|
50
|
+
git = Git.open(path)
|
51
|
+
git.add(pathname)
|
52
|
+
git.commit("Added '#{basename}'")
|
53
|
+
git.push
|
54
|
+
end
|
55
|
+
|
56
|
+
def exists?
|
57
|
+
File.exist?(pathname)
|
58
|
+
end
|
59
|
+
|
60
|
+
def path
|
61
|
+
File.dirname(pathname)
|
62
|
+
end
|
63
|
+
|
64
|
+
def basename
|
65
|
+
File.basename(pathname)
|
66
|
+
end
|
67
|
+
|
68
|
+
def content_for_empty_file
|
69
|
+
"Empty Gist"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
4
73
|
class GistDirectory
|
5
74
|
MAJOR = 0
|
6
|
-
MINOR =
|
7
|
-
REVISION =
|
75
|
+
MINOR = 1
|
76
|
+
REVISION = 1
|
8
77
|
VERSION = [MAJOR, MINOR, REVISION].map(&:to_s).join('.')
|
9
78
|
|
10
|
-
attr_reader :
|
11
|
-
attr_reader :git
|
79
|
+
attr_reader :filename
|
12
80
|
|
13
|
-
def initialize(
|
14
|
-
@
|
81
|
+
def initialize(filename: nil)
|
82
|
+
@filename = File.expand_path(filename)
|
15
83
|
end
|
16
84
|
|
17
|
-
def
|
18
|
-
|
85
|
+
def path
|
86
|
+
@path = File.dirname(filename)
|
87
|
+
end
|
19
88
|
|
89
|
+
def create
|
20
90
|
if path_exists?
|
21
91
|
fail_unless_gist_directory
|
22
|
-
fail_if_filename_exists filename
|
23
|
-
add_file_to_directory filename: filename
|
24
|
-
else
|
25
|
-
do_gist_create filename: filename
|
26
92
|
end
|
27
|
-
end
|
28
|
-
|
29
|
-
private
|
30
93
|
|
31
|
-
|
32
|
-
do_file_add filename: filename
|
94
|
+
file.create
|
33
95
|
end
|
34
96
|
|
35
|
-
def
|
36
|
-
|
37
|
-
|
97
|
+
def edit
|
98
|
+
file.edit
|
99
|
+
end
|
38
100
|
|
39
|
-
|
101
|
+
def view
|
102
|
+
file.view
|
40
103
|
end
|
41
104
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
git.commit("Added '#{filename}'")
|
47
|
-
git.push
|
105
|
+
private
|
106
|
+
|
107
|
+
def file
|
108
|
+
@file ||= GistFile.new(filename)
|
48
109
|
end
|
49
110
|
|
50
111
|
def fail_unless_gist_directory
|
@@ -52,35 +113,30 @@ class GistDirectory
|
|
52
113
|
raise "#{path} exists and does not contain a Gist repository"
|
53
114
|
end
|
54
115
|
end
|
55
|
-
|
56
|
-
def fail_if_filename_exists(filename)
|
57
|
-
absolute = filepath(filename: filename)
|
58
|
-
if File.exist?(absolute)
|
59
|
-
raise "A file called #{filename} already exists in #{path}"
|
60
|
-
end
|
61
|
-
end
|
62
116
|
|
63
|
-
def
|
64
|
-
|
117
|
+
def is_gist_directory?
|
118
|
+
return false if ! is_git_directory?
|
119
|
+
origin = origin_remote
|
120
|
+
return false if origin.nil?
|
121
|
+
is_gist_url? origin.url
|
65
122
|
end
|
66
123
|
|
67
|
-
def
|
68
|
-
File.join(path,
|
124
|
+
def is_git_directory?
|
125
|
+
git_path = File.join(path, ".git")
|
126
|
+
File.directory?(git_path)
|
69
127
|
end
|
70
128
|
|
71
|
-
def
|
72
|
-
|
129
|
+
def origin_remote
|
130
|
+
git = Git.open(path)
|
131
|
+
git.remotes.select { |r| r.name == "origin" }[0]
|
73
132
|
end
|
74
133
|
|
75
|
-
def
|
76
|
-
|
77
|
-
origin = git.remotes.select { |r| r.name == "origin" }[0]
|
78
|
-
return false if origin.nil?
|
79
|
-
m = %r(^git@gist\.github\.com:\w+$).match(origin.url)
|
134
|
+
def is_gist_url?(url)
|
135
|
+
m = %r(^git@gist\.github\.com:\w+$).match(url)
|
80
136
|
! m.nil?
|
81
137
|
end
|
82
138
|
|
83
|
-
def
|
84
|
-
|
139
|
+
def path_exists?
|
140
|
+
File.directory?(path)
|
85
141
|
end
|
86
142
|
end
|
data/spec/gist_directory_spec.rb
CHANGED
@@ -3,26 +3,27 @@ require "gist_directory"
|
|
3
3
|
RSpec.describe GistDirectory do
|
4
4
|
let(:path) { "/foo/bar" }
|
5
5
|
let(:path_exists) { false }
|
6
|
-
let(:filename) { "baz.txt" }
|
6
|
+
let(:filename) { File.join(path, "baz.txt") }
|
7
7
|
let(:gist_result) { {"id" => gist_hash} }
|
8
8
|
let(:gist_hash) { "gist_sha1_hash" }
|
9
9
|
let(:git) { double(Git) }
|
10
|
-
subject { described_class.new(
|
10
|
+
subject { described_class.new(filename: filename) }
|
11
11
|
|
12
12
|
before do
|
13
13
|
allow(File).to receive(:directory?).and_call_original
|
14
14
|
allow(File).to receive(:directory?).with(path) { path_exists }
|
15
15
|
allow(Gist).to receive(:gist) { gist_result }
|
16
16
|
allow(Git).to receive(:clone)
|
17
|
-
allow(Git).to receive(:open)
|
17
|
+
allow(Git).to receive(:open).and_raise("Unexpected path")
|
18
|
+
allow(Git).to receive(:open).with(path) { git }
|
18
19
|
end
|
19
20
|
|
20
21
|
describe "#create" do
|
21
22
|
context "when the path doesn't exist" do
|
22
23
|
it "creates a Gist, adding the file" do
|
23
|
-
subject.create
|
24
|
+
subject.create
|
24
25
|
expect(Gist).to have_received(:gist).with(
|
25
|
-
"Empty Gist", {filename: filename, public: true}
|
26
|
+
"Empty Gist", {filename: File.basename(filename), public: true}
|
26
27
|
)
|
27
28
|
end
|
28
29
|
end
|
@@ -38,14 +39,13 @@ RSpec.describe GistDirectory do
|
|
38
39
|
context "and contains a Git repo" do
|
39
40
|
let(:dot_git_exists) { true }
|
40
41
|
let(:remotes) { [double(Git::Remote, name: "origin", url: repo_url)] }
|
41
|
-
let(:filepath) { File.join(path, filename) }
|
42
42
|
let(:file) { double(File, puts: nil) }
|
43
43
|
|
44
44
|
before do
|
45
45
|
allow(File).to receive(:open).and_call_original
|
46
|
-
allow(File).to receive(:open).with(
|
46
|
+
allow(File).to receive(:open).with(filename, "w").and_yield(file)
|
47
47
|
allow(File).to receive(:exist?).and_call_original
|
48
|
-
allow(File).to receive(:exist?).with(
|
48
|
+
allow(File).to receive(:exist?).with(filename) { file_exists }
|
49
49
|
allow(git).to receive(:remotes) { remotes }
|
50
50
|
allow(git).to receive(:add)
|
51
51
|
allow(git).to receive(:commit)
|
@@ -59,13 +59,13 @@ RSpec.describe GistDirectory do
|
|
59
59
|
let(:file_exists) { false }
|
60
60
|
|
61
61
|
it "creates the file" do
|
62
|
-
subject.create
|
62
|
+
subject.create
|
63
63
|
expect(file).to have_received(:puts)
|
64
64
|
end
|
65
65
|
|
66
66
|
it "adds the file" do
|
67
|
-
subject.create
|
68
|
-
expect(git).to have_received(:add).with(
|
67
|
+
subject.create
|
68
|
+
expect(git).to have_received(:add).with(filename)
|
69
69
|
expect(git).to have_received(:commit)
|
70
70
|
end
|
71
71
|
end
|
@@ -75,7 +75,7 @@ RSpec.describe GistDirectory do
|
|
75
75
|
|
76
76
|
it "fails" do
|
77
77
|
expect do
|
78
|
-
subject.create
|
78
|
+
subject.create
|
79
79
|
end.to raise_error(RuntimeError, /#{filename} already exists/)
|
80
80
|
end
|
81
81
|
end
|
@@ -86,7 +86,7 @@ RSpec.describe GistDirectory do
|
|
86
86
|
|
87
87
|
it "fails" do
|
88
88
|
expect do
|
89
|
-
subject.create
|
89
|
+
subject.create
|
90
90
|
end.to raise_error(RuntimeError, /not contain a Gist repository/)
|
91
91
|
end
|
92
92
|
end
|
@@ -97,7 +97,7 @@ RSpec.describe GistDirectory do
|
|
97
97
|
|
98
98
|
it "fails" do
|
99
99
|
expect do
|
100
|
-
subject.create
|
100
|
+
subject.create
|
101
101
|
end.to raise_error(RuntimeError, /not contain a Gist repository/)
|
102
102
|
end
|
103
103
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gist_directory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Yates
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: git
|