j2j 0.1.1 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f31a54131544c0d9cac9547e566d71a1ad62af81
4
- data.tar.gz: f2c2a12b448256f333cc721e715471ba8fad3d7d
3
+ metadata.gz: 238f5a8434d7b84e025a144fdb43aa02ee53bcce
4
+ data.tar.gz: 2cd7fa1a31d5063e2129084bf975e05739576716
5
5
  SHA512:
6
- metadata.gz: 0ca8381d43e8d5e49197a4910097f09bca5cf92f60f036b4797ad3bda16c29cc7c209e0811b78634434bc531b9f710ce960411d33937a9c438e98bd5ebcc2e1a
7
- data.tar.gz: 715cfdf1418cf60a3b80ba6a8abb67fae467f4a985f132086bcc2f6307d68df2ae6be0696f120beda444f91738ccc91ae25389f57d3afa074699451e79f4d68c
6
+ metadata.gz: a99af232b6b9da3912f35d61a2463cef16bd1482afbd4f08d2ca67c0b81621e5e6a70e5909b4a653c67a9535a2f6e8987a142f13c932417876be04564dddb389
7
+ data.tar.gz: fa5d39edff363587293cc591a6c053abbfad96cfbb168234444d0c829286e31b204077ed2a874755e91fd810a511299b3c1c1d8f397c2d4f830263d8c3daf70c
data/README.md CHANGED
@@ -1,36 +1,76 @@
1
- # J2j
1
+ # j2j
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/j2j`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Convert any **Files.json** to corresponding **Classe.java** files
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
6
5
 
7
6
  ## Installation
8
7
 
9
- Add this line to your application's Gemfile:
8
+ $ gem install j2j
9
+
10
+ ## Usage
11
+
12
+ $ j2j ~/sample.json -o ~/destination_folder
13
+
14
+ > `sample.json`:
10
15
 
11
- ```ruby
12
- gem 'j2j'
16
+ ```json
17
+ {
18
+ "total": 2,
19
+ "people": [
20
+ { "name": "jose" },
21
+ { "name": "maria" }
22
+ ]
23
+ }
13
24
  ```
14
25
 
15
- And then execute:
26
+ Lets look at the `~/destination_folder`...
16
27
 
17
- $ bundle
28
+ The files `Sample.java` and `Person.java` were created
18
29
 
19
- Or install it yourself as:
30
+ > `Sample.java`:
20
31
 
21
- $ gem install j2j
32
+ ```java
33
+ public class Sample {
22
34
 
23
- ## Usage
35
+ private Long total;
36
+ private List<Person> people;
37
+
38
+ public Long getTotal() { return total; }
39
+ public void setTotal(Long total) { this.total = total; }
40
+ public List<Person> getPerson() { return people; }
41
+ public void setPerson(List<Person> people) { this.people = people; }
42
+
43
+ }
44
+ ```
45
+
46
+ > `Person.java`:
47
+
48
+ ```java
49
+ public class Person {
50
+
51
+ private String name;
52
+
53
+ public String getName() { return name; }
54
+ public void setName(String name) { this.name = name; }
55
+
56
+ }
57
+ ```
58
+
59
+ ... and you're golden :)
24
60
 
25
- TODO: Write usage instructions here
61
+ # Advanced
26
62
 
27
- ## Development
63
+ | Param | Shortcut | Default value | Usage |
64
+ |:------------:|:---------:| :------------:|:------------|
65
+ | root_class | -r | Example.java | $ **j2j ~/file.json -r Person** |
66
+ | package | -p | com.example | $ **j2j ~/file.json -p com.company** |
67
+ | output | -o | out | $ **j2j ~/file.json -o src/** |
28
68
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
69
+ Complete example:
70
+ > $ j2j ~/file.json -r Person -p com.compay -o src/
30
71
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
72
 
33
73
  ## Contributing
34
74
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/j2j.
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/cesarferreira/j2j.
36
76
 
data/j2j.gemspec CHANGED
@@ -10,8 +10,20 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["cesar.manuel.ferreira@gmail.com"]
11
11
 
12
12
  spec.license = 'MIT'
13
- spec.summary = %q{Convert any file.json to Classe.java files}
14
- spec.description = %q{Convert any file.json to Classe.java files}
13
+ spec.summary = %q{Convert any Files.json to corresponding Classe.java files}
14
+ spec.description = <<-EOF
15
+ Convert any Files.json to corresponding Classe.java files
16
+ EOF
17
+
18
+ #spec.extra_rdoc_files = ['README', 'README.md']
19
+
20
+ spec.post_install_message = <<-EOF
21
+ Thanks for installing!
22
+ Head here for documentation: https://github.com/cesarferreira/j2j
23
+ EOF
24
+
25
+ spec.metadata = { "issue_tracker" => "https://example/issues" }
26
+
15
27
  spec.homepage = "https://github.com/cesarferreira/j2j"
16
28
 
17
29
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
@@ -24,11 +36,11 @@ Gem::Specification.new do |spec|
24
36
  spec.add_development_dependency "guard"
25
37
  spec.add_development_dependency "guard-rspec"
26
38
  spec.add_development_dependency "coveralls"
39
+ spec.add_development_dependency 'pry-byebug', '~> 3.1'
27
40
 
28
- spec.add_dependency 'pry-byebug', '~> 3.1'
29
41
  spec.add_dependency 'thor', '~> 0.19.1'
30
42
  spec.add_dependency 'colorize', '~> 0.7'
31
43
  spec.add_dependency 'json', '~> 1.8.3'
32
-
44
+ spec.add_dependency 'activesupport', '~> 4.2'
33
45
 
34
46
  end
data/lib/j2j.rb CHANGED
@@ -10,30 +10,14 @@ module J2j
10
10
 
11
11
  include Thor::Actions
12
12
 
13
- no_commands do
14
- def log(str)
15
- puts str if options[:verbose]
16
- end
17
- end
18
-
19
-
20
- def self.source_root
21
- File.dirname(__FILE__)
22
- end
23
-
24
13
  desc 'path/to/file.json', 'indicate the path to the file.json'
25
14
  class_option :root_class, :aliases => '-r', :default => 'Example.java'
26
15
  class_option :package, :aliases => '-p', :default => 'com.example'
27
- class_option :output, :aliases => '-o', :description => 'Number to call', :default => 'out'
16
+ class_option :output, :aliases => '-o', :description => 'Output folder', :default => 'out'
28
17
  def json(path_to_json)
29
- puts "JSON: #{path_to_json}"
30
-
31
18
  convert(path_to_json, options)
32
-
33
19
  end
34
20
 
35
-
36
-
37
21
  # map %w[--version -v] => :__print_version
38
22
  # desc "--version, -v", "print the version"
39
23
  # def __print_version
data/lib/j2j/converter.rb CHANGED
@@ -1,15 +1,12 @@
1
1
  require 'json'
2
- require 'pry'
2
+
3
+ require 'active_support'
4
+ require 'active_support/inflector'
3
5
 
4
6
  require_relative './configurator'
5
7
  require_relative './field_details'
6
8
 
7
9
 
8
- @java_classes = {}
9
- @java_fields = {}
10
- @java_lists = {}
11
- @field_info = {}
12
-
13
10
  ##
14
11
  ## GET JAVA TYPES
15
12
  ##
@@ -25,13 +22,13 @@ def get_java_type(value, field_details, key)
25
22
  inner_value = get_java_type(value[0], field_details, key)
26
23
  if inner_value == @config.unknown_class
27
24
  inner_value = to_java_class_name(key)
28
- if @do_chop
29
- inner_value.chop!
30
- end
31
- inner_value = add_class_prefix_and_suffix(inner_value)
25
+ # if @do_chop
26
+ # inner_value.chop!
27
+ # end
32
28
  setup_data(value[0], key)
33
29
  field_details.write_class_file = true
34
30
  end
31
+ binding.pry
35
32
  java_type = "List<" + inner_value + ">"
36
33
  elsif value.is_a?(String)
37
34
  java_type = "String"
@@ -107,7 +104,8 @@ def to_java_class_name(class_name)
107
104
  end
108
105
  end
109
106
 
110
- return java_class_name
107
+ singular_class_name = java_class_name.singularize
108
+ return singular_class_name
111
109
  end
112
110
 
113
111
 
@@ -199,9 +197,6 @@ end
199
197
  def java_class_output(class_name, parent)
200
198
 
201
199
  proper_class_name = to_java_class_name(class_name)
202
- if @java_lists[class_name]
203
- proper_class_name.chop! if @do_chop
204
- end
205
200
 
206
201
  field_list = ""
207
202
  list_import = ""
@@ -214,15 +209,15 @@ def java_class_output(class_name, parent)
214
209
  field_type = @field_info[field].type
215
210
  method_name = to_java_method_name(field)
216
211
  java_field = to_java_field_name(field)
212
+
217
213
  if @java_fields[field]
218
214
  field_type = to_java_class_name(field)
219
215
  method_name = to_java_method_name(field_type)
220
216
  end
217
+
221
218
  if (@java_lists[field] and @java_lists[field] == class_name)
222
219
  if @field_info[field].array_type
223
220
  field_type = @field_info[field].array_type
224
- elsif @do_chop
225
- field_type.chop!
226
221
  end
227
222
  field_type = "List<#{field_type}>"
228
223
  list_import = "import java.util.List;"
@@ -234,7 +229,11 @@ def java_class_output(class_name, parent)
234
229
  json_annotation = " @SerializedName(\"#{field}\")\n"
235
230
 
236
231
  end
237
- field_list = field_list + json_annotation + " private #{field_type} #{java_field};\n"
232
+
233
+ json_serialize_import = @config.json_serialize_import
234
+
235
+
236
+ field_list = field_list + json_annotation + " private #{field_type} #{java_field};\n\n"
238
237
  getters_and_setters = getters_and_setters + <<GS
239
238
 
240
239
  public #{field_type} get#{method_name}() {
@@ -249,7 +248,6 @@ GS
249
248
  end
250
249
 
251
250
  doc = <<JCO
252
- #{@java_class_header}
253
251
  package #@package;
254
252
 
255
253
  #{list_import}
@@ -275,7 +273,9 @@ JCO
275
273
 
276
274
  # don't write java file files "primitive" array/list fields
277
275
  if write_file(@field_info[class_name]) == true
278
- File.open("#{@config.output_directory}/#{proper_class_name}.java", 'w') {|f| f.write(doc) }
276
+ fileName = "#{@config.output_directory}/#{proper_class_name}.java"
277
+ File.open(fileName, 'w') {|f| f.write(doc) }
278
+ puts "Creating.... #{fileName.green}"
279
279
  end
280
280
 
281
281
  end
@@ -286,8 +286,6 @@ end
286
286
  ##
287
287
  def parse_args(path, params)
288
288
 
289
- ############################## TODO: analise path
290
-
291
289
  @config.json_file = path
292
290
  @config.package = params[:package]
293
291
  @config.top_level_class = params[:root_class].gsub(/.java/,'')
@@ -295,6 +293,27 @@ def parse_args(path, params)
295
293
 
296
294
  end
297
295
 
296
+ def valid_json?(json)
297
+ begin
298
+ JSON.parse(IO.read(json))
299
+ return true
300
+ rescue Exception => e
301
+ return false
302
+ end
303
+ end
304
+
305
+ def print_intro
306
+ puts "\nThis are the settings you wanted:"
307
+
308
+ puts " JSON file: #{@config.json_file.green}"
309
+ puts " Java package: #{@config.package.green}"
310
+ puts " Java top level class: #{@config.top_level_class.green}"
311
+ puts " Output directory: #{@config.output_directory.green}"
312
+
313
+ puts "\n"
314
+
315
+ end
316
+
298
317
  ##
299
318
  ## DO THE ACTUAL CONVERTING
300
319
  ##
@@ -310,19 +329,23 @@ def convert(path, options)
310
329
 
311
330
  parse_args(path, options)
312
331
 
313
- puts "Using JSON file: #{@config.json_file.green}"
314
- puts "Using Java package: #{@config.package.green}"
315
- puts "Using Java top level class: #{@config.top_level_class.green}"
316
- puts "Using output directory: #{@config.output_directory.green}"
317
-
318
- @package = @config.package
319
-
320
332
  if !File.exists?(@config.json_file)
321
- puts "JSON file: #{@config.json_file} does not exist!"
322
- puts "Try -h flag for help"
333
+ puts "The file '#{@config.json_file.red}' does not exist!"
323
334
  exit -1
335
+ else
336
+ if !valid_json?(@config.json_file)
337
+ puts "'#{@config.json_file.red}' is not a valid json file!"
338
+ exit -1
339
+ end
324
340
  end
325
341
 
342
+
343
+ print_intro()
344
+
345
+
346
+ @package = @config.package
347
+
348
+
326
349
  json_file_contents = IO.read(@config.json_file)
327
350
  json = JSON.parse(json_file_contents)
328
351
 
data/lib/j2j/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module J2j
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: j2j
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - cesar ferreira
@@ -87,7 +87,7 @@ dependencies:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
89
  version: '3.1'
90
- type: :runtime
90
+ type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
@@ -136,7 +136,22 @@ dependencies:
136
136
  - - "~>"
137
137
  - !ruby/object:Gem::Version
138
138
  version: 1.8.3
139
- description: Convert any file.json to Classe.java files
139
+ - !ruby/object:Gem::Dependency
140
+ name: activesupport
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '4.2'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '4.2'
153
+ description: |2
154
+ Convert any Files.json to corresponding Classe.java files
140
155
  email:
141
156
  - cesar.manuel.ferreira@gmail.com
142
157
  executables:
@@ -166,8 +181,11 @@ files:
166
181
  homepage: https://github.com/cesarferreira/j2j
167
182
  licenses:
168
183
  - MIT
169
- metadata: {}
170
- post_install_message:
184
+ metadata:
185
+ issue_tracker: https://example/issues
186
+ post_install_message: |2
187
+ Thanks for installing!
188
+ Head here for documentation: https://github.com/cesarferreira/j2j
171
189
  rdoc_options: []
172
190
  require_paths:
173
191
  - lib
@@ -186,5 +204,5 @@ rubyforge_project:
186
204
  rubygems_version: 2.4.7
187
205
  signing_key:
188
206
  specification_version: 4
189
- summary: Convert any file.json to Classe.java files
207
+ summary: Convert any Files.json to corresponding Classe.java files
190
208
  test_files: []