codgen 0.0.6 → 0.0.7
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 +4 -4
- data/.idea/codgen.iml +10 -38
- data/.idea/workspace.xml +276 -254
- data/README.md +7 -2
- data/Rakefile +8 -3
- data/codgen.gemspec +1 -1
- data/lib/codgen.rb +7 -1
- data/lib/codgen/statement.rb +15 -34
- data/lib/codgen/version.rb +1 -1
- data/test/behavior/output_spec.rb +33 -2
- data/test/data/Input-Web/CreateDatabase.sql.mustache +10 -0
- data/test/data/Input-Web/DbContextTemplate.cs.mustache +18 -0
- data/test/data/Input-Web/Map.json +36 -0
- data/test/data/Input-Web/ModelTemplate.cs.mustache +26 -0
- data/test/data/Input-Web/config.json +44 -0
- data/test/data/Input-Web/example-data.json +76 -0
- data/test/data/Input-Web/explicit_names.txt +21 -0
- data/test/data/Input-Web/hello_world.txt.mustache +1 -0
- data/test/data/Input-Web/i_can_ride_my_bike_with_no.handlebars +28 -0
- data/test/data/Input-Web/model_template.rb.mustache +8 -0
- data/test/data/Input-Web/package_test_dir/config.json +8 -0
- data/test/data/Input-Web/package_test_dir/templates/package_hello_world.txt.mustache +1 -0
- data/test/data/Input-Web/package_test_zip.zip +0 -0
- data/test/data/Input-Web/package_test_zip/config.json +8 -0
- data/test/data/Input-Web/package_test_zip/templates/package_zip_hello_world.txt.mustache +1 -0
- data/test/data/Input-Web/static_text.txt +1 -0
- data/test/data/Input/i_can_ride_my_bike_with_no.handlebars +2 -1
- data/test/data/Output-Web/CreateMyBlogDatabase.sql +17 -0
- data/test/data/Output-Web/MsMvc/MyBlog/DAL/MyBlogContext.cs +17 -0
- data/test/data/Output-Web/MsMvc/MyBlog/Models/Comment.cs +14 -0
- data/test/data/Output-Web/MsMvc/MyBlog/Models/Post.cs +17 -0
- data/test/data/Output-Web/Ruby/my_blog/Models/comment.rb +3 -0
- data/test/data/Output-Web/Ruby/my_blog/Models/post.rb +3 -0
- data/test/data/Output-Web/explicit_names.txt +21 -0
- data/test/data/Output-Web/i_can_ride_my_bike_with_no +32 -0
- data/test/data/Output-Web/package_hello_world.txt +1 -0
- data/test/data/Output-Web/package_zip_hello_world.txt +1 -0
- data/test/data/Output-Web/static_text.txt +1 -0
- data/test/data/expected_output/i_can_ride_my_bike_with_no +2 -1
- metadata +60 -6
data/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Codgen
|
|
1
|
+
# Codgen [](http://badge.fury.io/rb/codgen) [](https://travis-ci.org/beattyml1/codgen) [](https://codeclimate.com/github/beattyml1/codgen)
|
|
2
2
|
|
|
3
3
|
Codgen is a cross language, template based, code generator, capable of generating multiple applications from a common model.
|
|
4
4
|
|
|
@@ -7,7 +7,7 @@ It uses JSON for it's model and config and you can use mustache or handlebars te
|
|
|
7
7
|
If you find bugs or bad error messages be sure to log an issue. If it's not to big I'll probably get to pretty quickly. Pull requests are also most welcome!
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
|
|
11
11
|
|
|
12
12
|
## Installation
|
|
13
13
|
|
|
@@ -15,7 +15,12 @@ If you find bugs or bad error messages be sure to log an issue. If it's not to b
|
|
|
15
15
|
|
|
16
16
|
## Usage
|
|
17
17
|
|
|
18
|
+
$ codgen.rb [input_directory_path|config_path.json] [output_directory_path]
|
|
19
|
+
|
|
20
|
+
Examples
|
|
21
|
+
|
|
18
22
|
$ codgen.rb codgen_config.json
|
|
23
|
+
$ codgen.rb input_dir output_dir
|
|
19
24
|
|
|
20
25
|
For more info see the wiki page
|
|
21
26
|
|
data/Rakefile
CHANGED
|
@@ -3,20 +3,25 @@ require "bundler/gem_tasks"
|
|
|
3
3
|
#Dir['test/unit/*.rb'].each {|file| require_relative file }
|
|
4
4
|
require 'rspec/core/rake_task'
|
|
5
5
|
|
|
6
|
-
|
|
7
6
|
task :unit_tests do
|
|
8
7
|
Dir.chdir('test/data/Input')
|
|
9
8
|
puts ''
|
|
9
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
|
10
|
+
t.pattern = '../../unit/*_spec.rb'
|
|
11
|
+
end
|
|
10
12
|
puts 'Running unit tests'
|
|
11
|
-
|
|
13
|
+
Rake::Task["spec"].execute
|
|
12
14
|
puts ''
|
|
13
15
|
puts ''
|
|
14
16
|
Dir.chdir('../../..')
|
|
15
17
|
end
|
|
16
18
|
|
|
17
19
|
task :behavior_tests do
|
|
20
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
|
21
|
+
t.pattern = 'test/behavior/output_spec.rb'
|
|
22
|
+
end
|
|
18
23
|
puts 'Running behavior tests'
|
|
19
|
-
|
|
24
|
+
Rake::Task["spec"].execute
|
|
20
25
|
end
|
|
21
26
|
|
|
22
27
|
task :default => [:unit_tests, :behavior_tests] do
|
data/codgen.gemspec
CHANGED
data/lib/codgen.rb
CHANGED
|
@@ -4,6 +4,7 @@ require_relative 'codgen/mapping'
|
|
|
4
4
|
require_relative 'codgen/logger'
|
|
5
5
|
require_relative 'codgen/template'
|
|
6
6
|
require_relative 'codgen/package'
|
|
7
|
+
require 'net/http'
|
|
7
8
|
|
|
8
9
|
module Codgen
|
|
9
10
|
def self.run(json_config)
|
|
@@ -39,7 +40,12 @@ module Codgen
|
|
|
39
40
|
def self.get_data_if_not_data(filepath_or_data)
|
|
40
41
|
if filepath_or_data != nil
|
|
41
42
|
if filepath_or_data.is_a?(String)
|
|
42
|
-
|
|
43
|
+
if /https?:\/\//.match(filepath_or_data)
|
|
44
|
+
resp = Net::HTTP.get_response(URI.parse(filepath_or_data))
|
|
45
|
+
file = resp.body
|
|
46
|
+
else
|
|
47
|
+
file = File.read(filepath_or_data)
|
|
48
|
+
end
|
|
43
49
|
JSON.parse(file)
|
|
44
50
|
elsif filepath_or_data.is_a?(Hash)
|
|
45
51
|
filepath_or_data
|
data/lib/codgen/statement.rb
CHANGED
|
@@ -99,37 +99,14 @@ module Codgen
|
|
|
99
99
|
end
|
|
100
100
|
|
|
101
101
|
def expand_and
|
|
102
|
-
|
|
103
|
-
index = -1
|
|
104
|
-
skip = false
|
|
105
|
-
@words.each do |word|
|
|
106
|
-
index+=1
|
|
107
|
-
if skip
|
|
108
|
-
skip = false
|
|
109
|
-
next
|
|
110
|
-
end
|
|
111
|
-
if word.is_a?(Statement)
|
|
112
|
-
new_words.push(word)
|
|
113
|
-
elsif word.index(/and|&/)
|
|
114
|
-
if new_words.count > 0 && @words.count > index + 1
|
|
115
|
-
left = new_words[-1]
|
|
116
|
-
new_words.delete_at(-1)
|
|
117
|
-
new_words.push(Statement.new(:and))
|
|
118
|
-
new_words[-1].words.push(left)
|
|
119
|
-
new_words[-1].words.push(@words[index+1])
|
|
120
|
-
skip = true
|
|
121
|
-
next
|
|
122
|
-
else
|
|
123
|
-
throw 'Operator must have a left and a right side'
|
|
124
|
-
end
|
|
125
|
-
else
|
|
126
|
-
new_words.push(word)
|
|
127
|
-
end
|
|
128
|
-
end
|
|
129
|
-
@words = new_words
|
|
102
|
+
expand_two_sided(:and, /and|&/)
|
|
130
103
|
end
|
|
131
104
|
|
|
132
105
|
def expand_or
|
|
106
|
+
expand_two_sided(:or, /or|\|/)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def expand_two_sided(operator, regex)
|
|
133
110
|
new_words = Array.new
|
|
134
111
|
index = -1
|
|
135
112
|
skip = false
|
|
@@ -141,13 +118,9 @@ module Codgen
|
|
|
141
118
|
end
|
|
142
119
|
if word.is_a?(Statement)
|
|
143
120
|
new_words.push(word)
|
|
144
|
-
elsif word.index(
|
|
121
|
+
elsif word.index(regex)
|
|
145
122
|
if new_words.count > 0 && @words.count > index + 1
|
|
146
|
-
|
|
147
|
-
new_words.delete_at(-1)
|
|
148
|
-
new_words.push(Statement.new(:or))
|
|
149
|
-
new_words[-1].words.push(left)
|
|
150
|
-
new_words[-1].words.push(@words[index+1])
|
|
123
|
+
replace_with_statement(new_words, operator)
|
|
151
124
|
skip = true
|
|
152
125
|
next
|
|
153
126
|
else
|
|
@@ -159,5 +132,13 @@ module Codgen
|
|
|
159
132
|
end
|
|
160
133
|
@words = new_words
|
|
161
134
|
end
|
|
135
|
+
|
|
136
|
+
def replace_with_statement(new_words, operator)
|
|
137
|
+
left = new_words[-1]
|
|
138
|
+
new_words.delete_at(-1)
|
|
139
|
+
new_words.push(Statement.new(operator))
|
|
140
|
+
new_words[-1].words.push(left)
|
|
141
|
+
new_words[-1].words.push(@words[index+1])
|
|
142
|
+
end
|
|
162
143
|
end
|
|
163
144
|
end
|
data/lib/codgen/version.rb
CHANGED
|
@@ -2,9 +2,9 @@ require 'rspec'
|
|
|
2
2
|
require 'fileutils'
|
|
3
3
|
require_relative 'test_utils'
|
|
4
4
|
|
|
5
|
+
$expectation_directory = 'test/data/expected_output'
|
|
6
|
+
$output_directory = 'test/data/Output'
|
|
5
7
|
describe 'Application' do
|
|
6
|
-
$expectation_directory = 'test/data/expected_output'
|
|
7
|
-
$output_directory = 'test/data/Output'
|
|
8
8
|
before :all do
|
|
9
9
|
|
|
10
10
|
FileUtils.rm_rf($output_directory)
|
|
@@ -27,6 +27,37 @@ describe 'Application' do
|
|
|
27
27
|
files_and_folders.each do |expected_file_path|
|
|
28
28
|
if File.file?(expected_file_path)
|
|
29
29
|
output_file_path = expected_file_path.sub($expectation_directory, $output_directory)
|
|
30
|
+
it "should produce file: '#{output_file_path}' that is the same as '#{expected_file_path}'" do
|
|
31
|
+
expect(File.exist?(output_file_path)).to be_truthy
|
|
32
|
+
expect(File.read(output_file_path)).to eq File.read(expected_file_path)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
describe 'Application in web context' do
|
|
39
|
+
before :all do
|
|
40
|
+
|
|
41
|
+
FileUtils.rm_rf($output_directory)
|
|
42
|
+
|
|
43
|
+
puts `bin/codgen.rb test/data/Input-Web/ test/data/Output-Web`
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it 'should produce all expected files' do
|
|
47
|
+
files_and_folders = Dir.glob($expectation_directory+'/**/*')
|
|
48
|
+
files_and_folders.each do |expected_file_path|
|
|
49
|
+
if File.file?(expected_file_path)
|
|
50
|
+
output_file_path = expected_file_path.sub($expectation_directory, $output_directory+'-Web')
|
|
51
|
+
expect(File.exist?(output_file_path)).to be_truthy
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
files_and_folders = Dir.glob($expectation_directory+'/**/*')
|
|
58
|
+
files_and_folders.each do |expected_file_path|
|
|
59
|
+
if File.file?(expected_file_path)
|
|
60
|
+
output_file_path = expected_file_path.sub($expectation_directory, $output_directory+'-Web')
|
|
30
61
|
it "should produce file: '#{output_file_path}' that is the same as '#{expected_file_path}'" do
|
|
31
62
|
expect(File.exist?(output_file_path)).to be_truthy
|
|
32
63
|
expect(FileUtils.cmp(output_file_path, expected_file_path)).to be_truthy
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
using {{ApplicationName}}.Models;
|
|
2
|
+
using System.Data.Entity;
|
|
3
|
+
using System.Data.Entity.ModelConfiguration.Conventions;
|
|
4
|
+
|
|
5
|
+
namespace {{ApplicationName}}.DAL
|
|
6
|
+
{
|
|
7
|
+
public class {{ApplicationName}}Context : DbContext
|
|
8
|
+
{
|
|
9
|
+
{{#entities}}
|
|
10
|
+
public DbSet<Blog> {{EntityNames}} { get; set; }
|
|
11
|
+
{{/entities}}
|
|
12
|
+
|
|
13
|
+
protected override void OnModelCreating(DbModelBuilder modelBuilder)
|
|
14
|
+
{
|
|
15
|
+
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"caseTo": "lower",
|
|
3
|
+
"type": {
|
|
4
|
+
"cstype": {
|
|
5
|
+
"(?i)bigint": "long",
|
|
6
|
+
"(?i)int": "int",
|
|
7
|
+
"(?i)varchar\\([0-9]*\\)": "string",
|
|
8
|
+
"(?i)nvarchar(\\([0-9]*\\))": "string",
|
|
9
|
+
"(?i)text": "string",
|
|
10
|
+
"(?i)smallint": "short",
|
|
11
|
+
"(?i)uniqueidentifier": "Guid",
|
|
12
|
+
"(?i)DateTime": "DateTime",
|
|
13
|
+
"(?i)Date": "DateTime"
|
|
14
|
+
},
|
|
15
|
+
"rbtype": {
|
|
16
|
+
"(?i)bigint": "Integer",
|
|
17
|
+
"(?i)int": "Integer",
|
|
18
|
+
"(?i)varchar\\([0-9]*\\)": "String",
|
|
19
|
+
"(?i)nvarchar(\\([0-9]*\\))": "String",
|
|
20
|
+
"(?i)text": "String",
|
|
21
|
+
"(?i)smallint": "Integer",
|
|
22
|
+
"(?i)uniqueidentifier": "String",
|
|
23
|
+
"(?i)DateTime": "DateTime",
|
|
24
|
+
"(?i)Date": "Date"
|
|
25
|
+
},
|
|
26
|
+
"jstype": {
|
|
27
|
+
"(?i)bigint": "Number",
|
|
28
|
+
"(?i)int": "Number",
|
|
29
|
+
"(?i)varchar\\([0-9]*\\)": "String",
|
|
30
|
+
"(?i)nvarchar(\\([0-9]*\\))": "String",
|
|
31
|
+
"(?i)text": "String",
|
|
32
|
+
"(?i)smallint": "Number",
|
|
33
|
+
"(?i)uniqueidentifier": "String"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
using System;
|
|
2
|
+
using System.Collections.Generic;
|
|
3
|
+
using System.ComponentModel.DataAnnotations.Schema;
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
namespace {{ApplicationName}}.Models
|
|
7
|
+
{
|
|
8
|
+
public class {{EntityName}}
|
|
9
|
+
{
|
|
10
|
+
{{#fields}}
|
|
11
|
+
{{#Required}}
|
|
12
|
+
[Required]
|
|
13
|
+
{{/Required}}
|
|
14
|
+
{{#hasMaxLength}}
|
|
15
|
+
[StringLength({{maxLength}})]
|
|
16
|
+
{{/hasMaxLength}}
|
|
17
|
+
public {{cstype}} {{FieldName}} { get; set; }
|
|
18
|
+
{{/fields}}
|
|
19
|
+
{{#hasmany}}
|
|
20
|
+
ICollection<{{EntityName}}> {{EntityNames}} { get; set; }
|
|
21
|
+
{{/hasmany}}
|
|
22
|
+
{{#belongsto}}
|
|
23
|
+
{{EntityName}} {{EntityName}} { get; set; }
|
|
24
|
+
{{/belongsto}}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"map": "Map.json",
|
|
3
|
+
"templates": [
|
|
4
|
+
{
|
|
5
|
+
"in": "model_template.rb.mustache",
|
|
6
|
+
"source": "entities",
|
|
7
|
+
"out": "Ruby/{{application_name}}/Models/{{entity_name}}.rb"
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
"in": "ModelTemplate.cs.mustache",
|
|
11
|
+
"source": "entities",
|
|
12
|
+
"out": "MsMvc/{{ApplicationName}}/Models/{{EntityName}}.cs"
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"in": "DbContextTemplate.cs.mustache",
|
|
16
|
+
"source": "entities",
|
|
17
|
+
"out": "MsMvc/{{ApplicationName}}/DAL/{{ApplicationName}}Context.cs"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"in": "CreateDatabase.sql.mustache",
|
|
21
|
+
"out": "Create{{ApplicationName}}Database.sql"
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
"in": "explicit_names.txt",
|
|
25
|
+
"out": "explicit_names.txt",
|
|
26
|
+
"template engine": "mustache"
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
"in": "static_text.txt",
|
|
30
|
+
"out": "static_text.txt",
|
|
31
|
+
"template engine": "static"
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
"in": "i_can_ride_my_bike_with_no.handlebars",
|
|
35
|
+
"out": "i_can_ride_my_bike_with_no"
|
|
36
|
+
}
|
|
37
|
+
],
|
|
38
|
+
|
|
39
|
+
"packages": [
|
|
40
|
+
"package_test_dir",
|
|
41
|
+
"package_test_zip.zip"
|
|
42
|
+
],
|
|
43
|
+
"data": "https://raw.githubusercontent.com/beattyml1/codgen/master/test/data/Input/example-data.json"
|
|
44
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
{
|
|
2
|
+
"@application name": "my blog",
|
|
3
|
+
"test_escapes": [
|
|
4
|
+
{
|
|
5
|
+
"codgenVar": "The Rain in Spain stays mainly in the plains"
|
|
6
|
+
}
|
|
7
|
+
],
|
|
8
|
+
"entities": [
|
|
9
|
+
{
|
|
10
|
+
"@entity name": "post",
|
|
11
|
+
"fields": [
|
|
12
|
+
{
|
|
13
|
+
"@field name": "post id",
|
|
14
|
+
"type": "UniqueIdentifier",
|
|
15
|
+
"read only": true
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"@field name": "post title",
|
|
19
|
+
"type": "varchar(200)",
|
|
20
|
+
"$required": true,
|
|
21
|
+
"hasMaxLength": true,
|
|
22
|
+
"maxLength": "200"
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
"@field name": "post text",
|
|
26
|
+
"type": "text"
|
|
27
|
+
}
|
|
28
|
+
],
|
|
29
|
+
"hasmany": [
|
|
30
|
+
{
|
|
31
|
+
"@entity name": "comment"
|
|
32
|
+
}
|
|
33
|
+
]
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
"@entity name": "comment",
|
|
37
|
+
"fields": [
|
|
38
|
+
{
|
|
39
|
+
"@field name": "comment id",
|
|
40
|
+
"type": "UniqueIdentifier",
|
|
41
|
+
"read only": true
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
"@field name": "comment text",
|
|
45
|
+
"type": "text"
|
|
46
|
+
}
|
|
47
|
+
],
|
|
48
|
+
"belongsto": [
|
|
49
|
+
{
|
|
50
|
+
"@entity name": "post"
|
|
51
|
+
}
|
|
52
|
+
]
|
|
53
|
+
}
|
|
54
|
+
],
|
|
55
|
+
"test_conditionals": [
|
|
56
|
+
{
|
|
57
|
+
"name": "if",
|
|
58
|
+
"a": true,
|
|
59
|
+
"b": true,
|
|
60
|
+
"c": true
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
"name": "elseif",
|
|
64
|
+
"a": true,
|
|
65
|
+
"b": false,
|
|
66
|
+
"c": false
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
"name": "else",
|
|
70
|
+
"a": false,
|
|
71
|
+
"b": false,
|
|
72
|
+
"c": false
|
|
73
|
+
}
|
|
74
|
+
],
|
|
75
|
+
"@name" : "World"
|
|
76
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{{ApplicationName}}
|
|
2
|
+
{{ApplicationNames}}
|
|
3
|
+
{{ApplicationName?CapCamel}}
|
|
4
|
+
{{ApplicationNames?CapCamel}}
|
|
5
|
+
{{ApplicationName?CapCamel?singular}}
|
|
6
|
+
{{ApplicationNames?CapCamel?plural}}
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
{{application_name}}
|
|
10
|
+
{{application_names}}
|
|
11
|
+
{{application_name?underscored}}
|
|
12
|
+
{{application_names?underscored}}
|
|
13
|
+
{{application_name?underscored?singular}}
|
|
14
|
+
{{application_names?underscored?plural}}
|
|
15
|
+
|
|
16
|
+
{{applicationName}}
|
|
17
|
+
{{applicationNames}}
|
|
18
|
+
{{applicationName?camelCase}}
|
|
19
|
+
{{applicationNames?camelCase}}
|
|
20
|
+
{{applicationName?camelCase?singular}}
|
|
21
|
+
{{applicationNames?camelCase?plural}}
|