Skip to content

Commit

Permalink
Make Crystal::Init a class
Browse files Browse the repository at this point in the history
  • Loading branch information
bew committed Jan 3, 2018
1 parent 487d7b9 commit e750686
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions src/compiler/crystal/tools/init.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ module Crystal
WHICH_GIT_COMMAND = "which git >/dev/null"

def self.run(args, *, stderr = STDERR)
config = new(stderr).parse_args(args)
InitProject.new(config).run
end

def initialize(@stderr : IO)
end

def parse_args(args)
config = Config.new

OptionParser.parse(args) do |opts|
Expand All @@ -31,69 +39,69 @@ module Crystal
end

opts.unknown_args do |args, after_dash|
config.skeleton_type = fetch_skeleton_type(opts, args, stderr)
config.name = fetch_name(opts, args, stderr)
config.dir = fetch_directory(args, config.name, stderr)
config.skeleton_type = fetch_skeleton_type(opts, args)
config.name = fetch_name(opts, args)
config.dir = fetch_directory(args, config.name)
end
end

config.author = fetch_author
config.email = fetch_email
config.github_name = fetch_github_name
InitProject.new(config).run
config
end

def self.fetch_author
def fetch_author
if system(WHICH_GIT_COMMAND)
user_name = `git config --get user.name`.strip
user_name = nil if user_name.empty?
end
user_name || "your-name-here"
end

def self.fetch_email
def fetch_email
if system(WHICH_GIT_COMMAND)
user_email = `git config --get user.email`.strip
user_email = nil if user_email.empty?
end
user_email || "your-email-here"
end

def self.fetch_github_name
def fetch_github_name
if system(WHICH_GIT_COMMAND)
github_user = `git config --get github.user`.strip
github_user = nil if github_user.empty?
end
github_user || "your-github-user"
end

def self.fetch_name(opts, args, stderr)
fetch_required_parameter(opts, args, "NAME", stderr)
def fetch_name(opts, args)
fetch_required_parameter(opts, args, "NAME")
end

def self.fetch_directory(args, project_name, stderr)
def fetch_directory(args, project_name)
directory = args.empty? ? project_name : args.shift
if Dir.exists?(directory) || File.exists?(directory)
stderr.puts "file or directory #{directory} already exists"
@stderr.puts "file or directory #{directory} already exists"
exit 1
end
directory
end

def self.fetch_skeleton_type(opts, args, stderr)
skeleton_type = fetch_required_parameter(opts, args, "TYPE", stderr)
def fetch_skeleton_type(opts, args)
skeleton_type = fetch_required_parameter(opts, args, "TYPE")
unless {"lib", "app"}.includes?(skeleton_type)
stderr.puts "invalid TYPE value: #{skeleton_type}"
stderr.puts opts
@stderr.puts "invalid TYPE value: #{skeleton_type}"
@stderr.puts opts
exit 1
end
skeleton_type
end

def self.fetch_required_parameter(opts, args, name, stderr)
def fetch_required_parameter(opts, args, name, stderr)
if args.empty?
stderr.puts "#{name} is missing"
stderr.puts opts
@stderr.puts "#{name} is missing"
@stderr.puts opts
exit 1
end
args.shift
Expand Down

0 comments on commit e750686

Please sign in to comment.