kiip 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: edaf299795e1246288dbdaa727437701b4735b3f
4
- data.tar.gz: 5484c36987dcf1bf0e0689ead7363711bc4d7eaf
3
+ metadata.gz: 3240b679e955ee5fbd7d1e3a1cd1bd1fe000e20a
4
+ data.tar.gz: 2ebb7db6c11d52bc7807991b9354455fa87fd80e
5
5
  SHA512:
6
- metadata.gz: 854f64eb1af85ba527dbf117b877785a45f70af60eb621bb6723106a076e65f219d926ce58e2016af5a4ef7a37ef77c1f769c9ce01da4320c38283be75715f4f
7
- data.tar.gz: af9d9dbb390769891c12aea566ba9250daa91f6b8bd428b3c45545e2445eb3937c6e5f32bbb90b39ae9f1a4f7ac5f03440ea498bbccc86c4ebbe735b6cfb7692
6
+ metadata.gz: e8d3d7a910d44edb27f1960d82dff3631b7900c08100b886b96f1c192c98b5633f876bde0ffe2066146c494db6737b268264f6c90ec2a2be27ab0a904a026659
7
+ data.tar.gz: e701876d02d0ff455ad2423a9ce48dc7d3d6563b83beb4cc6bb69dcf08a3e978ec6715e05ba877d4357fa0c634090267734b96a3efea30f4fc8151bd8623ab85
data/lib/kiip/entry.rb ADDED
@@ -0,0 +1,64 @@
1
+ module Kiip
2
+ class Entry < Hashie::Dash
3
+ property :source, required: true, coerce: String
4
+ property :target, required: true, coerce: String
5
+ property :package, required: true
6
+
7
+ # restores the entry to it's original place
8
+ def restore
9
+ remove_source if correct_link?
10
+
11
+ # if file exists, remove or return
12
+ if File.exist?(expanded_source)
13
+ cli.agree("#{source} exists. Remove it?") ? remove_source : return
14
+ end
15
+
16
+ FileUtils.copy_entry(target, expanded_source, **file_options)
17
+ end
18
+
19
+ # removes the source. If source is not a link to target, it asks the users.
20
+ def unlink
21
+ if not File.exist?(expanded_source)
22
+ cli.say "#{source} does not exist." if file_options[:verbose]
23
+ return
24
+ end
25
+
26
+ if correct_link? or cli.agree("#{source} is not a symlink to #{target}. Still remove it?")
27
+ FileUtils.remove_entry(expanded_source, **file_options)
28
+ end
29
+ end
30
+
31
+ def status
32
+ return :linked if correct_link?
33
+ return :symlink if File.symlink?(expanded_source)
34
+ return :directory if File.directory?(expanded_source)
35
+ return :file if File.file?(expanded_source)
36
+ return :not_existent unless File.exist?(expanded_source)
37
+
38
+ raise 'unknown status'
39
+ end
40
+
41
+ private
42
+
43
+ def correct_link?
44
+ File.symlink?(expanded_source) and File.readlink(expanded_source) == target
45
+ end
46
+
47
+ def cli
48
+ @cli || HighLine.new
49
+ end
50
+
51
+ def expanded_source
52
+ File.expand_path(source)
53
+ end
54
+
55
+ def file_options
56
+ {verbose: package.repository.is_verbose, noop: package.repository.is_dry}
57
+ end
58
+
59
+ # asks the user if source should be removed and removes it
60
+ def remove_source
61
+ FileUtils.remove_entry(File.expand_path(source), **file_options)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,87 @@
1
+ require "base64"
2
+
3
+ module Kiip
4
+ class Package < Hashie::Dash
5
+ class << self
6
+ def encode path
7
+ Base64.encode64 path
8
+ end
9
+
10
+ def decode path
11
+ Base64.decode64 path
12
+ end
13
+ end
14
+
15
+ include Hashie::Extensions::Dash::Coercion
16
+ property :name, required: true, coerce: String
17
+ property :repository, required: true
18
+
19
+ def entries
20
+ content.map do |encoded_original_path|
21
+ Entry.new(
22
+ source: self.class.decode(encoded_original_path),
23
+ target: File.join(path, encoded_original_path),
24
+ package: self
25
+ )
26
+ end
27
+ end
28
+
29
+ def restore
30
+ entries.each &:restore
31
+ end
32
+
33
+ # removes the links to the package content
34
+ def unlink
35
+ entries.each &:unlink
36
+ end
37
+
38
+ def track(tracking_path)
39
+ raise "path does not exist: #{tracking_path}" unless File.exists?(File.expand_path tracking_path)
40
+
41
+ tracking_path = tracking_path.gsub %r{^#{File.expand_path('~')}}, '~'
42
+
43
+ # escape /
44
+ escaped_tracking_path = self.class.encode tracking_path
45
+
46
+ return if repository.is_dry
47
+
48
+ task = Tasks::SymlinkTask.new(name: 'task-name', source: tracking_path, target: File.join(path, escaped_tracking_path))
49
+ task.exec!
50
+ end
51
+
52
+ def sync!
53
+ content.each do |subpath|
54
+ source = self.class.decode subpath
55
+ task = Tasks::SymlinkTask.new(name: 'task-name', source: source, target: File.join(path, subpath))
56
+ task.exec!
57
+ end
58
+ end
59
+
60
+ def decoded_content
61
+ content.map { |s| self.class.decode s }
62
+ end
63
+
64
+ # creates the package or raises an error
65
+ def create!
66
+ Dir.mkdir(path)
67
+ end
68
+
69
+ def rm
70
+ FileUtils.rm_r(path, verbose: repository.is_verbose, noop: repository.is_dry)
71
+ end
72
+
73
+ # @return [boolean]
74
+ def exists?
75
+ File.directory?(path)
76
+ end
77
+
78
+ def path
79
+ File.join(repository.path, name)
80
+ end
81
+
82
+ # @return [String[]] array of package content files/folders
83
+ def content
84
+ Pathname.new(path).children.map(&:basename).map(&:to_s)
85
+ end
86
+ end
87
+ end
@@ -2,7 +2,6 @@ require "pathname"
2
2
 
3
3
  module Kiip
4
4
  class Repository < Hashie::Dash
5
- autoload :Package, 'kiip/repository/package'
6
5
  ID_FILE_NAME = '.kiip_repository'
7
6
 
8
7
  def self.get_instance(**options)
@@ -24,24 +23,27 @@ module Kiip
24
23
  end
25
24
 
26
25
  def unlink *package_names
26
+ ensure_existance!
27
27
  package_names.each do |name|
28
28
  get_package(name).unlink
29
29
  end
30
30
  end
31
31
 
32
32
  def restore *package_names
33
+ ensure_existance!
33
34
  package_names.each do |name|
34
35
  get_package(name).restore
35
36
  end
36
37
  end
37
38
 
38
39
  def sync! *names
40
+ ensure_existance!
39
41
  names = package_names if names.empty?
40
42
  names.each { |name| get_package(name).sync! }
41
43
  end
42
44
 
43
45
  def track package_name, path
44
- return unless ensure_existance
46
+ ensure_existance!
45
47
  package = get_package(package_name)
46
48
  package.create! unless package.exists?
47
49
  package.track(path)
@@ -49,26 +51,12 @@ module Kiip
49
51
 
50
52
  # @return [String] multi-line string with content of the repository
51
53
  def print_content
54
+ ensure_existance!
52
55
  StringIO.open do |result|
53
56
  packages.each do |package|
54
57
  result.puts package.name + ':'
55
- package.content.each do |content|
56
- decoded_path_original = Kiip::Repository::Package.decode(content)
57
- decoded_path_expanded = File.expand_path decoded_path_original
58
-
59
- if File.symlink?(decoded_path_expanded)
60
- status = File.readlink(decoded_path_expanded) == File.join(package.path, content) ? 'linked' : 'symlink'
61
- elsif File.directory?(decoded_path_expanded)
62
- status = 'directory'
63
- elsif File.file? decoded_path_expanded
64
- status = 'file'
65
- elsif not File.exist? decoded_path_expanded
66
- status = 'not existant'
67
- else
68
- raise 'unknown status'
69
- end
70
-
71
- result.puts " #{decoded_path_original} | #{status}"
58
+ package.entries.each do |entry|
59
+ result.puts " #{entry.source} | #{entry.status}"
72
60
  end
73
61
  end
74
62
 
@@ -77,6 +65,7 @@ module Kiip
77
65
  end
78
66
 
79
67
  def packages
68
+ ensure_existance!
80
69
  package_names.map do |package_name|
81
70
  get_package(package_name)
82
71
  end
@@ -88,25 +77,30 @@ module Kiip
88
77
  end
89
78
 
90
79
  def rm *package_names
80
+ ensure_existance!
91
81
  package_names.each do |package_name|
92
82
  get_package(package_name).rm
93
83
  end
94
84
  end
95
85
 
96
86
  private
87
+
97
88
  # asks user to create repository if it doesn't exist
98
89
  #
90
+ # @raise [IllegalStateError] if the repository doesn't exist
99
91
  # @return [boolean] true if repo exists now
100
- def ensure_existance
101
- return true if exists?
102
-
103
- if HighLine.new.agree("Repository #{path} does not exist. Want me to create it? (yes/no)")
92
+ def ensure_existance!
93
+ if not exists? and cli.agree("Repository #{path} does not exist. Want me to create it? (yes/no)")
104
94
  create!
105
95
  end
106
96
 
107
97
  exists?
108
98
  end
109
99
 
100
+ def cli
101
+ @cli ||= HighLine.new
102
+ end
103
+
110
104
  def get_package(package_name)
111
105
  Package.new(name: package_name, repository: self)
112
106
  end
data/lib/kiip/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Kiip
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/kiip.rb CHANGED
@@ -10,6 +10,8 @@ module Kiip
10
10
  autoload :Syncer, 'kiip/syncer'
11
11
  autoload :Repository, 'kiip/repository'
12
12
  autoload :Errors, 'kiip/errors'
13
+ autoload :Package, 'kiip/package'
14
+ autoload :Entry, 'kiip/entry'
13
15
 
14
16
  module Tasks
15
17
  autoload :SymlinkTask, 'kiip/tasks/symlink_task'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kiip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robin Wenglewski
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-01-04 00:00:00.000000000 Z
11
+ date: 2016-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -188,9 +188,10 @@ files:
188
188
  - kiip.gemspec
189
189
  - lib/kiip.rb
190
190
  - lib/kiip/cli.rb
191
+ - lib/kiip/entry.rb
191
192
  - lib/kiip/errors.rb
193
+ - lib/kiip/package.rb
192
194
  - lib/kiip/repository.rb
193
- - lib/kiip/repository/package.rb
194
195
  - lib/kiip/tasks/symlink_task.rb
195
196
  - lib/kiip/version.rb
196
197
  homepage: https://github.com/rweng/kiip
@@ -1,89 +0,0 @@
1
- require "base64"
2
-
3
- module Kiip
4
- class Repository
5
- class Package < Hashie::Dash
6
- class << self
7
- def encode path
8
- Base64.encode64 path
9
- end
10
-
11
- def decode path
12
- Base64.decode64 path
13
- end
14
- end
15
-
16
- include Hashie::Extensions::Dash::Coercion
17
- property :name, required: true, coerce: String
18
- property :repository, required: true
19
-
20
- def restore
21
- unlink
22
- content.each do |encoded_orginal_path|
23
- decoded_original_path = self.class.decode encoded_orginal_path
24
- FileUtils.cp_r(File.join(path, encoded_orginal_path), decoded_original_path,
25
- verbose: repository.is_verbose, noop: repository.is_dry)
26
- end
27
- end
28
-
29
- # removes the links to the package content
30
- def unlink
31
- content.each do |encoded_orginal_path|
32
- decoded_original_path = self.class.decode encoded_orginal_path
33
- if File.symlink?(decoded_original_path) and File.readlink(decoded_original_path) == File.join(path, encoded_orginal_path)
34
- FileUtils.rm decoded_original_path
35
- end
36
- end
37
- end
38
-
39
- def track(tracking_path)
40
- raise "path does not exist: #{tracking_path}" unless File.exists?(File.expand_path tracking_path)
41
-
42
- tracking_path = tracking_path.gsub %r{^#{File.expand_path('~')}}, '~'
43
-
44
- # escape /
45
- escaped_tracking_path = self.class.encode tracking_path
46
-
47
- return if repository.is_dry
48
-
49
- task = Tasks::SymlinkTask.new(name: 'task-name', source: tracking_path, target: File.join(path, escaped_tracking_path))
50
- task.exec!
51
- end
52
-
53
- def sync!
54
- content.each do |subpath|
55
- source = self.class.decode subpath
56
- task = Tasks::SymlinkTask.new(name: 'task-name', source: source, target: File.join(path, subpath))
57
- task.exec!
58
- end
59
- end
60
-
61
- def decoded_content
62
- content.map { |s| self.class.decode s }
63
- end
64
-
65
- # creates the package or raises an error
66
- def create!
67
- Dir.mkdir(path)
68
- end
69
-
70
- def rm
71
- FileUtils.rm_r(path, verbose: repository.is_verbose, noop: repository.is_dry)
72
- end
73
-
74
- # @return [boolean]
75
- def exists?
76
- File.directory?(path)
77
- end
78
-
79
- def path
80
- File.join(repository.path, name)
81
- end
82
-
83
- # @return [String[]] array of package content files/folders
84
- def content
85
- Pathname.new(path).children.map(&:basename).map(&:to_s)
86
- end
87
- end
88
- end
89
- end