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

"itamae list" command to list itamae plugin recipes #321

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions lib/itamae.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require "itamae/ext"
require "itamae/generators"
require "itamae/mash"
require "itamae/list"

module Itamae
# Your code goes here...
Expand Down
5 changes: 5 additions & 0 deletions lib/itamae/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ def destroy(target, name)
generator.remove_files
end

desc 'list', 'list plugin recipes'
def list
Itamae::List.new.run
end

private
def options
@itamae_options ||= super.dup.tap do |options|
Expand Down
69 changes: 69 additions & 0 deletions lib/itamae/list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module Itamae
class List
def run
require 'rubygems'
require 'rubygems/exceptions'
require 'pathname'

pattern = /^itamae-plugin-recipe/
# from rubygems Gem::Commands::QueryCommand#show_local_gems
specs = Gem::Specification.find_all do |s|
s.name =~ pattern
end

req = Gem::Requirement.default
dep = Gem::Deprecate.skip_during { Gem::Dependency.new pattern, req }
specs.select! do |s|
dep.match?(s.name, s.version, false)
end
Comment on lines +19 to +23
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fuminori-ido I can't understand these lines. What purpose of this?

spec_tuples = specs.map do |spec|
[spec.name_tuple, spec]
end

# from rubygems Gem::Commands::QueryCommand#output_query_results
versions = Hash.new { |h,name| h[name] = [] }
spec_tuples.each do |spec_tuple, source|
versions[spec_tuple.name] << [spec_tuple, source]
end
versions = versions.sort_by do |(n,_),_|
n.downcase
end

# from rubygems Gem::Commands::QueryCommand#output_versions
versions.each do |gem_name, matching_tuples|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is gem_name unused? I think you should add a prefix _ if that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your pointing out! I did so at 80fadc1

matching_tuples = matching_tuples.sort_by { |n,_| n.version }.reverse
platforms = Hash.new { |h,version| h[version] = [] }
matching_tuples.each do |n, _|
platforms[n.version] << n.platform if n.platform
end
seen = {}
matching_tuples.delete_if do |n,_|
if seen[n.version]
true
else
seen[n.version] = true
false
end
end
scan_lib(matching_tuples[0][1].name, matching_tuples[0][1].gem_dir)
end
end

private

def scan_lib(name, dir)
puts name + ' gem:'
Dir.glob(dir + '/lib/itamae/plugin/recipe/**/*.rb') do |f|
puts ' ' + to_recipe_name(f, dir)
end
end

def to_recipe_name(path, dir)
pn = Pathname.new(path)
relative_path = pn.relative_path_from(Pathname.new(dir + '/lib/itamae/plugin/recipe')).to_s
relative_path.gsub(/\.rb$/, '').
gsub('/', '::').
gsub(/::default$/, '')
end
end
end