Permalink
Browse files

Create Homebrew::Formula with the import system in a Sidekiq worker

1 parent 70111c6 commit 5601e22df9afdf4dd08648c32e5a4f25dcbff9ba @zedtux committed Nov 17, 2013
View
@@ -76,3 +76,6 @@ pickle-email-*.html
# Ignore application configuration
/config/application.yml
+
+# Ignore Yard
+.yardoc
View
@@ -0,0 +1 @@
+--use-cache --no-private --protected --tag nodoc:"Not for documentation" --hide-tag nodoc app/models/*.rb app/models/**/*.rb lib/**/*.rb config/initializers/*.rb
View
@@ -1,5 +1,6 @@
source 'https://rubygems.org'
ruby '2.0.0'
+
gem 'rails', '4.0.1'
gem 'sass-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
@@ -12,23 +13,25 @@ gem 'figaro'
gem 'pg'
gem 'puma'
gem 'slim'
+gem 'git'
+gem 'appconfig', :require => 'app_config'
+gem 'sidekiq'
+gem 'yard'
+
group :development do
gem 'better_errors'
gem 'binding_of_caller', :platforms=>[:mri_19, :mri_20, :rbx]
- gem 'haml-rails'
- gem 'haml2slim'
- gem 'html2haml'
gem 'hub', :require=>nil
gem 'quiet_assets'
gem 'rails_layout'
end
-group :development, :test do
- gem 'rspec-rails'
-end
+
group :test do
gem 'capybara'
gem 'cucumber-rails', :require=>false
gem 'database_cleaner', '1.0.1'
gem 'email_spec'
gem 'launchy'
+ gem 'rspec-rails'
+ gem 'shoulda-matchers'
end
View
@@ -26,6 +26,7 @@ GEM
thread_safe (~> 0.1)
tzinfo (~> 0.3.37)
addressable (2.3.5)
+ appconfig (0.1.1)
arel (4.0.1)
atomic (1.1.14)
better_errors (1.0.1)
@@ -42,6 +43,8 @@ GEM
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
+ celluloid (0.15.2)
+ timers (~> 1.1.0)
coderay (1.1.0)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
@@ -50,6 +53,7 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.6.3)
+ connection_pool (1.1.0)
cucumber (1.3.10)
builder (>= 2.1.2)
diff-lcs (>= 1.1.3)
@@ -74,24 +78,8 @@ GEM
rails (>= 3, < 5)
gherkin (2.12.2)
multi_json (~> 1.3)
- haml (4.1.0.alpha.2)
- tilt
- haml-rails (0.4)
- actionpack (>= 3.1, < 4.1)
- activesupport (>= 3.1, < 4.1)
- haml (>= 3.1, < 4.1)
- railties (>= 3.1, < 4.1)
- haml2slim (0.4.7)
- haml (>= 3.0)
- nokogiri
- ruby_parser
+ git (1.2.6)
hike (1.2.3)
- hpricot (0.8.6)
- html2haml (1.0.1)
- erubis (~> 2.7.0)
- haml (>= 4.0.0.rc.1)
- hpricot (~> 0.8.6)
- ruby_parser (~> 3.1.1)
hub (1.10.6)
i18n (0.6.5)
jbuilder (1.5.2)
@@ -137,6 +125,9 @@ GEM
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (10.1.0)
+ redis (3.0.6)
+ redis-namespace (1.3.2)
+ redis (~> 3.0.4)
rspec-core (2.14.7)
rspec-expectations (2.14.4)
diff-lcs (>= 1.1.3, < 2.0)
@@ -148,14 +139,19 @@ GEM
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
- ruby_parser (3.1.3)
- sexp_processor (~> 4.1)
sass (3.2.12)
sass-rails (4.0.1)
railties (>= 4.0.0, < 5.0)
sass (>= 3.1.10)
sprockets-rails (~> 2.0.0)
- sexp_processor (4.4.0)
+ shoulda-matchers (2.4.0)
+ activesupport (>= 3.0.0)
+ sidekiq (2.16.1)
+ celluloid (>= 0.15.2)
+ connection_pool (>= 1.0.0)
+ json
+ redis (>= 3.0.4)
+ redis-namespace (>= 1.3.1)
slim (2.0.2)
temple (~> 0.6.6)
tilt (>= 1.3.3, < 2.1)
@@ -173,6 +169,7 @@ GEM
thread_safe (0.1.3)
atomic
tilt (1.4.1)
+ timers (1.1.0)
treetop (1.4.15)
polyglot
polyglot (>= 0.3.1)
@@ -184,11 +181,13 @@ GEM
json (>= 1.8.0)
xpath (2.0.0)
nokogiri (~> 1.3)
+ yard (0.8.7.3)
PLATFORMS
ruby
DEPENDENCIES
+ appconfig
better_errors
binding_of_caller
bootstrap-sass (>= 3.0.0.0)
@@ -198,9 +197,7 @@ DEPENDENCIES
database_cleaner (= 1.0.1)
email_spec
figaro
- haml-rails
- haml2slim
- html2haml
+ git
hub
jbuilder (~> 1.2)
jquery-rails
@@ -212,6 +209,9 @@ DEPENDENCIES
rails_layout
rspec-rails
sass-rails (~> 4.0.0)
+ shoulda-matchers
+ sidekiq
slim
turbolinks
uglifier (>= 1.3.0)
+ yard
@@ -0,0 +1,2 @@
+module Homebrew
+end
@@ -0,0 +1,76 @@
+# Override the Homebrew Formula class
+# in order to only read the interesting values
+#
+# @author [guillaumeh]
+module Homebrew
+ class FakeFormula
+ class << self
+ [
+ :url,
+ :mirror,
+ :version,
+ :homepage,
+ :sha1,
+ :conflicts_with,
+ :depends_on,
+ :head
+ ].each do |attribute|
+ self.class_eval <<-eos
+ def #{attribute.to_s}(*value)
+ return @#{attribute.to_s} unless value.first
+ @#{attribute.to_s} = value.first
+ end
+ eos
+ end
+
+ def test; end
+ def include?(value); false; end
+
+ def method_missing(method, *args, &block)
+ self
+ end
+
+ ENV.class.send(:define_method, :compiler){}
+ String.send(:define_method, :undent){}
+ end
+
+ end
+
+ class MacOS
+ class << self
+ def version; :mavericks; end
+
+ def method_missing(method, *args, &block)
+ self
+ end
+ end
+ end
+
+ class DummyClass
+ class << self
+ def test; end
+ def include?(*args); ""; end
+
+ def method_missing(method, *args, &block)
+ self
+ end
+ end
+ end
+ Homebrew::AmazonWebServicesFormula = DummyClass
+ Homebrew::CurlDownloadStrategy = DummyClass
+ Homebrew::CurlUnsafeDownloadStrategy = DummyClass
+ Homebrew::GitDownloadStrategy = DummyClass
+ Homebrew::GithubGistFormula = DummyClass
+ Homebrew::MacOS::CLT = DummyClass
+ Homebrew::MacOS::Xcode = DummyClass
+ Homebrew::MacOS::X11 = DummyClass
+ Homebrew::MysqlDependency = DummyClass
+ Homebrew::Requirement = DummyClass
+ Homebrew::ScriptFileFormula = DummyClass
+ Homebrew::StrictSubversionDownloadStrategy = DummyClass
+ Homebrew::Tab = DummyClass
+ Homebrew::UnsafeSubversionDownloadStrategy = DummyClass
+ Homebrew::Version = DummyClass
+
+ HOMEBREW_PREFIX=""
+end
@@ -0,0 +1,20 @@
+# A Homebrew formula
+#
+# Represents a formula used by Homebrew.
+#
+# @author [guillaumeh]
+module Homebrew
+ class Formula < ActiveRecord::Base
+
+ # @nodoc ~~~ special behaviours ~~~
+ self.table_name = "homebrew_formulas"
+
+ # @nodoc ~~~ validations ~~~
+ validates :name, presence: true, uniqueness: true
+
+ # @nodoc ~~~ custom class methods ~~~
+
+ # @nodoc ~~~ custom instance methods ~~~
+
+ end
+end
@@ -0,0 +1,73 @@
+class HomebrewGit
+ include Sidekiq::Worker
+
+ #
+ # Get the Homebrew source code from Github
+ # and keep it up-to-date.
+ #
+ # Clone the repository the first time, then use `git pull`
+ #
+ def perform
+ git = if File.exists?(AppConfig.homebrew.git_repository.location)
+ Git.open(
+ File.join(
+ AppConfig.homebrew.git_repository.location,
+ AppConfig.homebrew.git_repository.name
+ )
+ )
+ else
+ # Create the location path
+ FileUtils.mkdir_p(AppConfig.homebrew.git_repository.location)
+ # Clone the Git repo to the location path
+ Git.clone(
+ AppConfig.homebrew.git_repository.url,
+ AppConfig.homebrew.git_repository.name,
+ path: AppConfig.homebrew.git_repository.location,
+ depth: 1 # Without the history
+ )
+ end
+
+ # Update the code to the HEAD version
+ git.pull
+
+ # Build the formulas folder path
+ formulas_path = File.join(
+ AppConfig.homebrew.git_repository.location,
+ AppConfig.homebrew.git_repository.name,
+ "Library", "Formula", "*.rb"
+ )
+
+ # Treat each files (Ruby files)
+ Dir[formulas_path].each do |formula_path|
+ # Read the Ruby file content
+ formula = File.read(formula_path)
+
+ # Extract the class name
+ formula_class_name = formula.scan(/require\s?'formula'\n+class\s(\w+) < Formula$/).flatten.first
+ formula_class_name = "Homebrew::#{formula_class_name}"
+
+ # Prepend the class with a namespace
+ formula.gsub!(/require\s?'formula'\n+class\s\w+ < Formula$/, "require 'homebrew/fake_formula'\n\nclass #{formula_class_name} < Homebrew::FakeFormula")
+
+ # Eval the formula
+ eval formula
+
+ # Now access the formula attributes like a normal Ruby class
+ klass = formula_class_name.constantize
+
+ # Look for an existing formula
+ if homebrew_formula = Homebrew::Formula.where(name: klass.name.demodulize).first
+ [:version, :homepage].each do |column|
+ homebrew_formula.send "#{column.to_s}=", klass.try(column)
+ end
+ homebrew_formula.save!
+ else
+ Homebrew::Formula.create(
+ name: klass.name.demodulize,
+ version: klass.try(:version),
+ homepage: klass.try(:homepage)
+ )
+ end
+ end
+ end
+end
@@ -0,0 +1,10 @@
+homebrew:
+ git_repository:
+ url: "https://github.com/mxcl/homebrew.git"
+ name: "homebrew"
+ location: <%= File.join(Rails.root, "cache") %>
+redis:
+ server: localhost
+ port: 5678
+ db_num: 0
+ namespace: "brewformulas"
@@ -0,0 +1,8 @@
+Rails.logger.info "Sidekiq using Redis setting redis://#{AppConfig.redis.server}:#{AppConfig.redis.port}/#{AppConfig.redis.db_num} with namespace \"#{AppConfig.redis.namespace}\""
+
+Sidekiq.configure_server do |config|
+ config.redis = {
+ url: "redis://#{AppConfig.redis.server}:#{AppConfig.redis.port}/#{AppConfig.redis.db_num}",
+ namespace: AppConfig.redis.namespace
+ }
+end
@@ -0,0 +1,11 @@
+class CreateHomebrewFormulasTable < ActiveRecord::Migration
+ def change
+ create_table :homebrew_formulas do |t|
+ t.string :name, :null => false
+ t.string :version
+ t.string :homepage
+ t.text :description
+ t.timestamps
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 5601e22

Please sign in to comment.