packnga 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ h1. NEWS
2
+
3
+ h2. 0.9.0: 2011-08-19
4
+
5
+ The first release!
@@ -0,0 +1,91 @@
1
+ h1. Tutorial
2
+
3
+ We introduce how to use Packnga with Packnga's Rakefile in this page.
4
+
5
+ h2. Install
6
+
7
+ You can install Packnga from Rubygems.
8
+
9
+ <pre>
10
+ !!!command_line
11
+ % sudo gem install packnga
12
+ </pre>
13
+
14
+ h2. Prepare to create tasks
15
+
16
+ Before using Packnga, you should create @spec@ variable with Jeweler::Task.new.
17
+ Packnga create tasks with @spec@.
18
+ Please see below example.
19
+
20
+ <pre>
21
+ spec = nil
22
+ Jeweler::Tasks.new do |_spec|
23
+ spec = _spec
24
+ spec.name = "packnga"
25
+ spec.version = version
26
+ spec.rubyforge_project = "groonga"
27
+ spec.homepage = "http://groonga.rubyforge.org/"
28
+ spec.authors = ["Haruka Yoshihara", "Kouhei Sutou"]
29
+ spec.email = ["yoshihara@clear-code.com", "kou@clear-code.com"]
30
+ entries = File.read("README.textile").split(/^h2\.\s(.*)$/)
31
+ description = cleanup_white_space(entries[entries.index("Description") + 1])
32
+ spec.summary, spec.description, = description.split(/\n\n+/, 3)
33
+ spec.license = "LGPLv2"
34
+ spec.files = FileList["lib/**/*.rb",
35
+ "Rakefile",
36
+ "README.textile",
37
+ "Gemfile",
38
+ "doc/text/**"]
39
+ spec.test_files = FileList["test/**/*.rb"]
40
+ end
41
+ </pre>
42
+
43
+ If you set the attribute @rubyforge_project@ of @spec@,
44
+ @Release Task@ create tasks for rubyforge.
45
+ See below in detail.
46
+
47
+ h2. Create tasks
48
+
49
+ Packnga's classes has charge of each tasks.
50
+ They are given block with .new method in order to set parameters of tasks.
51
+ This table describes Packnga's classes.
52
+
53
+ - Packnga::DocumentTask :=
54
+ This class create tasks for generating references.
55
+ It define tasks to generate YARD documentation, po files, and
56
+ to translate documents by po files.
57
+ =:
58
+
59
+ - Packnga::ReleaseTask :=
60
+ This class create tasks for uploading references and package and preparing to upload them.
61
+ It defines task to tag the current version in git and task to user-install gem for test.
62
+ It also create tasks for uploading rubyforge if you set @rubyforge_project@ in @spec@ .
63
+ =:
64
+
65
+ See below for creating tasks.
66
+
67
+ <pre>
68
+ Packnga::DocumentTask.new(spec)
69
+ Packnga::ReleaseTask.new(spec)
70
+ </pre>
71
+
72
+ h3. Set the document path.
73
+
74
+ You can set parameters with block when creating object of Packnga::DocumentTask and Releaesask.
75
+ For example, We introduce to how to set document base directory.
76
+ Document is created in this directory.
77
+ You can write Rakefile to set this directory path, see below.
78
+ NOTE: Please set same path to each clasess.
79
+
80
+ <pre>
81
+ Packnga::DocumentTask.new(spec) do |task|
82
+ task.base_dir = "doc/"
83
+ end
84
+
85
+ Packnga::ReleaseTask.new(spec) do |task|
86
+ task.base_dir = "doc/"
87
+ end
88
+ </pre>
89
+
90
+ You can set other parameters.
91
+ Please see Packnga's reference manual in detail.
data/lib/packnga.rb ADDED
@@ -0,0 +1,23 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Copyright (C) 2011 yoshihara haruka <yoshihara@clear-code.com>
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License version 2.1 as published by the Free Software Foundation.
8
+ #
9
+ # This library is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ # Lesser General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU Lesser General Public
15
+ # License along with this library; if not, write to the Free Software
16
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
+
18
+ require "rake"
19
+
20
+ require "packnga/version"
21
+ require "packnga/document-task"
22
+ require "packnga/reference-task"
23
+ require "packnga/release-task"
@@ -0,0 +1,77 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Copyright (C) 2011 yoshihara haruka <yoshihara@clear-code.com>
4
+ # Copyright (C) 2011 Kouhei Sutou <kou@clear-code.com>
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License version 2.1 as published by the Free Software Foundation.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+
19
+ require "pathname"
20
+ require "packnga/yard-task"
21
+ require "packnga/reference-task"
22
+
23
+ module Packnga
24
+ # This class creates docment tasks.
25
+ # They generate YARD doucment or references.
26
+ #
27
+ # @since 0.9.0
28
+ class DocumentTask
29
+ include Rake::DSL
30
+ # Defines tasks to generate YARD documentation
31
+ # and to translate references.
32
+ # @param [Jeweler::Task] spec created by Jeweler::Task.new.
33
+ def initialize(spec)
34
+ @spec = spec
35
+ @yard_task = YARDTask.new(@spec)
36
+ @reference_task = ReferenceTask.new(@spec)
37
+ self.base_dir = "doc"
38
+ if block_given?
39
+ yield(self)
40
+ define
41
+ end
42
+ end
43
+
44
+ # @private
45
+ def define
46
+ set_default_values
47
+ define_tasks
48
+ end
49
+
50
+ # Sets base directory for documents. Default value is "doc".
51
+ # @param [String] base direcory path
52
+ def base_dir=(dir)
53
+ dir = Pathname.new(dir)
54
+ @yard_task.base_dir = dir
55
+ @reference_task.base_dir = dir
56
+ end
57
+
58
+ # Runs block to task for YARD documentation.
59
+ def yard
60
+ yield(@yard_task)
61
+ end
62
+
63
+ # Runs block to tasks for references.
64
+ def reference
65
+ yield(@reference_task)
66
+ end
67
+
68
+ private
69
+ def set_default_values
70
+ end
71
+
72
+ def define_tasks
73
+ @yard_task.define
74
+ @reference_task.define
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,263 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Copyright (C) 2011 yoshihara haruka <yoshihara@clear-code.com>
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License version 2.1 as published by the Free Software Foundation.
8
+ #
9
+ # This library is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ # Lesser General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU Lesser General Public
15
+ # License along with this library; if not, write to the Free Software
16
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
+
18
+ module Packnga
19
+ # This class creates reference tasks.
20
+ # They generate, translate and prepare to publish references.
21
+ #
22
+ # @since 0.9.0
23
+ class ReferenceTask
24
+ include Rake::DSL
25
+ include ERB::Util
26
+ # This attribute is path of base directory of document.
27
+ # @param [String] path of base directory of document
28
+ attr_writer :base_dir
29
+ # @private
30
+ def initialize(spec)
31
+ @spec = spec
32
+ @base_dir = nil
33
+ @translate_languages = nil
34
+ @supported_languages = nil
35
+ @html_files = nil
36
+ @po_dir = nil
37
+ @pot_file = nil
38
+ if block_given?
39
+ yield(self)
40
+ define
41
+ end
42
+ end
43
+
44
+ # @private
45
+ def define
46
+ set_default_values
47
+ define_tasks
48
+ end
49
+
50
+ private
51
+ def set_default_values
52
+ @base_dir ||= Pathname.new("doc")
53
+ @translate_languages ||= [:ja]
54
+ @supported_languages = [:en, *@translate_languages]
55
+ @html_files = FileList[(doc_en_dir + "**/*.html").to_s].to_a
56
+
57
+ @po_dir = "doc/po"
58
+ @pot_file = "#{@po_dir}/#{@spec.name}.pot"
59
+
60
+ end
61
+
62
+ def reference_base_dir
63
+ @base_dir + "reference"
64
+ end
65
+
66
+ def doc_en_dir
67
+ @base_dir + "reference/en"
68
+ end
69
+
70
+ def html_base_dir
71
+ @base_dir + "html"
72
+ end
73
+
74
+ def html_reference_dir
75
+ html_base_dir + @spec.name
76
+ end
77
+
78
+ def define_tasks
79
+ namespace :reference do
80
+ define_pot_tasks
81
+ define_po_tasks
82
+ define_translate_task
83
+ define_generate_task
84
+ define_publication_task
85
+ end
86
+ end
87
+
88
+ def define_pot_tasks
89
+ namespace :pot do
90
+ directory @po_dir
91
+ file @pot_file => [@po_dir, *@html_files] do |t|
92
+ sh("xml2po", "--keep-entities", "--output", t.name, *@html_files)
93
+ end
94
+
95
+ desc "Generates pot file."
96
+ task :generate => @pot_file do |t|
97
+ end
98
+ end
99
+ end
100
+
101
+ def define_po_tasks
102
+ namespace :po do
103
+ @translate_languages.each do |language|
104
+ namespace language do
105
+ po_file = "#{@po_dir}/#{language}.po"
106
+
107
+ if File.exist?(po_file)
108
+ file po_file => @html_files do |t|
109
+ sh("xml2po", "--keep-entities", "--update", t.name, *@html_files)
110
+ end
111
+ else
112
+ file po_file => @pot_file do |t|
113
+ sh("msginit",
114
+ "--input=#{@pot_file}",
115
+ "--output=#{t.name}",
116
+ "--locale=#{language}")
117
+ end
118
+ end
119
+
120
+ desc "Updates po file for #{language}."
121
+ task :update => po_file
122
+ end
123
+ end
124
+
125
+ desc "Updates po files."
126
+ task :update do
127
+ ruby($0, "clobber")
128
+ ruby($0, "yard")
129
+ @translate_languages.each do |language|
130
+ ruby($0, "reference:po:#{language}:update")
131
+ end
132
+ end
133
+ end
134
+ end
135
+
136
+ def define_translate_task
137
+ namespace :translate do
138
+ @translate_languages.each do |language|
139
+ po_file = "#{@po_dir}/#{language}.po"
140
+ translate_doc_dir = "#{reference_base_dir}/#{language}"
141
+ desc "Translates documents to #{language}."
142
+ task language => [po_file, reference_base_dir, *@html_files] do
143
+ doc_en_dir.find do |path|
144
+ base_path = path.relative_path_from(doc_en_dir)
145
+ translated_path = "#{translate_doc_dir}/#{base_path}"
146
+ if path.directory?
147
+ mkdir_p(translated_path)
148
+ next
149
+ end
150
+ case path.extname
151
+ when ".html"
152
+ sh("xml2po --keep-entities " +
153
+ "--po-file #{po_file} --language #{language} " +
154
+ "#{path} > #{translated_path}")
155
+ else
156
+ cp(path.to_s, translated_path, :preserve => true)
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end
162
+
163
+ translate_task_names = @translate_languages.collect do |language|
164
+ "reference:translate:#{language}"
165
+ end
166
+ desc "Translates references."
167
+ task :translate => translate_task_names
168
+ end
169
+
170
+ def define_generate_task
171
+ desc "Generates references."
172
+ task :generate => [:yard, :translate]
173
+ end
174
+
175
+ def define_publication_task
176
+ namespace :publication do
177
+ task :prepare do
178
+ @supported_languages.each do |language|
179
+ raw_reference_dir = reference_base_dir + language.to_s
180
+ prepared_reference_dir = html_reference_dir + language.to_s
181
+ rm_rf(prepared_reference_dir.to_s)
182
+ head = erb_template("head.#{language}")
183
+ header = erb_template("header.#{language}")
184
+ footer = erb_template("footer.#{language}")
185
+ raw_reference_dir.find do |path|
186
+ relative_path = path.relative_path_from(raw_reference_dir)
187
+ prepared_path = prepared_reference_dir + relative_path
188
+ if path.directory?
189
+ mkdir_p(prepared_path.to_s)
190
+ else
191
+ case path.basename.to_s
192
+ when /(?:file|method|class)_list\.html\z/
193
+ cp(path.to_s, prepared_path.to_s)
194
+ when /\.html\z/
195
+ relative_dir_path = relative_path.dirname
196
+ current_path = relative_dir_path + path.basename
197
+ if current_path.basename.to_s == "index.html"
198
+ current_path = current_path.dirname
199
+ end
200
+ top_path = html_base_dir.relative_path_from(prepared_path.dirname)
201
+ package_path = top_path + @spec.name
202
+ paths = {
203
+ :top => top_path,
204
+ :current => current_path,
205
+ :package => package_path,
206
+ }
207
+ templates = {
208
+ :head => head,
209
+ :header => header,
210
+ :footer => footer
211
+ }
212
+ content = apply_template(File.read(path.to_s),
213
+ paths,
214
+ templates,
215
+ language)
216
+ File.open(prepared_path.to_s, "w") do |file|
217
+ file.print(content)
218
+ end
219
+ else
220
+ cp(path.to_s, prepared_path.to_s)
221
+ end
222
+ end
223
+ end
224
+ end
225
+ File.open("#{html_reference_dir}/.htaccess", "w") do |file|
226
+ file.puts("Redirect permanent /#{@spec.name}/text/TUTORIAL_ja_rdoc.html " +
227
+ "#{@spec.homepage}#{@spec.name}/ja/file.tutorial.html")
228
+ file.puts("RedirectMatch permanent ^/#{@spec.name}/$ " +
229
+ "#{@spec.homepage}#{@spec.name}/en/")
230
+ end
231
+ end
232
+ end
233
+ end
234
+
235
+ def apply_template(content, paths, templates, language)
236
+ content = content.sub(/lang="en"/, "lang=\"#{language}\"")
237
+
238
+ title = nil
239
+ content = content.sub(/<title>(.+?)<\/title>/m) do
240
+ title = $1
241
+ templates[:head].result(binding)
242
+ end
243
+
244
+ content = content.sub(/<body(?:.*?)>/) do |body_start|
245
+ "#{body_start}\n#{templates[:header].result(binding)}\n"
246
+ end
247
+
248
+ content = content.sub(/<\/body/) do |body_end|
249
+ "\n#{templates[:footer].result(binding)}\n#{body_end}"
250
+ end
251
+
252
+ content
253
+ end
254
+
255
+ def erb_template(name)
256
+ file = File.join("doc/templates", "#{name}.html.erb")
257
+ template = File.read(file)
258
+ erb = ERB.new(template, nil, "-")
259
+ erb.filename = file
260
+ erb
261
+ end
262
+ end
263
+ end