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

Add a --cache option and fix CI #20

Merged
merged 7 commits into from
Aug 13, 2021
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
50 changes: 50 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: gha-workflow-libyear-bundler-test
on: [push, pull_request]
jobs:

# Linting is a separate job, primary because it only needs to be done once,
# and secondarily because jobs are performed concurrently.
gha-job-pt-lint:
name: Lint
runs-on: ubuntu-18.04
steps:
- name: Checkout source
uses: actions/checkout@v2
- name: Setup ruby
uses: ruby/setup-ruby@v1
with:
# Lowest supported ruby version. See gemspec
ruby-version: '2.1'
- name: Bundle
run: |
bundle install --jobs 4 --retry 3
- name: Lint
run: bundle exec rubocop
gha-job-pt-test:
name: Ruby ${{ matrix.ruby }}
runs-on: ubuntu-18.04
strategy:
# Currently a one-dimensional matrix of ruby versions. In the future we
# add bundler version as the second dimension.
matrix:
# See lowest supported ruby version in gemspec
ruby:
- '2.1'
- '2.2'
- '2.3'
- '2.4'
- '2.5'
- '2.6'
- '2.7'
- '3.0'
steps:
- name: Checkout source
uses: actions/checkout@v2
- name: Setup ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
- name: Bundle
run: bundle install --jobs 4 --retry 3
- name: Test
run: bundle exec rspec
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
/spec/reports/
/tmp/
.ruby-version
.idea
.byebug_history
16 changes: 13 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,25 @@ Layout/MultilineMethodCallIndentation:
Metrics/AbcSize:
Max: 27

# Not a useful metric compared to, e.g. `AbcSize`.
Metrics/BlockLength:
Exclude:
- 'spec/**/*'
Enabled: false

# Please use semantic style, e.g. `do` when there's a side-effect, else `{}`.
# The semantic style is too nuanced to lint, so the cop is disabled.
Style/BlockDelimiters:
Enabled: false

# Annotated tokens harm readability in 90% of format strings.
Style/FormatStringToken:
Enabled: false

# The decision of when to use a guard clause to improve readability is subtle,
# and it's not clear that it can be linted. Certainly, the default
# `MinBodyLength` of 1 can actually hurt readability.
Style/GuardClause:
Enabled: false

# Too subtle to lint. Prefer normal conditionals, except on very simple lines.
Style/IfUnlessModifier:
Enabled: false
Expand All @@ -36,4 +47,3 @@ Metrics/MethodLength:

Style/StringLiterals:
Enabled: false

18 changes: 0 additions & 18 deletions .travis.yml

This file was deleted.

22 changes: 21 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,29 @@

Pull requests are welcome.

## Support for old rubies

We test all minor versions of ruby, back to 2.1. It's important that people with
badly out-of-date systems can still measure how bad they are.

### Installing old rubies

> When building Ruby 2.3 or older, [use] OpenSSL 1.0 ..
> https://github.com/rbenv/ruby-build/wiki#openssl-version-compatibility

```bash
brew install rbenv/tap/openssl@1.0
RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@1.0)" \
rbenv install 2.1.10
```

## Test

```bash
rbenv shell 2.1.10 # See installing, above
bundle install
bin/test
bundle exec rubocop
bundle exec rspec
```

## Releases
Expand Down
2 changes: 1 addition & 1 deletion bin/run
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
# Used only by the maintainers of libyear_bundler, this file is a convenient
# way to run during local development.

ruby -I lib -r libyear_bundler -e 'LibyearBundler::CLI.new(ARGV).run'
ruby -I lib -r libyear_bundler -e 'LibyearBundler::CLI.new(ARGV).run' -- $@
6 changes: 0 additions & 6 deletions bin/test

This file was deleted.

6 changes: 4 additions & 2 deletions lib/libyear_bundler/bundle_outdated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ class BundleOutdated
# Format of `bundle outdated --parseable` (BOP)
BOP_FMT = /\A(?<name>[^ ]+) \(newest (?<newest>[^,]+), installed (?<installed>[^,)]+)/

def initialize(gemfile_path)
def initialize(gemfile_path, release_date_cache)
@gemfile_path = gemfile_path
@release_date_cache = release_date_cache
end

def execute
Expand All @@ -27,7 +28,8 @@ def execute
gem = ::LibyearBundler::Models::Gem.new(
match['name'],
match['installed'],
match['newest']
match['newest'],
@release_date_cache
)
gems.push(gem)
end
Expand Down
19 changes: 17 additions & 2 deletions lib/libyear_bundler/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require "bundler/cli/outdated"
require "libyear_bundler/bundle_outdated"
require "libyear_bundler/options"
require "libyear_bundler/release_date_cache"
require "libyear_bundler/report"
require 'libyear_bundler/models/ruby'

Expand All @@ -27,6 +28,12 @@ def run
else
print report.to_s
end

# Update cache
cache_path = @options.cache_path
if cache_path && release_date_cache
release_date_cache.save(cache_path)
end
end

private
Expand Down Expand Up @@ -54,7 +61,15 @@ def load_gemfile_path
end

def bundle_outdated
BundleOutdated.new(@gemfile_path).execute
BundleOutdated.new(@gemfile_path, release_date_cache).execute
end

def release_date_cache
@_release_date_cache ||= begin
path = @options.cache_path
return if path.nil?
ReleaseDateCache.load(path)
end
end

def report
Expand All @@ -63,7 +78,7 @@ def report

def ruby
lockfile = @gemfile_path + '.lock'
::LibyearBundler::Models::Ruby.new(lockfile)
::LibyearBundler::Models::Ruby.new(lockfile, release_date_cache)
end

def grand_total
Expand Down
68 changes: 40 additions & 28 deletions lib/libyear_bundler/models/gem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,51 @@ module Models
# Logic and information pertaining to the installed and newest versions of
# a gem
class Gem
def initialize(name, installed_version, newest_version)
def initialize(name, installed_version, newest_version, release_date_cache)
unless release_date_cache.nil? || release_date_cache.is_a?(ReleaseDateCache)
raise TypeError, 'Invalid release_date_cache'
end
@name = name
@installed_version = installed_version
@newest_version = newest_version
@release_date_cache = release_date_cache
end

class << self
def release_date(gem_name, gem_version)
dep = nil
begin
dep = ::Bundler::Dependency.new(gem_name, gem_version)
rescue ::Gem::Requirement::BadRequirementError => e
$stderr.puts "Could not find release date for: #{gem_name}"
$stderr.puts(e)
$stderr.puts(
"Maybe you used git in your Gemfile, which libyear doesn't support " \
"yet. Contributions welcome."
)
return nil
end
tuples, _errors = ::Gem::SpecFetcher.fetcher.search_for_dependency(dep)
if tuples.empty?
$stderr.puts "Could not find release date for: #{gem_name}"
return nil
end
tup, source = tuples.first # Gem::NameTuple
spec = source.fetch_spec(tup) # raises Gem::RemoteFetcher::FetchError
spec.date.to_date
end
end

def installed_version
::Gem::Version.new(@installed_version)
end

def installed_version_release_date
release_date(name, installed_version)
if @release_date_cache.nil?
self.class.release_date(name, installed_version)
else
@release_date_cache[name, installed_version]
end
end

def installed_version_sequence_index
Expand All @@ -45,7 +78,11 @@ def newest_version_sequence_index
end

def newest_version_release_date
release_date(name, newest_version)
if @release_date_cache.nil?
self.class.release_date(name, newest_version)
else
@release_date_cache[name, newest_version]
end
end

def version_number_delta
Expand Down Expand Up @@ -74,31 +111,6 @@ def versions_sequence
parsed_response.map { |version| version['number'] }
end
end

# Known issue: Probably performs a network request every time, unless
# there's some kind of caching.
def release_date(gem_name, gem_version)
dep = nil
begin
dep = ::Bundler::Dependency.new(gem_name, gem_version)
rescue ::Gem::Requirement::BadRequirementError => e
$stderr.puts "Could not find release date for: #{gem_name}"
$stderr.puts(e)
$stderr.puts(
"Maybe you used git in your Gemfile, which libyear doesn't support " \
"yet. Contributions welcome."
)
return nil
end
tuples, _errors = ::Gem::SpecFetcher.fetcher.search_for_dependency(dep)
if tuples.empty?
$stderr.puts "Could not find release date for: #{gem_name}"
return nil
end
tup, source = tuples.first # Gem::NameTuple
spec = source.fetch_spec(tup) # raises Gem::RemoteFetcher::FetchError
spec.date.to_date
end
end
end
end
Loading