Skip to content

Commit

Permalink
Rubocop it up
Browse files Browse the repository at this point in the history
  • Loading branch information
gjtorikian committed Nov 17, 2019
1 parent fdf77b3 commit 51396fe
Show file tree
Hide file tree
Showing 17 changed files with 190 additions and 132 deletions.
16 changes: 16 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
inherit_gem:
rubocop-standard:
- config/default.yml

Style/StringLiterals:
Enabled: true
EnforcedStyle: single_quotes

RequireParentheses:
Enabled: true

Naming/FileName:
Enabled: false

Style/Documentation:
Enabled: false
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,13 @@ rvm:
- 2.5
- 2.6

git:
depth: 10

sudo: false
cache: bundler

matrix:
include:
- script: bundle exec rake rubocop
rvm: 2.6.0
6 changes: 4 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
source "https://rubygems.org"
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec

gem "jekyll", ENV["JEKYLL_VERSION"] if ENV["JEKYLL_VERSION"]
gem 'jekyll', ENV['JEKYLL_VERSION'] if ENV['JEKYLL_VERSION']
8 changes: 7 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# frozen_string_literal: true

require 'bundler'
Bundler::GemHelper.install_tasks

require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task default: :spec

require 'rubocop/rake_task'

RuboCop::RakeTask.new(:rubocop)
29 changes: 17 additions & 12 deletions jekyll-last-modified-at.gemspec
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
require File.expand_path('../lib/jekyll-last-modified-at/version.rb', __FILE__)
# frozen_string_literal: true

require File.expand_path('lib/jekyll-last-modified-at/version.rb', __dir__)
Gem::Specification.new do |s|
s.name = "jekyll-last-modified-at"
s.name = 'jekyll-last-modified-at'
s.version = Jekyll::LastModifiedAt::VERSION
s.summary = "A liquid tag for Jekyll to indicate the last time a file was modified."
s.authors = "Garen J. Torikian"
s.homepage = "https://github.com/gjtorikian/jekyll-last-modified-at"
s.license = "MIT"
s.files = Dir["lib/**/*.rb"]
s.summary = 'A liquid tag for Jekyll to indicate the last time a file was modified.'
s.authors = 'Garen J. Torikian'
s.homepage = 'https://github.com/gjtorikian/jekyll-last-modified-at'
s.license = 'MIT'
s.files = Dir['lib/**/*.rb']

s.add_dependency "jekyll", ">= 3.7", " < 5.0"
s.add_dependency "posix-spawn", "~> 0.3.9"
s.add_dependency 'jekyll', '>= 3.7', ' < 5.0'
s.add_dependency 'posix-spawn', '~> 0.3.9'

s.add_development_dependency "rspec", "~> 3.4"
s.add_development_dependency "rake"
s.add_development_dependency "spork"
s.add_development_dependency 'rake'
s.add_development_dependency 'rspec', '~> 3.4'
s.add_development_dependency 'rubocop'
s.add_development_dependency 'rubocop-performance'
s.add_development_dependency 'rubocop-standard'
s.add_development_dependency 'spork'
end
8 changes: 3 additions & 5 deletions lib/jekyll-last-modified-at.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true

module Jekyll
module LastModifiedAt
Expand All @@ -8,10 +9,7 @@ module LastModifiedAt
autoload :Hook, 'jekyll-last-modified-at/hook'
autoload :Git, 'jekyll-last-modified-at/git'

Tag ; Hook

PATH_CACHE = {}

REPO_CACHE = {}
PATH_CACHE = {} # rubocop:disable Style/MutableConstant
REPO_CACHE = {} # rubocop:disable Style/MutableConstant
end
end
25 changes: 14 additions & 11 deletions lib/jekyll-last-modified-at/determinator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Jekyll
module LastModifiedAt
class Determinator
Expand All @@ -11,27 +13,27 @@ def initialize(site_source, page_path, opts = {})

def git
return REPO_CACHE[site_source] unless REPO_CACHE[site_source].nil?

REPO_CACHE[site_source] = Git.new(site_source)
REPO_CACHE[site_source]
end

def formatted_last_modified_date
return PATH_CACHE[page_path] unless PATH_CACHE[page_path].nil?

last_modified = last_modified_at_time.strftime(format)
PATH_CACHE[page_path] = last_modified
last_modified
end

def last_modified_at_time
unless File.exists? absolute_path_to_article
raise Errno::ENOENT, "#{absolute_path_to_article} does not exist!"
end
raise Errno::ENOENT, "#{absolute_path_to_article} does not exist!" unless File.exist? absolute_path_to_article

Time.at(last_modified_at_unix.to_i)
end

def last_modified_at_unix
if git.is_git_repo?
if git.git_repo?
last_commit_date = Executor.sh(
'git',
'--git-dir',
Expand All @@ -44,7 +46,7 @@ def last_modified_at_unix
relative_path_from_git_dir
)[/\d+/]
# last_commit_date can be nil iff the file was not committed.
(last_commit_date.nil? || last_commit_date.empty?) ? mtime(absolute_path_to_article) : last_commit_date
last_commit_date.nil? || last_commit_date.empty? ? mtime(absolute_path_to_article) : last_commit_date
else
mtime(absolute_path_to_article)
end
Expand All @@ -59,7 +61,7 @@ def to_liquid
end

def format
opts['format'] ||= "%d-%b-%y"
opts['format'] ||= '%d-%b-%y'
end

def format=(new_format)
Expand All @@ -69,15 +71,16 @@ def format=(new_format)
private

def absolute_path_to_article
@article_file_path ||= Jekyll.sanitized_path(site_source, @page_path)
@absolute_path_to_article ||= Jekyll.sanitized_path(site_source, @page_path)
end

def relative_path_from_git_dir
return nil unless git.is_git_repo?
return nil unless git.git_repo?

@relative_path_from_git_dir ||= Pathname.new(absolute_path_to_article)
.relative_path_from(
Pathname.new(File.dirname(git.top_level_directory))
).to_s
.relative_path_from(
Pathname.new(File.dirname(git.top_level_directory))
).to_s
end

def mtime(file)
Expand Down
23 changes: 14 additions & 9 deletions lib/jekyll-last-modified-at/executor.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'posix/spawn'

module Jekyll
Expand All @@ -8,23 +10,26 @@ module Executor
def self.sh(*args)
r, w = IO.pipe
e, eo = IO.pipe
pid = spawn(*args, {
:out => w, r => :close,
:err => eo, e => :close
})
pid = spawn(*args,
:out => w, r => :close,
:err => eo, e => :close)

if pid > 0
if pid.positive?
w.close
eo.close
out = r.read
err = e.read
::Process.waitpid(pid)
if out
"#{out} #{err}".strip
end
"#{out} #{err}".strip if out
end
ensure
[r, w, e, eo].each{ |io| io.close rescue nil }
[r, w, e, eo].each do |io|
begin
io.close
rescue StandardError
nil
end
end
end
end
end
Expand Down
21 changes: 12 additions & 9 deletions lib/jekyll-last-modified-at/git.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Jekyll
module LastModifiedAt
class Git
Expand All @@ -9,27 +11,28 @@ def initialize(site_source)
end

def top_level_directory
return nil unless is_git_repo?
return nil unless git_repo?

@top_level_directory ||= begin
Dir.chdir(@site_source) do
top_level_directory = File.join(Executor.sh("git", "rev-parse", "--show-toplevel"), ".git")
@top_level_directory = File.join(Executor.sh('git', 'rev-parse', '--show-toplevel'), '.git')
end
rescue
""
rescue StandardError
''
end
end

def is_git_repo?
def git_repo?
return @is_git_repo unless @is_git_repo.nil?

@is_git_repo = begin
Dir.chdir(@site_source) do
Executor.sh("git", "rev-parse", "--is-inside-work-tree").eql? "true"
Executor.sh('git', 'rev-parse', '--is-inside-work-tree').eql? 'true'
end
rescue
false
rescue StandardError
false
end
end

end
end
end
2 changes: 2 additions & 0 deletions lib/jekyll-last-modified-at/hook.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Jekyll
module LastModifiedAt
module Hook
Expand Down
9 changes: 5 additions & 4 deletions lib/jekyll-last-modified-at/tag.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Jekyll
module LastModifiedAt
class Tag < Liquid::Tag
Expand All @@ -8,11 +10,10 @@ def initialize(tag_name, format, tokens)

def render(context)
site_source = context.registers[:site].source
article_file = context.environments.first["page"]["path"]
article_file = context.environments.first['page']['path']

Determinator.new(site_source, article_file, {
"format" => @format
}).formatted_last_modified_date
Determinator.new(site_source, article_file,
'format' => @format).formatted_last_modified_date
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/jekyll-last-modified-at/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

module Jekyll
module LastModifiedAt
VERSION = "1.1.0"
VERSION = '1.1.0'
end
end
Loading

0 comments on commit 51396fe

Please sign in to comment.