crawling 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 16535e151189a5b08dd80f2ee96eb80c773c4c89
4
- data.tar.gz: 546fb69bf2ce963cf18f151bd3d1a10888e93b2f
3
+ metadata.gz: 58b06f8d2ecfcaf9426d38d525f90e0385db30f0
4
+ data.tar.gz: e03d6cac9a48affb12ef7f3dc0b327c914960686
5
5
  SHA512:
6
- metadata.gz: a5012d5fdc91a4d57a648267651cf8d5f7c3f53db251207ed1539f227be611e293e35adec77cf1c3b04ee1dc3329e65bdd8b074953366ba67e6fec05952360ec
7
- data.tar.gz: f95a2b676c9c37f872ea452cda34e665e84a9b39fc7aec3d00582a9da5996dafff4cc64b09e115b462f9b2c9c684873fd71ec33ee2abd2f72a5c4c721688f891
6
+ metadata.gz: 60a167da46d0fb9bad4f679062328f1c8a49da7908bfda35b9312aa9df2f1887a31c39f374a8cb050c540d706b94d07d790a5aa3ee0d429775b5a68dcbd153b5
7
+ data.tar.gz: d0d95bb35f2be14eca28ccdd2611b85c3cceb605860f885a11ca08fc3e6e1aeedcc6f15b058138951cb289bfce1104beefe0cae8b754da813022e5164515badb
data/bin/crawling CHANGED
@@ -6,6 +6,10 @@ require 'moister'
6
6
  require 'ostruct'
7
7
 
8
8
  module Crawling::Command
9
+ def self.nil_if_empty arr
10
+ arr.empty? ? nil : arr
11
+ end
12
+
9
13
  def self.run args
10
14
  config = nil
11
15
  command = nil
@@ -25,16 +29,15 @@ module Crawling::Command
25
29
  op.on '-H', '--home dir', 'path to home directory', 'home_dir'
26
30
 
27
31
  op.subcommand 'cd', 'run shell in store directory'
28
- op.subcommand 'add', 'add files to store directory'
29
- op.subcommand 'get', 'get files from store directory'
30
- op.subcommand 'diff', 'show differences between store directory and filesystem'
31
- op.subcommand 'merge', 'merge differences between store directory and filesystem' do |subop|
32
+ op.subcommand 'add,a *paths', 'add paths to store directory'
33
+ op.subcommand 'get,g *paths', 'get paths from store directory'
34
+ op.subcommand 'diff,d [*paths]', 'show differences between store directory and filesystem'
35
+ op.subcommand 'merge,m [*paths]', 'merge differences between store directory and filesystem' do |subop|
32
36
  op.on '-m', '--merge-app app', 'command used to merge files', 'merge_app'
33
37
  end
34
38
  op.subcommand 'clone', 'clone or pull latest store using configured commands'
35
39
 
36
40
  parsed_cfg = op.parse(args)
37
- positionals = parsed_cfg.positionals
38
41
  command = parsed_cfg.command
39
42
  config = OpenStruct.new parsed_cfg[:config]
40
43
  end
@@ -44,18 +47,21 @@ module Crawling::Command
44
47
  exit
45
48
  end
46
49
 
50
+ command_cfg = OpenStruct.new config[command]
51
+ config.delete_field command
52
+
47
53
  crawling = Crawling::Instance.new(**config.to_h)
48
54
  case command
49
55
  when 'cd'
50
- crawling.cd positionals[0]
56
+ crawling.cd
51
57
  when 'add'
52
- crawling.add positionals
58
+ crawling.add command_cfg.paths
53
59
  when 'get'
54
- crawling.get positionals
60
+ crawling.get command_cfg.paths
55
61
  when 'diff'
56
- crawling.diff positionals
62
+ crawling.diff nil_if_empty(command_cfg.paths)
57
63
  when 'merge'
58
- crawling.merge positionals
64
+ crawling.merge nil_if_empty(command_cfg.paths)
59
65
  when 'clone'
60
66
  crawling.clone
61
67
  end
data/crawling.gemspec CHANGED
@@ -31,6 +31,6 @@ Gem::Specification.new do |spec|
31
31
  spec.add_development_dependency "rake", "~> 10.0"
32
32
  spec.add_development_dependency "rspec"
33
33
 
34
- spec.add_dependency "moister", "~> 0.2"
34
+ spec.add_dependency "moister", "~> 0.3"
35
35
  spec.add_dependency "diffy", "~> 3.1.0"
36
36
  end
@@ -1,3 +1,3 @@
1
1
  module Crawling
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/crawling.rb CHANGED
@@ -15,6 +15,11 @@ module Crawling
15
15
  Dir.glob("#{path}/**/*", File::FNM_DOTMATCH).reject(&File.method(:directory?))
16
16
  end
17
17
 
18
+ # relative_to must be an absolute Pathname
19
+ def self.relative_path_to target, relative_to
20
+ Pathname.new(target).expand_path.relative_path_from relative_to
21
+ end
22
+
18
23
  # Like File.cp but also creates the parent directory at destination if it doesn't exist
19
24
  def self.copy_file src_file, dest_file
20
25
  dest_parent_dir = File.dirname dest_file
@@ -26,22 +31,49 @@ module Crawling
26
31
  end
27
32
  end
28
33
 
34
+ class Store
35
+ attr_reader :store_dir
36
+
37
+ def initialize store_dir, sys_dir
38
+ @store_dir = File.absolute_path store_dir
39
+ @store_pathname = Pathname.new(store_dir).expand_path
40
+ @sys_dir = File.absolute_path sys_dir
41
+ @sys_pathname = Pathname.new(sys_dir).expand_path
42
+ end
43
+
44
+ # if path is within store then return system path otherwise return nil
45
+ def get_sys_path path
46
+ if path.start_with? @store_dir
47
+ File.join @sys_dir, Crawling.relative_path_to(path, @store_pathname)
48
+ end
49
+ end
50
+
51
+ # if path is within system then return store path otherwise return nil
52
+ def get_store_path path
53
+ if path.start_with? @sys_dir
54
+ File.join @store_dir, Crawling.relative_path_to(path, @sys_pathname)
55
+ end
56
+ end
57
+ end
58
+
29
59
  class Instance
30
60
  def initialize(config_dir: nil, home_dir: nil, merge_app: nil)
31
61
  @home_dir = home_dir || ENV['HOME']
32
- @home_pathname = Pathname.new(@home_dir).expand_path
33
62
  @config_dir = config_dir || "#{@home_dir}/.config/crawling"
34
63
  @config_pathname = Pathname.new(@config_dir).expand_path
35
64
  @merge_app = merge_app || 'vimdiff %s %h'
36
- end
37
65
 
38
- def cd(subdir = nil)
39
- cd_dir = get_config_dir
40
- cd_dir = Path.join(cd_dir, subdir) if subdir
41
- raise "directory #{subdir} doesn't exist" unless Dir.exists? cd_dir
66
+ stores = { 'home' => @home_dir }
67
+ @stores = stores.map do |store_dir, sys_dir|
68
+ store_dir = File.join(@config_dir, store_dir)
69
+ Store.new store_dir, sys_dir
70
+ end
71
+ end
42
72
 
43
- Dir.chdir cd_dir
44
- puts "creating shell in #{cd_dir}, type exit or ctrl-D to exit"
73
+ def cd
74
+ FileUtils::mkdir_p @config_dir unless Dir.exists? @config_dir
75
+ Dir.chdir @config_dir
76
+ puts "creating shell in #{@config_dir}, type exit or ctrl-D to exit"
45
77
  system ENV['SHELL']
46
78
  puts "crawling shell exited"
47
79
  end
@@ -65,13 +97,18 @@ module Crawling
65
97
  raise "path #{path} does not exist in storage" unless File.exists? storage_path
66
98
 
67
99
  files_from(storage_path).each do |storage_file|
68
- sys_path = from_storage_path storage_file
69
- Crawling.copy_file storage_file, sys_path
100
+ if storage_file == storage_path
101
+ Crawling.copy_file storage_file, path
102
+ else
103
+ # path was a directory so recalculate new system path
104
+ path_offset = storage_file[storage_path.length..-1]
105
+ Crawling.copy_file storage_file, path + path_offset
106
+ end
70
107
  end
71
108
  end
72
109
  end
73
110
 
74
- def diff paths
111
+ def diff paths = nil
75
112
  each_with_storage_path(files_from_paths_or_all paths) do |file, storage_file|
76
113
  missing_from = file_or_storage_file_doesnt_exist file, storage_file
77
114
  if missing_from
@@ -88,7 +125,7 @@ module Crawling
88
125
  end
89
126
  end
90
127
 
91
- def merge paths
128
+ def merge paths = nil
92
129
  each_with_storage_path(files_from_paths_or_all paths) do |file, storage_file|
93
130
  missing_from = file_or_storage_file_doesnt_exist file, storage_file
94
131
  if missing_from
@@ -138,47 +175,33 @@ module Crawling
138
175
 
139
176
  private
140
177
  def get_config_dir
141
- FileUtils::mkdir_p @config_dir unless Dir.exists? @config_dir
142
178
  @config_dir
143
179
  end
144
180
 
145
- def relative_path_to target, relative_to
146
- Pathname.new(target).expand_path.relative_path_from relative_to
147
- end
148
-
149
181
  def each_with_storage_path paths
150
182
  paths.each do |path|
151
- yield path, get_storage_path(path)
183
+ pair = get_path_pair(path)
184
+ raise "could not resolve #{path} to store" if pair.nil?
185
+ yield pair
152
186
  end
153
187
  end
154
188
 
155
- def get_home_path path
156
- relative_path_to path, @home_pathname
157
- end
158
-
159
- def get_storage_path path
160
- # TODO: get system or home path depending on whether it is a subdirectory of the current user
161
- File.join @config_dir, HOME_PARENT_DIR, get_home_path(path)
162
- end
163
-
164
- def from_storage_path path
165
- cfg_rel_path = Pathname.new(relative_path_to path, @config_pathname)
166
- head, *tail = Pathname(cfg_rel_path).each_filename.to_a
167
- if head === 'home'
168
- File.join @home_dir, *tail
169
- else
170
- raise "storage type #{head} not supported yet"
189
+ def get_path_pair path
190
+ path = File.absolute_path path
191
+ @stores.each do |store|
192
+ sys_path = store.get_sys_path path
193
+ return [ sys_path, path ] if sys_path
194
+ store_path = store.get_store_path path
195
+ return [ path, store_path ] if store_path
171
196
  end
197
+ nil
172
198
  end
173
199
 
174
- # if paths is empty then get home paths for all paths in storage, else get the
175
- # files recursively reachable from the provided paths
200
+ # if paths is empty then get all paths from stores otherwise get files
201
+ # recursively reachable from the provided paths
176
202
  def files_from_paths_or_all paths
177
- if paths.empty?
178
- # TODO: also support 'SYSTEM_PARENT_DIR'
179
- Crawling.child_files_recursive(
180
- File.join(@config_dir, HOME_PARENT_DIR)
181
- ).map &method(:from_storage_path)
203
+ if paths.nil?
204
+ @stores.map { |store| Crawling.child_files_recursive(store.store_dir) }.flatten
182
205
  else
183
206
  paths.map(&method(:files_from)).flatten
184
207
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crawling
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Pike
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-16 00:00:00.000000000 Z
11
+ date: 2016-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0.2'
61
+ version: '0.3'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0.2'
68
+ version: '0.3'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: diffy
71
71
  requirement: !ruby/object:Gem::Requirement