codgen 0.0.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 +7 -0
- data/.gitignore +14 -0
- data/.idea/.name +1 -0
- data/.idea/.rakeTasks +7 -0
- data/.idea/codgen.iml +142 -0
- data/.idea/encodings.xml +5 -0
- data/.idea/idea/.name +1 -0
- data/.idea/idea/codgen.iml +140 -0
- data/.idea/idea/encodings.xml +5 -0
- data/.idea/idea/misc.xml +5 -0
- data/.idea/idea/modules.xml +9 -0
- data/.idea/idea/scopes/scope_settings.xml +5 -0
- data/.idea/idea/vcs.xml +7 -0
- data/.idea/idea/workspace.xml +830 -0
- data/.idea/inspectionProfiles/Project_Default.xml +7 -0
- data/.idea/inspectionProfiles/profiles_settings.xml +7 -0
- data/.idea/misc.xml +5 -0
- data/.idea/modules.xml +9 -0
- data/.idea/scopes/scope_settings.xml +5 -0
- data/.idea/vcs.xml +7 -0
- data/.idea/workspace.xml +1213 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +32 -0
- data/Rakefile +2 -0
- data/bin/codgen.rb +50 -0
- data/codgen.gemspec +27 -0
- data/lib/codgen/auto_style.rb +111 -0
- data/lib/codgen/constants.rb +5 -0
- data/lib/codgen/debug_helper.rb +15 -0
- data/lib/codgen/flattener.rb +32 -0
- data/lib/codgen/logger.rb +8 -0
- data/lib/codgen/mapping.rb +40 -0
- data/lib/codgen/statement.rb +163 -0
- data/lib/codgen/template.rb +40 -0
- data/lib/codgen/version.rb +3 -0
- data/lib/codgen.rb +59 -0
- data/test/behavior/basic_test.rb +10 -0
- data/test/behavior/output_spec.rb +36 -0
- data/test/behavior/test_utils.rb +16 -0
- data/test/data/Input/CreateDatabase.sql +10 -0
- data/test/data/Input/DbContextTemplate.cs +18 -0
- data/test/data/Input/ModelTemplate.cs +26 -0
- data/test/data/Input/example-data.json +75 -0
- data/test/data/Input/hello_world.txt.mustache +1 -0
- data/test/data/Input/model_template.rb.ctemp +8 -0
- data/test/data/Input/test_conditionals +7 -0
- data/test/data/Input/test_escapes +6 -0
- data/test/data/Map.json +36 -0
- data/test/data/Output/CreateMyBlogDatabase.sql +17 -0
- data/test/data/Output/MsMvc/MyBlog/DAL/MyBlogContext.cs +17 -0
- data/test/data/Output/MsMvc/MyBlog/Models/Comment.cs +14 -0
- data/test/data/Output/MsMvc/MyBlog/Models/Post.cs +17 -0
- data/test/data/Output/Ruby/my_blog/Models/comment.rb +3 -0
- data/test/data/Output/Ruby/my_blog/Models/post.rb +3 -0
- data/test/data/config.json +25 -0
- data/test/data/expected_output/MsMvc/MyBlog/DAL/MyBlogContext.cs +17 -0
- data/test/data/expected_output/MsMvc/MyBlog/Models/Comment.cs +14 -0
- data/test/data/expected_output/MsMvc/MyBlog/Models/Post.cs +17 -0
- data/test/data/expected_output/Ruby/my_blog/Models/comment.rb +3 -0
- data/test/data/expected_output/Ruby/my_blog/Models/post.rb +3 -0
- data/test/data/test.mustache +5 -0
- data/test/unit/codgen_engine_spec.rb +12 -0
- data/test/unit/flattener_spec.rb +51 -0
- data/test/unit/template_spec.rb +30 -0
- metadata +222 -0
@@ -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,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,75 @@
|
|
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
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
Hello {{name}}
|
data/test/data/Map.json
ADDED
@@ -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,17 @@
|
|
1
|
+
Create Database MyBlog
|
2
|
+
|
3
|
+
Create Table Posts (
|
4
|
+
PostId UniqueIdentifier
|
5
|
+
, ##:!IsLastTemplateInstance?
|
6
|
+
PostTitle varchar(200)
|
7
|
+
, ##:!IsLastTemplateInstance?
|
8
|
+
PostText text
|
9
|
+
, ##:!IsLastTemplateInstance?
|
10
|
+
)
|
11
|
+
|
12
|
+
Create Table Comments (
|
13
|
+
CommentId UniqueIdentifier
|
14
|
+
, ##:!IsLastTemplateInstance?
|
15
|
+
CommentText text
|
16
|
+
, ##:!IsLastTemplateInstance?
|
17
|
+
)
|
@@ -0,0 +1,17 @@
|
|
1
|
+
using MyBlog.Models;
|
2
|
+
using System.Data.Entity;
|
3
|
+
using System.Data.Entity.ModelConfiguration.Conventions;
|
4
|
+
|
5
|
+
namespace MyBlog.DAL
|
6
|
+
{
|
7
|
+
public class MyBlogContext : DbContext
|
8
|
+
{
|
9
|
+
public DbSet<Blog> Posts { get; set; }
|
10
|
+
public DbSet<Blog> Comments { get; set; }
|
11
|
+
|
12
|
+
protected override void OnModelCreating(DbModelBuilder modelBuilder)
|
13
|
+
{
|
14
|
+
|
15
|
+
}
|
16
|
+
}
|
17
|
+
}
|
@@ -0,0 +1,14 @@
|
|
1
|
+
using System;
|
2
|
+
using System.Collections.Generic;
|
3
|
+
using System.ComponentModel.DataAnnotations.Schema;
|
4
|
+
|
5
|
+
|
6
|
+
namespace MyBlog.Models
|
7
|
+
{
|
8
|
+
public class Comment
|
9
|
+
{
|
10
|
+
public Guid CommentId { get; set; }
|
11
|
+
public string CommentText { get; set; }
|
12
|
+
Post Post { get; set; }
|
13
|
+
}
|
14
|
+
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
using System;
|
2
|
+
using System.Collections.Generic;
|
3
|
+
using System.ComponentModel.DataAnnotations.Schema;
|
4
|
+
|
5
|
+
|
6
|
+
namespace MyBlog.Models
|
7
|
+
{
|
8
|
+
public class Post
|
9
|
+
{
|
10
|
+
public Guid PostId { get; set; }
|
11
|
+
[Required]
|
12
|
+
[StringLength(200)]
|
13
|
+
public string PostTitle { get; set; }
|
14
|
+
public string PostText { get; set; }
|
15
|
+
ICollection<Comment> Comments { get; set; }
|
16
|
+
}
|
17
|
+
}
|
@@ -0,0 +1,25 @@
|
|
1
|
+
{
|
2
|
+
"map": "Map.json",
|
3
|
+
"templates": [
|
4
|
+
{
|
5
|
+
"in": "Input/model_template.rb.ctemp",
|
6
|
+
"source": "entities",
|
7
|
+
"out": "Output/Ruby/{{application_name}}/Models/{{entity_name}}.rb"
|
8
|
+
},
|
9
|
+
{
|
10
|
+
"in": "Input/ModelTemplate.cs",
|
11
|
+
"source": "entities",
|
12
|
+
"out": "Output/MsMvc/{{ApplicationName}}/Models/{{EntityName}}.cs"
|
13
|
+
},
|
14
|
+
{
|
15
|
+
"in": "Input/DbContextTemplate.cs",
|
16
|
+
"source": "entities",
|
17
|
+
"out": "Output/MsMvc/{{ApplicationName}}/DAL/{{ApplicationName}}Context.cs"
|
18
|
+
},
|
19
|
+
{
|
20
|
+
"in": "Input/CreateDatabase.sql",
|
21
|
+
"out": "Output/Create{{ApplicationName}}Database.sql"
|
22
|
+
}
|
23
|
+
],
|
24
|
+
"data": "Input/example-data.json"
|
25
|
+
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
using MyBlog.Models;
|
2
|
+
using System.Data.Entity;
|
3
|
+
using System.Data.Entity.ModelConfiguration.Conventions;
|
4
|
+
|
5
|
+
namespace MyBlog.DAL
|
6
|
+
{
|
7
|
+
public class MyBlogContext : DbContext
|
8
|
+
{
|
9
|
+
public DbSet<Blog> Posts { get; set; }
|
10
|
+
public DbSet<Blog> Comments { get; set; }
|
11
|
+
|
12
|
+
protected override void OnModelCreating(DbModelBuilder modelBuilder)
|
13
|
+
{
|
14
|
+
|
15
|
+
}
|
16
|
+
}
|
17
|
+
}
|
@@ -0,0 +1,14 @@
|
|
1
|
+
using System;
|
2
|
+
using System.Collections.Generic;
|
3
|
+
using System.ComponentModel.DataAnnotations.Schema;
|
4
|
+
|
5
|
+
|
6
|
+
namespace MyBlog.Models
|
7
|
+
{
|
8
|
+
public class Comment
|
9
|
+
{
|
10
|
+
public Guid CommentId { get; set; }
|
11
|
+
public string CommentText { get; set; }
|
12
|
+
Post Post { get; set; }
|
13
|
+
}
|
14
|
+
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
using System;
|
2
|
+
using System.Collections.Generic;
|
3
|
+
using System.ComponentModel.DataAnnotations.Schema;
|
4
|
+
|
5
|
+
|
6
|
+
namespace MyBlog.Models
|
7
|
+
{
|
8
|
+
public class Post
|
9
|
+
{
|
10
|
+
public Guid PostId { get; set; }
|
11
|
+
[Required]
|
12
|
+
[StringLength(200)]
|
13
|
+
public string PostTitle { get; set; }
|
14
|
+
public string PostText { get; set; }
|
15
|
+
ICollection<Comment> Comments { get; set; }
|
16
|
+
}
|
17
|
+
}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require_relative '../../lib/codgen'
|
3
|
+
|
4
|
+
describe 'codgen' do
|
5
|
+
config = {
|
6
|
+
'templates' => [{'source' => 'data', 'in' => 'Input/hello_world.txt.mustache', 'out' => 'Output/hello_world.txt' }],
|
7
|
+
'data' => { 'data' => [{'name' => 'World'}], 'foo' => 'blah' }
|
8
|
+
}
|
9
|
+
it 'should have one result' do
|
10
|
+
expect(Codgen.run(config).length).to eq 1
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require_relative '../../lib/codgen/flattener'
|
3
|
+
|
4
|
+
describe 'Flattener' do
|
5
|
+
data_root = {
|
6
|
+
'root_prop1' => 'root_prop1_val',
|
7
|
+
'prop2' => 'root_prop2_val',
|
8
|
+
'object1' => {
|
9
|
+
'prop3' => 'prop3_val',
|
10
|
+
'object2' => {
|
11
|
+
'array' =>[
|
12
|
+
{
|
13
|
+
'prop1' => 'prop1_val',
|
14
|
+
'prop2' => 'prop2_val'
|
15
|
+
}
|
16
|
+
]
|
17
|
+
}
|
18
|
+
}
|
19
|
+
}
|
20
|
+
|
21
|
+
data_root2 = { 'data' => [{'name' => 'World'}], 'foo' => 'blah'}
|
22
|
+
|
23
|
+
it 'should have prop1 = prop1_val when it flattens to object1.object2[0] given @data_root' do
|
24
|
+
expect(Codgen::Flattener.merge(data_root, ['object1', 'object2', 'array'])[0]['prop1']).to eq('prop1_val')
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should have prop2 = prop2_val when it flattens to object1.object2[0] given @data_root' do
|
28
|
+
expect(Codgen::Flattener.merge(data_root, ['object1', 'object2', 'array'])[0]['prop2']).to eq('prop2_val')
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should have root_prop1 = root_prop1_val when it flattens to object1.object2[0] @given data_root' do
|
32
|
+
expect(Codgen::Flattener.merge(data_root, ['object1', 'object2', 'array'])[0]['root_prop1']).to eq('root_prop1_val')
|
33
|
+
end
|
34
|
+
|
35
|
+
# When data_root2
|
36
|
+
it 'should have data as an array' do
|
37
|
+
expect(Codgen::Flattener.merge(data_root2, ['data']).is_a?(Array)).to eq(true)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should have a count of 1' do
|
41
|
+
expect(Codgen::Flattener.merge(data_root2, ['data']).length).to eq(1)
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should get the main data properly' do
|
45
|
+
expect(Codgen::Flattener.merge(data_root2, ['data'])[0]['name']).to eq('World')
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should get the parent data properly' do
|
49
|
+
expect(Codgen::Flattener.merge(data_root2, ['data'])[0]['foo']).to eq('blah')
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require_relative '../../lib/codgen/template'
|
3
|
+
|
4
|
+
describe 'template' do
|
5
|
+
config = {'source' => 'data', 'in' => 'Input/hello_world.txt.mustache', 'out' => 'Output/hello_world.txt' }
|
6
|
+
data_root = { 'data' => [{'name' => 'World'}], 'foo' => 'blah' }
|
7
|
+
template_text = 'Hello {{name}}'
|
8
|
+
template = Codgen::Template.new(config, data_root)
|
9
|
+
|
10
|
+
describe 'initialize' do
|
11
|
+
it 'should get the input template file content properly' do
|
12
|
+
expect(template.template).to eq('Hello {{name}}')
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should have data that is a flattened form of data_root.data' do
|
16
|
+
expect(template.data).to eq(Codgen::Flattener.merge(data_root, ['data']))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
describe 'fill_template' do
|
20
|
+
it 'should return a hash' do
|
21
|
+
expect(template.fill_template.is_a?(Hash)).to eq(true)
|
22
|
+
end
|
23
|
+
it 'should return a output file with location "Output/hello_world.txt"' do
|
24
|
+
expect(template.fill_template.keys[0]).to eq('Output/hello_world.txt')
|
25
|
+
end
|
26
|
+
it 'should return a output file with location "Output/hello_world.txt"' do
|
27
|
+
expect(template.fill_template['Output/hello_world.txt']).to eq('Hello World')
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|