yard-sftp 1.0.3 → 1.1.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: 0a532899b4dc8c6831ad3c996b0299d9113bacf9
4
- data.tar.gz: e4df2c6aa42689df9c079ecaf8053bda05a8062f
3
+ metadata.gz: 374d10d47e1269d06b96f8b4d8974159567aa3f7
4
+ data.tar.gz: fa22ce24200815406771c182fdc691f4e25e7db1
5
5
  SHA512:
6
- metadata.gz: ad0eb191e483d66be03f70113a62adf2b412d30b8de1c4617da97bced12578e310b4334d03ab1c9d4fcd210474249ad502f5e8d76e09359b1b643d9382b0fb6b
7
- data.tar.gz: 636d717244d4005dc9ededb82d2ef253e4c39fd8af36742dbc1f945e4ea70473616bc04bcfb0158cd9bbf97caf746681c2dcd09b9deae4b7e127060d10146714
6
+ metadata.gz: dd658d85251d41ee2da9ad07c1c29bac12d9cd6357ff17b93718d7b839faea2b7d0de17be477d552260b50bd7fd078a50c01b298d5ea56ac522ff369b6fdb53d
7
+ data.tar.gz: 46b8664f4c46d375d7682e91c616d756d27ac499749095e1226efc47fa25269a06f58d3dcedb9bfc4484a5e8a893be0a914dfaec72bd3681d464897b75316a57
@@ -0,0 +1 @@
1
+ 2.0.0-p247
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
- # yard-sftp
1
+ [![Code Climate](https://codeclimate.com/github/jonathanchrisp/yard-sftp.png)](https://codeclimate.com/github/jonathanchrisp/yard-sftp)
2
2
 
3
+ # yard-sftp
3
4
  Move your new shiny yard documentation to a remote location with SFTP!
4
5
 
5
6
  ## Getting Started
@@ -1,3 +1,3 @@
1
1
  module YARDSFTP
2
- VERSION = '1.0.3'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -2,7 +2,6 @@ require 'fileutils'
2
2
  require 'colored'
3
3
  require 'yaml'
4
4
  require 'net/sftp'
5
- YAML::ENGINE.yamler = 'psych'
6
5
 
7
6
  class File
8
7
 
@@ -15,8 +14,7 @@ class File
15
14
  FileUtils.mkdir_p(dir) unless directory?(dir)
16
15
  open(file, *args, &block)
17
16
 
18
- # Create base directory, file directories and upload file via sftp
19
- sftp_create_base_directory
17
+ sftp_create_path("#{BASE_DIR}")
20
18
  sftp_file(file)
21
19
  end
22
20
 
@@ -24,86 +22,106 @@ class File
24
22
  #
25
23
  # @return [Hash] opts Hash
26
24
  def self.sftp_config
27
- begin
28
25
  opts = YAML.load_file('.yardsftp')
29
26
  rescue Psych::SyntaxError
30
27
  abort 'Your .yardsftp file did not parse as expected!'.red.underline
31
28
  rescue Errno::ENOENT
32
29
  abort 'Your .yardsftp file is missing!'.red.underline
33
- end
34
-
35
- return opts
36
30
  end
37
31
 
38
32
  # Creates or returns sftp instance
39
33
  #
40
34
  # @return [Object] sftp instance
41
35
  def self.sftp
42
- sftp ||= Net::SFTP.start(HOST, USER, :password => PWD)
36
+ tries ||= 3
37
+ connection ||= Net::SFTP.start(HOST, USER, :password => PWD)
38
+ rescue Errno::EADDRINUSE
39
+ log.progress('SSH Connection Error - retrying', nil)
40
+ retry unless (tries -= 1).zero?
43
41
  end
44
42
 
45
43
  # Uploads file
44
+ #
45
+ # @param opts [String] file_path path of the file
46
46
  def self.sftp_file(file_path)
47
- directories = sftp_split_all(file_path)
48
- directories.pop #removes actual file from array
47
+ paths = sftp_split_all(file_path)
48
+ paths.pop
49
+ return if paths.include?(".yardoc")
49
50
 
50
- unless directories.empty?
51
- sftp_build_directories(directories)
51
+ unless paths.empty?
52
+ sftp_create_paths(paths)
52
53
  end
53
54
 
54
- sftp.upload!(file_path, "#{BASE_PATH}/#{BASE_FOLDER}/#{file_path}")
55
+ log.progress("Uploading #{file_path}", nil)
56
+ sftp.upload!(file_path, "#{BASE_DIR}/#{file_path}")
55
57
  end
56
58
 
57
- # Creates directories
59
+ # Creates paths relevant for file
58
60
  #
59
- # @param opts [Array] directories Array of directory names
60
- def self.sftp_build_directories(directories)
61
- directories.each.with_index do |dir, i|
62
- if i == 0
63
- path = "#{BASE_PATH}/#{BASE_FOLDER}/#{dir}"
64
- elsif i == 1
65
- path = "#{BASE_PATH}/#{BASE_FOLDER}/#{directories[0]}/#{dir}"
66
- else
67
- path = "#{BASE_PATH}/#{BASE_FOLDER}/#{directories.take(i).join('/')}/#{dir}"
61
+ # @param opts [Array] paths Array of paths
62
+ def self.sftp_create_paths(paths)
63
+ # Check if path doesn't already exists
64
+ if sftp_path_exists?("#{BASE_DIR}/#{paths.join('/')}")
65
+ sftp_clean_path("#{BASE_DIR}/#{paths.join('/')}")
66
+ else
67
+ # Iterate through each path and create
68
+ paths.each.with_index do |p, i|
69
+ case i
70
+ when 0
71
+ sftp_create_path("#{BASE_DIR}/#{p}")
72
+ when 1
73
+ sftp_create_path("#{BASE_DIR}/#{paths[0]}/#{p}")
74
+ else
75
+ sftp_create_path("#{BASE_DIR}/#{paths.take(i).join('/')}/#{p}")
76
+ end
68
77
  end
69
-
70
- sftp_create_directory(path)
71
78
  end
72
79
  end
73
80
 
74
- # Creates base directory
75
- def self.sftp_create_base_directory
76
- unless sftp_directory_exists?("#{BASE_PATH}/#{BASE_FOLDER}")
77
- sftp_create_directory("#{BASE_PATH}/#{BASE_FOLDER}")
81
+ # Creates path if it doesn't exist
82
+ #
83
+ # @param opts [String] path the path of directory
84
+ def self.sftp_create_path(path)
85
+ unless sftp_path_exists?(path)
86
+ log.progress("Creating directory: #{path}", nil)
87
+ sftp.mkdir!(path)
78
88
  end
79
89
  end
80
90
 
81
- # Creates base directory
91
+ # Removes files from path if older than upload time
82
92
  #
83
93
  # @param opts [String] path the path of directory
84
- def self.sftp_create_directory(path)
85
- unless sftp_directory_exists?(path)
86
- sftp.mkdir(path).wait
94
+ def self.sftp_clean_path(path)
95
+ sftp.dir.foreach(path) do |f|
96
+ if !File.extname("#{path}/#{f.name}").empty? && f.attributes.mtime < UPLOAD_TIME
97
+ sftp_remove_path("#{path}/#{f.name}")
98
+ end
87
99
  end
88
100
  end
89
101
 
90
- # Checks if the directory exists
102
+ # Checks if the path exists
91
103
  #
92
104
  # @param opts [String] path the path of directory
93
- def self.sftp_directory_exists?(path)
94
- begin
105
+ # @return [Boolean] returns boolean of result
106
+ def self.sftp_path_exists?(path)
95
107
  sftp.stat!(path)
96
- rescue
108
+ rescue Net::SFTP::StatusException
97
109
  return false
98
110
  else
99
111
  return true
100
- end
101
112
  end
102
113
 
103
- # Splits file path
114
+ # Removes file
104
115
  #
105
- # @param opts [String] path the path of directory
106
- # @return [Array] returns array of directories
116
+ # @param opts [String] path the path of file
117
+ def self.sftp_remove_path(path)
118
+ sftp.remove!(path)
119
+ end
120
+
121
+ # Splits path
122
+ #
123
+ # @param opts [String] path the path of file
124
+ # @return [Array] returns array of file path
107
125
  def self.sftp_split_all(path)
108
126
  head, tail = File.split(path)
109
127
  return [tail] if head == '.' || tail == '/'
@@ -116,6 +134,6 @@ class File
116
134
  HOST = OPTS['yard-sftp']['host']
117
135
  USER = OPTS['yard-sftp']['username']
118
136
  PWD = OPTS['yard-sftp']['password']
119
- BASE_PATH = OPTS['yard-sftp']['base_path']
120
- BASE_FOLDER = OPTS['yard-sftp']['base_folder']
137
+ BASE_DIR = OPTS['yard-sftp']['base_path'] + '/' + OPTS['yard-sftp']['base_folder']
138
+ UPLOAD_TIME = Time.now.to_i
121
139
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yard-sftp
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Chrisp
@@ -87,6 +87,7 @@ extensions: []
87
87
  extra_rdoc_files: []
88
88
  files:
89
89
  - .gitignore
90
+ - .ruby-version
90
91
  - .travis.yml
91
92
  - Gemfile
92
93
  - LICENCE.md