milkode 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. data/.document +5 -0
  2. data/Gemfile +12 -0
  3. data/Gemfile.lock +18 -0
  4. data/HISTORY.rdoc +5 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.rdoc +19 -0
  7. data/Rakefile +78 -0
  8. data/VERSION +1 -0
  9. data/bin/cdv +6 -0
  10. data/bin/cdview +10 -0
  11. data/bin/milk +10 -0
  12. data/lib/milkode/cdstk/cdstk.rb +508 -0
  13. data/lib/milkode/cdstk/cdstk_yaml.rb +106 -0
  14. data/lib/milkode/cdstk/cli_cdstk.rb +103 -0
  15. data/lib/milkode/cdstk/cli_cdstksub.rb +102 -0
  16. data/lib/milkode/cdview/cli_cdview.rb +43 -0
  17. data/lib/milkode/cdweb/app.rb +136 -0
  18. data/lib/milkode/cdweb/cli_cdweb.rb +111 -0
  19. data/lib/milkode/cdweb/config.ru +3 -0
  20. data/lib/milkode/cdweb/lib/coderay_wrapper.rb +94 -0
  21. data/lib/milkode/cdweb/lib/command.rb +67 -0
  22. data/lib/milkode/cdweb/lib/database.rb +260 -0
  23. data/lib/milkode/cdweb/lib/grep.rb +68 -0
  24. data/lib/milkode/cdweb/lib/mkurl.rb +56 -0
  25. data/lib/milkode/cdweb/lib/query.rb +81 -0
  26. data/lib/milkode/cdweb/lib/search_contents.rb +117 -0
  27. data/lib/milkode/cdweb/lib/search_files.rb +88 -0
  28. data/lib/milkode/cdweb/public/css/coderay.css +131 -0
  29. data/lib/milkode/cdweb/public/css/gren.css +75 -0
  30. data/lib/milkode/cdweb/public/images/directory.png +0 -0
  31. data/lib/milkode/cdweb/public/images/file.png +0 -0
  32. data/lib/milkode/cdweb/public/images/gren-icon-mini.png +0 -0
  33. data/lib/milkode/cdweb/public/images/gren-icon.png +0 -0
  34. data/lib/milkode/cdweb/views/filelist.haml +14 -0
  35. data/lib/milkode/cdweb/views/help.haml +16 -0
  36. data/lib/milkode/cdweb/views/index.haml +17 -0
  37. data/lib/milkode/cdweb/views/layout.haml +33 -0
  38. data/lib/milkode/cdweb/views/search.haml +12 -0
  39. data/lib/milkode/cdweb/views/view.haml +16 -0
  40. data/lib/milkode/common/dbdir.rb +39 -0
  41. data/lib/milkode/common/display_util.rb +62 -0
  42. data/lib/milkode/common/grenfiletest.rb +19 -0
  43. data/lib/milkode/common/grensnip.rb +37 -0
  44. data/lib/milkode/common/platform.rb +17 -0
  45. data/lib/milkode/common/string_snip.rb +61 -0
  46. data/lib/milkode/common/util.rb +144 -0
  47. data/lib/milkode/findgrep/findgrep.rb +408 -0
  48. data/lib/milkode/findgrep/result.rb +43 -0
  49. data/milkode.gemspec +159 -0
  50. data/test/data/abc.zip +0 -0
  51. data/test/data/nodir_abc.zip +0 -0
  52. data/test/data/nodir_abc_xpi.xpi +0 -0
  53. data/test/file_assert.rb +41 -0
  54. data/test/file_test_utils.rb +59 -0
  55. data/test/rake_test_loader.rb +7 -0
  56. data/test/runner.rb +11 -0
  57. data/test/test_bin_exec.rb +30 -0
  58. data/test/test_cdstk.rb +90 -0
  59. data/test/test_cdstk_yaml.rb +157 -0
  60. data/test/test_coderay_wrapper.rb +22 -0
  61. data/test/test_coderay_wrapper_data.rb +91 -0
  62. data/test/test_database.rb +78 -0
  63. data/test/test_dbdir.rb +60 -0
  64. data/test/test_gren_util.rb +34 -0
  65. data/test/test_helper.rb +2 -0
  66. data/test/test_mkurl.rb +30 -0
  67. data/test/test_query.rb +54 -0
  68. data/test/test_string_snip.rb +31 -0
  69. data/test/test_util.rb +40 -0
  70. metadata +300 -0
@@ -0,0 +1,106 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # @file
4
+ # @brief
5
+ # @author ongaeshi
6
+ # @date 2011/02/20
7
+
8
+ require 'yaml'
9
+ require 'pathname'
10
+ require 'milkode/common/dbdir'
11
+
12
+ module Milkode
13
+ class CdstkYaml
14
+ class YAMLAlreadyExist < RuntimeError
15
+ end
16
+
17
+ class YAMLNotExist < RuntimeError
18
+ end
19
+
20
+ def self.create(path = ".")
21
+ yf = yaml_file(path)
22
+ raise YAMLAlreadyExist.new if FileTest.exist? yf
23
+ obj = CdstkYaml.new(yf, {'contents' => [], 'version' => 0.1})
24
+ obj.save
25
+ return obj
26
+ end
27
+
28
+ def self.load(path = ".")
29
+ yf = yaml_file(path)
30
+ raise YAMLNotExist.new unless FileTest.exist? yf
31
+ open(yf) do |f|
32
+ return CdstkYaml.new(yf, YAML.load(f.read()))
33
+ end
34
+ end
35
+
36
+ def initialize(yaml_file, data)
37
+ @yaml_file = yaml_file
38
+ @data = data
39
+ end
40
+
41
+ def add(dirs)
42
+ contents.concat(dirs.map{|v|{'directory' => v}})
43
+ contents.uniq!
44
+ end
45
+
46
+ def remove(query)
47
+ r = query.select(contents)
48
+ r.each {|v| contents.delete v}
49
+ end
50
+
51
+ def save
52
+ open(@yaml_file, "w") { |f| YAML.dump(@data, f) }
53
+ end
54
+
55
+ def contents
56
+ @data['contents']
57
+ end
58
+
59
+ def package_num
60
+ @data['contents'].size
61
+ end
62
+
63
+ def directorys
64
+ contents.map{|v|v['directory']}
65
+ end
66
+
67
+ def version
68
+ @data['version']
69
+ end
70
+
71
+ def list(query = nil)
72
+ query ? query.select(contents) : contents
73
+ end
74
+
75
+ def exist?(shortname)
76
+ @data['contents'].find {|v| File.basename(v['directory']) == shortname }
77
+ end
78
+
79
+ def cleanup
80
+ contents.delete_if do |v|
81
+ if (!File.exist? v['directory'])
82
+ yield v if block_given?
83
+ true
84
+ else
85
+ false
86
+ end
87
+ end
88
+ end
89
+
90
+ def self.yaml_file(path)
91
+ Dbdir.yaml_path(path)
92
+ end
93
+
94
+ class Query
95
+ def initialize(keywords)
96
+ @keywords = keywords
97
+ end
98
+
99
+ def select(contents)
100
+ contents.find_all do |v|
101
+ @keywords.any? {|s| File.basename(v['directory']).include? s }
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,103 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'optparse'
3
+ require 'milkode/cdstk/cli_cdstksub'
4
+ require 'milkode/cdstk/cdstk'
5
+ require 'milkode/common/dbdir.rb'
6
+ require 'milkode/cdweb/cli_cdweb'
7
+ include Milkode
8
+
9
+ module Milkode
10
+ class CLI_Cdstk
11
+ def self.execute(stdout, arguments=[])
12
+ opt = OptionParser.new <<EOF
13
+ #{File.basename($0)} COMMAND [ARGS]
14
+
15
+ The most commonly used #{File.basename($0)} are:
16
+ init Init db.
17
+ add Add packages.
18
+ update Update packages.
19
+ web Run web-app.
20
+ remove Remove packages.
21
+ list List packages.
22
+ pwd Disp current db.
23
+ cleanup Cleanup garbage records.
24
+ rebuild Rebuild db.
25
+ dump Dump records.
26
+ EOF
27
+
28
+ subopt = Hash.new
29
+ suboptions = Hash.new
30
+
31
+ subopt['init'], suboptions['init'] = CLI_Cdstksub.setup_init
32
+ subopt['add'] = CLI_Cdstksub.setup_add
33
+ subopt['update'] = OptionParser.new("#{File.basename($0)} update package1 [package2 ...]")
34
+ subopt['remove'], suboptions['remove'] = CLI_Cdstksub.setup_remove
35
+ subopt['list'], suboptions['list'] = CLI_Cdstksub.setup_list
36
+ subopt['pwd'], suboptions['pwd'] = CLI_Cdstksub.setup_pwd
37
+ subopt['cleanup'], suboptions['cleanup'] = CLI_Cdstksub.setup_cleanup
38
+ subopt['rebuild'] = OptionParser.new("#{File.basename($0)} rebuild")
39
+ subopt['dump'] = OptionParser.new("#{File.basename($0)} dump")
40
+ subopt['web'], suboptions['web'] = CLI_Cdstksub.setup_web
41
+
42
+ opt.order!(arguments)
43
+ subcommand = arguments.shift
44
+
45
+ if (subopt[subcommand])
46
+ subopt[subcommand].parse!(arguments) unless arguments.empty?
47
+ init_default = suboptions['init'][:init_default]
48
+
49
+ db_dir = select_dbdir(subcommand, init_default)
50
+ obj = Cdstk.new(stdout, db_dir)
51
+
52
+ case subcommand
53
+ when 'init'
54
+ FileUtils.mkdir_p db_dir if (init_default)
55
+ obj.init
56
+ when 'update'
57
+ obj.update(arguments)
58
+ when 'add'
59
+ obj.add(arguments)
60
+ when 'remove'
61
+ obj.remove(arguments, suboptions[subcommand])
62
+ when 'list'
63
+ obj.list(arguments, suboptions[subcommand])
64
+ when 'pwd'
65
+ obj.pwd(suboptions[subcommand])
66
+ when 'cleanup'
67
+ obj.cleanup(suboptions[subcommand])
68
+ when 'rebuild'
69
+ obj.rebuild
70
+ when 'dump'
71
+ obj.dump
72
+ when 'web'
73
+ Milkode::CLI_Cdweb.execute_with_options(stdout, suboptions[subcommand])
74
+ end
75
+ else
76
+ if subcommand
77
+ $stderr.puts "#{File.basename($0)}: '#{subcommand}' is not a #{File.basename($0)} command. See '#{File.basename($0)} --help'"
78
+ else
79
+ stdout.puts opt.help
80
+ end
81
+ end
82
+ end
83
+
84
+ private
85
+
86
+ def self.select_dbdir(subcommand, init_default)
87
+ if (subcommand == 'init')
88
+ if (init_default)
89
+ Dbdir.default_dir
90
+ else
91
+ '.'
92
+ end
93
+ else
94
+ if (Dbdir.dbdir?('.') || !Dbdir.dbdir?(Dbdir.default_dir))
95
+ '.'
96
+ else
97
+ Dbdir.default_dir
98
+ end
99
+ end
100
+ end
101
+
102
+ end
103
+ end
@@ -0,0 +1,102 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'optparse'
3
+ require 'milkode/cdstk/cdstk'
4
+ require 'milkode/common/dbdir.rb'
5
+ require 'milkode/cdweb/cli_cdweb'
6
+ include Milkode
7
+
8
+ module Milkode
9
+ class CLI_Cdstksub
10
+ def self.setup_init
11
+ options = {:init_default => false}
12
+
13
+ opt = OptionParser.new("#{File.basename($0)} init")
14
+ opt.on('--default', 'Init default db, ENV[\'MILKODE_DEFAULT_DIR\'] or ~/.milkode.') { options[:init_default] = true }
15
+
16
+ return opt, options
17
+ end
18
+
19
+ def self.setup_add
20
+ bin = File.basename($0)
21
+
22
+ opt = OptionParser.new(<<EOF)
23
+ #{bin} add package1 [package2 ...]
24
+ usage:
25
+ #{bin} add /path/to/dir1
26
+ #{bin} add /path/to/dir2 /path/to/dir3
27
+ #{bin} add /path/is/*
28
+ #{bin} add /path/to/zipfile.zip
29
+ #{bin} add /path/to/addon.xpi
30
+ #{bin} add http://example.com/urlfile.zip
31
+ EOF
32
+
33
+ opt
34
+ end
35
+
36
+ def self.setup_remove
37
+ options = {:force => false}
38
+
39
+ opt = OptionParser.new("#{File.basename($0)} remove package1 [package2 ...]")
40
+ opt.on('-f', '--force', 'Force remove.') { options[:force] = true }
41
+
42
+ return opt, options
43
+ end
44
+
45
+ def self.setup_list
46
+ options = {:verbose => false}
47
+
48
+ opt = OptionParser.new("#{File.basename($0)} list package1 [package2 ...]")
49
+ opt.on('-v', '--verbose', 'Be verbose.') { options[:verbose] = true }
50
+
51
+ return opt, options
52
+ end
53
+
54
+ def self.setup_pwd
55
+ options = {:default => false}
56
+
57
+ opt = OptionParser.new("#{File.basename($0)} pwd package1 [package2 ...]")
58
+ opt.on('--default', 'Show default db, ENV[\'MILKODE_DEFAULT_DIR\'] or ~/.milkode.') { options[:default] = true }
59
+
60
+ return opt, options
61
+ end
62
+
63
+ def self.setup_cleanup
64
+ options = {:verbose => false, :force => false}
65
+
66
+ opt = OptionParser.new("#{File.basename($0)} cleanup")
67
+ opt.on('-f', '--force', 'Force cleanup.') { options[:force] = true }
68
+
69
+ return opt, options
70
+ end
71
+
72
+ def self.setup_web
73
+ options = {
74
+ :environment => ENV['RACK_ENV'] || "development",
75
+ :pid => nil,
76
+ :Port => 9292,
77
+ :Host => "0.0.0.0",
78
+ :AccessLog => [],
79
+ :config => "config.ru",
80
+ # ----------------------------
81
+ :server => "thin",
82
+ :LaunchBrowser => true,
83
+ :DbDir => Milkode::CLI_Cdweb::select_dbdir,
84
+ }
85
+
86
+ opts = OptionParser.new("#{File.basename($0)} web")
87
+ opts.on('--db DB_DIR', 'Database dir (default : current_dir)') {|v| options[:DbDir] = v }
88
+ opts.on("-o", "--host HOST", "listen on HOST (default: 0.0.0.0)") {|host| options[:Host] = host }
89
+ opts.on('-p', '--port PORT', 'use PORT (default: 9292)') {|v| options[:Port] = v }
90
+ opts.on("-s", "--server SERVER", "serve using SERVER (default : thin)") {|s| options[:server] = s }
91
+ opts.on('-n', '--no-browser', 'No launch browser.') {|v| options[:LaunchBrowser] = false }
92
+
93
+ # --hostが'-h'を上書きするので、'-h'を再定義してあげる
94
+ opts.on_tail("-h", "-?", "--help", "Show this message") do
95
+ puts opts
96
+ exit
97
+ end
98
+
99
+ return opts, options
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,43 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'optparse'
3
+ require 'milkode/findgrep/findgrep'
4
+ require 'milkode/common/dbdir'
5
+ include Milkode
6
+
7
+ module Milkode
8
+ class CLI_Cdview
9
+ def self.execute(stdout, arguments=[])
10
+ # オプション
11
+ option = FindGrep::FindGrep::DEFAULT_OPTION
12
+ option.dbFile = Dbdir.groonga_path(Dbdir.default_dir)
13
+
14
+ # デフォルトのマッチモードは'File'
15
+ option.isMatchFile = true
16
+
17
+ # オプション解析
18
+ opt = OptionParser.new("#{File.basename($0)} [option] keyword1 [keyword2 ...]")
19
+ opt.on('--db [GREN_DB_FILE]', 'Search from the grendb database.') {|v| option.dbFile = Dbdir.groonga_path(v) }
20
+ opt.on('-f KEYWORD', '--file-keyword KEYWORD', 'Path keyword. (Enable multiple call)') {|v| option.filePatterns << v}
21
+ opt.on('-s SUFFIX', '--suffix SUFFIX', 'Search suffix.') {|v| option.suffixs << v }
22
+ opt.on('-i', '--ignore', 'Ignore case.') {|v| option.ignoreCase = true}
23
+ opt.on('-c', '--color', 'Color highlight.') {|v| option.colorHighlight = true}
24
+ opt.on('--no-snip', 'There being a long line, it does not snip.') {|v| option.noSnip = true }
25
+ opt.on('-g', '--groonga-only', 'Search only groonga db.') {|v| option.groongaOnly = true }
26
+ opt.on('--mf', '--match-file', 'Match file. (Default)') {|v| option.isMatchFile = true }
27
+ opt.on('-l', '--ml', '--match-line', 'Match line, same mode as "gren".') {|v| option.isMatchFile = false }
28
+
29
+ opt.parse!(arguments)
30
+
31
+ # 検索オブジェクトの生成
32
+ if (option.dbFile && (arguments.size > 0 || option.keywordsOr.size > 0))
33
+ findGrep = FindGrep::FindGrep.new(arguments, option)
34
+ findGrep.searchAndPrint(stdout)
35
+ else
36
+ stdout.print opt.help
37
+ stdout.puts
38
+ stdout.puts "please set GREN DATABSE FILE!! (--db option, or set ENV['GRENDB_DEFAULT_DB'].)" unless option.dbFile
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,136 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # @file
4
+ # @brief
5
+ # @author ongaeshi
6
+ # @date 2011/06/25
7
+
8
+ require 'rubygems'
9
+ require 'sinatra'
10
+ require 'haml'
11
+ require 'sass'
12
+
13
+ $LOAD_PATH.unshift '../..'
14
+ require 'milkode/cdweb/lib/database'
15
+ require 'milkode/cdweb/lib/command'
16
+ require 'milkode/cdweb/lib/mkurl'
17
+
18
+ set :haml, :format => :html5
19
+
20
+ get '/' do
21
+ @version = "0.1.0"
22
+ @package_num = Database.instance.fileList('').size
23
+ @file_num = Database.instance.fileNum
24
+ haml :index
25
+ end
26
+
27
+ post '/home*' do |path|
28
+ path = path.sub(/^\//, "")
29
+
30
+ case params[:shead]
31
+ when 'all'
32
+ path = ""
33
+ when 'package'
34
+ path = path.split('/')[0]
35
+ end
36
+
37
+ redirect Mkurl.new("home/#{path}", params).inherit_query_shead
38
+ end
39
+
40
+ get '/home*' do |path|
41
+ before = Time.now
42
+ path = path.sub(/^\//, "")
43
+ record = Database.instance.record(path)
44
+
45
+ if (record)
46
+ view(record, params, before)
47
+ else
48
+ unless (params[:query])
49
+ filelist(path, params, before)
50
+ else
51
+ search(path, params, before)
52
+ end
53
+ end
54
+ end
55
+
56
+ get %r{/help} do
57
+ haml :help
58
+ end
59
+
60
+ # -- helper function --
61
+
62
+ helpers do
63
+ # -- escape functions --
64
+ alias h escape_html
65
+ alias escape_url escape
66
+
67
+ def escape_path(src)
68
+ escape_url(src).gsub("%2F", '/')
69
+ end
70
+
71
+ # -- utility --
72
+ def link(query)
73
+ "<a href='#{'/home?query=' + escape_url(query)}'>#{query}</a>"
74
+ end
75
+
76
+ def create_form(path, query, shead)
77
+ shead = shead || 'directory'
78
+
79
+ # こっちにすると'検索'ボタンを押した時に新しくウィンドウが開く
80
+ # <form action='' target='_blank' method='post'>
81
+ <<EOF
82
+ <form action='' method='post'>
83
+ <p>
84
+ <input name='query' size='60' type='text' value='#{query}' />
85
+ <input type='submit' value='検索'><br></input>
86
+ #{create_radio('all', shead)}
87
+ <label>全体を検索</label>
88
+ #{create_radio('package', shead)}
89
+ <label> #{package_name(path)} 以下</label>
90
+ #{create_radio('directory', shead)}
91
+ <label> #{current_name(path)} 以下</label>
92
+ </p>
93
+ </form>
94
+ EOF
95
+ end
96
+
97
+ def create_radio(value, shead)
98
+ str = (value == shead) ? 'checked' : ''
99
+ "<input name='shead' type='radio' value='#{value}' #{str}/>"
100
+ end
101
+
102
+ def create_headmenu(path, query)
103
+ # href = "#{request.path_info}?'#{request.query_string}"
104
+ # href = '/home/rack-1.3.0/lib/rack/handler/cgi.rb?shead=directory'
105
+ href = Mkurl.new('/home/' + path, params).inherit_query_shead
106
+ <<EOF
107
+ <a href="/home" class="headmenu">全てのパッケージ</a>
108
+ <a href="#{href}" class="headmenu" onclick="window.open('#{href}'); return false;">新しい検索</a>
109
+ EOF
110
+ end
111
+
112
+ def topic_path(path, params)
113
+ href = '/home'
114
+ path.split('/').map {|v|
115
+ href += '/' + v
116
+ "<a href='#{Mkurl.new(href, params).inherit_shead}'>#{v}</a>"
117
+ }.join('/')
118
+ end
119
+
120
+ def package_name(path)
121
+ (path == "") ? 'root' : path.split('/')[0]
122
+ end
123
+
124
+ def current_name(path)
125
+ (path == "") ? 'root' : File.basename(path)
126
+ end
127
+
128
+ def path_title(path)
129
+ (path == "") ? "root" : path
130
+ end
131
+
132
+ def filelist_title(path)
133
+ (path == "") ? "Package List" : path
134
+ end
135
+ end
136
+