@@ -123,15 +123,35 @@ end # task :preview
123123
124124namespace :theme do
125125
126- # 0.1.0 version of theme install will be simple 1:1 file matching.
126+ # Public: Install a theme using the theme packager.
127+ # Version 0.1.0 simple 1:1 file matching.
128+ #
129+ # git - String, Optional path to the git repository of the theme to be installed.
130+ # name - String, Optional name of the theme you want to install.
131+ # Passing name requires that the theme package already exist.
132+ #
133+ # Examples
134+ #
135+ # rake theme:install git="https://github.com/jekyllbootstrap/theme-twitter.git"
136+ # rake theme:install name="cool-theme"
137+ #
138+ # Returns Success/failure messages.
127139 desc "Install theme"
128140 task :install do
129- name = ENV [ "name" ] . to_s . downcase
130- packaged_theme_path = JB ::Path . build ( :theme_packages , :node => name )
141+ if ENV [ "git" ]
142+ manifest = theme_from_git_url ( ENV [ "git" ] )
143+ name = manifest [ "name" ]
144+ else
145+ name = ENV [ "name" ] . to_s . downcase
146+ end
131147
148+ packaged_theme_path = JB ::Path . build ( :theme_packages , :node => name )
149+
132150 abort ( "rake aborted: name cannot be blank" ) if name . empty?
133151 abort ( "rake aborted: '#{ packaged_theme_path } ' directory not found." ) unless FileTest . directory? ( packaged_theme_path )
134152
153+ manifest = verify_manifest ( packaged_theme_path )
154+
135155 # Get relative paths to packaged theme files
136156 packaged_theme_files = [ ]
137157 FileUtils . cd ( packaged_theme_path ) { packaged_theme_files += Dir [ "**/*.*" ] }
@@ -155,10 +175,18 @@ namespace :theme do
155175 system ( "rake switch_theme name='#{ name } '" )
156176 end
157177 end
158-
159- # The 0.1.0 version of theme packaging will be simple 1:1 file matching.
160- # The theme repo will outline it's files in the same structure
161- # it expects to be included into the Jekyll structure.
178+
179+ # Public: Package a theme using the theme packager.
180+ # The theme must be structured using valid JB API.
181+ # In other words packaging is essentially the reverse of installing.
182+ #
183+ # name - String, Required name of the theme you want to package.
184+ #
185+ # Examples
186+ #
187+ # rake theme:package name="twitter"
188+ #
189+ # Returns Success/failure messages.
162190 desc "Package theme"
163191 task :package do
164192 name = ENV [ "name" ] . to_s . downcase
@@ -190,6 +218,39 @@ namespace :theme do
190218
191219end # end namespace :theme
192220
221+ # Internal: Download and process a theme from a git url.
222+ # Notice we don't know the name of the theme until we look it up in the manifest.
223+ # So we'll have to change the folder name once we get the name.
224+ #
225+ # url - String, Required url to git repository.
226+ #
227+ # Returns theme manifest hash
228+ def theme_from_git_url ( url )
229+ tmp_path = JB ::Path . build ( :theme_packages , :node => "_tmp" )
230+ system ( "git clone #{ url } #{ tmp_path } " )
231+ manifest = verify_manifest ( tmp_path )
232+ new_path = JB ::Path . build ( :theme_packages , :node => manifest [ "name" ] )
233+ if File . exist? ( new_path ) && ask ( "=> #{ new_path } theme package already exists. Override?" , [ 'y' , 'n' ] ) == 'n'
234+ remove_dir ( tmp_path )
235+ abort ( "rake aborted: '#{ manifest [ "name" ] } ' already exists as theme package." )
236+ end
237+
238+ remove_dir ( new_path ) if File . exist? ( new_path )
239+ mv ( tmp_path , new_path )
240+ manifest
241+ end
242+
243+ # Internal: Process theme package manifest file.
244+ #
245+ # theme_path - String, Required. File path to theme package.
246+ #
247+ # Returns theme manifest hash
248+ def verify_manifest ( theme_path )
249+ manifest = File . join ( theme_path , "manifest.yml" )
250+ abort ( "rake aborted: repo must contain valid manifest.yml" ) unless File . exist? manifest
251+ manifest = YAML . load_file ( manifest )
252+ manifest
253+ end
193254
194255def ask ( message , valid_options )
195256 if valid_options
0 commit comments