From 7ecc0cfd0b9736758f97260292f1276901348e76 Mon Sep 17 00:00:00 2001 From: Dale Stevens Date: Sat, 10 Oct 2015 09:30:13 -0600 Subject: [PATCH] Allows for building specific versions --- tasks/angularjs-rails/updater.rb | 141 +++++++++++++++++-------------- 1 file changed, 76 insertions(+), 65 deletions(-) diff --git a/tasks/angularjs-rails/updater.rb b/tasks/angularjs-rails/updater.rb index 5f3431c..a42316e 100644 --- a/tasks/angularjs-rails/updater.rb +++ b/tasks/angularjs-rails/updater.rb @@ -4,94 +4,105 @@ require 'pathname' require 'tempfile' -module AngularJS::Rails - class Updater - BASE_URL = 'https://code.angularjs.org' - ROOT_PATH = Pathname.new('vendor/assets/javascripts') +# Run with `ruby -r "./tasks/angularjs-rails/updater.rb" -e "AngularJS::Rails::Updater.new.build_js('1.2.28')"` +module AngularJS + module Rails + class Updater + BASE_URL = 'https://code.angularjs.org' + ROOT_PATH = Pathname.new('vendor/assets/javascripts') + + def initialize + end - def initialize - end + def base_path + ROOT_PATH + end - def base_path - ROOT_PATH - end + def update_js! + if latest_version >= current_gem_version + clean + download_files + update_version_to(latest_version) + end + end - def update_js! - if latest_version >= current_gem_version + def build_js(version) clean - download_files - update_version_to(latest_version) + download_files(version) + update_version_to(version) end - end - def current_gem_version - raise NotImplementedError - end + def current_gem_version + raise NotImplementedError + end - def available_versions - upstream_versions.find_all {|v| own_version(v) } - end + def available_versions + upstream_versions.find_all {|v| own_version(v) } + end - def latest_version - available_versions.last - end + def latest_version + available_versions.last + end - def version_constant_name - "VERSION" - end + def version_constant_name + "VERSION" + end - private + private - def clean - base_path.children.each do |f| - f.delete if f.file? + def clean + base_path.children.each do |f| + f.delete if f.file? + end end - end - def update_version_to(version) - version_file = Pathname.new('lib/angularjs-rails/version.rb') - content = version_file.read - version_line = content.lines.find { |l| l =~ /^\s+#{version_constant_name}/ } + def update_version_to(version) + version_file = Pathname.new('lib/angularjs-rails/version.rb') + content = version_file.read + version_line = content.lines.find { |l| l =~ /^\s+#{version_constant_name}/ } - # version_to_convert = Versionomy.parse(version).convert(:rubygems).to_s + # version_to_convert = Versionomy.parse(version).convert(:rubygems).to_s - new_version_line = version_line.gsub(/"[^"]+"/, %Q{"#{version}"}) - version_file.open('w+') do |f| - f.write content.gsub(version_line, new_version_line) + new_version_line = version_line.gsub(/"[^"]+"/, %Q{"#{version}"}) + version_file.open('w+') do |f| + f.write content.gsub(version_line, new_version_line) + end end - end - def upstream_versions - @upstream_versions ||= Nokogiri::HTML.parse(open(BASE_URL)). - css('a').map { |e| Versionomy.parse e.text[0..-2] rescue nil }.compact.sort - end + def upstream_versions + @upstream_versions ||= Nokogiri::HTML.parse(open(BASE_URL)). + css('a').map { |e| Versionomy.parse e.text[0..-2] rescue nil }.compact.sort + end - def own_version(v) - true - end + def own_version(v) + true + end - def self.update_js! - self.new.update_js! - end + def self.update_js! + self.new.update_js! + end - def download_files - url = BASE_URL + "/" + latest_version.to_s - Nokogiri::HTML.parse(open(url)). - css('a'). + def download_files(version=latest_version) + url = BASE_URL + "/" + version.to_s + puts "Fetching #{url}" + Nokogiri::HTML.parse(open(url)). + css('a'). - #only angular js files - map{|a| a[:href] =~ /angular[^.]*\.js/ ? a : nil }.compact. + #only angular js files + map{|a| a[:href] =~ /angular[^.]*\.js/ ? a : nil }.compact. - each do |a| - download_file(a[:href], url) - end - end + each do |a| + download_file(a[:href], url) + end + end - def download_file(file, url) - full_url = url + "/" + file - full_path = base_path.join(file) - full_path.open('w+') do |f| - f.write open(full_url).read + def download_file(file, url) + puts "Downloading #{file} from #{url}" + full_url = url + "/" + file + full_path = base_path.join(file) + full_path.open('w+') do |f| + f.write open(full_url).read + end end end end