Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to call collection coomand in chid dir #1135

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/rbs/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def initialize()

@libs = []
@dirs = []
@config_path = Collection::Config::PATH
@config_path = Collection::Config.find_config_path || Collection::Config::PATH
end

def loader
Expand Down Expand Up @@ -1034,16 +1034,17 @@ def run_collection(args, options)
opts.order args.drop(1), into: params
config_path = options.config_path or raise
lock_path = Collection::Config.to_lockfile_path(config_path)
gemfile_lock_path = Bundler.default_lockfile

case args[0]
when 'install'
unless params[:frozen]
Collection::Config.generate_lockfile(config_path: config_path, gemfile_lock_path: Pathname('./Gemfile.lock'))
Collection::Config.generate_lockfile(config_path: config_path, gemfile_lock_path: gemfile_lock_path)
end
Collection::Installer.new(lockfile_path: lock_path, stdout: stdout).install_from_lockfile
when 'update'
# TODO: Be aware of argv to update only specified gem
Collection::Config.generate_lockfile(config_path: config_path, gemfile_lock_path: Pathname('./Gemfile.lock'), with_lockfile: false)
Collection::Config.generate_lockfile(config_path: config_path, gemfile_lock_path: gemfile_lock_path, with_lockfile: false)
Collection::Installer.new(lockfile_path: lock_path, stdout: stdout).install_from_lockfile
when 'init'
if config_path.exist?
Expand Down
21 changes: 21 additions & 0 deletions lib/rbs/collection/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ def initialize

PATH = Pathname('rbs_collection.yaml')

def self.find_config_path
current = Pathname.pwd

loop do
config_path = current.join(PATH)
return config_path if config_path.exist?
current = current.join('..')
return nil if current.root?
end
end

# Generate a rbs lockfile from Gemfile.lock to `config_path`.
# If `with_lockfile` is true, it respects existing rbs lockfile.
def self.generate_lockfile(config_path:, gemfile_lock_path:, with_lockfile: true)
Expand Down Expand Up @@ -69,6 +80,16 @@ def gems
@data['gems'] ||= []
end

def gemfile_lock_path=(path)
@data['gemfile_lock_path'] = path.relative_path_from(@config_path.dirname).to_s
end

def gemfile_lock_path
path = @data['gemfile_lock_path']
return unless path
@config_path.dirname.join path
end

# It raises an error when there are non-available libraries
def check_rbs_availability!
raise CollectionNotAvailable unless repo_path.exist?
Expand Down
30 changes: 28 additions & 2 deletions lib/rbs/collection/config/lockfile_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@

module RBS
module Collection

# This class represent the configration file.
class Config
class LockfileGenerator
class GemfileLockMismatchError < StandardError
def initialize(expected:, actual:)
@expected = expected
@actual = actual
end

def message
<<~MESSAGE
RBS Collection loads a different Gemfile.lock from before.
The Gemfile.lock must be the same as that is recorded in rbs_collection.lock.yaml.
Expected Gemfile.lock: #{@expected}
Actual Gemfile.lock: #{@actual}
MESSAGE
end
end

attr_reader :config, :lock, :gemfile_lock, :lock_path

def self.generate(config_path:, gemfile_lock_path:, with_lockfile: true)
Expand All @@ -18,6 +32,10 @@ def initialize(config_path:, gemfile_lock_path:, with_lockfile:)
@lock = Config.from_path(lock_path) if lock_path.exist? && with_lockfile
@gemfile_lock = Bundler::LockfileParser.new(gemfile_lock_path.read)
@gem_queue = []

validate_gemfile_lock_path!(lock: lock, gemfile_lock_path: gemfile_lock_path)

config.gemfile_lock_path = gemfile_lock_path
end

def generate
Expand All @@ -38,6 +56,14 @@ def generate
config
end

private def validate_gemfile_lock_path!(lock:, gemfile_lock_path:)
return unless lock
return unless lock.gemfile_lock_path
return if lock.gemfile_lock_path == gemfile_lock_path

raise GemfileLockMismatchError.new(expected: lock.gemfile_lock_path, actual: gemfile_lock_path)
end

private def assign_gem(name:, version:)
# @type var locked: gem_entry?
locked = lock&.gem(name)
Expand Down
8 changes: 8 additions & 0 deletions sig/collection/config.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ module RBS

private

def validate_gemfile_lock_path!: (lock: Config?, gemfile_lock_path: Pathname) -> void

def assign_gem: (name: String, version: String?) -> void

def upsert_gem: (gem_entry? old, gem_entry new) -> void
Expand Down Expand Up @@ -52,6 +54,8 @@ module RBS

@sources: Array[Sources::_Source]

def self.find_config_path: () -> Pathname?

def self.generate_lockfile: (config_path: Pathname, gemfile_lock_path: Pathname, ?with_lockfile: boolish) -> Config

def self.from_path: (Pathname path) -> Config
Expand All @@ -75,6 +79,10 @@ module RBS

def gems: () -> Array[gem_entry]

def gemfile_lock_path=: (Pathname) -> Pathname

def gemfile_lock_path: () -> Pathname?

def check_rbs_availability!: () -> void
end
end
Expand Down
17 changes: 15 additions & 2 deletions test/rbs/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,21 @@ def test_collection_install

with_cli do |cli|
cli.run(%w[collection install])
assert dir.join('rbs_collection.lock.yaml').exist?
assert dir.join('gem_rbs_collection/ast').exist?

rbs_collection_lock = dir.join('rbs_collection.lock.yaml')
assert rbs_collection_lock.exist?
rbs_collection_lock.delete

collection_dir = dir.join('gem_rbs_collection/ast')
assert collection_dir.exist?
collection_dir.rmtree

Dir.mkdir("child")
Dir.chdir("child") do
cli.run(%w[collection install])
assert rbs_collection_lock.exist?
assert collection_dir.exist?
end
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions test/rbs/collection/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def test_generate_lock_from_collection_repository
revision: cde6057e7546843ace6420c5783dd945c6ccda54
repo_dir: gems
path: "/path/to/somewhere"
gemfile_lock_path: 'Gemfile.lock'
gems:
- name: ast
version: "2.4"
Expand Down Expand Up @@ -107,6 +108,7 @@ def test_generate_lock_from_relative_git_repository
revision: b4d3b346d9657543099a35a1fd20347e75b8c523
repo_dir: gems
path: "/path/to/somewhere"
gemfile_lock_path: 'Gemfile.lock'
gems:
- name: ast
version: "2.4"
Expand Down Expand Up @@ -143,6 +145,7 @@ def test_generate_lock_from_collection_repository_with_lockfile
revision: cde6057e7546843ace6420c5783dd945c6ccda54
repo_dir: gems
path: "/path/to/somewhere"
gemfile_lock_path: 'Gemfile.lock'
gems:
- name: ast
version: "2.4"
Expand Down Expand Up @@ -195,6 +198,7 @@ def test_generate_lock_from_collection_repository_ignoring
revision: cde6057e7546843ace6420c5783dd945c6ccda54
repo_dir: gems
path: "/path/to/somewhere"
gemfile_lock_path: 'Gemfile.lock'
gems:
- name: rainbow
ignore: false
Expand Down Expand Up @@ -243,6 +247,7 @@ def test_generate_lock_from_collection_repository_specified
revision: cde6057e7546843ace6420c5783dd945c6ccda54
repo_dir: gems
path: "/path/to/somewhere"
gemfile_lock_path: 'Gemfile.lock'
gems:
- name: ast
version: "2.4"
Expand Down Expand Up @@ -308,6 +313,7 @@ def test_generate_lock_from_collection_with_manifest_yaml
revision: cde6057e7546843ace6420c5783dd945c6ccda54
repo_dir: gems
path: "/path/to/somewhere"
gemfile_lock_path: 'Gemfile.lock'
gems:
- name: activesupport
version: "6.0"
Expand Down Expand Up @@ -381,6 +387,7 @@ def test_generate_lock_from_stdlib
revision: cde6057e7546843ace6420c5783dd945c6ccda54
repo_dir: gems
path: "/path/to/somewhere"
gemfile_lock_path: 'Gemfile.lock'
gems:
- name: csv
version: "0"
Expand Down Expand Up @@ -428,6 +435,7 @@ def test_generate_lock_from_rubygems
revision: cde6057e7546843ace6420c5783dd945c6ccda54
repo_dir: gems
path: "/path/to/somewhere"
gemfile_lock_path: 'Gemfile.lock'
gems:
- name: rbs-amber
version: "1.0.0"
Expand Down Expand Up @@ -471,11 +479,29 @@ def test_generate_lock_with_empty_gemfile_lock
revision: cde6057e7546843ace6420c5783dd945c6ccda54
repo_dir: gems
path: "/path/to/somewhere"
gemfile_lock_path: 'Gemfile.lock'
gems: []
YAML
end
end

def test_generate_lock_with_different_gemfile_lock
mktmpdir do |tmpdir|
config_path = tmpdir / 'rbs_collection.yaml'
config_path.write CONFIG
lock_path = tmpdir / 'rbs_collection.lock.yaml'
lock_path.write CONFIG + "gemfile_lock_path: Gemfile.lock"
gemfile_lock_path = tmpdir / 'Gemfile.lock'
gemfile_lock_path.write GEMFILE_LOCK
gemfile_lock_path2 = tmpdir / 'Gemfile.2.lock'
gemfile_lock_path2.write GEMFILE_LOCK

assert_raises(RBS::Collection::Config::LockfileGenerator::GemfileLockMismatchError) do
RBS::Collection::Config.generate_lockfile(config_path: config_path, gemfile_lock_path: gemfile_lock_path2)
end
end
end

def test_repo_path
mktmpdir do |tmpdir|
config_path = tmpdir / 'rbs_collection.yaml'
Expand Down