Skip to content

Instantly share code, notes, and snippets.

@teliosdev
Last active December 25, 2015 08:09
Show Gist options
  • Save teliosdev/6944560 to your computer and use it in GitHub Desktop.
Save teliosdev/6944560 to your computer and use it in GitHub Desktop.
Creates a basic project for creating a ruby library.
#!/usr/bin/env ruby
require 'fileutils'
def init_project(project_name, user_name, user_email, directory)
project_directory = File.join(directory,
project_name.gsub("-", "/")).gsub("./", "")
project_const = project_name.gsub(/_([a-z])/) do |match|
match[1].upcase
end.gsub("-", "::").gsub(/(\A|::)[a-z]/) { |match| match.upcase }
write_to_file = ->(file_name, &block) do
puts "[RBInit] Creating file \"#{file_name}\"..."
FileUtils.mkdir_p(File.dirname(file_name))
File.open(File.join(directory, file_name), "w") { |f| f << block.call }
end
create_dir = ->(new_dir) do
puts "[RBInit] Creating directory \"#{new_dir}\"..."
FileUtils.mkdir_p File.join(directory, new_dir)
end
execute = ->(command) do
puts "[RBInit] Running command \"#{command}\"..."
Process.wait Process.spawn(command,
:out => :out,
:err => :err,
:chdir => directory)
end
unless Dir.exists? directory
Dir.mkdir directory
end
write_to_file.call "#{project_name}.gemspec" do
<<-GEMSPEC
# encoding: utf-8
$LOAD_PATH.unshift "lib"
require "#{project_directory}/version"
Gem::Specification.new do |gem|
gem.name = "#{project_name}"
gem.version = #{project_const}::VERSION
gem.date = Time.now.strftime('%Y-%m-%d')
gem.summary = "I forgot to add a summary."
gem.homepage = "http://github.com/#{user_name}/#{project_name}"
gem.email = ["#{user_email}"]
gem.authors = ["#{user_name}"]
gem.license = 'MIT'
gem.files = `git ls-files`.split("\\n")
gem.require_paths = ["lib"]
gem.test_files = gem.files.grep(/\\A(spec|test|features)\\//)
gem.description = <<DESC
DESC
gem.add_development_dependency 'rspec'
end
GEMSPEC
end
write_to_file.call ".rspec" do
<<-RSPEC
-I./lib
-r#{project_name}
--format Fuubar
--color
RSPEC
end
write_to_file.call ".gitignore" do
<<-GITIGNORE
*.gem
*.rbc
.bundle
.config
coverage
InstalledFiles
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
# YARD artifacts
.yardoc
_yardoc
doc/
GITIGNORE
end
write_to_file.call "Gemfile" do
<<-GITIGNORE
source "https://rubygems.org"
gem 'rspec'
gem 'fuubar'
GITIGNORE
end
write_to_file.call ".travis.yml" do
<<-TRAVIS
language: ruby
rvm:
- 1.9.3
- rbx-19mode
- 2.0.0
script: "bundle exec rspec spec"
TRAVIS
end
create_dir.call "lib/#{project_directory}"
create_dir.call "spec/#{project_directory}"
write_to_file.call "lib/#{project_directory}.rb" do
<<-PROJECTFILE
# Requires here.
require "#{project_name}/version"
# Some documentation here.
module #{project_const}
end
PROJECTFILE
end
write_to_file.call "lib/#{project_directory}/version.rb" do
<<-VERSIONFILE
module #{project_const}
# The current version.
VERSION = "0.0.0".freeze
end
VERSIONFILE
end
write_to_file.call "spec/spec_helper.rb" do
<<-SPECHELPER
Rspec.configure do |config|
# ...
config.expect_with :rspec do |c|
c.syntax = :expect
end
end
SPECHELPER
end
write_to_file.call "spec/#{project_directory}/version_spec.rb" do
<<-VERSIONSPEC
describe #{project_const}::VERSION do
it { should be_frozen }
end
VERSIONSPEC
end
write_to_file.call "README.md" do
<<-README
# #{project_name}
Nothing here but us chickens.
README
end
write_to_file.call "LICENSE.txt" do
<<-LICENSE
Copyright (c) 2013 #{user_name}
MIT License
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
LICENSE
end
execute.call 'bundle install'
execute.call 'git init'
execute.call 'git add .'
execute.call 'git commit -m "Initial Commit"'
end
# When using the executable form of the script, it accepts 0, 1, or 2 arguments.
# If one or two arguments are given, the first argument is used as the project
# name; if two arguments are given, the second argument is used as the directory
# to put the project files in. By default, it puts it in the current directory.
#
# The script uses information from git in order to fill in the relevant
# information - this means that it is dependant on git to provide the correct
# information. Maybe you should configure git?
if File.basename($0, '.rb') == "rbinit_project"
project_name = ARGV.shift || File.basename(Dir.pwd)
user_name = `git config --get user.name`.chomp
user_email = `git config --get user.email`.chomp
directory = ARGV.shift || '.'
init_project(project_name, user_name, user_email, directory)
end
require_relative "rbinit_project"
init_project File.basename(Dir.pwd), `git config --get user.name`.chomp, `git config --get user.email`.chomp, '.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment