Skip to content

Commit ad96f8d

Browse files
committedMay 10, 2013
Adding in a Rakefile
1 parent 31b5d91 commit ad96f8d

File tree

1 file changed

+231
-0
lines changed

1 file changed

+231
-0
lines changed
 

‎Rakefile

+231
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
# This file is a rake build file. The purpose of this file is to simplify
2+
# setting up and using Awestruct. It's not required to use Awestruct, though it
3+
# does save you time (hopefully). If you don't want to use rake, just ignore or
4+
# delete this file.
5+
#
6+
# If you're just getting started, execute this command to install Awestruct and
7+
# the libraries on which it depends:
8+
#
9+
# rake setup
10+
#
11+
# The setup task installs the necessary libraries according to which Ruby
12+
# environment you are using. If you want the libraries kept inside the project,
13+
# execute this command instead:
14+
#
15+
# rake setup[local]
16+
#
17+
# IMPORTANT: To install gems, you'll need development tools on your machine,
18+
# which include a C compiler, the Ruby development libraries and some other
19+
# development libraries as well.
20+
#
21+
# There are also tasks for running Awestruct. The build will auto-detect
22+
# whether you are using Bundler and, if you are, wrap calls to awestruct in
23+
# `bundle exec`.
24+
#
25+
# To run in Awestruct in development mode, execute:
26+
#
27+
# rake
28+
#
29+
# To clean the generated site before you build, execute:
30+
#
31+
# rake clean preview
32+
#
33+
# To deploy using the production profile, execute:
34+
#
35+
# rake deploy
36+
#
37+
# To get a list of all tasks, execute:
38+
#
39+
# rake -T
40+
#
41+
# Now you're Awestruct with rake!
42+
43+
$use_bundle_exec = true
44+
$install_gems = ['awestruct -v "~> 0.5.0"', 'rb-inotify -v "~> 0.9.0"']
45+
$awestruct_cmd = nil
46+
task :default => :preview
47+
48+
desc 'Setup the environment to run Awestruct'
49+
task :setup, [:env] => :init do |task, args|
50+
next if !which('awestruct').nil?
51+
52+
if File.exist? 'Gemfile'
53+
if args[:env] == 'local'
54+
require 'fileutils'
55+
FileUtils.remove_file 'Gemfile.lock', true
56+
FileUtils.remove_dir '.bundle', true
57+
system 'bundle install --binstubs=_bin --path=.bundle'
58+
else
59+
system 'bundle install'
60+
end
61+
else
62+
if args[:env] == 'local'
63+
$install_gems.each do |gem|
64+
msg "Installing #{gem}..."
65+
system "gem install --bindir=_bin --install-dir=.bundle #{gem}"
66+
end
67+
else
68+
$install_gems.each do |gem|
69+
msg "Installing #{gem}..."
70+
system "gem install #{gem}"
71+
end
72+
end
73+
end
74+
msg 'Run awestruct using `awestruct` or `rake`'
75+
# Don't execute any more tasks, need to reset env
76+
exit 0
77+
end
78+
79+
desc 'Update the environment to run Awestruct'
80+
task :update => :init do
81+
if File.exist? 'Gemfile'
82+
system 'bundle update'
83+
else
84+
system 'gem update awestruct'
85+
end
86+
# Don't execute any more tasks, need to reset env
87+
exit 0
88+
end
89+
90+
desc 'Build and preview the site locally in development mode'
91+
task :preview => :check do
92+
run_awestruct '-d'
93+
end
94+
95+
desc 'Generate the site using the development profile'
96+
task :gen => :check do
97+
run_awestruct '-P development -g --force'
98+
end
99+
100+
desc 'Push local commits to origin/master'
101+
task :push do
102+
system 'git push origin master'
103+
end
104+
105+
#desc 'Generate the site and deploy to production'
106+
# TODO: This will need to be tweaked a bit for our site, we may need to shell out to a system command
107+
#task :deploy => [:check, :push] do
108+
#run_awestruct '-P production -g --force --deploy'
109+
#end
110+
111+
#desc 'Generate site from Travis CI and, if not a pull request, publish site to production (GitHub Pages)'
112+
#task :travis => :check do
113+
## if this is a pull request, do a simple build of the site and stop
114+
#if ENV['TRAVIS_PULL_REQUEST'] == '1' || ENV['TRAVIS_PULL_REQUEST'] == 'true'
115+
#run_awestruct '-P production -g'
116+
#next
117+
#end
118+
119+
#require 'yaml'
120+
121+
## TODO use the Git library for these commands rather than system
122+
#repo = %x(git config remote.origin.url).gsub(/^git:/, 'https:')
123+
#system "git remote set-url --push origin #{repo}"
124+
#system 'git remote set-branches --add origin master'
125+
#system 'git fetch -q'
126+
##git_user = YAML.load_file('_config/git.yml')
127+
##system "git config user.name '#{git_user['name']}'"
128+
##system "git config user.email '#{git_user['email']}'"
129+
#system "git config user.name '#{ENV['GIT_NAME']}'"
130+
#system "git config user.email '#{ENV['GIT_EMAIL']}'"
131+
#system 'git config credential.helper "store --file=.git/credentials"'
132+
## CREDENTIALS assigned by a Travis CI Secure Environment Variable
133+
## see http://about.travis-ci.org/docs/user/build-configuration/#Secure-environment-variables for details
134+
#File.open('.git/credentials', 'w') {|f| f.write("https://#{ENV['GH_TOKEN']}:@github.com") }
135+
#set_pub_dates 'develop'
136+
#system 'git branch master origin/master'
137+
#run_awestruct '-P production -g --deploy'
138+
#File.delete '.git/credentials'
139+
#end
140+
141+
desc 'Clean out generated site and temporary files'
142+
task :clean, :spec do |task, args|
143+
require 'fileutils'
144+
dirs = ['.awestruct', '.sass-cache', '_site']
145+
if args[:spec] == 'all'
146+
dirs << '_tmp'
147+
end
148+
dirs.each do |dir|
149+
FileUtils.remove_dir dir unless !File.directory? dir
150+
end
151+
end
152+
153+
# Perform initialization steps, such as setting up the PATH
154+
task :init do
155+
# Detect using gems local to project
156+
if File.exist? '_bin'
157+
ENV['PATH'] = "_bin#{File::PATH_SEPARATOR}#{ENV['PATH']}"
158+
ENV['GEM_HOME'] = '.bundle'
159+
end
160+
end
161+
162+
desc 'Check to ensure the environment is properly configured'
163+
task :check => :init do
164+
if !File.exist? 'Gemfile'
165+
if which('awestruct').nil?
166+
msg 'Could not find awestruct.', :warn
167+
msg 'Run `rake setup` or `rake setup[local]` to install from RubyGems.'
168+
# Enable once the rubygem-awestruct RPM is available
169+
#msg 'Run `sudo yum install rubygem-awestruct` to install via RPM. (Fedora >= 18)'
170+
exit 1
171+
else
172+
$use_bundle_exec = false
173+
next
174+
end
175+
end
176+
177+
begin
178+
require 'bundler'
179+
Bundler.setup
180+
rescue LoadError
181+
$use_bundle_exec = false
182+
rescue StandardError => e
183+
msg e.message, :warn
184+
if which('awestruct').nil?
185+
msg 'Run `rake setup` or `rake setup[local]` to install required gems from RubyGems.'
186+
else
187+
msg 'Run `rake update` to install additional required gems from RubyGems.'
188+
end
189+
exit e.status_code
190+
end
191+
end
192+
193+
# Execute Awestruct
194+
def run_awestruct(args)
195+
system "#{$use_bundle_exec ? 'bundle exec ' : ''}awestruct #{args}"
196+
end
197+
198+
# A cross-platform means of finding an executable in the $PATH.
199+
# Respects $PATHEXT, which lists valid file extensions for executables on Windows
200+
#
201+
# which 'awestruct'
202+
# => /usr/bin/awestruct
203+
def which(cmd, opts = {})
204+
unless $awestruct_cmd.nil? || opts[:clear_cache]
205+
return $awestruct_cmd
206+
end
207+
208+
$awestruct_cmd = nil
209+
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
210+
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
211+
exts.each do |ext|
212+
candidate = File.join path, "#{cmd}#{ext}"
213+
if File.executable? candidate
214+
$awestruct_cmd = candidate
215+
return $awestruct_cmd
216+
end
217+
end
218+
end
219+
return $awestruct_cmd
220+
end
221+
222+
# Print a message to STDOUT
223+
def msg(text, level = :info)
224+
case level
225+
when :warn
226+
puts "\e[31m#{text}\e[0m"
227+
else
228+
puts "\e[33m#{text}\e[0m"
229+
end
230+
end
231+

0 commit comments

Comments
 (0)
Please sign in to comment.