Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

move setup code into setup module

  • Loading branch information...
commit c45cfe4742b6c2031440fcd458b9a417e19dbdee 1 parent 151f3e6
@mmb authored
View
26 Rakefile
@@ -83,7 +83,7 @@ namespace :db do
if args.force == 'force'
url_to_delete.destroy
else
- response = Murlsh.ask('Delete this url?', 'n')
+ response = Murlsh::Setup.ask('Delete this url?', 'n')
url_to_delete.destroy if %w{y yes}.include?(response.downcase)
end
}
@@ -198,9 +198,9 @@ namespace :user do
desc 'Add a new user.'
task :add do
puts "adding to #{config.fetch('auth_file')}"
- username = Murlsh.ask('Username:')
- email = Murlsh.ask('Email:')
- password = Murlsh.ask('Password:')
+ username = Murlsh::Setup.ask('Username:')
+ email = Murlsh::Setup.ask('Email:')
+ password = Murlsh::Setup.ask('Password:')
Murlsh::Auth.new(config.fetch('auth_file')).add_user(username, email,
password)
@@ -210,7 +210,7 @@ end
desc 'Generate code for a bookmarklet that will post a new url.'
task :post_bookmarklet do
- password = Murlsh.ask('Password:')
+ password = Murlsh::Setup.ask('Password:')
password = CGI.escape(CGI.escape(password))
puts <<EOS
javascript:var%20d=document,dgs=d.getSelection,enc=encodeURIComponent,w=window,wgs=w.getSelection,s=''+(wgs?wgs():dgs?dgs():d.selection.createRange().text);void(window.open('#{config.fetch('root_url')}bookmarklet?title='+enc(s)+'&url='+enc(location.href)+'&auth=#{password}'));
@@ -240,7 +240,7 @@ namespace :css do
desc 'Combine and compress css.'
task :compress => ['public/css'] do
- combined = Murlsh.cat_files(
+ combined = Murlsh::Setup.cat_files(
config['css_files'].map { |x| "public/#{x}" }, "\n")
md5sum = Digest::MD5.hexdigest(combined)
@@ -256,7 +256,7 @@ namespace :css do
unless config['css_compressed'] == compressed_url
config['css_compressed'] = compressed_url
- Murlsh.write_ordered_hash config, 'config.yaml'
+ Murlsh::Setup.write_ordered_hash config, 'config.yaml'
puts "updated config with css_compressed = #{compressed_url}"
end
end
@@ -271,7 +271,7 @@ namespace :js do
desc 'Combine and compress javascript.'
task :compress => ['public/js'] do
- combined = Murlsh.cat_files(
+ combined = Murlsh::Setup.cat_files(
config['js_files'].map { |x| "public/#{x}" } )
compressed = Net::HTTP.post_form(
@@ -295,7 +295,7 @@ namespace :js do
unless config['js_compressed'] == compressed_url
config['js_compressed'] = compressed_url
- Murlsh.write_ordered_hash config, 'config.yaml'
+ Murlsh::Setup.write_ordered_hash config, 'config.yaml'
puts "updated config with js_compressed = #{compressed_url}"
end
end
@@ -304,7 +304,7 @@ namespace :js do
task :jslint do
local_jslint = 'jslint_rhino.js'
open(local_jslint, 'w') do |f|
- f.write(Murlsh.cat_files(%w{
+ f.write(Murlsh::Setup.cat_files(%w{
https://github.com/AndyStricker/JSLint/raw/rhinocmdline/fulljslint.js
https://github.com/AndyStricker/JSLint/raw/rhinocmdline/rhino.js
}))
@@ -389,7 +389,7 @@ fi
SITE_URL='#{config.fetch('root_url')}'
EOS
- Murlsh.delicious_parse(args.source) do |b|
+ Murlsh::Setup.delicious_parse(args.source) do |b|
# escape single quotes because these will be in single quotes in output
href_escaped = b[:href].to_s.gsub("'", "'\"'\"'")
via_url_escaped = b[:via_url].to_s.gsub("'", "'\"'\"'")
@@ -406,7 +406,7 @@ task :config, :key, :value do |t, args|
orig_value = config[args.key]
if args.value != orig_value
config[args.key] = args.value
- Murlsh.write_ordered_hash config, 'config.yaml'
+ Murlsh::Setup.write_ordered_hash config, 'config.yaml'
puts "updated '#{args.key}' '#{orig_value}' => '#{args.value}'"
else
puts "'#{args.key}' is already set to '#{args.value}'"
@@ -454,7 +454,7 @@ namespace :heroku do
puts "Set root_url to #{config['root_url']}"
end
- Murlsh.write_ordered_hash config, 'config.yaml'
+ Murlsh::Setup.write_ordered_hash config, 'config.yaml'
end
end
View
2  bin/murlsh
@@ -15,4 +15,4 @@ if ARGV.empty?; usage; exit 1; end
dest_dir = ARGV[0]
-Murlsh.install(dest_dir)
+Murlsh::Setup.install(dest_dir)
View
8 lib/murlsh.rb
@@ -2,12 +2,8 @@
require 'murlsh/server'
require 'murlsh/atom_server'
-require 'murlsh/ask'
require 'murlsh/auth'
require 'murlsh/build_query'
-require 'murlsh/cat_files'
-require 'murlsh/cp_r_safe'
-require 'murlsh/delicious_parse'
require 'murlsh/dispatch'
require 'murlsh/doc'
require 'murlsh/etag_add_encoding'
@@ -17,7 +13,6 @@
require 'murlsh/feed_body'
require 'murlsh/image_list'
require 'murlsh/img_store'
-require 'murlsh/install'
require 'murlsh/json_body'
require 'murlsh/json_server'
require 'murlsh/m3u_body'
@@ -32,6 +27,7 @@
require 'murlsh/rss_body'
require 'murlsh/rss_server'
require 'murlsh/search_grammar'
+require 'murlsh/setup'
require 'murlsh/time_ago'
require 'murlsh/uri_ask'
require 'murlsh/uri_domain'
@@ -41,7 +37,5 @@
require 'murlsh/url_result_set'
require 'murlsh/url_server'
require 'murlsh/version'
-require 'murlsh/write_ordered_hash'
-require 'murlsh/yaml_ordered_hash'
require 'murlsh/atom_body'
View
15 lib/murlsh/ask.rb
@@ -1,15 +0,0 @@
-module Murlsh
-
- module_function
-
- # Ask the user a question and return the answer.
- def ask(prompt, default=nil)
- default_given = !default.to_s.empty?
- print "#{prompt} "
- print "[#{default}] " if default_given
- answer = $stdin.gets.strip
- answer = default if answer.empty? and default_given
- answer
- end
-
-end
View
17 lib/murlsh/cat_files.rb
@@ -1,17 +0,0 @@
-module Murlsh
-
- module_function
-
- # Concatenate some files and return the result as a string.
- def cat_files(files, sep=nil)
- result = ''
- files.each do |fname|
- open(fname) do |h|
- while (line = h.gets) do; result << line; end
- result << sep if sep
- end
- end
- result
- end
-
-end
View
33 lib/murlsh/cp_r_safe.rb
@@ -1,33 +0,0 @@
-require 'fileutils'
-
-require 'murlsh'
-
-module Murlsh
-
- module_function
-
- # Recursive copy from sources to destination but ask before overwriting.
- #
- # Options are passed into FileUtils.mkdir_p FileUtils.copy.
- def cp_r_safe(sources, dest, options)
- sources.each do |source|
- new = File.join(dest, File.split(File.expand_path(source)).last)
-
- if File.directory?(source)
- FileUtils.mkdir_p(new, options)
- cp_r_safe(Dir.entries(source).
- reject { |f| %w{. ..}.include?(f) }.
- map { |f| File.join(source, f) }, new, options)
- else
- answer = if File.exists?(new)
- Murlsh.ask("#{new} exists. Overwrite?", 'n')
- else
- 'y'
- end
-
- FileUtils.copy(source, new, options) if answer == 'y'
- end
- end
- end
-
-end
View
39 lib/murlsh/delicious_parse.rb
@@ -1,39 +0,0 @@
-require 'open-uri'
-require 'time'
-require 'uri'
-
-require 'nokogiri'
-
-module Murlsh
-
- module_function
-
- # Parse a delicious xml export and yield a hash for each bookmark.
- #
- # To export your delicious bookmarks:
- # curl https://user:password@api.del.icio.us/v1/posts/all > delicious.xml
- def delicious_parse(source)
- doc = Nokogiri::XML(open(source))
-
- doc.xpath('//post').each do |p|
- result = {}
- p.each { |k,v| result[k.to_sym] = v }
-
- result[:tag] = result[:tag].split
- result[:time] = Time.parse(result[:time])
-
- # extract via information from extended
- result[:via] = result[:extended].chomp(')')[%r{via\s+([^\s]+)}, 1]
- result[:via_url] = begin
- if result[:via] and
- %w{http https}.include?(URI(result[:via]).scheme.to_s.downcase)
- result[:via]
- end
- rescue URI::InvalidURIError
- end
-
- yield result
- end
- end
-
-end
View
30 lib/murlsh/install.rb
@@ -1,30 +0,0 @@
-require 'fileutils'
-
-require 'murlsh'
-
-module Murlsh
-
- MurlshRoot = File.join(File.dirname(__FILE__), '..', '..')
-
- module_function
-
- # Install a murlsh site to a web directory.
- #
- # Copies files that are different per-site to make a site instance.
- def install(dest_dir)
- Murlsh.cp_r_safe(
- %w{
- .htaccess
- Rakefile
- config/
- config.ru
- config.yaml
- db/
- plugins/
- public/
- }.map { |x| File.join(MurlshRoot, x) }, dest_dir, :verbose => true)
-
- FileUtils.mkdir_p(File.join(dest_dir, 'tmp'), :verbose => true)
- end
-
-end
View
7 lib/murlsh/setup.rb
@@ -0,0 +1,7 @@
+require 'murlsh/setup/ask'
+require 'murlsh/setup/cat_files'
+require 'murlsh/setup/cp_r_safe'
+require 'murlsh/setup/delicious_parse'
+require 'murlsh/setup/install'
+require 'murlsh/setup/write_ordered_hash'
+require 'murlsh/setup/yaml_ordered_hash'
View
19 lib/murlsh/setup/ask.rb
@@ -0,0 +1,19 @@
+module Murlsh
+
+ module Setup
+
+ module_function
+
+ # Ask the user a question and return the answer.
+ def ask(prompt, default=nil)
+ default_given = !default.to_s.empty?
+ print "#{prompt} "
+ print "[#{default}] " if default_given
+ answer = $stdin.gets.strip
+ answer = default if answer.empty? and default_given
+ answer
+ end
+
+ end
+
+end
View
21 lib/murlsh/setup/cat_files.rb
@@ -0,0 +1,21 @@
+module Murlsh
+
+ module Setup
+
+ module_function
+
+ # Concatenate some files and return the result as a string.
+ def cat_files(files, sep=nil)
+ result = ''
+ files.each do |fname|
+ open(fname) do |h|
+ while (line = h.gets) do; result << line; end
+ result << sep if sep
+ end
+ end
+ result
+ end
+
+ end
+
+end
View
35 lib/murlsh/setup/cp_r_safe.rb
@@ -0,0 +1,35 @@
+require 'fileutils'
+
+module Murlsh
+
+ module Setup
+
+ module_function
+
+ # Recursive copy from sources to destination but ask before overwriting.
+ #
+ # Options are passed into FileUtils.mkdir_p FileUtils.copy.
+ def cp_r_safe(sources, dest, options)
+ sources.each do |source|
+ new = File.join(dest, File.split(File.expand_path(source)).last)
+
+ if File.directory?(source)
+ FileUtils.mkdir_p(new, options)
+ cp_r_safe(Dir.entries(source).
+ reject { |f| %w{. ..}.include?(f) }.
+ map { |f| File.join(source, f) }, new, options)
+ else
+ answer = if File.exists?(new)
+ Setup.ask("#{new} exists. Overwrite?", 'n')
+ else
+ 'y'
+ end
+
+ FileUtils.copy(source, new, options) if answer == 'y'
+ end
+ end
+ end
+
+ end
+
+end
View
43 lib/murlsh/setup/delicious_parse.rb
@@ -0,0 +1,43 @@
+require 'open-uri'
+require 'time'
+require 'uri'
+
+require 'nokogiri'
+
+module Murlsh
+
+ module Setup
+
+ module_function
+
+ # Parse a delicious xml export and yield a hash for each bookmark.
+ #
+ # To export your delicious bookmarks:
+ # curl https://user:password@api.del.icio.us/v1/posts/all > delicious.xml
+ def delicious_parse(source)
+ doc = Nokogiri::XML(open(source))
+
+ doc.xpath('//post').each do |p|
+ result = {}
+ p.each { |k,v| result[k.to_sym] = v }
+
+ result[:tag] = result[:tag].split
+ result[:time] = Time.parse(result[:time])
+
+ # extract via information from extended
+ result[:via] = result[:extended].chomp(')')[%r{via\s+([^\s]+)}, 1]
+ result[:via_url] = begin
+ if result[:via] and
+ %w{http https}.include?(URI(result[:via]).scheme.to_s.downcase)
+ result[:via]
+ end
+ rescue URI::InvalidURIError
+ end
+
+ yield result
+ end
+ end
+
+ end
+
+end
View
32 lib/murlsh/setup/install.rb
@@ -0,0 +1,32 @@
+require 'fileutils'
+
+module Murlsh
+
+ module Setup
+
+ MurlshRoot = File.join(File.dirname(__FILE__), '..', '..', '..')
+
+ module_function
+
+ # Install a murlsh site to a web directory.
+ #
+ # Copies files that are different per-site to make a site instance.
+ def install(dest_dir)
+ Setup.cp_r_safe(
+ %w{
+ .htaccess
+ Rakefile
+ config/
+ config.ru
+ config.yaml
+ db/
+ plugins/
+ public/
+ }.map { |x| File.join(MurlshRoot, x) }, dest_dir, :verbose => true)
+
+ FileUtils.mkdir_p(File.join(dest_dir, 'tmp'), :verbose => true)
+ end
+
+ end
+
+end
View
19 lib/murlsh/setup/write_ordered_hash.rb
@@ -0,0 +1,19 @@
+require 'yaml'
+
+module Murlsh
+
+ module Setup
+
+ module_function
+
+ # Sort a hash by key and write it to a file as YAML.
+ def write_ordered_hash(h, path)
+ h.extend(YamlOrderedHash)
+ h.each_value { |v| v.extend(YamlOrderedHash) if v.is_a?(Hash) }
+
+ open(path, 'w') { |f| YAML.dump(h, f) }
+ end
+
+ end
+
+end
View
24 lib/murlsh/setup/yaml_ordered_hash.rb
@@ -0,0 +1,24 @@
+require 'yaml'
+
+module Murlsh
+
+ module Setup
+
+ # Hash mixin to generate yaml with hash keys in sorted order.
+ module YamlOrderedHash
+
+ def to_yaml(opts={})
+ YAML::quick_emit(self, opts) do |out|
+ out.map(taguri, to_yaml_style) do |map|
+ sort { |a,b| a[0].to_s <=> b[0].to_s }.each do |k, v|
+ map.add k, v
+ end
+ end
+ end
+ end
+
+ end
+
+ end
+
+end
View
17 lib/murlsh/write_ordered_hash.rb
@@ -1,17 +0,0 @@
-require 'murlsh'
-
-module Murlsh
-
- module_function
-
- # Sort a hash by key and write it to a file as YAML.
- def write_ordered_hash(h, path)
- h.extend(Murlsh::YamlOrderedHash)
- h.each_value do |v|
- v.extend(Murlsh::YamlOrderedHash) if v.is_a?(Hash)
- end
-
- open(path, 'w') { |f| YAML.dump(h, f) }
- end
-
-end
View
20 lib/murlsh/yaml_ordered_hash.rb
@@ -1,20 +0,0 @@
-require 'yaml'
-
-module Murlsh
-
- # Hash mixin to generate yaml with hash keys in sorted order.
- module YamlOrderedHash
-
- def to_yaml(opts={})
- YAML::quick_emit(self, opts) do |out|
- out.map(taguri, to_yaml_style) do |map|
- sort { |a,b| a[0].to_s <=> b[0].to_s }.each do |k, v|
- map.add k, v
- end
- end
- end
- end
-
- end
-
-end
View
6 spec/cat_files_spec.rb
@@ -24,7 +24,7 @@
end
it 'should cat the files together and return the result' do
- Murlsh.cat_files([@f1.path, @f2.path]).should == <<eos
+ Murlsh::Setup.cat_files([@f1.path, @f2.path]).should == <<eos
1
2
1
@@ -38,8 +38,8 @@
context 'when one or more files are missing' do
it 'should raise no such file or directory' do
- lambda { Murlsh.cat_files(['does_not_exist']) }.should raise_error(
- Errno::ENOENT)
+ lambda { Murlsh::Setup.cat_files(
+ ['does_not_exist']) }.should raise_error(Errno::ENOENT)
end
end
View
4 spec/yaml_ordered_hash_spec.rb
@@ -1,6 +1,6 @@
require 'murlsh'
-describe Murlsh::YamlOrderedHash do
+describe Murlsh::Setup::YamlOrderedHash do
subject do
h = {
@@ -10,7 +10,7 @@
'b' => 2,
}
- h.extend(Murlsh::YamlOrderedHash)
+ h.extend(Murlsh::Setup::YamlOrderedHash)
end
its(:to_yaml) { should == <<EOS
Please sign in to comment.
Something went wrong with that request. Please try again.