Skip to content

Commit

Permalink
Add migration generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Yacine Petitprez committed Mar 21, 2018
1 parent d9161d4 commit 3fb53ba
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 27 deletions.
47 changes: 26 additions & 21 deletions src/clear/cli/generator.cr
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
require "ecr"

module Clear::CLI
record Generator, name : String, desc : String, callback : Hash(Symbol, String) -> Void
module Generate
record Generator, name : String, desc : String, callback : Hash(Symbol, String) -> Void

@@generators : Hash(String, Generator) = {} of String => Generator
@@generators : Hash(String, Generator) = {} of String => Generator

def self.add_generator(name, desc, &block : Hash(Symbol, String) -> Void)
add_generator Generator.new(name, desc, block)
end
def self.add_generator(name, desc, &block : Hash(Symbol, String) -> Void)
add_generator Generator.new(name, desc, block)
end

def self.add_generator(generator : Generator)
@@generators[generator.name] = generator
end
def self.add_generator(generator : Generator)
@@generators[generator.name] = generator
end

def self.run_generator(name, options, directory : String? = nil)
opts = {} of Symbol => String
def self.run_generator(name, options, directory : String? = nil)
opts = {} of Symbol => String

opts[:email] = `git config user.email`.chomp || "[email protected]"
opts[:user_name] = `git config user.name`.chomp || "Your Name"
opts[:app_name] = "MyApp"
opts[:app_name_underscore] = opts[:app_name].underscore
opts[:directory] = directory || opts[:app_name_underscore]
opts[:email] = `git config user.email`.chomp || "[email protected]"
opts[:user_name] = `git config user.name`.chomp || "Your Name"
opts[:app_name] = "MyApp"
opts[:app_name_underscore] = opts[:app_name].underscore
opts[:directory] = directory || opts[:app_name_underscore]

@@generators[name].callback.call(opts)
end
@@generators[name].callback.call(opts)
end

macro ecr_to_s(file, opts)
io = IO::Memory.new
ECR.embed {{file}}, io
io.to_s
end

macro ecr_to_s(file, opts)
io = IO::Memory.new
ECR.embed {{file}}, io
io.to_s
def self.run(args)
end
end
end

Expand Down
14 changes: 14 additions & 0 deletions src/clear/cli/generators/migration.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Clear::CLI::Generate.add_generator("migration",
"Create a new migration") do |opts|
g = Generate::Generator.new
g.target_directory = "."

timestamp = Time.now.epoch.to_s.rjust(10, '0')

file_name = "#{timestamp}_#{opts[:name].underscore}.cr"
opts[:class_name] = "#{opts[:name].camelcase}#{timestamp}"

g.in_directory "src/db/migrations" do
g.file(file_name, "./templates/migration.ecr")
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "generate"

Clear::CLI.add_generator("kemal",
Clear::CLI::Generate.add_generator("new/kemal",
"Setup a minimal application with kemal and clear") do |opts|
g = Generate::Generator.new
g.target_directory = opts[:directory] || "."
Expand Down Expand Up @@ -57,5 +57,5 @@ Clear::CLI.add_generator("kemal",
system("chmod +x #{opts[:directory]}/bin/appctl")
system("cd #{opts[:directory]} && shards")

puts "Clear + Kemal template is now generated. Use `clear-cli server` to start the server!"
puts "Clear + Kemal template is now generated. `cd #{opts[:directory]} && clear-cli server` to play ! :-)"
end
7 changes: 4 additions & 3 deletions src/clear/cli/migration.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ module Clear::CLI::Migration
down XXX # Turn down a specific migration.
set XXX # Go to a specific step. Down all migration after, up all migration before.
Helpers:
table2model # Output a model based on a pg table.
model # Generate a model.
Other related helpers:
table2model # Output a model based on a pg table.
generate model # Generate a model + migration
generate migration
HELP

exit
Expand Down
1 change: 0 additions & 1 deletion src/clear/cli/table_to_model.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module Clear::CLI::TableToModel
def self.run(opts)
pp opts
end
end
7 changes: 7 additions & 0 deletions templates/migration.cr.ecr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class <%= opts[:class_name] %>
include Clear::Migration

def change(dir)
# TODO: Fill migration
end
end

0 comments on commit 3fb53ba

Please sign in to comment.