Skip to content

Instantly share code, notes, and snippets.

@timriley
Created June 9, 2016 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timriley/6bdfa7b5e7d52714078e6b69ca7b0f55 to your computer and use it in GitHub Desktop.
Save timriley/6bdfa7b5e7d52714078e6b69ca7b0f55 to your computer and use it in GitHub Desktop.
dry-skeleton CLI "wrapper"
#!/usr/bin/env ruby
require "bundler/setup"
require "inflecto"
require "thor"
module Dry
module Skeleton
class CLI < Thor
desc "generate APP", "Generate an app"
def generate(generator_name, *args)
# app_name could be:
#
# - dry_web_roda (should look for a matching gem)
# - icelab/dry_web_roda (a github-hosted project, latest tag)
# - icelab/dry_web_roda#master (a github-hosted project, master branch)
# Helpful notes about programatically working with gems: https://gist.github.com/adamjmurray/3154437
if generator_name !=~ %r[/]
# It's a gem, look for an already installed copy
installed_gem = Gem::Specification.find_by_name(generator_name)
# Or install it fresh
unless installed_gem
require "rubygems/commands/install_command"
cmd = Gem::Commands::InstallCommand.new
cmd.handle_options [generator_name]
begin
cmd.execute
rescue Gem::SystemExitException => e
unless SystemExit.new(e.exit_code).success?
abort "Failed to install #{generator_name} gem"
end
end
end
else
# This is a github-style name so let's fetch it:
#
# - Clone the repo into ~/.dry-skeleton/gems (TODO)
# - Add ~/.dry-skeleton/gems to gem path
path_with_skeletons = ["~/.dry-skeleton/gems", Gem.path].join(":")
env_with_path = ENV.to_h.merge("GEM_PATH" => path_with_skeletons)
Gem.paths = env_with_path
end
# Now load and require the skeleton gem
gem generator_name
require generator_name
# Get its generator at an expected constant (e.g. MyGeneratorName::Generator)
generator = Inflecto.constantize(Inflecto.camelize("#{generator_name}/generator")).new
# The skeleton's generator should provide a Thor CLI app, which we can
# then forward the rest of the args to, and it can handle the
# generation from there
generator.cli.start(*args) # TODO: work out how to properly pass the args
end
end
end
end
Dry::Skeleton::CLI.start(ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment