Skip to content

Commit 51d8156

Browse files
authored
Add rubyzip 3.2 type signature (#1022)
* init `rubyzip` version `3.2` * add boilerplate types for `Zip::File` and `Zip::Entry` from the `rambling-trie` gem: https://github.com/gonzedge/rambling-trie/tree/bc3cb892096887bdd2e50594143047a77cfa5b6e/sig/lib/zip * first pass at generating all .rbs files * first pass at test_example, based off of 2.3 * add `zlib` dependency * remove `DelegateClass(Entry)`, use `< Entry` to get around it unfortunately, `DelegateClass[Entry]` does not work yet per upstream rbs there are Github issues requesting that feature since Nov 2021, last one mentioning it is ruby/rbs#2061 * add `singleton` to deps * use `extend` for `FileSplit`, not `include` and fix tests corresponding tests to use correct params! * fix `(T | void)` to `T?` * rubocop -A
1 parent 824a000 commit 51d8156

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1265
-0
lines changed

gems/rubyzip/.rubocop.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1+
# This configuration inherits from /.rubocop.yml.
2+
# You can configure RBS style of this gem.
3+
# This file is used on CI. It is configured to automatically
4+
# make rubocop suggestions on pull requests for this gem.
5+
# If you do not like the style enforcement, you should remove this file.
16
inherit_from: ../../.rubocop.yml
27

8+
##
9+
# If you want to customize the style, please consult with the gem reviewers.
10+
# You can see the list of cops at https://github.com/ksss/rubocop-on-rbs/blob/main/docs/modules/ROOT/pages/cops.adoc
11+
312
RBS/Layout:
413
Enabled: true
14+
515
RBS/Lint:
616
Enabled: true
17+
718
RBS/Style:
819
Enabled: true
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# If the test needs gem's RBS files, declare them here.
2+
#
3+
# additional_gems:
4+
# - GEM_NAME

gems/rubyzip/3.2/_test/test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Write Ruby code to test the RBS.
2+
# It is type checked by `steep check` command.
3+
4+
require "rubyzip"
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Taken from https://github.com/rubyzip/rubyzip/blob/v3.2.0/samples/example.rb
2+
3+
require 'zip'
4+
5+
####### Using ZipInputStream alone: #######
6+
7+
Zip::InputStream.open('example.zip') do |zis|
8+
entry = zis.get_next_entry
9+
print "First line of '#{entry&.name} (#{entry&.size} bytes): "
10+
puts "'#{zis.gets&.chomp}'"
11+
entry = zis.get_next_entry
12+
print "First line of '#{entry&.name} (#{entry&.size} bytes): "
13+
puts "'#{zis.gets&.chomp}'"
14+
end
15+
16+
####### Using ZipFile to read the directory of a zip file: #######
17+
18+
zf = Zip::File.new('example.zip')
19+
zf.each_with_index do |entry, index|
20+
puts "entry #{index} is #{entry.name}, size = #{entry.size}, compressed size = #{entry.compressed_size}"
21+
# use zf.get_input_stream(entry) to get a ZipInputStream for the entry
22+
# entry can be the ZipEntry object or any object which has a to_s method that
23+
# returns the name of the entry.
24+
end
25+
26+
####### Using ZipOutputStream to write a zip file: #######
27+
28+
Zip::OutputStream.open('exampleout.zip') do |zos|
29+
zos.put_next_entry('the first little entry')
30+
zos.puts 'Hello hello hello hello hello hello hello hello hello'
31+
32+
zos.put_next_entry('the second little entry')
33+
zos.puts 'Hello again'
34+
35+
# Use rubyzip or your zip client of choice to verify
36+
# the contents of exampleout.zip
37+
end
38+
39+
####### Using ZipFile to change a zip file: #######
40+
41+
Zip::File.open('exampleout.zip') do |zip_file|
42+
zip_file.add('thisFile.rb', 'example.rb')
43+
zip_file.rename('thisFile.rb', 'ILikeThisName.rb')
44+
zip_file.add('Again', 'example.rb')
45+
end
46+
47+
# Lets check
48+
Zip::File.open('exampleout.zip') do |zip_file|
49+
puts "Changed zip file contains: #{zip_file.entries.join(', ')}"
50+
zip_file.remove('Again')
51+
puts "Without 'Again': #{zip_file.entries.join(', ')}"
52+
end
53+
54+
####### Using ZipFile to split a zip file: #######
55+
56+
# Creating large zip file for splitting
57+
Zip::OutputStream.open('large_zip_file.zip') do |zos|
58+
puts 'Creating zip file...'
59+
10.times do |i|
60+
zos.put_next_entry("large_entry_#{i}.txt")
61+
zos.puts 'Hello' * 104_857_600
62+
end
63+
end
64+
65+
# Splitting created large zip file
66+
part_zips_count = Zip::File.split('large_zip_file.zip', segment_size: 2_097_152, delete_original: false, partial_zip_file_name: 'part_zip_file')
67+
puts "Zip file splitted in #{part_zips_count} parts"

gems/rubyzip/3.2/manifest.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# manifest.yaml describes dependencies which do not appear in the gemspec.
2+
# If this gem includes such dependencies, comment-out the following lines and
3+
# declare the dependencies.
4+
# If all dependencies appear in the gemspec, you should remove this file.
5+
#
6+
dependencies:
7+
- name: singleton
8+
- name: zlib

gems/rubyzip/3.2/rubyzip.rbs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module Zip
2+
DEFAULT_RESTORE_OPTIONS: { restore_ownership: bool, restore_permissions: bool, restore_times: bool }
3+
ZLIB_FLUSHING_STRATEGY: Integer
4+
5+
def self.unicode_names: () -> bool
6+
def self.unicode_names=: (bool) -> bool
7+
8+
def self.on_exists_proc: () -> bool
9+
def self.on_exists_proc=: (bool) -> bool
10+
11+
def self.continue_on_exists_proc: () -> bool
12+
def self.continue_on_exists_proc=: (bool) -> bool
13+
14+
def self.sort_entries: () -> bool
15+
def self.sort_entries=: (bool) -> bool
16+
17+
def self.default_compression: () -> Integer
18+
def self.default_compression=: (Integer) -> Integer
19+
20+
def self.write_zip64_support: () -> bool
21+
def self.write_zip64_support=: (bool) -> bool
22+
23+
def self.warn_invalid_date: () -> bool
24+
def self.warn_invalid_date=: (bool) -> bool
25+
26+
def self.case_insensitive_match: () -> bool
27+
def self.case_insensitive_match=: (bool) -> bool
28+
29+
def self.force_entry_names_encoding: () -> String?
30+
def self.force_entry_names_encoding=: (String?) -> String?
31+
32+
def self.validate_entry_sizes: () -> bool
33+
def self.validate_entry_sizes=: (bool) -> bool
34+
35+
def self.setup: () { (Zip) -> void } -> void
36+
37+
def self.reset!: () -> void
38+
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module Zip
2+
class CentralDirectory
3+
include Dirtyable
4+
5+
END_OF_CD_SIG: Integer
6+
ZIP64_END_OF_CD_SIG: Integer
7+
ZIP64_EOCD_LOCATOR_SIG: Integer
8+
STATIC_EOCD_SIZE: Integer
9+
ZIP64_STATIC_EOCD_SIZE: Integer
10+
ZIP64_EOCD_LOC_SIZE: Integer
11+
MAX_FILE_COMMENT_SIZE: Integer
12+
MAX_END_OF_CD_SIZE: Integer
13+
14+
attr_accessor comment: String
15+
16+
def initialize: (?EntrySet entries, ?String comment) -> void
17+
18+
def <<: (Entry entry) -> void
19+
20+
def delete: (Entry | String entry) -> Entry?
21+
22+
def each: () { (Entry) -> void } -> void
23+
24+
def entries: () -> Array[Entry]
25+
26+
def find_entry: (String | Entry entry_name) -> Entry?
27+
28+
def glob: (String pattern, ?Integer flags) ?{ (Entry) -> void } -> Array[Entry]
29+
30+
def include?: (Entry | String entry) -> bool
31+
32+
def size: () -> Integer
33+
34+
def read_from_stream: (IO io) -> void
35+
36+
def write_to_stream: (IO io, ?suppress_extra_fields: bool) -> void
37+
38+
def count_entries: (IO io) -> Integer
39+
40+
def ==: (untyped other) -> bool
41+
end
42+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Zip
2+
class Compressor
3+
def finish: () -> void
4+
end
5+
end

gems/rubyzip/3.2/zip/constants.rbs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module Zip
2+
WINDOWS: bool
3+
4+
CENTRAL_DIRECTORY_ENTRY_SIGNATURE: Integer
5+
CDIR_ENTRY_STATIC_HEADER_LENGTH: Integer
6+
LOCAL_ENTRY_SIGNATURE: Integer
7+
LOCAL_ENTRY_STATIC_HEADER_LENGTH: Integer
8+
LOCAL_ENTRY_TRAILING_DESCRIPTOR_SIZE: Integer
9+
SPLIT_FILE_SIGNATURE: Integer
10+
11+
VERSION_MADE_BY: Integer
12+
VERSION_NEEDED_TO_EXTRACT: Integer
13+
VERSION_NEEDED_TO_EXTRACT_ZIP64: Integer
14+
15+
FILE_TYPE_FILE: Integer
16+
FILE_TYPE_DIR: Integer
17+
FILE_TYPE_SYMLINK: Integer
18+
19+
FSTYPE_FAT: Integer
20+
FSTYPE_AMIGA: Integer
21+
FSTYPE_VMS: Integer
22+
FSTYPE_UNIX: Integer
23+
FSTYPE_VM_CMS: Integer
24+
FSTYPE_ATARI: Integer
25+
FSTYPE_HPFS: Integer
26+
FSTYPE_MAC: Integer
27+
FSTYPE_Z_SYSTEM: Integer
28+
FSTYPE_CPM: Integer
29+
FSTYPE_TOPS20: Integer
30+
FSTYPE_NTFS: Integer
31+
FSTYPE_QDOS: Integer
32+
FSTYPE_ACORN: Integer
33+
FSTYPE_VFAT: Integer
34+
FSTYPE_MVS: Integer
35+
FSTYPE_BEOS: Integer
36+
FSTYPE_TANDEM: Integer
37+
FSTYPE_THEOS: Integer
38+
FSTYPE_MAC_OSX: Integer
39+
FSTYPE_ATHEOS: Integer
40+
41+
FSTYPES: Hash[Integer, String]
42+
43+
COMPRESSION_METHOD_STORE: Integer
44+
COMPRESSION_METHOD_DEFLATE: Integer
45+
46+
COMPRESSION_METHODS: Hash[Integer, String]
47+
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module Zip
2+
module AESEncryption
3+
VERIFIER_LENGTH: Integer
4+
BLOCK_SIZE: Integer
5+
AUTHENTICATION_CODE_LENGTH: Integer
6+
VERSION_AE_1: Integer
7+
VERSION_AE_2: Integer
8+
VERSIONS: Array[Integer]
9+
STRENGTHS: Array[Integer]
10+
BITS: Hash[Integer, Integer]
11+
KEY_LENGTHS: Hash[Integer, Integer]
12+
SALT_LENGTHS: Hash[Integer, Integer]
13+
14+
def initialize: (String password, Integer strength) -> void
15+
16+
def header_bytesize: () -> Integer
17+
18+
def gp_flags: () -> Integer
19+
end
20+
21+
class AESDecrypter < Decrypter
22+
include AESEncryption
23+
24+
def decrypt: (String encrypted_data) -> String
25+
26+
def reset!: (String header) -> void
27+
28+
def check_integrity!: (IO io) -> void
29+
end
30+
end

0 commit comments

Comments
 (0)