yard-sftp 1.0.3 → 1.1.0
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 +4 -4
- data/.ruby-version +1 -0
- data/README.md +2 -1
- data/lib/yard-sftp/version.rb +1 -1
- data/lib/yard/core_ext/file.rb +61 -43
- metadata +2 -1
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 374d10d47e1269d06b96f8b4d8974159567aa3f7
         | 
| 4 | 
            +
              data.tar.gz: fa22ce24200815406771c182fdc691f4e25e7db1
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: dd658d85251d41ee2da9ad07c1c29bac12d9cd6357ff17b93718d7b839faea2b7d0de17be477d552260b50bd7fd078a50c01b298d5ea56ac522ff369b6fdb53d
         | 
| 7 | 
            +
              data.tar.gz: 46b8664f4c46d375d7682e91c616d756d27ac499749095e1226efc47fa25269a06f58d3dcedb9bfc4484a5e8a893be0a914dfaec72bd3681d464897b75316a57
         | 
    
        data/.ruby-version
    ADDED
    
    | @@ -0,0 +1 @@ | |
| 1 | 
            +
            2.0.0-p247
         | 
    
        data/README.md
    CHANGED
    
    
    
        data/lib/yard-sftp/version.rb
    CHANGED
    
    
    
        data/lib/yard/core_ext/file.rb
    CHANGED
    
    | @@ -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 | 
            -
                # | 
| 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 | 
            -
             | 
| 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 | 
            -
                 | 
| 48 | 
            -
                 | 
| 47 | 
            +
                paths = sftp_split_all(file_path)
         | 
| 48 | 
            +
                paths.pop
         | 
| 49 | 
            +
                return if paths.include?(".yardoc")
         | 
| 49 50 |  | 
| 50 | 
            -
                unless  | 
| 51 | 
            -
                   | 
| 51 | 
            +
                unless paths.empty?
         | 
| 52 | 
            +
                  sftp_create_paths(paths)
         | 
| 52 53 | 
             
                end
         | 
| 53 54 |  | 
| 54 | 
            -
                 | 
| 55 | 
            +
                log.progress("Uploading #{file_path}", nil)
         | 
| 56 | 
            +
                sftp.upload!(file_path, "#{BASE_DIR}/#{file_path}")
         | 
| 55 57 | 
             
              end
         | 
| 56 58 |  | 
| 57 | 
            -
              # Creates  | 
| 59 | 
            +
              # Creates paths relevant for file
         | 
| 58 60 | 
             
              #
         | 
| 59 | 
            -
              # @param opts [Array]  | 
| 60 | 
            -
              def self. | 
| 61 | 
            -
                 | 
| 62 | 
            -
             | 
| 63 | 
            -
             | 
| 64 | 
            -
             | 
| 65 | 
            -
             | 
| 66 | 
            -
                   | 
| 67 | 
            -
                     | 
| 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  | 
| 75 | 
            -
               | 
| 76 | 
            -
             | 
| 77 | 
            -
             | 
| 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 | 
            -
              #  | 
| 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. | 
| 85 | 
            -
             | 
| 86 | 
            -
                   | 
| 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  | 
| 102 | 
            +
              # Checks if the path exists
         | 
| 91 103 | 
             
              #
         | 
| 92 104 | 
             
              # @param opts [String] path the path of directory
         | 
| 93 | 
            -
               | 
| 94 | 
            -
             | 
| 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 | 
            -
              #  | 
| 114 | 
            +
              # Removes file
         | 
| 104 115 | 
             
              #
         | 
| 105 | 
            -
              # @param opts [String] path the path of  | 
| 106 | 
            -
               | 
| 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 | 
            -
               | 
| 120 | 
            -
               | 
| 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 | 
| 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
         |