From c6f194fb1ed0d1d16b4191579015d6f7d2f34456 Mon Sep 17 00:00:00 2001 From: Adam Williams Date: Wed, 18 Jan 2012 16:25:14 -0500 Subject: [PATCH] Downloading theme-classic for default behavior of create command. --- octopress/features/create.feature | 6 +- .../step_definitions/octopress_steps.rb | 26 ++++++++- octopress/features/support/env.rb | 4 ++ octopress/lib/octopress/commands/create.rb | 55 ++++++++++++++++++- octopress/octopress.gemspec | 4 +- .../spec/octopress/commands/create_spec.rb | 16 ++++++ octopress/spec/spec_helper.rb | 2 + 7 files changed, 104 insertions(+), 9 deletions(-) create mode 100644 octopress/spec/octopress/commands/create_spec.rb create mode 100644 octopress/spec/spec_helper.rb diff --git a/octopress/features/create.feature b/octopress/features/create.feature index b6049fc..9c4628b 100644 --- a/octopress/features/create.feature +++ b/octopress/features/create.feature @@ -2,16 +2,16 @@ Feature: create subcommand In order to make blogs for hackers As a hacker using the octopress executable - I want to create a blog from a template + I want to create a blog using a theme Scenario: No flags, switches or arguments When I successfully run `octopress create` - Then an empty project structure should exist + Then a theme-classic project structure should exist Scenario: Path argument provided When I successfully run `octopress create pathto/myblog` Then a directory named "pathto/myblog" should exist When I cd to "pathto/myblog" - Then an empty project structure should exist + Then a theme-classic project structure should exist Scenario: directory exists diff --git a/octopress/features/step_definitions/octopress_steps.rb b/octopress/features/step_definitions/octopress_steps.rb index 3d6e83b..1123925 100644 --- a/octopress/features/step_definitions/octopress_steps.rb +++ b/octopress/features/step_definitions/octopress_steps.rb @@ -1,6 +1,9 @@ -Then /an empty project structure should exist/ do +Then /a theme-classic project structure should exist/ do + + # Basic project structure check_directory_presence(%w( _plugins + _posts ), true) check_file_presence(%w( @@ -8,6 +11,25 @@ Then /an empty project structure should exist/ do .rbenv-version _config.yml Gemfile - _plugins/octopress.rb ), true) + + # Classic theme files + check_directory_presence(%w( + _includes + _layouts + _sass + assets + blog + images + javascripts + ), true) + + check_file_presence(%w( + blog/archives/index.html + atom.xml + favicon.png + index.html + _sass/screen.scss + ), true) + end diff --git a/octopress/features/support/env.rb b/octopress/features/support/env.rb index fb0a661..452e179 100644 --- a/octopress/features/support/env.rb +++ b/octopress/features/support/env.rb @@ -1 +1,5 @@ require 'aruba/cucumber' + +Before do + @aruba_timeout_seconds = 10 +end diff --git a/octopress/lib/octopress/commands/create.rb b/octopress/lib/octopress/commands/create.rb index 4f0ccb3..41e5d81 100644 --- a/octopress/lib/octopress/commands/create.rb +++ b/octopress/lib/octopress/commands/create.rb @@ -1,13 +1,62 @@ +require 'fileutils' + class Octopress::Commands::Create - include FileUtils def initialize(project_path) @project_root = File.expand_path project_path + @project_tmp = File.join @project_root, 'tmp' @template_root = Octopress.template_root end def execute - mkdir_p @project_root - cp_r File.join(@template_root, '.'), @project_root + FileUtils.mkdir_p @project_root + FileUtils.cp_r File.join(@template_root, '.'), @project_root + FileUtils.mkdir File.join(@project_root, '_posts') + zip_file_path = download 'https://github.com/octopress/theme-classic/zipball/master', 'theme-classic.zip' + unzip zip_file_path, @project_tmp + extracted_root = Dir[File.join(@project_tmp, 'octopress-theme-classic-*')].first + FileUtils.cp_r Dir.glob(File.join(extracted_root, '*')), @project_root end + + def read_url(url, &block) + require 'net/http' + success_response, limit, tries = false, 5, 0 + uri = URI(url) + until success_response || tries == limit + tries += 1 + Net::HTTP.start(uri.host, uri.port, use_ssl:(uri.scheme == 'https')) do |http| + http.request_get(uri.path) { |response| + case response + when Net::HTTPRedirection + success_response = false + uri = URI(response['location']) + when Net::HTTPSuccess + success_response = true + block.call response + end + } + end + end + end + + def download(url, filename) + FileUtils.mkdir_p @project_tmp + File.join(@project_tmp, filename).tap do |download_path| + File.open(download_path, 'w') do |f| + read_url(url) { |resp| resp.read_body { |segment| f.write(segment) } } + end + end + end + + def unzip(file, destination) + require 'zip/zip' + Zip::ZipFile.open(file) do |zip_file| + zip_file.each do |f| + f_path = File.join(destination, f.name) + FileUtils.mkdir_p File.dirname(f_path) + zip_file.extract f, f_path unless File.exist?(f_path) + end + end + end + end diff --git a/octopress/octopress.gemspec b/octopress/octopress.gemspec index df6b4b4..923817b 100644 --- a/octopress/octopress.gemspec +++ b/octopress/octopress.gemspec @@ -19,7 +19,9 @@ bin/octopress s.executables << 'octopress' s.add_dependency('gli', '~> 1.4.0') s.add_dependency('jekyll', '~> 0.11.0') + s.add_dependency('rubyzip', '~> 0.9.5') s.add_development_dependency('rake') s.add_development_dependency('rdoc') - s.add_development_dependency('aruba', '~> 0.4.9') + s.add_development_dependency('fakefs') + s.add_development_dependency('aruba', '0.4.10') end diff --git a/octopress/spec/octopress/commands/create_spec.rb b/octopress/spec/octopress/commands/create_spec.rb new file mode 100644 index 0000000..5d76852 --- /dev/null +++ b/octopress/spec/octopress/commands/create_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe Octopress::Commands::Create do + include FakeFS::SpecHelpers + + let(:project_path) { File.expand_path File.join('tmp', 'create') } + + subject { described_class.new(project_path) } + + it 'downloads to tmp location in project_path' do + download_path = subject.download 'https://github.com/octopress/theme-classic/zipball/master', 'theme-classic.zip' + download_path.should == File.join(project_path, 'tmp', 'theme-classic.zip') + File.exists?(download_path).should be_true + File.size(download_path).should > 0 + end +end diff --git a/octopress/spec/spec_helper.rb b/octopress/spec/spec_helper.rb new file mode 100644 index 0000000..9ed1610 --- /dev/null +++ b/octopress/spec/spec_helper.rb @@ -0,0 +1,2 @@ +require 'fakefs/spec_helpers' +require 'octopress'