grabass 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/grabass +55 -0
- data/lib/grabass.rb +120 -0
- metadata +47 -0
data/bin/grabass
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
require 'json'
|
5
|
+
require 'fileutils'
|
6
|
+
require 'grabass'
|
7
|
+
|
8
|
+
ABOUT = '''
|
9
|
+
Grabass
|
10
|
+
Grab static assets and put them somewhere.
|
11
|
+
http://github.com/brian-c/grabass
|
12
|
+
|
13
|
+
Use it like this:
|
14
|
+
grabass assets.json
|
15
|
+
# That JSON looks like this:
|
16
|
+
[{"http://some.remote/file.js": "./lib/file.js"}]
|
17
|
+
|
18
|
+
grabass http://some.remote/file.js ./lib/file.js
|
19
|
+
grabass http://some.remote/archive.zip ./lib/archive/
|
20
|
+
'''
|
21
|
+
|
22
|
+
options = {}
|
23
|
+
|
24
|
+
optparse = OptionParser.new do |args|
|
25
|
+
options[:force] = false
|
26
|
+
args.on '-f', '--force', 'Replace existing files' do
|
27
|
+
$options[:force] = true
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
optparse.parse!
|
32
|
+
|
33
|
+
started = Time.now
|
34
|
+
|
35
|
+
if ARGV.length == 0
|
36
|
+
puts ABOUT
|
37
|
+
elsif ARGV.length == 1
|
38
|
+
if File.exists? ARGV[0]
|
39
|
+
manifest = Grabass::AssetManifest.new ARGV[0]
|
40
|
+
manifest.fetch(options)
|
41
|
+
finished = Time.now
|
42
|
+
else
|
43
|
+
puts "Asset manifest \"#{ARGV[0]}\" not found."
|
44
|
+
end
|
45
|
+
elsif ARGV.length == 2
|
46
|
+
asset = Grabass::Asset.new 'source' => ARGV[0], 'destination' => ARGV[1]
|
47
|
+
asset.fetch(options)
|
48
|
+
finished = Time.now
|
49
|
+
end
|
50
|
+
|
51
|
+
if finished
|
52
|
+
puts '', "Grabass took in #{'%.2f' % (finished - started)} seconds."
|
53
|
+
end
|
54
|
+
|
55
|
+
puts ''
|
data/lib/grabass.rb
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'fileutils'
|
5
|
+
|
6
|
+
module Grabass
|
7
|
+
def self.underline(string, char = '-')
|
8
|
+
"#{string}\n#{char * (string.length / char.length)}"
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.indent(string, prefix = '', spaces = 2)
|
12
|
+
prefix + (' ' * (spaces - prefix.length)) + string
|
13
|
+
end
|
14
|
+
|
15
|
+
SCRIPT_DIR = File.dirname __FILE__
|
16
|
+
TEMP_DIR = "#{SCRIPT_DIR}/_grabass_temp"
|
17
|
+
|
18
|
+
class AssetManifest
|
19
|
+
def initialize(filename)
|
20
|
+
@filename = filename
|
21
|
+
|
22
|
+
@assets = JSON.parse File.read filename
|
23
|
+
@assets.map! do |definition|
|
24
|
+
source = definition.keys[0]
|
25
|
+
destination = definition[source]
|
26
|
+
|
27
|
+
Asset.new source, destination
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def fetch(options)
|
32
|
+
puts ''
|
33
|
+
puts Grabass::underline "Fetching assets from #{@filename}", '='
|
34
|
+
|
35
|
+
@assets.each do |asset|
|
36
|
+
asset.fetch options
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Asset
|
42
|
+
def initialize(source, destination)
|
43
|
+
@source, @destination = source, destination
|
44
|
+
@temp_file = TEMP_DIR + '/' + source.gsub(/\W+/, '-')
|
45
|
+
end
|
46
|
+
|
47
|
+
def fetch(options)
|
48
|
+
FileUtils.remove_dir TEMP_DIR if File.exists? TEMP_DIR
|
49
|
+
FileUtils.mkpath TEMP_DIR
|
50
|
+
|
51
|
+
puts ''
|
52
|
+
puts Grabass::underline @source
|
53
|
+
|
54
|
+
if not options[:force] and File.exists? @destination
|
55
|
+
puts Grabass::indent 'Destination file already exists!', '!'
|
56
|
+
puts Grabass::indent 'Use the --force option to overwrite it.'
|
57
|
+
else
|
58
|
+
if download
|
59
|
+
unzip if mime_type.include? 'zip'
|
60
|
+
move_to_destination
|
61
|
+
else
|
62
|
+
puts Grabass::indent 'Download failed!', '!'
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
FileUtils.remove_dir TEMP_DIR
|
67
|
+
end
|
68
|
+
|
69
|
+
def download
|
70
|
+
puts Grabass::indent "Downloading #{@source}"
|
71
|
+
`curl --silent --location #{@source} --output "#{@temp_file}"`
|
72
|
+
File.exists? @temp_file
|
73
|
+
end
|
74
|
+
|
75
|
+
def mime_type
|
76
|
+
`file --brief --mime-type #{@temp_file}`
|
77
|
+
end
|
78
|
+
|
79
|
+
def unzip
|
80
|
+
puts Grabass::indent 'Unzipping...'
|
81
|
+
|
82
|
+
# Remember what files exist now so we can see exactly what gets unzipped.
|
83
|
+
originals = Dir.entries TEMP_DIR
|
84
|
+
|
85
|
+
`unzip #{@temp_file} -d #{TEMP_DIR}`
|
86
|
+
|
87
|
+
FileUtils.rm "#{@temp_file}"
|
88
|
+
|
89
|
+
new_files = Dir.entries(TEMP_DIR) - originals
|
90
|
+
new_files.map! do |filename|
|
91
|
+
"#{TEMP_DIR}/#{filename}"
|
92
|
+
end
|
93
|
+
|
94
|
+
if new_files.length == 1
|
95
|
+
# Rename to the original temp name if the zip only unzipped one thing.
|
96
|
+
FileUtils.move new_files[0], @temp_file
|
97
|
+
else
|
98
|
+
# Pack more than one thing into a single directory with the temp name.
|
99
|
+
FileUtils.mkpath @temp_file
|
100
|
+
FileUtils.move new_files, @temp_file
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def move_to_destination
|
105
|
+
if File.exists? @destination and options[:force]
|
106
|
+
puts Grabass::indent "Replacing existing #{@destination}"
|
107
|
+
|
108
|
+
if Dir.exists? @destination
|
109
|
+
FileUtils.remove_dir @destination
|
110
|
+
else
|
111
|
+
FileUtils.remove @destination
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
puts Grabass::indent "Moving to #{@destination}..."
|
116
|
+
FileUtils.mkpath(File.dirname(@destination))
|
117
|
+
FileUtils.move @temp_file, @destination
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: grabass
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Brian Carstensen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-19 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Grab static assets and put them somewhere
|
15
|
+
email: brian.carstensen@gmail.com
|
16
|
+
executables:
|
17
|
+
- grabass
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/grabass.rb
|
22
|
+
- bin/grabass
|
23
|
+
homepage: http://github.com/brian-c/grabass
|
24
|
+
licenses: []
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 1.8.11
|
44
|
+
signing_key:
|
45
|
+
specification_version: 3
|
46
|
+
summary: Grabass
|
47
|
+
test_files: []
|