Class: SparkEngine::Scaffold

Inherits:
Object
  • Object
show all
Defined in:
lib/spark_engine/scaffold.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Scaffold

Returns a new instance of Scaffold.



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
# File 'lib/spark_engine/scaffold.rb', line 8

def initialize(options)
  @cwd = File.expand_path(File.dirname(options[:name]))
  @gem = underscorize(File.basename(options[:name]))
  @engine = underscorize(options[:engine] || @gem)
  @namespace = @engine
  @plugin_module = modulize @engine
  @gem_module = modulize @gem
  @gem_temp = ".#{@gem}-temp"

  FileUtils.mkdir_p @cwd

  Dir.chdir @cwd do
    puts "Creating new plugin #{@namespace}".bold

    @gemspec_path = new_gem
    @spec = Gem::Specification.load(@gemspec_path)
    @path = File.expand_path(File.dirname(@gemspec_path))

    engine_scaffold

    bootstrap_gem
    setup_package_json
    update_git
  end

  post_install
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



6
7
8
# File 'lib/spark_engine/scaffold.rb', line 6

def engine
  @engine
end

#gemObject (readonly)

Returns the value of attribute gem.



6
7
8
# File 'lib/spark_engine/scaffold.rb', line 6

def gem
  @gem
end

#gemspec_pathObject (readonly)

Returns the value of attribute gemspec_path.



6
7
8
# File 'lib/spark_engine/scaffold.rb', line 6

def gemspec_path
  @gemspec_path
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



6
7
8
# File 'lib/spark_engine/scaffold.rb', line 6

def namespace
  @namespace
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/spark_engine/scaffold.rb', line 6

def path
  @path
end

#plugin_moduleObject (readonly)

Returns the value of attribute plugin_module.



6
7
8
# File 'lib/spark_engine/scaffold.rb', line 6

def plugin_module
  @plugin_module
end

Instance Method Details

#action_log(action, path) ⇒ Object



175
176
177
# File 'lib/spark_engine/scaffold.rb', line 175

def action_log(action, path)
  puts action.rjust(12).colorize(:green).bold + "  #{path}"
end

#bootstrap_gemObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/spark_engine/scaffold.rb', line 49

def bootstrap_gem

  # Remove bin
  FileUtils.rm_rf(File.join(path, 'bin'))

  scaffold_path = File.expand_path("scaffold/**/*", File.dirname(__FILE__))

  Dir.glob(scaffold_path, File::FNM_DOTMATCH).select{|f| File.file? f}.each do |f|
    write_template f.split(/spark_engine\/scaffold\//)[1]
  end
end

#engine_copyObject

Copy parts of the engine scaffold into site directory



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/spark_engine/scaffold.rb', line 84

def engine_copy
  site_path = File.join path, 'site'
  FileUtils.mkdir_p site_path

  ## Copy Rails plugin files
  Dir.chdir "#{@gem_temp}/#{gem}/site" do
    %w(app config bin config.ru Rakefile public log).each do |item|
      target = File.join site_path, item

      FileUtils.cp_r item, target

      action_log "create", target.sub(@cwd+'/','')
    end

  end

  # Remove temp dir
  FileUtils.rm_rf @gem_temp
end

#engine_scaffoldObject

Create an Rails plugin engine for documentation site



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/spark_engine/scaffold.rb', line 62

def engine_scaffold
  FileUtils.mkdir_p(@gem_temp)
  Dir.chdir(@gem_temp) do
    response = Open3.capture3("rails plugin new #{gem} --mountable --dummy-path=site --skip-test-unit")
    if !response[1].empty?
      puts response[1]
      abort "FAILED: Please be sure you have the rails gem installed with `gem install rails`"
    end

    # Remove files and directories that are unnecessary for the
    # light-weight Rails documentation site
    remove = %w(mailers models assets channels jobs views).map{ |f| File.join('app', f) }
    remove.concat %w(cable.yml storage.yml database.yml).map{ |f| File.join('config', f) }

    remove.each { |f| FileUtils.rm_rf File.join(@gem, 'site', f), secure: true }
  end
  

  engine_copy
end

#modulize(input) ⇒ Object



179
180
181
182
183
# File 'lib/spark_engine/scaffold.rb', line 179

def modulize(input)
  input.split('_').collect { |name|
    (name =~ /[A-Z]/) ? name : name.capitalize
  }.join
end

#new_gemObject

Create a new gem with Bundle’s gem command



38
39
40
41
# File 'lib/spark_engine/scaffold.rb', line 38

def new_gem
  system "bundler gem #{gem}"
  Dir.glob(File.join(gem, "/*.gemspec")).first
end

#post_installObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/spark_engine/scaffold.rb', line 153

def post_install
  require 'pathname'

  target = Pathname.new File.join(@cwd, @gem) 
  dir = target.relative_path_from Pathname.new(Dir.pwd)
  victory = "#{@plugin_module} Design System created at #{dir}. Huzzah!"
  dashes = ''
  victory.size.times{ dashes += '-' }

  puts "\n#{victory}\n#{dashes}".bold

  puts "Install dependencies:"
  puts "  - cd #{dir}"
  puts "  - bundle"
  puts "  - yarn install (or npm install)\n\n"
  puts "Then give it a spin.\n\n"
  puts "  spark build".bold + "  - builds assets"
  puts "  spark server".bold + " - view documentation site in a server"
  puts "  spark help".bold + "   - learn more…"
  puts dashes + "\n\n"
end

#read_template(file_path) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/spark_engine/scaffold.rb', line 127

def read_template(file_path)
  contents = ''
  File.open file_path do |f|
    contents = ERB.new(f.read).result(binding)
  end
  contents
end

#setup_package_jsonObject



43
44
45
46
47
# File 'lib/spark_engine/scaffold.rb', line 43

def setup_package_json
  Dir.chdir path do
    NPM.setup
  end
end

#underscorize(input) ⇒ Object



185
186
187
188
189
# File 'lib/spark_engine/scaffold.rb', line 185

def underscorize(input)
  input.gsub(/[A-Z]/) do |char|
    '_'+char
  end.sub(/^_/,'').downcase
end

#update_gitObject



104
105
106
107
108
109
# File 'lib/spark_engine/scaffold.rb', line 104

def update_git
  Dir.chdir gem do
    system "git reset"
    system "git add -A"
  end
end

#write_file(paths, content = '', mode = 'w') ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/spark_engine/scaffold.rb', line 135

def write_file(paths, content='', mode='w')
  paths = [paths].flatten
  paths.each do |path|
    if File.exist?(path)
      type = 'update'
    else
      FileUtils.mkdir_p(File.dirname(path))
      type = 'create'
    end

    File.open path, mode do |io|
      io.write(content)
    end

    action_log(type, path)
  end
end

#write_template(template, target = nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/spark_engine/scaffold.rb', line 111

def write_template(template, target=nil)
  template_path = File.expand_path("scaffold/#{template}", File.dirname(__FILE__))

  # Extract file extension
  ext = File.extname(template)

  # Replace keywords with correct names (excluding file extensions)
  target_path = template.sub(/#{ext}$/, '').gsub(/(gem|engine|namespace)/, { 
    'gem' => @gem, 
    'engine' => @engine,
    'namespace' => @namespace
  }) + ext

  write_file target_path, read_template(template_path)
end