ruby-bsdconv 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README +1 -0
- data/Rakefile +23 -0
- data/example/example.rb +17 -0
- data/example/example2.rb +21 -0
- data/example/example3.rb +21 -0
- data/ext/ruby-bsdconv/bsdconv.c +317 -0
- data/ext/ruby-bsdconv/extconf.rb +11 -0
- data/ruby-bsdconv.gemspec +36 -0
- data/task/gemgem.rb +164 -0
- metadata +56 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pkg
|
data/README
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
see example/* for examples
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "#{dir = File.dirname(__FILE__)}/task/gemgem"
|
4
|
+
|
5
|
+
Gemgem.dir = dir
|
6
|
+
($LOAD_PATH << File.expand_path("#{Gemgem.dir}/lib")).uniq!
|
7
|
+
|
8
|
+
desc 'Generate gemspec'
|
9
|
+
task 'gem:spec' do
|
10
|
+
Gemgem.spec = Gemgem.create do |s|
|
11
|
+
s.name = 'ruby-bsdconv'
|
12
|
+
s.version = '0.9.0'
|
13
|
+
s.extensions = 'ext/ruby-bsdconv/extconf.rb'
|
14
|
+
s.authors = ['Buganini Q']
|
15
|
+
s.homepage = 'https://github.com/buganini/ruby-bsdconv'
|
16
|
+
s.summary = 'ruby wrapper for bsdconv'
|
17
|
+
s.description =
|
18
|
+
"#{s.summary}. bsdconv is a BSD licensed charset/encoding converter" \
|
19
|
+
" library with more functionalities than libiconv"
|
20
|
+
end
|
21
|
+
|
22
|
+
Gemgem.write
|
23
|
+
end
|
data/example/example.rb
ADDED
data/example/example2.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bsdconv'
|
4
|
+
|
5
|
+
c=Bsdconv.new('utf-8:utf-8')
|
6
|
+
|
7
|
+
if c.nil?
|
8
|
+
abort(Bsdconv.error())
|
9
|
+
end
|
10
|
+
|
11
|
+
c.insert_phase('full',Bsdconv::INTER, 1)
|
12
|
+
puts c.conv('test')
|
13
|
+
|
14
|
+
p Bsdconv.codec_check(Bsdconv::FROM, "_utf-8")
|
15
|
+
p Bsdconv.codec_check(Bsdconv::INTER, "_utf-8")
|
16
|
+
puts 'From:'
|
17
|
+
p Bsdconv.codecs_list(Bsdconv::FROM)
|
18
|
+
puts 'Inter:'
|
19
|
+
p Bsdconv.codecs_list(Bsdconv::INTER)
|
20
|
+
puts 'To:'
|
21
|
+
p Bsdconv.codecs_list(Bsdconv::TO)
|
data/example/example3.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bsdconv'
|
4
|
+
|
5
|
+
score,path=Bsdconv::mktemp("score.XXXXXX")
|
6
|
+
list=open("characters_list.txt","w+")
|
7
|
+
|
8
|
+
c=Bsdconv.new('utf-8:score_train:null')
|
9
|
+
c.init
|
10
|
+
|
11
|
+
c.ctl(Bsdconv::CTL_ATTACH_SCORE, score, 0);
|
12
|
+
c.ctl(Bsdconv::CTL_ATTACH_OUTPUT_FILE, list, 0);
|
13
|
+
|
14
|
+
open(ARGV[0], "r") { |fp|
|
15
|
+
while !fp.eof?
|
16
|
+
c.conv_chunk(fp.read(1024))
|
17
|
+
end
|
18
|
+
c.conv_chunk_last("")
|
19
|
+
}
|
20
|
+
|
21
|
+
File.unlink(path)
|
@@ -0,0 +1,317 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
#ifdef HAVE_RUBY_IO_H /* Ruby 1.9 and later */
|
3
|
+
#include "ruby/io.h"
|
4
|
+
#else /* Ruby 1.8.x */
|
5
|
+
#include "rubyio.h"
|
6
|
+
#define rb_io_stdio_file(iot) ((iot)->f)
|
7
|
+
#endif
|
8
|
+
#include <bsdconv.h>
|
9
|
+
|
10
|
+
#ifndef WIN32
|
11
|
+
#include <sys/types.h>
|
12
|
+
#include <sys/stat.h>
|
13
|
+
#include <unistd.h>
|
14
|
+
#endif
|
15
|
+
|
16
|
+
#define IBUFLEN 1024
|
17
|
+
|
18
|
+
void Init_bsdconv();
|
19
|
+
static VALUE m_new(VALUE, VALUE);
|
20
|
+
static VALUE m_insert_phase(VALUE, VALUE, VALUE, VALUE);
|
21
|
+
static VALUE m_insert_codec(VALUE, VALUE, VALUE, VALUE);
|
22
|
+
static VALUE m_replace_phase(VALUE, VALUE, VALUE, VALUE);
|
23
|
+
static VALUE m_replace_codec(VALUE, VALUE, VALUE, VALUE);
|
24
|
+
static VALUE m_conv(VALUE, VALUE);
|
25
|
+
static VALUE m_init(VALUE);
|
26
|
+
static VALUE m_ctl(VALUE, VALUE, VALUE, VALUE);
|
27
|
+
static VALUE m_conv_chunk(VALUE, VALUE);
|
28
|
+
static VALUE m_conv_chunk_last(VALUE, VALUE);
|
29
|
+
static VALUE m_conv_file(VALUE, VALUE, VALUE);
|
30
|
+
static VALUE m_info(VALUE);
|
31
|
+
static VALUE m_nil(VALUE);
|
32
|
+
static VALUE m_inspect(VALUE);
|
33
|
+
|
34
|
+
static VALUE f_error(VALUE);
|
35
|
+
static VALUE f_codecs_list(VALUE, VALUE);
|
36
|
+
static VALUE f_codec_check(VALUE, VALUE, VALUE);
|
37
|
+
static VALUE f_mktemp(VALUE, VALUE);
|
38
|
+
static VALUE f_fopen(VALUE, VALUE, VALUE);
|
39
|
+
|
40
|
+
VALUE Bsdconv_file;
|
41
|
+
|
42
|
+
void Init_bsdconv(){
|
43
|
+
VALUE Bsdconv = rb_define_class("Bsdconv", rb_cObject);
|
44
|
+
rb_define_singleton_method(Bsdconv, "new", m_new, 1);
|
45
|
+
rb_define_method(Bsdconv, "insert_phase", m_insert_phase, 3);
|
46
|
+
rb_define_method(Bsdconv, "insert_codec", m_insert_codec, 3);
|
47
|
+
rb_define_method(Bsdconv, "replace_phase", m_replace_phase, 3);
|
48
|
+
rb_define_method(Bsdconv, "replace_codec", m_replace_codec, 3);
|
49
|
+
rb_define_method(Bsdconv, "conv", m_conv, 1);
|
50
|
+
rb_define_method(Bsdconv, "init", m_init, 0);
|
51
|
+
rb_define_method(Bsdconv, "ctl", m_ctl, 3);
|
52
|
+
rb_define_method(Bsdconv, "conv_chunk", m_conv_chunk, 1);
|
53
|
+
rb_define_method(Bsdconv, "conv_chunk_last", m_conv_chunk_last, 1);
|
54
|
+
rb_define_method(Bsdconv, "conv_file", m_conv_file, 2);
|
55
|
+
rb_define_method(Bsdconv, "info", m_info, 0);
|
56
|
+
rb_define_method(Bsdconv, "inspect", m_inspect, 0);
|
57
|
+
|
58
|
+
rb_define_const(Bsdconv, "FROM", INT2NUM(FROM));
|
59
|
+
rb_define_const(Bsdconv, "INTER", INT2NUM(INTER));
|
60
|
+
rb_define_const(Bsdconv, "TO", INT2NUM(TO));
|
61
|
+
|
62
|
+
rb_define_const(Bsdconv, "CTL_ATTACH_SCORE", INT2NUM(BSDCONV_ATTACH_SCORE));
|
63
|
+
rb_define_const(Bsdconv, "CTL_SET_WIDE_AMBI", INT2NUM(BSDCONV_SET_WIDE_AMBI));
|
64
|
+
rb_define_const(Bsdconv, "CTL_SET_TRIM_WIDTH", INT2NUM(BSDCONV_SET_TRIM_WIDTH));
|
65
|
+
rb_define_const(Bsdconv, "CTL_ATTACH_OUTPUT_FILE", INT2NUM(BSDCONV_ATTACH_OUTPUT_FILE));
|
66
|
+
|
67
|
+
rb_define_singleton_method(Bsdconv, "error", f_error, 0);
|
68
|
+
rb_define_singleton_method(Bsdconv, "codecs_list", f_codecs_list, 1);
|
69
|
+
rb_define_singleton_method(Bsdconv, "codec_check", f_codec_check, 2);
|
70
|
+
rb_define_singleton_method(Bsdconv, "mktemp", f_mktemp, 1);
|
71
|
+
rb_define_singleton_method(Bsdconv, "fopen", f_fopen, 2);
|
72
|
+
|
73
|
+
Bsdconv_file = rb_define_class("Bsdconv_file", rb_cObject);
|
74
|
+
}
|
75
|
+
|
76
|
+
static VALUE m_new(VALUE class, VALUE conversion){
|
77
|
+
struct bsdconv_instance *ins;
|
78
|
+
if(TYPE(conversion)!=T_STRING)
|
79
|
+
ins=bsdconv_create("");
|
80
|
+
else
|
81
|
+
ins=bsdconv_create(RSTRING_PTR(conversion));
|
82
|
+
if(ins==NULL)
|
83
|
+
return Qnil;
|
84
|
+
return Data_Wrap_Struct(class, 0, bsdconv_destroy, ins);
|
85
|
+
}
|
86
|
+
|
87
|
+
static VALUE m_insert_phase(VALUE self, VALUE conversion, VALUE phase_type, VALUE phasen){
|
88
|
+
struct bsdconv_instance *ins;
|
89
|
+
Data_Get_Struct(self, struct bsdconv_instance, ins);
|
90
|
+
return INT2NUM(bsdconv_insert_phase(ins, RSTRING_PTR(conversion), NUM2INT(phase_type), NUM2INT(phasen)));
|
91
|
+
}
|
92
|
+
|
93
|
+
static VALUE m_insert_codec(VALUE self, VALUE conversion, VALUE phasen, VALUE codecn){
|
94
|
+
struct bsdconv_instance *ins;
|
95
|
+
Data_Get_Struct(self, struct bsdconv_instance, ins);
|
96
|
+
return INT2NUM(bsdconv_insert_phase(ins, RSTRING_PTR(conversion), NUM2INT(phasen), NUM2INT(codecn)));
|
97
|
+
}
|
98
|
+
|
99
|
+
static VALUE m_replace_phase(VALUE self, VALUE conversion, VALUE phase_type, VALUE phasen){
|
100
|
+
struct bsdconv_instance *ins;
|
101
|
+
Data_Get_Struct(self, struct bsdconv_instance, ins);
|
102
|
+
return INT2NUM(bsdconv_insert_phase(ins, RSTRING_PTR(conversion), NUM2INT(phase_type), NUM2INT(phasen)));
|
103
|
+
}
|
104
|
+
|
105
|
+
static VALUE m_replace_codec(VALUE self, VALUE conversion, VALUE phasen, VALUE codecn){
|
106
|
+
struct bsdconv_instance *ins;
|
107
|
+
Data_Get_Struct(self, struct bsdconv_instance, ins);
|
108
|
+
return INT2NUM(bsdconv_insert_phase(ins, RSTRING_PTR(conversion), NUM2INT(phasen), NUM2INT(codecn)));
|
109
|
+
}
|
110
|
+
|
111
|
+
static VALUE m_conv(VALUE self, VALUE str){
|
112
|
+
VALUE ret;
|
113
|
+
struct bsdconv_instance *ins;
|
114
|
+
Data_Get_Struct(self, struct bsdconv_instance, ins);
|
115
|
+
bsdconv_init(ins);
|
116
|
+
ins->output_mode=BSDCONV_AUTOMALLOC;
|
117
|
+
ins->input.data=RSTRING_PTR(str);
|
118
|
+
ins->input.len=RSTRING_LEN(str);
|
119
|
+
ins->input.flags=0;
|
120
|
+
ins->flush=1;
|
121
|
+
bsdconv(ins);
|
122
|
+
ret=rb_str_new(ins->output.data, ins->output.len);
|
123
|
+
bsdconv_free(ins->output.data);
|
124
|
+
return ret;
|
125
|
+
}
|
126
|
+
|
127
|
+
static VALUE m_init(VALUE self){
|
128
|
+
struct bsdconv_instance *ins;
|
129
|
+
Data_Get_Struct(self, struct bsdconv_instance, ins);
|
130
|
+
bsdconv_init(ins);
|
131
|
+
return Qtrue;
|
132
|
+
}
|
133
|
+
|
134
|
+
static VALUE m_ctl(VALUE self, VALUE action, VALUE res, VALUE num){
|
135
|
+
struct bsdconv_instance *ins;
|
136
|
+
rb_io_t *fptr = NULL;
|
137
|
+
void *ptr=NULL;
|
138
|
+
Data_Get_Struct(self, struct bsdconv_instance, ins);
|
139
|
+
if(TYPE(res)==T_FILE){
|
140
|
+
GetOpenFile(res, fptr);
|
141
|
+
ptr=rb_io_stdio_file(fptr);
|
142
|
+
}else{
|
143
|
+
Data_Get_Struct(res, FILE, ptr);
|
144
|
+
}
|
145
|
+
bsdconv_ctl(ins, NUM2INT(action), ptr, NUM2INT(num));
|
146
|
+
return Qtrue;
|
147
|
+
}
|
148
|
+
|
149
|
+
static VALUE m_conv_chunk(VALUE self, VALUE str){
|
150
|
+
VALUE ret;
|
151
|
+
struct bsdconv_instance *ins;
|
152
|
+
Data_Get_Struct(self, struct bsdconv_instance, ins);
|
153
|
+
ins->output_mode=BSDCONV_AUTOMALLOC;
|
154
|
+
ins->input.data=RSTRING_PTR(str);
|
155
|
+
ins->input.len=RSTRING_LEN(str);
|
156
|
+
ins->input.flags=0;
|
157
|
+
bsdconv(ins);
|
158
|
+
ret=rb_str_new(ins->output.data, ins->output.len);
|
159
|
+
bsdconv_free(ins->output.data);
|
160
|
+
return ret;
|
161
|
+
}
|
162
|
+
|
163
|
+
static VALUE m_conv_chunk_last(VALUE self, VALUE str){
|
164
|
+
VALUE ret;
|
165
|
+
struct bsdconv_instance *ins;
|
166
|
+
Data_Get_Struct(self, struct bsdconv_instance, ins);
|
167
|
+
ins->output_mode=BSDCONV_AUTOMALLOC;
|
168
|
+
ins->input.data=RSTRING_PTR(str);
|
169
|
+
ins->input.len=RSTRING_LEN(str);
|
170
|
+
ins->input.flags=1;
|
171
|
+
bsdconv(ins);
|
172
|
+
ret=rb_str_new(ins->output.data, ins->output.len);
|
173
|
+
bsdconv_free(ins->output.data);
|
174
|
+
return ret;
|
175
|
+
}
|
176
|
+
|
177
|
+
static VALUE m_conv_file(VALUE self, VALUE ifile, VALUE ofile){
|
178
|
+
struct bsdconv_instance *ins;
|
179
|
+
FILE *inf, *otf;
|
180
|
+
char *s1=RSTRING_PTR(ifile);
|
181
|
+
char *s2=RSTRING_PTR(ofile);
|
182
|
+
char *in;
|
183
|
+
char *tmp;
|
184
|
+
int fd;
|
185
|
+
|
186
|
+
Data_Get_Struct(self, struct bsdconv_instance, ins);
|
187
|
+
inf=fopen(s1,"r");
|
188
|
+
if(!inf){
|
189
|
+
return Qfalse;
|
190
|
+
}
|
191
|
+
tmp=malloc(strlen(s2)+8);
|
192
|
+
strcpy(tmp, s2);
|
193
|
+
strcat(tmp, ".XXXXXX");
|
194
|
+
if((fd=mkstemp(tmp))==-1){
|
195
|
+
free(tmp);
|
196
|
+
return Qfalse;
|
197
|
+
}
|
198
|
+
otf=fdopen(fd,"w");
|
199
|
+
if(!otf){
|
200
|
+
free(tmp);
|
201
|
+
return Qfalse;
|
202
|
+
}
|
203
|
+
|
204
|
+
#ifndef WIN32
|
205
|
+
struct stat stat;
|
206
|
+
fstat(fileno(inf), &stat);
|
207
|
+
fchown(fileno(otf), stat.st_uid, stat.st_gid);
|
208
|
+
fchmod(fileno(otf), stat.st_mode);
|
209
|
+
#endif
|
210
|
+
|
211
|
+
bsdconv_init(ins);
|
212
|
+
do{
|
213
|
+
in=bsdconv_malloc(IBUFLEN);
|
214
|
+
ins->input.data=in;
|
215
|
+
ins->input.len=fread(in, 1, IBUFLEN, inf);
|
216
|
+
ins->input.flags|=F_FREE;
|
217
|
+
if(ins->input.len==0){
|
218
|
+
ins->flush=1;
|
219
|
+
}
|
220
|
+
ins->output_mode=BSDCONV_FILE;
|
221
|
+
ins->output.data=otf;
|
222
|
+
bsdconv(ins);
|
223
|
+
}while(ins->flush==0);
|
224
|
+
|
225
|
+
fclose(inf);
|
226
|
+
fclose(otf);
|
227
|
+
unlink(s2);
|
228
|
+
rename(tmp,s2);
|
229
|
+
free(tmp);
|
230
|
+
|
231
|
+
return Qtrue;
|
232
|
+
}
|
233
|
+
|
234
|
+
static VALUE m_info(VALUE self){
|
235
|
+
VALUE ret;
|
236
|
+
struct bsdconv_instance *ins;
|
237
|
+
Data_Get_Struct(self, struct bsdconv_instance, ins);
|
238
|
+
ret=rb_hash_new();
|
239
|
+
rb_hash_aset(ret, rb_str_new2("ierr"), INT2FIX(ins->ierr));
|
240
|
+
rb_hash_aset(ret, rb_str_new2("oerr"), INT2FIX(ins->oerr));
|
241
|
+
rb_hash_aset(ret, rb_str_new2("score"), rb_float_new(ins->score));
|
242
|
+
rb_hash_aset(ret, rb_str_new2("full"), INT2FIX(ins->full));
|
243
|
+
rb_hash_aset(ret, rb_str_new2("half"), INT2FIX(ins->half));
|
244
|
+
rb_hash_aset(ret, rb_str_new2("ambi"), INT2FIX(ins->ambi));
|
245
|
+
return ret;
|
246
|
+
}
|
247
|
+
|
248
|
+
static VALUE m_inspect(VALUE self){
|
249
|
+
#define TEMPLATE "Bsdconv.new(\"%s\")"
|
250
|
+
VALUE ret;
|
251
|
+
struct bsdconv_instance *ins;
|
252
|
+
char *s;
|
253
|
+
char *s2;
|
254
|
+
int len=sizeof(TEMPLATE);
|
255
|
+
Data_Get_Struct(self, struct bsdconv_instance, ins);
|
256
|
+
s=bsdconv_pack(ins);
|
257
|
+
len+=strlen(s);
|
258
|
+
s2=malloc(len);
|
259
|
+
sprintf(s2, TEMPLATE, s);
|
260
|
+
bsdconv_free(s);
|
261
|
+
ret=rb_str_new2(s2);
|
262
|
+
free(s2);
|
263
|
+
return ret;
|
264
|
+
}
|
265
|
+
|
266
|
+
static VALUE f_error(VALUE self){
|
267
|
+
VALUE ret;
|
268
|
+
char *s=bsdconv_error();
|
269
|
+
ret=rb_str_new2(s);
|
270
|
+
bsdconv_free(s);
|
271
|
+
return ret;
|
272
|
+
}
|
273
|
+
|
274
|
+
static VALUE f_codecs_list(VALUE self, VALUE phase_type){
|
275
|
+
char **list, **p;
|
276
|
+
VALUE ret;
|
277
|
+
ret=rb_ary_new();
|
278
|
+
list=bsdconv_codecs_list(NUM2INT(phase_type));
|
279
|
+
p=list;
|
280
|
+
while(*p!=NULL){
|
281
|
+
rb_ary_push(ret, rb_str_new2(*p));
|
282
|
+
bsdconv_free(*p);
|
283
|
+
p+=1;
|
284
|
+
}
|
285
|
+
bsdconv_free(list);
|
286
|
+
return ret;
|
287
|
+
}
|
288
|
+
|
289
|
+
static VALUE f_codec_check(VALUE self, VALUE phase_type, VALUE codec){
|
290
|
+
if(bsdconv_codec_check(NUM2INT(phase_type), RSTRING_PTR(codec))){
|
291
|
+
return Qtrue;
|
292
|
+
}
|
293
|
+
return Qfalse;
|
294
|
+
}
|
295
|
+
|
296
|
+
static VALUE f_mktemp(VALUE self, VALUE template){
|
297
|
+
char *fn=strdup(RSTRING_PTR(template));
|
298
|
+
int fd=bsdconv_mkstemp(fn);
|
299
|
+
if(fd==-1)
|
300
|
+
return Qnil;
|
301
|
+
FILE *fp=fdopen(fd, "wb+");
|
302
|
+
VALUE rfp=Data_Wrap_Struct(Bsdconv_file, 0, fclose, fp);
|
303
|
+
VALUE ret;
|
304
|
+
ret=rb_ary_new();
|
305
|
+
rb_ary_push(ret, rfp);
|
306
|
+
rb_ary_push(ret, rb_str_new2(fn));
|
307
|
+
free(fn);
|
308
|
+
return ret;
|
309
|
+
}
|
310
|
+
|
311
|
+
static VALUE f_fopen(VALUE self, VALUE filename, VALUE mode){
|
312
|
+
FILE *fp=fopen(RSTRING_PTR(filename), RSTRING_PTR(mode));
|
313
|
+
if(!fp)
|
314
|
+
return Qnil;
|
315
|
+
VALUE ret=Data_Wrap_Struct(Bsdconv_file, 0, fclose, fp);
|
316
|
+
return ret;
|
317
|
+
}
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "ruby-bsdconv"
|
5
|
+
s.version = "0.9.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Buganini Q"]
|
9
|
+
s.date = "2012-12-03"
|
10
|
+
s.description = "ruby wrapper for bsdconv. bsdconv is a BSD licensed charset/encoding converter library with more functionalities than libiconv"
|
11
|
+
s.extensions = ["ext/ruby-bsdconv/extconf.rb"]
|
12
|
+
s.files = [
|
13
|
+
".gitignore",
|
14
|
+
"README",
|
15
|
+
"Rakefile",
|
16
|
+
"example/example.rb",
|
17
|
+
"example/example2.rb",
|
18
|
+
"example/example3.rb",
|
19
|
+
"ext/ruby-bsdconv/bsdconv.c",
|
20
|
+
"ext/ruby-bsdconv/extconf.rb",
|
21
|
+
"ruby-bsdconv.gemspec",
|
22
|
+
"task/gemgem.rb"]
|
23
|
+
s.homepage = "https://github.com/buganini/ruby-bsdconv"
|
24
|
+
s.require_paths = ["lib"]
|
25
|
+
s.rubygems_version = "1.8.23"
|
26
|
+
s.summary = "ruby wrapper for bsdconv"
|
27
|
+
|
28
|
+
if s.respond_to? :specification_version then
|
29
|
+
s.specification_version = 3
|
30
|
+
|
31
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
32
|
+
else
|
33
|
+
end
|
34
|
+
else
|
35
|
+
end
|
36
|
+
end
|
data/task/gemgem.rb
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
|
2
|
+
require 'pathname'
|
3
|
+
|
4
|
+
module Gemgem
|
5
|
+
class << self
|
6
|
+
attr_accessor :dir, :spec
|
7
|
+
end
|
8
|
+
|
9
|
+
module_function
|
10
|
+
def create
|
11
|
+
yield(spec = Gem::Specification.new{ |s|
|
12
|
+
s.rubygems_version = Gem::VERSION
|
13
|
+
s.date = Time.now.strftime('%Y-%m-%d')
|
14
|
+
s.files = gem_files
|
15
|
+
s.test_files = gem_files.grep(%r{^test/(.+?/)*test_.+?\.rb$})
|
16
|
+
s.executables = Dir['bin/*'].map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = %w[lib]
|
18
|
+
})
|
19
|
+
spec
|
20
|
+
end
|
21
|
+
|
22
|
+
def gem_tag
|
23
|
+
"#{spec.name}-#{spec.version}"
|
24
|
+
end
|
25
|
+
|
26
|
+
def write
|
27
|
+
File.open("#{dir}/#{spec.name}.gemspec", 'w'){ |f|
|
28
|
+
f << split_lines(spec.to_ruby) }
|
29
|
+
end
|
30
|
+
|
31
|
+
def split_lines ruby
|
32
|
+
ruby.gsub(/(.+?)\[(.+?)\]/){ |s|
|
33
|
+
if $2.index(',')
|
34
|
+
"#{$1}[\n #{$2.split(',').map(&:strip).join(",\n ")}]"
|
35
|
+
else
|
36
|
+
s
|
37
|
+
end
|
38
|
+
}
|
39
|
+
end
|
40
|
+
|
41
|
+
def all_files
|
42
|
+
@all_files ||= find_files(Pathname.new(dir)).map{ |file|
|
43
|
+
if file.to_s =~ %r{\.git/|\.git$}
|
44
|
+
nil
|
45
|
+
else
|
46
|
+
file.to_s
|
47
|
+
end
|
48
|
+
}.compact.sort
|
49
|
+
end
|
50
|
+
|
51
|
+
def gem_files
|
52
|
+
@gem_files ||= all_files - ignored_files
|
53
|
+
end
|
54
|
+
|
55
|
+
def ignored_files
|
56
|
+
@ignored_file ||= all_files.select{ |path| ignore_patterns.find{ |ignore|
|
57
|
+
path =~ ignore && !git_files.include?(path)}}
|
58
|
+
end
|
59
|
+
|
60
|
+
def git_files
|
61
|
+
@git_files ||= if File.exist?("#{dir}/.git")
|
62
|
+
`git ls-files`.split("\n")
|
63
|
+
else
|
64
|
+
[]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# protected
|
69
|
+
def find_files path
|
70
|
+
path.children.select(&:file?).map{|file| file.to_s[(dir.size+1)..-1]} +
|
71
|
+
path.children.select(&:directory?).map{|dir| find_files(dir)}.flatten
|
72
|
+
end
|
73
|
+
|
74
|
+
def ignore_patterns
|
75
|
+
@ignore_files ||= expand_patterns(
|
76
|
+
gitignore.split("\n").reject{ |pattern|
|
77
|
+
pattern.strip == ''
|
78
|
+
}).map{ |pattern| %r{^([^/]+/)*?#{Regexp.escape(pattern)}(/[^/]+)*?$} }
|
79
|
+
end
|
80
|
+
|
81
|
+
def expand_patterns pathes
|
82
|
+
pathes.map{ |path|
|
83
|
+
if path !~ /\*/
|
84
|
+
path
|
85
|
+
else
|
86
|
+
expand_patterns(
|
87
|
+
Dir[path] +
|
88
|
+
Pathname.new(File.dirname(path)).children.select(&:directory?).
|
89
|
+
map{ |prefix| "#{prefix}/#{File.basename(path)}" })
|
90
|
+
end
|
91
|
+
}.flatten
|
92
|
+
end
|
93
|
+
|
94
|
+
def gitignore
|
95
|
+
if File.exist?(path = "#{dir}/.gitignore")
|
96
|
+
File.read(path)
|
97
|
+
else
|
98
|
+
''
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
namespace :gem do
|
104
|
+
|
105
|
+
desc 'Install gem'
|
106
|
+
task :install => [:build] do
|
107
|
+
sh("#{Gem.ruby} -S gem install pkg/#{Gemgem.gem_tag}")
|
108
|
+
end
|
109
|
+
|
110
|
+
desc 'Build gem'
|
111
|
+
task :build => [:spec] do
|
112
|
+
sh("#{Gem.ruby} -S gem build #{Gemgem.spec.name}.gemspec")
|
113
|
+
sh("mkdir -p pkg")
|
114
|
+
sh("mv #{Gemgem.gem_tag}.gem pkg/")
|
115
|
+
end
|
116
|
+
|
117
|
+
desc 'Release gem'
|
118
|
+
task :release => [:spec, :check, :build] do
|
119
|
+
sh("git tag #{Gemgem.gem_tag}")
|
120
|
+
sh("git push")
|
121
|
+
sh("git push --tags")
|
122
|
+
sh("#{Gem.ruby} -S gem push pkg/#{Gemgem.gem_tag}.gem")
|
123
|
+
end
|
124
|
+
|
125
|
+
task :check do
|
126
|
+
ver = Gemgem.spec.version.to_s
|
127
|
+
|
128
|
+
if ENV['VERSION'].nil?
|
129
|
+
puts("\e[35mExpected " \
|
130
|
+
"\e[33mVERSION\e[35m=\e[33m#{ver}\e[0m")
|
131
|
+
exit(1)
|
132
|
+
|
133
|
+
elsif ENV['VERSION'] != ver
|
134
|
+
puts("\e[35mExpected \e[33mVERSION\e[35m=\e[33m#{ver} " \
|
135
|
+
"\e[35mbut got\n " \
|
136
|
+
"\e[33mVERSION\e[35m=\e[33m#{ENV['VERSION']}\e[0m")
|
137
|
+
exit(2)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
end # of gem namespace
|
142
|
+
|
143
|
+
desc 'Run tests in memory'
|
144
|
+
task :test do
|
145
|
+
require 'bacon'
|
146
|
+
Bacon.extend(Bacon::TestUnitOutput)
|
147
|
+
Bacon.summary_on_exit
|
148
|
+
$LOAD_PATH.unshift('lib')
|
149
|
+
Dir['./test/**/test_*.rb'].each{ |file| require file[0..-4] }
|
150
|
+
end
|
151
|
+
|
152
|
+
desc 'Run tests with shell'
|
153
|
+
task 'test:shell', :RUBY_OPTS do |t, args|
|
154
|
+
files = Dir['test/**/test_*.rb'].join(' ')
|
155
|
+
|
156
|
+
cmd = [Gem.ruby, args[:RUBY_OPTS],
|
157
|
+
'-I', 'lib', '-S', 'bacon', '--quiet', files]
|
158
|
+
|
159
|
+
sh(cmd.compact.join(' '))
|
160
|
+
end
|
161
|
+
|
162
|
+
task :default do
|
163
|
+
puts `#{Gem.ruby} -S #{$PROGRAM_NAME} -T`
|
164
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-bsdconv
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Buganini Q
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-03 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ruby wrapper for bsdconv. bsdconv is a BSD licensed charset/encoding
|
15
|
+
converter library with more functionalities than libiconv
|
16
|
+
email:
|
17
|
+
executables: []
|
18
|
+
extensions:
|
19
|
+
- ext/ruby-bsdconv/extconf.rb
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- README
|
24
|
+
- Rakefile
|
25
|
+
- example/example.rb
|
26
|
+
- example/example2.rb
|
27
|
+
- example/example3.rb
|
28
|
+
- ext/ruby-bsdconv/bsdconv.c
|
29
|
+
- ext/ruby-bsdconv/extconf.rb
|
30
|
+
- ruby-bsdconv.gemspec
|
31
|
+
- task/gemgem.rb
|
32
|
+
homepage: https://github.com/buganini/ruby-bsdconv
|
33
|
+
licenses: []
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.23
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: ruby wrapper for bsdconv
|
56
|
+
test_files: []
|