Class: Octopress::Ink::Commands::New
- Inherits:
-
Object
- Object
- Octopress::Ink::Commands::New
- Defined in:
- lib/octopress-ink/commands/new.rb
Class Method Summary collapse
- .add_assets ⇒ Object
-
.add_dependency ⇒ Object
Add Octopress Ink dependency to Gemspec.
-
.add_plugin ⇒ Object
Add Octopress Ink plugin to core module file.
-
.add_simple_plugin(mod) ⇒ Object
New plugin uses a simple configuration hash.
- .add_to_gitignore ⇒ Object
- .create_empty_dirs(dirs) ⇒ Object
-
.create_gem(name) ⇒ Object
Create a new gem with Bundle’s gem command.
-
.dependencies ⇒ Object
Returns lines which need to be added as a dependency.
- .fix_name(path) ⇒ Object
- .fix_spec_files ⇒ Object
-
.format_name(name) ⇒ Object
Add spaces between capital letters.
-
.gem_settings(gem_path) ⇒ Object
Read gem settings from the gemspec.
-
.indent(input, level = 1) ⇒ Object
Indent each line of a string.
-
.insert_before(str, pos, input) ⇒ Object
Insert a string before a position.
- .new_plugin ⇒ Object
-
.plugin_config ⇒ Object
Return an Ink Plugin configuration hash desinged for this gem.
- .process_command(p) ⇒ Object
- .rename(path, target) ⇒ Object
- .rewrite_name(path, name) ⇒ Object
- .write(path, contents) ⇒ Object
Class Method Details
.add_assets ⇒ Object
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/octopress-ink/commands/new.rb', line 154 def self.add_assets dirs = %w{docs images fonts pages templates files layouts includes stylesheets javascripts}.map do |asset| File.join(@settings[:path], 'assets', asset) end create_empty_dirs dirs # Add Jekyll configuration file # FileUtils.touch File.join(@settings[:path], 'assets', 'config.yml') end |
.add_dependency ⇒ Object
Add Octopress Ink dependency to Gemspec
122 123 124 125 126 127 |
# File 'lib/octopress-ink/commands/new.rb', line 122 def self.add_dependency pos = @settings[:gemspec].rindex("end") @settings[:gemspec] = insert_before(@settings[:gemspec], pos, indent(dependencies)) File.open(@settings[:gemspec_path], 'w+') {|f| f.write(@settings[:gemspec]) } end |
.add_plugin ⇒ Object
Add Octopress Ink plugin to core module file
144 145 146 147 148 149 150 151 152 |
# File 'lib/octopress-ink/commands/new.rb', line 144 def self.add_plugin # Grab the module directory from the version.rb require. # If a gem is created with dashes e.g. "some-gem", Bundler puts the module file at lib/some/gem.rb file = File.join(@settings[:path], @settings[:module_path]) mod = File.open(file).read mod = add_simple_plugin mod File.open(file, 'w+') {|f| f.write(mod) } end |
.add_simple_plugin(mod) ⇒ Object
New plugin uses a simple configuration hash
167 168 169 170 171 172 173 174 175 |
# File 'lib/octopress-ink/commands/new.rb', line 167 def self.add_simple_plugin(mod) mod = "#{@settings[:require_version]}\n" mod += "require 'octopress-ink'\n" if @settings[:type] == 'theme' mod += "\nOctopress::Ink.add_theme({\n#{indent(plugin_config)}\n})\n" else mod += "\nOctopress::Ink.add_plugin({\n#{indent(plugin_config)}\n})\n" end end |
.add_to_gitignore ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/octopress-ink/commands/new.rb', line 94 def self.add_to_gitignore path = File.join @settings[:path], '.gitignore' content = File.open(path).read << <<-HERE .*-cache _site HERE File.open(path, 'w+') do |f| f.write(content) end end |
.create_empty_dirs(dirs) ⇒ Object
206 207 208 209 210 211 212 213 |
# File 'lib/octopress-ink/commands/new.rb', line 206 def self.create_empty_dirs(dirs) dirs.each do |d| dir = File.join(d) action = Dir.exist?(dir) ? "exists".rjust(12).blue.bold : "create".rjust(12).green.bold FileUtils.mkdir_p dir puts "#{action} #{dir.sub("#{Dir.pwd}/", '')}/" end end |
.create_gem(name) ⇒ Object
Create a new gem with Bundle’s gem command
108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/octopress-ink/commands/new.rb', line 108 def self.create_gem(name) begin require 'bundler' require 'bundler/cli' Bundler::CLI.start(['gem', name]) rescue LoadError raise "To use this feature you'll need to install the bundler gem with `gem install bundler`." end end |
.dependencies ⇒ Object
Returns lines which need to be added as a dependency
spec_var - variable used to assign gemspec attributes, e.g. “spec” as in spec.name = “gem name”
134 135 136 137 138 139 140 |
# File 'lib/octopress-ink/commands/new.rb', line 134 def self.dependencies minor_version = VERSION.scan(/\d+\.\d/)[0] current_patch_version = VERSION.sub(/\.(alpha|rc).*/i, '') d = "#{@settings[:spec_var]}.add_development_dependency \"octopress\"\n\n" d = "#{@settings[:spec_var]}.add_development_dependency \"clash\"\n\n" d += "#{@settings[:spec_var]}.add_runtime_dependency \"octopress-ink\", \"~> #{minor_version}\"\n" end |
.fix_name(path) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/octopress-ink/commands/new.rb', line 54 def self.fix_name(path) name = @options['name'].gsub(/-/,'_') path = rename('./', name) rename(path, "lib/#{name}") gemspec = rename(path, "#{name}.gemspec") gem_file = rename(path, "lib/#{name}.rb") rewrite_name(gemspec, name) rewrite_name(gem_file, name) rewrite_name(File.join(path, 'README.md'), name) rewrite_name(File.join(path, 'Gemfile'), name) action = "rename".rjust(12).green.bold puts "#{action} #{name} to #{@options['name']}" end |
.fix_spec_files ⇒ Object
88 89 90 91 92 |
# File 'lib/octopress-ink/commands/new.rb', line 88 def self.fix_spec_files @settings[:gemspec].sub! /(#{@settings[:spec_var]}\.files\s+=\s+)(.+)$/ do $1+"`git ls-files -z`.split(\"\\x0\").grep(%r{^(bin/|lib/|assets/|changelog|readme|license)}i)" end.sub!(/\s*#{@settings[:spec_var]}\.test_files.+$/, '') end |
.format_name(name) ⇒ Object
Add spaces between capital letters
245 246 247 |
# File 'lib/octopress-ink/commands/new.rb', line 245 def self.format_name(name) name.scan(/.*?[a-z](?=[A-Z]|$)/).join(' ') end |
.gem_settings(gem_path) ⇒ Object
Read gem settings from the gemspec
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/octopress-ink/commands/new.rb', line 217 def self.gem_settings(gem_path) gemspec_path = Dir.glob(File.join(gem_path, "/*.gemspec")).first if gemspec_path.nil? raise "No gemspec file found at #{gem_path}/. To create a new plugin use `octopress ink new <PLUGIN_NAME>`" end gemspec = File.open(gemspec_path).read require_version = gemspec.scan(/require.+version.+$/)[0] module_subpath = require_version.scan(/['"](.+)\/version/).flatten[0] version = gemspec.scan(/version.+=\s+(.+)$/).flatten[0] module_name = format_name(version.split('::')[-2]) { path: gem_path, gemspec: gemspec, gemspec_path: gemspec_path, spec_var: gemspec.scan(/(\w+)\.name/).flatten[0], name: gemspec.scan(/name.+['"](.+)['"]/).flatten[0], version: version, require_version: require_version, module_path: File.join('lib', module_subpath + '.rb'), module_name: module_name } end |
.indent(input, level = 1) ⇒ Object
Indent each line of a string
251 252 253 |
# File 'lib/octopress-ink/commands/new.rb', line 251 def self.indent(input, level=1) input.gsub(/^/, ' ' * level) end |
.insert_before(str, pos, input) ⇒ Object
Insert a string before a position
257 258 259 260 261 262 263 |
# File 'lib/octopress-ink/commands/new.rb', line 257 def self.insert_before(str, pos, input) if pos str[0..(pos - 1)] + input + str[pos..-1] else str end end |
.new_plugin ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/octopress-ink/commands/new.rb', line 24 def self.new_plugin path = @options['path'] ||= Dir.pwd gem_name = @options['name'] path_to_gem = File.join(path, gem_name) if !File.exist?(path) raise "Directory not found: #{File.(path)}." end Dir.chdir(path) do if Dir.exist?(gem_name) && !Dir["#{gem_name}/*"].empty? raise "Directory not empty: #{File.(gem_name)}." end name = gem_name.gsub(/-/,'_') create_gem(name) fix_name(gem_name) if name != gem_name @settings = gem_settings(gem_name) @settings[:type] = @options['theme'] ? 'theme' : 'plugin' fix_spec_files add_to_gitignore add_dependency add_plugin add_assets end end |
.plugin_config ⇒ Object
Return an Ink Plugin configuration hash desinged for this gem
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/octopress-ink/commands/new.rb', line 180 def self.plugin_config depth = @settings[:module_path].count('/') assets_path = ("../" * depth) + 'assets' config = <<-HERE name: "#{@settings[:module_name]}", slug: "#{@settings[:type] == 'theme' ? 'theme' : @settings[:name]}", gem: "#{@settings[:name]}", path: File.expand_path(File.join(File.dirname(__FILE__), "..")), version: #{@settings[:version]}, description: "", # What does your theme/plugin do? source_url: "https://github.com/user/project", # <- Update info website: "" # Optional project website HERE config.rstrip end |
.process_command(p) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/octopress-ink/commands/new.rb', line 5 def self.process_command(p) p.command(:new) do |c| c.syntax "new <PLUGIN_NAME> [options]" c.description "Create a new Octopress Ink plugin with Ruby gem scaffolding." c.option "theme", "--theme", "Create a new theme." c.option "path", "--path PATH", "Create a plugin at a specified path (defaults to current directory)." c.action do |args, | if args.empty? raise "Please provide a plugin name, e.g. my_awesome_plugin." else @options = @options['name'] = args[0] new_plugin end end end end |
.rename(path, target) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/octopress-ink/commands/new.rb', line 81 def self.rename(path, target) old = File.join(path, target) new = File.join(path, target.gsub(/_/, '-')) FileUtils.mv(old, new) new end |
.rewrite_name(path, name) ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/octopress-ink/commands/new.rb', line 72 def self.rewrite_name(path, name) new_name = name.gsub(/_/, '-') content = File.read(path).gsub(name, new_name) File.open(path, 'w+') do |f| f.write(content) end end |
.write(path, contents) ⇒ Object
197 198 199 200 201 202 203 204 |
# File 'lib/octopress-ink/commands/new.rb', line 197 def self.write(path, contents) action = File.exist?(path) ? "written".rjust(12).blue.bold : "create".rjust(12).green.bold File.open(path, 'w+') do |f| f.write(contents) end puts "#{action} #{path.sub("#{Dir.pwd}/", '')}" end |