Module: Dynamics

Defined in:
lib/dynamics.rb

Class Method Summary collapse

Class Method Details

.create_scaffold(name, path) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/dynamics.rb', line 4

def self.create_scaffold(name, path)
  if File.exists?(name)    
    puts "Error, #{name} already exists!"
  else  
    Dir.mkdir(name)

    # Base

    app_dir = File.join(name, 'app')    
    build_dir = File.join(name, 'build')      
    resources_dir = File.join(name, 'resources')     
    lib_dir = File.join(name, 'lib')   
    dynamics_dir = File.join(lib_dir, 'dynamics') 
    
    Dir.mkdir(app_dir) 
    Dir.mkdir(build_dir)       
    Dir.mkdir(resources_dir)      
    Dir.mkdir(lib_dir)    
    Dir.mkdir(dynamics_dir)  
    
    base_dir = File.join(path, '..', 'base')

    f = File.new(File.join(name, 'Rakefile'), 'w+')   
    code = render_code(File.join(base_dir, 'Rakefile'))
    code.gsub!("app.name = ''", "app.name = '#{name}'")
    code.gsub!("app.delegate_class = ''", "app.delegate_class = '#{name}'")    
    f.write(code) 
    f.close  

    # App

    f = File.new(File.join(app_dir, 'application.rb'), 'w+')   
    code = render_code(File.join(base_dir, 'app', 'application.rb')) 
    code.gsub!("class Application", "class #{name}")    
    f.write(code) 
    f.close

    # Controllers 

    controllers_dir = File.join(app_dir, 'controllers')    
    Dir.mkdir(controllers_dir)    
    
    base_controllers_dir = File.join(base_dir, 'app', 'controllers')      
    Dir.foreach(base_controllers_dir) do |controller|
      if controller.include?('.rb')
        f = File.new(File.join(controllers_dir, controller), 'w+')   
        f.write(render_code(File.join(base_controllers_dir, controller))) 
        f.close          
      end
    end

    # Forms

    forms_dir = File.join(app_dir, 'forms')  
    Dir.mkdir(forms_dir)
    
    base_forms_dir = File.join(base_dir, 'app', 'forms')
    Dir.foreach(base_forms_dir) do |form|
      if form.include?('.rb')
        f = File.new(File.join(forms_dir, form), 'w+')   
        f.write(render_code(File.join(base_forms_dir, form))) 
        f.close          
      end
    end
    
    # Views

    views_dir = File.join(app_dir, 'views')  
    Dir.mkdir(views_dir)
    
    base_views_dir = File.join(base_dir, 'app', 'views')
    Dir.foreach(base_views_dir) do |view|
      if view.include?('.rb')
        f = File.new(File.join(views_dir, view), 'w+')   
        f.write(render_code(File.join(base_views_dir, view))) 
        f.close          
      end
    end
    
    # Resources
    
    base_resources_dir = File.join(base_dir, 'resources')
    Dir.foreach(base_resources_dir) do |resource|
      if resource.include?('.png')
        f = File.new(File.join(resources_dir, resource), 'w+')   
        f.write(render_code(File.join(base_resources_dir, resource))) 
        f.close        
      end
    end            
  end
end

.setup_framework(app, path) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/dynamics.rb', line 96

def self.setup_framework(app, path)    
  offset = app.files.find_index("./app/application.rb")
      
  lib_dir = File.join(path, 'lib', 'dynamics')    
  templates_dir = File.join(Gem.bin_path('dynamics', 'dynamics').gsub(File.join('bin', 'dynamics'), ''), 'base', 'templates') 
  Dir.foreach(templates_dir) do |template|
    if template.include?('.rb')
      lib_code = render_code(File.join(templates_dir, template))
      if template == 'application.rb'
        template_code = lib_code
        template_code.scan(/# @@.+?@@.+?# @@End@@/m) do |block|
          block.scan(/^# @@.+?@@/) do |placeholder|
            layout = placeholder.gsub('# @@', '').gsub('@', '')
            case layout
               when 'Navigation' then lib_code = lib_code.gsub(block, navigation_code(path))
               when 'Tab Bar' then lib_code = lib_code.gsub(block, tab_bar_code(path))        
               when 'Tab Nav' then lib_code = lib_code.gsub(block, tab_nav_code(path))                       
            end      
          end
        end
      end
      f = File.open(File.join(lib_dir, template), 'w+')   
      f.write(lib_code) 
      f.close
      app.files.insert(offset, File.join(lib_dir, template))
      if template.include?('cell_')     
        app.files_dependencies(File.join(lib_dir, template) => File.join(lib_dir, 'cell.rb'))              
      end   
    end
  end
end