Permalink
Browse files

Changing the generator to a Thor task.

It doesn't make sense to have the generator available from within
a Rails project, because it updates the mediaelement_rails files. I've
refactored it into a Thor task found in mediaelement_rails.thor,
which can be executed by calling `thor mediaelement_rails:update`.
Check out `thor mediaelement_rails:help update` for more information.
  • Loading branch information...
1 parent f2d0b55 commit acf6d7546b5c80e87bfe7bc4e79c6cdb8928826c @petebrowne petebrowne committed Oct 12, 2011
View
3 .gitignore 100644 → 100755
@@ -5,5 +5,4 @@ log/*.log
test/dummy/db/*.sqlite3
test/dummy/log/*.log
test/dummy/tmp/
-lib/generators/mediaelement_update/templates/mediaelement
-/nbproject/private/
+vendor/mediaelement
Binary file not shown.
@@ -1,7 +0,0 @@
-Description:
- Explain the generator
-
-Example:
- rails generate mediaelement_update
-
- This will create all assets from mediaelementc
@@ -1,46 +0,0 @@
-# encoding: utf-8
-
-class MediaelementUpdateGenerator < Rails::Generators::NamedBase
- source_root File.expand_path('../templates', __FILE__)
-
- def update_mediaelement
- # clone git repository
- git :clone => "https://github.com/johndyer/mediaelement.git #{self.class.source_root}/mediaelement"
-
- # copy all template files
- build_path = File.join(self.class.source_root, "mediaelement", "build")
- {
- '*.png' => { :path => 'images' },
- '*.gif' => { :path => 'images' },
- 'mediaelementplayer.css' => { :path => 'stylesheets', :extension => '.css.erb' },
- 'mejs-skins.css' => { :path => 'stylesheets', :extension => '.css.erb' },
- '*.swf' => { :path => 'flash' },
- '*.xap' => { :path => 'silverlight' },
- 'mediaelement.js' => { :path => 'javascripts' },
- 'mediaelementplayer.js' => { :path => 'javascripts' }
- }.each do |matcher, options|
- Dir[ File.join(build_path, matcher) ].each do |file|
- old_extension = File.extname(file)
- basename = File.basename(file, old_extension)
- extension = options.key?(:extension) ? options[:extension] : old_extension
- new_name = File.join("app","assets", options[:path], "mediaelement_rails", "#{basename}#{extension}")
- old_name = File.join("mediaelement", "build", "#{basename}#{old_extension}")
- copy_file old_name, new_name
- end
- end
-
- asset_base_path = File.join(Rails.root, 'app', 'assets', '*', 'mediaelement_rails')
- {
- '*.css.erb' => Proc.new{ |fp| fp.read.gsub(/([^(]+\.(png|gif))/m, "<%= asset_path('mediaelement_rails/\\1') %>") },
- '*.js' => Proc.new{ |fp| fp.read.gsub(//, '') }
- }.each do |matcher, proc|
- Dir[ File.join(asset_base_path, matcher) ].each do |file|
- File.open(file, "r+") do |fp|
- new_content = proc.call(fp)
- fp.rewind
- fp.write(new_content)
- end
- end
- end
- end
-end
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "i18n"
s.add_development_dependency "turn"
s.add_development_dependency "sqlite3"
+ s.add_development_dependency "thor"
s.add_development_dependency "rake"
s.files = `git ls-files`.split("\n")
View
@@ -0,0 +1,62 @@
+module MediaelementRails
+ class Updater < Thor
+ include Thor::Actions
+ namespace "mediaelement_rails"
+
+ UPDATE_FILES = {
+ "mediaelement.js" => "javascripts",
+ "mediaelementplayer.js" => "javascripts",
+ "mediaelementplayer.css" => "stylesheets",
+ "mejs-skins.css" => "stylesheets",
+ "*.{png,gif}" => "images",
+ "*.{swf,xap}" => "plugins"
+ }
+
+ def self.source_root
+ File.expand_path("..", __FILE__)
+ end
+
+ desc "update", "Update the MediaElement.js files"
+ method_option "vendor_path",
+ :desc => "Path to the the MediaElement.js git repo",
+ :default => "vendor/mediaelement",
+ :aliases => %w(-v)
+ method_option "assets_path",
+ :desc => "Path to the the mediaelement_rails assets",
+ :default => "app/assets",
+ :aliases => %w(-a)
+ method_option "git",
+ :desc => "URL of the MediaElement.js git repo to clone",
+ :default => "http://github.com/johndyer/mediaelement.git",
+ :aliases => %w(-g)
+ method_option "tag",
+ :desc => "The tag to checkout in the MediaElement.js git repo",
+ :default => "master",
+ :aliases => %w(-t)
+ def update
+ assets_path = options[:assets_path]
+ vendor_path = options[:vendor_path]
+
+ # Update the vendored MediaElement.js
+ run "git clone #{options[:git]} #{vendor_path}", :capture => true unless File.directory? vendor_path
+ run "cd #{vendor_path} && git pull origin master", :capture => true
+ run "cd #{vendor_path} && git co #{options[:tag]}", :capture => true
+
+ # Then copy the files we need to their correct locations
+ UPDATE_FILES.each do |matcher, path|
+ Dir[File.join(vendor_path, "build", matcher)].each do |file|
+ new_name = File.join assets_path, path, "mediaelement_rails", File.basename(file)
+ new_name << ".erb" if file =~ /\.css$/
+ copy_file file, new_name
+ end
+ end
+
+ # Use the #asset_path helper in the CSS files
+ Dir[File.join(assets_path, "**/*.css.erb")].each do |file|
+ content = File.binread(file)
+ content.gsub! /url\((.*?)\)/, 'url(<%= asset_path "mediaelement_rails/\1" %>)'
+ File.open(file, "wb") { |file| file.write(content) }
+ end
+ end
+ end
+end
View
No changes.

0 comments on commit acf6d75

Please sign in to comment.