power-build 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,217 @@
1
+ require 'colorize'
2
+ require 'erb'
3
+ require 'ostruct'
4
+ require 'json'
5
+
6
+ module PowerBuild
7
+ class Constructor
8
+
9
+ def initialize(config=nil)
10
+ @assets_base = "../../assets"
11
+ variables_set if config
12
+ end
13
+
14
+ def build_config
15
+ if File.file? "power-build.config"
16
+ puts "Config file already exists. Either:"
17
+ puts "1. Run 'power build' to build."
18
+ puts "2. Run 'power delete' to delete all."
19
+ else
20
+ @assets_base = "../../assets"
21
+ config = "power-build.config"
22
+ FileUtils.cp File.expand_path("#{@assets_base}/power-build.config", __FILE__), config
23
+ puts "Created: ".green + "power-build.config"
24
+ end
25
+ end
26
+
27
+ def copy_assets
28
+ if File.directory? "assets"
29
+ copy_assets_in_order
30
+ else
31
+ ["css", "js", "fonts"].each do |dir|
32
+ copy_assets_dir dir
33
+ puts "Created: ".green + "assets/#{dir}"
34
+ end
35
+ end
36
+ end
37
+
38
+ def generate_site
39
+ image_collection = collect_images
40
+ @variables.image_collection = image_collection
41
+
42
+ @variables.resource_prefix = "../../"
43
+ update_partials
44
+ image_collection.each do |category|
45
+ create_page_by(category)
46
+ category[:images].each do |image|
47
+ create_page_by_each(category, image)
48
+ end
49
+ end
50
+
51
+ @variables.title = @config["title"]
52
+ @variables.resource_prefix = ""
53
+ update_partials
54
+ render_page("index")
55
+ puts "Created: ".green + "index.html"
56
+ end
57
+
58
+ def remove_config
59
+ if File.file? "power-build.config"
60
+ FileUtils.remove "power-build.config"
61
+ puts "Removed:".red + " power-build.config"
62
+ else
63
+ puts "Config file does not exist."
64
+ end
65
+ if File.directory? "assets"
66
+ FileUtils.rm_rf "assets"
67
+ puts "Removed:".red + " assets"
68
+ end
69
+ if File.file? "index.html"
70
+ FileUtils.remove "index.html"
71
+ puts "Removed:".red + " index.html"
72
+ end
73
+ end
74
+
75
+ def open_config
76
+ if File.file? "power-build.config"
77
+ %x[open power-build.config]
78
+ else
79
+ puts "Config file does not exist."
80
+ end
81
+ end
82
+
83
+ private
84
+
85
+ def variables_set
86
+ @config = read_config
87
+ @variables = OpenStruct.new(title: @config["title"],
88
+ root_folder: @config["root_folder"],
89
+ site: @config["site"],
90
+ host_link: @config["host_link"],
91
+ host_display_text: @config["host_display_text"],
92
+ resource_prefix: ""
93
+ )
94
+ i18n(@config["language"]).each {|key, value| @variables.send("#{key.to_s}=".to_sym, value) }
95
+ end
96
+
97
+ def copy_assets_dir(dir_name)
98
+ dir = File.expand_path("#{@assets_base}/#{dir_name}", __FILE__)
99
+ Dir.mkdir "assets" unless File.directory? "assets"
100
+ FileUtils.copy_entry dir, "assets/#{dir_name}"
101
+ end
102
+
103
+ def copy_assets_in_order
104
+ ["css", "js", "fonts"].each do |dir|
105
+ directory = File.expand_path("#{@assets_base}/#{dir}", __FILE__)
106
+ Dir.entries(directory).each do |entry|
107
+ next if [".", ".."].include? entry
108
+ if File.file? "assets/#{dir}/#{entry}"
109
+ puts "Skipped: assets/#{dir}/#{entry}"
110
+ else
111
+ FileUtils.copy "#{directory}/#{entry}", "assets/#{dir}/#{entry}"
112
+ puts "Created: ".green + "assets/#{dir}/#{entry}"
113
+ end
114
+ end
115
+ end
116
+ end
117
+
118
+ def read_config
119
+ begin
120
+ JSON.parse(File.read("power-build.config"))
121
+ rescue JSON::ParserError
122
+ puts "============================================================"
123
+ puts "Oops, make sure 'power-build.config' is in correct JSON format."
124
+ puts "1. Wrap the whole content in brackets {}"
125
+ puts "2. Wrap each key and value with quotes ''"
126
+ puts "3. Separate each setting with comma ,"
127
+ puts "If you can't fix it, run 'power delete' to start over again."
128
+ puts "============================================================"
129
+ raise JSON::ParserError
130
+ end
131
+ end
132
+
133
+ def update_partials
134
+ add_partial("head")
135
+ add_partial("navbar")
136
+ add_partial("footer")
137
+ end
138
+
139
+ def add_partial(partial)
140
+ content = File.read(File.expand_path("#{@assets_base}/partials/_#{partial}.html.erb", __FILE__))
141
+ rendered = ERB.new(content).result(@variables.instance_eval{binding})
142
+ @variables.send("#{partial}=".to_sym, rendered)
143
+ end
144
+
145
+ def render_page(page)
146
+ content = File.read(File.expand_path("#{@assets_base}/templates/#{page}.html.erb", __FILE__))
147
+ erb = ERB.new(content).result(@variables.instance_eval{binding})
148
+ File.open("#{page}.html", "w") {|file| file.write(erb)}
149
+ end
150
+
151
+ def collect_images
152
+ collection = []
153
+ root_dir = @config["root_folder"]
154
+ dirs = Dir.entries(root_dir)
155
+ dirs.delete(".")
156
+ dirs.delete("..")
157
+ dirs.each do |dir|
158
+ if File.directory? "#{root_dir}/#{dir}"
159
+ images = Dir.entries("#{root_dir}/#{dir}")
160
+ images.delete(".")
161
+ images.delete("..")
162
+ category = {:tag => dir, :images => []}
163
+ images.each do |image|
164
+ if File.file? "#{root_dir}/#{dir}/#{image}"
165
+ if ["jpg", "png", "gif", "peg"].include? image[-3..-1].downcase
166
+ category[:images].push(image)
167
+ end
168
+ end
169
+ end
170
+ collection.push(category)
171
+ end
172
+ end
173
+ return collection
174
+ end
175
+
176
+ def create_page_by(category)
177
+ @dir = FileUtils.mkdir_p("assets/#{category[:tag]}").first
178
+ @variables.current_category = category
179
+ content = File.read(File.expand_path("#{@assets_base}/templates/category.html.erb", __FILE__))
180
+ erb = ERB.new(content).result(@variables.instance_eval{binding})
181
+ File.open("#{@dir}/index.html", "w") {|file| file.write(erb)}
182
+ puts "Created: ".green + "#{@dir}/index.html"
183
+ end
184
+
185
+ def create_page_by_each(category, image)
186
+ @variables.current_image = "#{@dir}/#{image}"
187
+ @variables.current_image_source = "../../#{@config["root_folder"]}/#{category[:tag]}/#{image}"
188
+ @variables.current_title = image
189
+ @variables.title = @variables.current_title
190
+ content = File.read(File.expand_path("#{@assets_base}/templates/show.html.erb", __FILE__))
191
+ erb = ERB.new(content).result(@variables.instance_eval{binding})
192
+ File.open("#{@dir}/#{@variables.current_title}.html", "w") {|file| file.write(erb)}
193
+ puts "Created: ".green + "#{@dir}/#{@variables.current_title}.html"
194
+ end
195
+
196
+ def i18n(setting)
197
+ case setting
198
+ when "zh-tw"
199
+ {
200
+ i_header: "總覽",
201
+ i_category: "類別",
202
+ i_copyright: "版權所有",
203
+ i_download: "下載",
204
+ i_home: "首頁"
205
+ }
206
+ when "en"
207
+ {
208
+ i_header: "Overview",
209
+ i_category: "Categories",
210
+ i_copyright: "Copyright",
211
+ i_download: "Download",
212
+ i_home: "Home"
213
+ }
214
+ end
215
+ end
216
+ end
217
+ end
@@ -0,0 +1,43 @@
1
+ require 'colorize'
2
+ require 'json'
3
+
4
+ module PowerBuild
5
+ class GitHubManager
6
+ def initialize
7
+
8
+ end
9
+
10
+ # def checkout_branch
11
+ # command = %x[git branch]
12
+ # if command.include? "gh-pages"
13
+ # command = %x[git checkout gh-pages]
14
+ # else
15
+ # command = %x[git checkout -b gh-pages]
16
+ # end
17
+ # add_and_commit
18
+ # end
19
+ #
20
+ # def add_and_commit
21
+ # status = %x[git status]
22
+ # if status.include? "assets"
23
+ # %x[git add assets]
24
+ # @commit = true
25
+ # end
26
+ # if status.include? "index.html"
27
+ # %x[git add index.html]
28
+ # @commit = true
29
+ # end
30
+ # if status.include? "power-build.config"
31
+ # %x[git add power-build.config]
32
+ # @commit = true
33
+ # end
34
+ # if @commit
35
+ # %x[git commit -m "regenerate site"]
36
+ # end
37
+ # end
38
+ #
39
+ # def push
40
+ # %x[git push]
41
+ # end
42
+ end
43
+ end
@@ -1,3 +1,3 @@
1
1
  module PowerBuild
2
- VERSION = "0.0.1"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: power-build
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-28 00:00:00.000000000 Z
11
+ date: 2015-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,8 +80,29 @@ files:
80
80
  - README.md
81
81
  - Rakefile
82
82
  - bin/power
83
+ - lib/assets/css/bootstrap.css
84
+ - lib/assets/css/bootstrap.min.css
85
+ - lib/assets/css/custom.css
86
+ - lib/assets/fonts/glyphicons-halflings-regular.eot
87
+ - lib/assets/fonts/glyphicons-halflings-regular.svg
88
+ - lib/assets/fonts/glyphicons-halflings-regular.ttf
89
+ - lib/assets/fonts/glyphicons-halflings-regular.woff
90
+ - lib/assets/fonts/glyphicons-halflings-regular.woff2
91
+ - lib/assets/js/bootstrap.js
92
+ - lib/assets/js/bootstrap.min.js
93
+ - lib/assets/js/custom.js
94
+ - lib/assets/js/jquery.js
95
+ - lib/assets/partials/_footer.html.erb
96
+ - lib/assets/partials/_head.html.erb
97
+ - lib/assets/partials/_navbar.html.erb
98
+ - lib/assets/power-build.config
99
+ - lib/assets/templates/category.html.erb
100
+ - lib/assets/templates/index.html.erb
101
+ - lib/assets/templates/show.html.erb
83
102
  - lib/power-build.rb
84
103
  - lib/power-build/base.rb
104
+ - lib/power-build/constructor.rb
105
+ - lib/power-build/github_manager.rb
85
106
  - lib/power-build/version.rb
86
107
  - power-build.gemspec
87
108
  homepage: https://github.com/nkj20932/power-build