rails_finder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rvmrc
19
+ .rspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails_finder.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Chris Thorn
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # RailsFinder
2
+
3
+ Finds Rails applications in a given directory and reports their version numbers.
4
+
5
+ Currently, I maintain quite a lot of Rails applications, and the recent flux of
6
+ security vulnerabilities left me wanting something a little more than one-off
7
+ shell scripts. I also felt like hacking on some Ruby that wasn't Rails, as it's
8
+ been a while since I've been able to. It felt good.
9
+
10
+ ## Installation
11
+
12
+ gem install rails_finder
13
+
14
+ ## Usage
15
+
16
+ To search in the current directory
17
+
18
+ $ find_rails
19
+
20
+ To specify a directory
21
+
22
+ $ find_rails path_to_search
23
+
24
+ Example
25
+
26
+ $ find_rails ~/code
27
+ the-oldtimer 2.3.16 /Users/chris/code/the-oldtimer
28
+ the-outlier 3.2 /Users/chris/code/the-outlier
29
+ the-good-one 3.2.11 /Users/chris/code/the-good-one
30
+ wat 4.0.0 /Users/chris/code/wat
31
+ templates n/a /Users/chris/code/rails/railties/lib/rails/generators/rails/app/templates
32
+
33
+ ## Limitations
34
+
35
+ * Only the Gemfile and config/environments.rb files are inspected. If the
36
+ version specified there is not the installed version, then the report will be
37
+ inaccurate. For example: `~> 3.2` will be reported as "3.2" regardless of the
38
+ installed version.
39
+
40
+ * The recursive search will pick up config files of dummy applications that are
41
+ only used for testing.
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/find_rails ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rails_finder"
4
+
5
+ def print_help
6
+ puts <<END
7
+ Finds Rails applications in the given directory and reports their
8
+ version numbers. If no directory is given, uses the current.
9
+ END
10
+ puts "\n\tusage: find_rails [directory]"
11
+ end
12
+
13
+ if ARGV.any? { |a| a == "-h" || a == "--help" }
14
+ print_help
15
+ exit
16
+ end
17
+
18
+ RailsFinder.run(ARGV.first || Dir.pwd)
@@ -0,0 +1,36 @@
1
+ require "rails_finder/gemfile"
2
+ require "rails_finder/environment_file"
3
+
4
+ module RailsFinder
5
+ class App
6
+ attr_reader :root
7
+
8
+ def initialize(root)
9
+ @root = root
10
+ end
11
+
12
+ def basename
13
+ File.basename(root)
14
+ end
15
+
16
+ def rails_version
17
+ if gemfile.exists?
18
+ gemfile.rails_version
19
+ elsif envfile.exists?
20
+ envfile.rails_version
21
+ else
22
+ "n/a"
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def gemfile
29
+ @gemfile ||= Gemfile.new(File.join(root, "Gemfile"))
30
+ end
31
+
32
+ def envfile
33
+ @envfile ||= EnvironmentFile.new(File.join(root, "config", "environment.rb"))
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,24 @@
1
+ module RailsFinder
2
+ class EnvironmentFile
3
+ attr_reader :path
4
+
5
+ def initialize(path)
6
+ @path = path
7
+ end
8
+
9
+ def exists?
10
+ File.exists?(path)
11
+ end
12
+
13
+ def rails_version
14
+ return @rails_version if @rails_version
15
+
16
+ version_line = File.readlines(path).find { |l| l =~ /RAILS_GEM_VERSION/ }
17
+ if version_line
18
+ @rails_version = version_line[/\d+\.\d+(\.\d+)?/].strip
19
+ else
20
+ @rails_version = "n/a"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ module RailsFinder
2
+ class Gemfile
3
+ attr_reader :path
4
+
5
+ def initialize(path)
6
+ @path = path
7
+ end
8
+
9
+ def exists?
10
+ File.exists?(path)
11
+ end
12
+
13
+ def rails_version
14
+ return @rails_version if @rails_version
15
+ line = File.readlines(path).find { |l| l =~ /^gem.+rails/i }
16
+ if line
17
+ @rails_version = line[/\d+\.\d+(\.\d+)?/]
18
+ else
19
+ @rails_version = "n/a"
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module RailsFinder
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,43 @@
1
+ require "rails_finder/version"
2
+ require "rails_finder/app"
3
+
4
+ module RailsFinder
5
+ def self.run(dir, output = $stdout)
6
+ Runner.new(dir, output).print
7
+ end
8
+
9
+ class Runner
10
+ attr_reader :dir, :output
11
+
12
+ def initialize(dir, output = $stdout)
13
+ @dir = dir
14
+ @output = output
15
+ end
16
+
17
+ def print
18
+ if apps.any?
19
+ apps.sort_by(&:rails_version).each do |app|
20
+ output.puts "#{app.basename.ljust(root_width)} #{app.rails_version.ljust(version_width)} #{app.root}"
21
+ end
22
+ else
23
+ output.puts "none found"
24
+ end
25
+ end
26
+
27
+ def apps
28
+ @apps ||= Dir["#{dir}/**/config/environment.rb"].map do |file|
29
+ App.new(File.expand_path("../..", file))
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def root_width
36
+ @root_label_width = (apps.map { |a| a.basename.length }.max || 0) + 2
37
+ end
38
+
39
+ def version_width
40
+ @version_width ||= (apps.map { |a| a.rails_version.length }.max || 0)+ 2
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails_finder/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "rails_finder"
8
+ gem.version = RailsFinder::VERSION
9
+ gem.authors = ["Chris Thorn"]
10
+ gem.email = ["thorncp@gmail.com"]
11
+ gem.description = %q{Overly simple utility to recursively find Rails apps and their versions}
12
+ gem.summary = %q{Overly simple utility to recursively find Rails apps and their versions}
13
+ gem.homepage = "https://github.com/thorncp/rails_finder"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency "rspec", "~> 2.12.0"
21
+ end
@@ -0,0 +1,35 @@
1
+ require "spec_helper"
2
+ require "rails_finder/app"
3
+
4
+ module RailsFinder
5
+ describe App do
6
+ it "knows path basname" do
7
+ with_dir do |dir|
8
+ path = File.join(dir, "awesome_app")
9
+ Dir.mkdir(path)
10
+ App.new(path).basename.should == "awesome_app"
11
+ end
12
+ end
13
+
14
+ it "reports Rails version from Gemfile" do
15
+ subject = App.new(stub)
16
+ subject.stub(gemfile: stub(exists?: true, rails_version: "3.2.11"))
17
+ subject.stub(envfile: stub(exists?: true, rails_version: "n/a"))
18
+ subject.rails_version.should == "3.2.11"
19
+ end
20
+
21
+ it "reports Rails version from environment file" do
22
+ subject = App.new(stub)
23
+ subject.stub(gemfile: stub(exists?: false, rails_version: "n/a"))
24
+ subject.stub(envfile: stub(exists?: true, rails_version: "2.3.16"))
25
+ subject.rails_version.should == "2.3.16"
26
+ end
27
+
28
+ it "reports n/a when no Rails version is found" do
29
+ subject = App.new(stub)
30
+ subject.stub(gemfile: stub(exists?: false))
31
+ subject.stub(envfile: stub(exists?: false))
32
+ subject.rails_version.should == "n/a"
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,67 @@
1
+ require "spec_helper"
2
+ require "rails_finder/environment_file"
3
+ require "tmpdir"
4
+ require "fileutils"
5
+
6
+ VALID_ENVFILE = <<END
7
+ RAILS_GEM_VERSION = '2.3.16' unless defined? RAILS_GEM_VERSION
8
+
9
+ require File.join(File.dirname(__FILE__), 'boot')
10
+
11
+ Rails::Initializer.run do |config|
12
+ config.time_zone = 'Pacific Time (US & Canada)'
13
+ config.cache_store = :mem_cache_store
14
+ end
15
+ END
16
+
17
+
18
+ module RailsFinder
19
+ describe EnvironmentFile do
20
+ it "knows if file exists" do
21
+ with_dir do |dir|
22
+ FileUtils.touch(envfile(dir))
23
+ EnvironmentFile.new(envfile(dir)).should exist
24
+ end
25
+ end
26
+
27
+ it "knows if file does not exist" do
28
+ with_dir do |dir|
29
+ EnvironmentFile.new(envfile(dir)).should_not exist
30
+ end
31
+ end
32
+
33
+ it "reports Rails version" do
34
+ with_dir do |dir|
35
+ File.open(envfile(dir), "w") do |file|
36
+ file.puts(VALID_ENVFILE)
37
+ end
38
+
39
+ EnvironmentFile.new(envfile(dir)).rails_version.should == "2.3.16"
40
+ end
41
+ end
42
+
43
+ it "reports none when no Rails version" do
44
+ with_dir do |dir|
45
+ File.open(envfile(dir), "w") do |file|
46
+ file.puts "require File.expand_path('../application', __FILE__)"
47
+ file.puts "Recipes::Application.initialize!"
48
+ end
49
+
50
+ EnvironmentFile.new(envfile(dir)).rails_version.should == "n/a"
51
+ end
52
+ end
53
+
54
+ it "memoizes rails version" do
55
+ with_dir do |dir|
56
+ File.open(envfile(dir), "w") do |file|
57
+ file.puts(VALID_ENVFILE)
58
+ end
59
+
60
+ @subject = EnvironmentFile.new(envfile(dir))
61
+ @subject.rails_version.should == "2.3.16"
62
+ end
63
+
64
+ @subject.rails_version.should == "2.3.16"
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,63 @@
1
+ require "spec_helper"
2
+ require "rails_finder/gemfile"
3
+ require "tmpdir"
4
+ require "fileutils"
5
+
6
+ VALID_GEMFILE = <<END
7
+ source :rubygems
8
+
9
+ gem "rails", "3.2.11"
10
+ gem "jquery-rails"
11
+ END
12
+
13
+ module RailsFinder
14
+ describe Gemfile do
15
+ it "knows if file exists" do
16
+ with_dir do |dir|
17
+ FileUtils.touch(gemfile(dir))
18
+
19
+ Gemfile.new(gemfile(dir)).should exist
20
+ end
21
+ end
22
+
23
+ it "knows if file does not exist" do
24
+ with_dir do |dir|
25
+ Gemfile.new(gemfile(dir)).should_not exist
26
+ end
27
+ end
28
+
29
+ it "reports Rails version" do
30
+ with_dir do |dir|
31
+ File.open(gemfile(dir), "w") do |file|
32
+ file.puts(VALID_GEMFILE)
33
+ end
34
+
35
+ Gemfile.new(gemfile(dir)).rails_version.should == "3.2.11"
36
+ end
37
+ end
38
+
39
+ it "reports none when no Rails version" do
40
+ with_dir do |dir|
41
+ File.open(gemfile(dir), "w") do |file|
42
+ file.puts "source :rubygems"
43
+ file.puts "gem 'rspec', '2.12.0'"
44
+ end
45
+
46
+ Gemfile.new(gemfile(dir)).rails_version.should == "n/a"
47
+ end
48
+ end
49
+
50
+ it "memoizes rails version" do
51
+ with_dir do |dir|
52
+ File.open(gemfile(dir), "w") do |file|
53
+ file.puts(VALID_GEMFILE)
54
+ end
55
+
56
+ @subject = Gemfile.new(gemfile(dir))
57
+ @subject.rails_version.should == "3.2.11"
58
+ end
59
+
60
+ @subject.rails_version.should == "3.2.11"
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,58 @@
1
+ require "spec_helper"
2
+ require "rails_finder"
3
+ require "tmpdir"
4
+ require "fileutils"
5
+
6
+ VALID_GEMFILE = <<END
7
+ source :rubygems
8
+
9
+ gem 'rails', '3.2.11'
10
+ gem 'jquery-rails'
11
+ gem 'sqlite3'
12
+
13
+ group :development, :test do
14
+ gem 'rspec', '~> 2.12'
15
+ end
16
+ END
17
+
18
+ VALID_2_3_ENVIRONMENT_FILE = <<END
19
+ RAILS_GEM_VERSION = '2.3.16' unless defined? RAILS_GEM_VERSION
20
+
21
+ require File.join(File.dirname(__FILE__), 'boot')
22
+
23
+ Rails::Initializer.run do |config|
24
+ config.time_zone = 'Pacific Time (US & Canada)'
25
+ config.cache_store = :mem_cache_store
26
+ end
27
+ END
28
+
29
+ describe RailsFinder do
30
+ it "finds and reports on valid Gemfile" do
31
+ Dir.mktmpdir "rails_finder-valid_app" do |dir|
32
+ FileUtils.mkdir_p(File.join(dir, "config"))
33
+ FileUtils.touch(File.join(dir, "config", "environment.rb"))
34
+ File.open(File.join(dir, "Gemfile"), "w") do |file|
35
+ file.puts(VALID_GEMFILE)
36
+ end
37
+
38
+ out = StringIO.new
39
+ RailsFinder.run(dir, out)
40
+ out.string.should include "3.2.11"
41
+ out.string.should include "rails_finder-valid_app"
42
+ end
43
+ end
44
+
45
+ it "finds and reports on valid 2.3 environment file" do
46
+ Dir.mktmpdir "valid_2.3_app" do |dir|
47
+ FileUtils.mkdir_p(File.join(dir, "config"))
48
+ File.open(File.join(dir, "config", "environment.rb"), "w") do |file|
49
+ file.puts(VALID_2_3_ENVIRONMENT_FILE)
50
+ end
51
+
52
+ out = StringIO.new
53
+ RailsFinder.run(dir, out)
54
+ out.string.should include "2.3.16"
55
+ out.string.should include "valid_2.3_app"
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,20 @@
1
+ require "rails_finder"
2
+ require "fileutils"
3
+ require "tmpdir"
4
+
5
+ RSpec.configure do |config|
6
+ def with_dir
7
+ Dir.mktmpdir("rails_version-spec") do |dir|
8
+ FileUtils.mkdir_p(File.join(dir, "config"))
9
+ yield dir
10
+ end
11
+ end
12
+
13
+ def gemfile(dir)
14
+ File.join(dir, "Gemfile")
15
+ end
16
+
17
+ def envfile(dir)
18
+ File.join(dir, "config", "environment.rb")
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_finder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chris Thorn
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.12.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.12.0
30
+ description: Overly simple utility to recursively find Rails apps and their versions
31
+ email:
32
+ - thorncp@gmail.com
33
+ executables:
34
+ - find_rails
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - .gitignore
39
+ - Gemfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - bin/find_rails
44
+ - lib/rails_finder.rb
45
+ - lib/rails_finder/app.rb
46
+ - lib/rails_finder/environment_file.rb
47
+ - lib/rails_finder/gemfile.rb
48
+ - lib/rails_finder/version.rb
49
+ - rails_finder.gemspec
50
+ - spec/rails_finder/app_spec.rb
51
+ - spec/rails_finder/environment_file_spec.rb
52
+ - spec/rails_finder/gemfile_spec.rb
53
+ - spec/rails_finder_spec.rb
54
+ - spec/spec_helper.rb
55
+ homepage: https://github.com/thorncp/rails_finder
56
+ licenses: []
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.25
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Overly simple utility to recursively find Rails apps and their versions
79
+ test_files:
80
+ - spec/rails_finder/app_spec.rb
81
+ - spec/rails_finder/environment_file_spec.rb
82
+ - spec/rails_finder/gemfile_spec.rb
83
+ - spec/rails_finder_spec.rb
84
+ - spec/spec_helper.rb