Permalink
Browse files

Selections seem to be working; put testing on hold, since I have no i…

…dea what I'm doing.
  • Loading branch information...
brian-c committed Mar 10, 2012
1 parent 61b5cd2 commit 10aa9fc0325ab2035091ab0718afefab364f048a
Showing with 91 additions and 30 deletions.
  1. +5 −7 bin/grabass
  2. +10 −4 example.json
  3. +1 −1 grabass.gemspec
  4. +62 −15 lib/grabass.rb
  5. +13 −3 spec/grabass_lib_spec.rb
View
@@ -1,9 +1,7 @@
#!/usr/bin/env ruby
require 'optparse'
-require 'json'
-require 'fileutils'
-require 'grabass'
+require_relative '../lib/grabass'
ABOUT = '''
Grabass
@@ -37,15 +35,15 @@ if ARGV.length == 0
elsif ARGV.length == 1
if File.exists? ARGV[0]
manifest = Grabass::AssetManifest.new ARGV[0]
- manifest.fetch(options)
+ manifest.grab(options[:force])
finished = Time.now
else
puts "Asset manifest \"#{ARGV[0]}\" not found."
end
elsif ARGV.length == 2
- asset = Grabass::Asset.new ARGV[0], ARGV[1]
- asset.fetch(options)
- finished = Time.now
+ # asset = Grabass::Asset.new ARGV[0], {'*' => ARGV[1]}
+ # asset.fetch(options)
+ # finished = Time.now
end
if finished
View
@@ -1,7 +1,13 @@
[
+ "A single JavaScript file",
{"http://code.jquery.com/jquery-1.7.1.js": "scripts/jquery.js"},
- {"http://documentcloud.github.com/underscore/underscore.js": "scripts/underscore.js"},
- {"http://requirejs.org/docs/release/${version}/comments/require.js": "scripts/require.js"},
- {"http://spinejs.com/pages/download": "scripts/spine"},
- {"http://www.fontsquirrel.com/fontfacekit/TitilliumText": "scripts/TitilliumText"}
+
+ "A zipped directory with a selection",
+ {"http://spinejs.com/pages/download": {"lib": "scripts/spine"}},
+
+ "A zipped group of loose files with multiple selections",
+ {"http://www.fontsquirrel.com/fontfacekit/TitilliumText": [
+ {"*.{svg,ttf}": "fonts/titillium/normal"},
+ {"*.{eot, woff}": "fonts/titillium/weird"}
+ ]}
]
View
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'grabass'
- s.version = '0.0.1'
+ s.version = '0.1.0'
s.summary = 'Grabass'
s.description = 'Grab static assets and put them somewhere'
View
@@ -7,16 +7,11 @@ def self.temp_dir
end
class Asset
- attr_accessor :source, :selections, :destination, :type
+ attr_accessor :source, :selections, :type
- def initialize(source, selectionsOrDestination = nil)
+ def initialize(source, selections = [])
@source = source
-
- if selectionsOrDestination.is_a? Array
- @selections = selections
- elsif selectionsOrDestination.is_a? String
- @destination = destination
- end
+ @selections = selections # [{'pattern': 'destination'}]
if source.end_with? '.git' or Dir.exists? File.join @source, '.git'
@type = :git
@@ -27,7 +22,24 @@ def initialize(source, selectionsOrDestination = nil)
end
end
- def grab(force = false)
+ def grab(destination_root, force = false)
+ fetch # Get the files in the temp_dir
+
+ @selections.each do |selection|
+ pattern, destination = selection.to_a[0]
+ destination = File.absolute_path destination, destination_root
+
+ Dir.chdir temp_dir
+ selected_files = Dir.glob(pattern)
+
+ if selected_files.length == 1
+ FileUtils.makedirs File.dirname destination
+ `mv #{selected_files[0]} #{destination}`
+ else
+ FileUtils.makedirs destination
+ FileUtils.move selected_files, destination
+ end
+ end
end
def fetch
@@ -80,11 +92,7 @@ def clone_git_repo
`git clone --quiet #{@source} #{temp_dir}`
end
- def select
- end
-
- def place
- end
+ private
def short_name
@source.gsub(/\W/, '')
@@ -101,8 +109,47 @@ def temp_file
end
end
+ class Comment
+ def initialize(content)
+ @content = content
+ end
+
+ def display
+ puts @content
+ end
+ end
+
class AssetManifest
- def initialize(manifest)
+ def initialize(filename)
+ @filename = filename
+ @root = File.absolute_path File.dirname filename
+
+ records = JSON.parse File.read @filename
+ @records = records.map do |record|
+ if record.is_a? String
+ Comment.new record
+ else
+ source, selections = record.to_a[0]
+
+ if selections.is_a? String
+ selections = [{'*' => selections}]
+ elsif selections.is_a? Hash
+ selections = [selections]
+ end
+
+ Asset.new source, selections
+ end
+ end
+ end
+
+ def grab(force = false)
+ @records.each do |record|
+ if record.is_a? Comment
+ record.display
+ elsif record.is_a? Asset
+ record.grab @root, force
+ end
+ end
end
end
end
View
@@ -1,6 +1,8 @@
require 'grabass'
require 'fileutils'
+TEMP_DESTINATION = File.join ENV['TMPDIR'], 'grabass-test-output'
+
describe 'Grabass' do
SINGLE_FILE = 'http://code.jquery.com/jquery-1.7.1.js'
@@ -59,10 +61,18 @@
end
end
- describe 'selection' do
- it 'should automatically select a single file'
+ describe 'make_selection' do
+ it 'should automatically select a single file' do
+ asset = Grabass::Asset.new SINGLE_FILE
+ asset.fetch
+ asset.make_selection('*').length.should == 1
+ end
- it 'should automatically select all files from a directory'
+ it 'should automatically select all files from a directory' do
+ asset = Grabass::Asset.new ZIPPED_DIR
+ asset.fetch
+ asset.make_selection('*').length.should == ZIPPED_DIR_LENGTH
+ end
it 'should automatically select all files from a Git repo'

0 comments on commit 10aa9fc

Please sign in to comment.