cdnjs-command 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +44 -0
  2. data/bin/cdnjs +215 -0
  3. metadata +68 -0
@@ -0,0 +1,44 @@
1
+ # cdnjs-command
2
+ #### Command line helper for cdnjs.com.
3
+
4
+ This makes including JS files in your project easy-peasy. To add a JS libary
5
+ to your project:
6
+
7
+ $ cdnjs jquery
8
+ Created jquery-1.5.1.js.
9
+
10
+ You probably want to link the online version as well:
11
+
12
+ $ cdnjs html jquery
13
+ <script src='https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js'></script>
14
+
15
+ Or you may want just the URL:
16
+
17
+ $ cdnjs url underscore
18
+ http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.4/underscore-min.js
19
+
20
+ ## Installation
21
+
22
+ Copy `bin/cdnjs` into your PATH somewhere, or use `gem install cdnjs-command`.
23
+
24
+ ## Usage
25
+
26
+ Usage:
27
+ cdnjs NAME - Download specific package
28
+
29
+ Other commands:
30
+ cdnjs list - List available packages
31
+ cdnjs info NAME - Show package info
32
+ cdnjs html NAME - Show HTML snippet to include given package
33
+ cdnjs url NAME - Show the URL for the given package
34
+ cdnjs update - Updates the package cache
35
+
36
+ You may also use the first letters of each command; e.g., `cdnjs i jquery`.
37
+ Here are some common examples:
38
+
39
+ $ cdnjs jquery
40
+ $ echo `cdnjs h underscore` >> index.html
41
+
42
+ ## Acknowledgements
43
+
44
+ * [www.cdnjs.com](http://cdnjs.com)
@@ -0,0 +1,215 @@
1
+ #!/usr/bin/env ruby
2
+ require 'open-uri'
3
+ require 'fileutils'
4
+ require 'json'
5
+
6
+ CDNJS_VERSION = "0.0.1"
7
+
8
+ module Params;
9
+ def extract(what) i = index(what) and slice!(i, 2)[1] end;
10
+ def first_is(what) shift if what.include?(self.first); end
11
+ def self.[](*what) what.extend Params; end
12
+ def ===(argv) argv.first_is(self); end
13
+ end
14
+
15
+ ARGV.extend Params
16
+
17
+ # ============================================================================
18
+
19
+ module Fetcher
20
+ CACHE_PATH = "~/.cache/cdnjs"
21
+
22
+ def self.fetch(url)
23
+ return cache_for(url) if cached?(url)
24
+
25
+ tip "Fetching #{url}..."
26
+ output = open(url).read
27
+
28
+ FileUtils.mkdir_p File.expand_path(CACHE_PATH)
29
+ File.open(cache_path_for(url), 'w') { |f| f.write output }
30
+ output
31
+ rescue => e
32
+ tip "Unable to fetch (#{e.class.name}: #{e.message})."
33
+ exit
34
+ end
35
+
36
+ def self.purge
37
+ FileUtils.rm_rf File.expand_path(CACHE_PATH)
38
+ end
39
+
40
+ def self.cached?(url)
41
+ File.file? cache_path_for(url)
42
+ end
43
+
44
+ def self.cache_path_for(url)
45
+ File.expand_path slug(url), CACHE_PATH
46
+ end
47
+
48
+ def self.cache_for(url)
49
+ File.open(cache_path_for(url)) { |f| f.read }
50
+ end
51
+
52
+ def self.slug(url)
53
+ url.gsub(/[\/:]/, '_')
54
+ end
55
+ end
56
+
57
+ class Package
58
+ INDEX_URL = 'http://cdnjs.com/packages.json'
59
+
60
+ attr_reader :data, :name, :version, :description, :homepage,
61
+ :maintainers, :repositories
62
+
63
+ def initialize(hash)
64
+ @data = hash
65
+ @name = hash['name'].gsub(/\.js$/, '')
66
+ @version = hash['version']
67
+ @description = hash['description']
68
+ @homepage = hash['homepage']
69
+ @filename = hash['filename']
70
+ @maintainers = hash['maintainers']
71
+ @repositories = hash['repositories']
72
+ end
73
+
74
+ def basename
75
+ "#{name}-#{version}.js"
76
+ end
77
+
78
+ def url
79
+ if @filename =~ /^http/
80
+ @filename
81
+ else
82
+ "http://ajax.cdnjs.com/ajax/libs/#{data['name']}/#{version}/#{@filename}"
83
+ end
84
+ end
85
+
86
+ # @return Array of {Package}s
87
+ def self.all
88
+ packages = JSON.parse(fetch(INDEX_URL))['packages']
89
+ packages.delete(Hash.new)
90
+ packages
91
+ .map { |hash| Package.new(hash) }
92
+ .sort_by { |pkg| pkg.name }
93
+ end
94
+
95
+ def self.fetch(url)
96
+ Fetcher.fetch(INDEX_URL).force_encoding('utf-8')
97
+ end
98
+
99
+ def self.[](id)
100
+ all.detect { |pkg| pkg.name == id } ||
101
+ all.detect { |pkg| pkg.name[0...id.size] == id }
102
+ end
103
+ end
104
+
105
+ # ============================================================================
106
+ # Helpers
107
+
108
+ def tip(message)
109
+ $stderr.write "#{message}\n"
110
+ end
111
+
112
+ def invalid_usage(usage="cdnjs --help")
113
+ puts "Invalid usage."
114
+ puts "Try: #{usage.gsub(/^cdnjs/, File.basename($0))}"
115
+ exit
116
+ end
117
+
118
+ def get_package(id)
119
+ Package[id] or begin
120
+ tip "Package not found."
121
+ tip "For a list of all packages, see: cdnjs list"
122
+ exit
123
+ end
124
+ end
125
+
126
+ def show_help
127
+ tip "Usage:"
128
+ tip " cdnjs NAME - Download specific package"
129
+ tip ""
130
+ tip "Other commands:"
131
+ tip " cdnjs list - List available packages"
132
+ tip " cdnjs info NAME - Show package info"
133
+ tip " cdnjs html NAME - Show HTML snippet to include given package"
134
+ tip " cdnjs url NAME - Show the URL for the given package"
135
+ tip " cdnjs update - Updates the package cache"
136
+ tip ""
137
+ tip "You may also use the first letters of each command; e.g., `cdnjs i jquery`."
138
+ tip "Here are some common examples:"
139
+ tip ""
140
+ tip " $ cdnjs jquery"
141
+ tip " $ echo `cdnjs h underscore` >> index.html"
142
+ tip ""
143
+ end
144
+
145
+ # ============================================================================
146
+ # Le controller
147
+
148
+ if __FILE__ == $0
149
+ case ARGV
150
+ when Params['-h', '--help']
151
+ show_help
152
+
153
+ when Params['-v', '--version']
154
+ puts "cdnjs-command #{CDNJS_VERSION}"
155
+
156
+ when Params['list', 'l']
157
+ Package.all.each { |pkg|
158
+ puts "%-20s %s" % [ pkg.name + " (" + pkg.version + ")", ""]
159
+ }
160
+
161
+ when Params['html', 'h']
162
+ name = ARGV.shift or invalid_usage("cdnjs html NAME")
163
+ pkg = get_package(name)
164
+
165
+ puts "<script src='#{pkg.url}'></script>"
166
+
167
+ when Params['url', 'u']
168
+ puts pkg.url
169
+
170
+ when Params['info', 'i']
171
+ name = ARGV.shift or invalid_usage("cdnjs info NAME")
172
+ pkg = get_package(name)
173
+
174
+ puts "%s (%s)" % [ pkg.name, pkg.version ]
175
+ puts ""
176
+ puts "%-10s %s" % [ "URL:", pkg.url ]
177
+ puts "%-10s %s" % [ "Homepage:", pkg.homepage ]
178
+ puts ""
179
+ puts pkg.description
180
+ puts ""
181
+ puts "<!-- Include this library in your project with: -->"
182
+ puts "<script src='#{pkg.url}'></script>"
183
+
184
+ if pkg.url =~ /^https/
185
+ puts ""
186
+ puts "<!-- (You may need to replace 'https' with 'http'.) -->"
187
+ end
188
+
189
+
190
+ p pkg if ARGV.delete('-v')
191
+
192
+ when Params['update']
193
+ Fetcher.purge
194
+ Package.all
195
+ tip "Done."
196
+
197
+ else
198
+ name = ARGV.shift or (show_help; exit)
199
+ pkg = Package[name] or begin
200
+ if ARGV.empty?
201
+ puts "Invalid package name."
202
+ else
203
+ puts "Invalid usage."
204
+ end
205
+ puts "For a list of all packages: cdnjs list"
206
+ puts "For more help: cdnjs --help"
207
+ exit
208
+ end
209
+
210
+ data = Fetcher.fetch(pkg.url)
211
+ File.open(pkg.basename, 'w') { |f| f.write data }
212
+
213
+ tip "Created #{pkg.basename}."
214
+ end
215
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cdnjs-command
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Rico Sta. Cruz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-27 00:00:00 +08:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: json
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ description: This package lets you download the popular JavaScript packages with one command, thanks to www.cdnjs.com.
28
+ email:
29
+ - rico@sinefunc.com
30
+ executables: []
31
+
32
+ extensions: []
33
+
34
+ extra_rdoc_files: []
35
+
36
+ files:
37
+ - bin/cdnjs
38
+ - README.md
39
+ has_rdoc: true
40
+ homepage: http://github.com/rstacruz/cdnjs-command
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.5.0
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Command line helper for cdnjs.com.
67
+ test_files: []
68
+