Skip to content

Commit

Permalink
Add Jennifer support
Browse files Browse the repository at this point in the history
  • Loading branch information
imdrasil committed May 28, 2018
1 parent 2b7876c commit d114155
Show file tree
Hide file tree
Showing 47 changed files with 986 additions and 308 deletions.
6 changes: 3 additions & 3 deletions spec/amber/cli/templates/app_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ require "../../../spec_helper"

module Amber::CLI
describe App do
pg_app = App.new("sample-app", "pg", "slang", false)
mysql_app = App.new("sample-app", "mysql", "slang", false)
sqlite_app = App.new("sample-app", "sqlite", "slang", false)
pg_app = App.new("sample-app", "pg", "slang", "jennifer", false)
mysql_app = App.new("sample-app", "mysql", "slang", "jennifer", false)
sqlite_app = App.new("sample-app", "sqlite", "slang", "jennifer", false)

describe "#database_name_base" do
it "should return a postgres compatible name" do
Expand Down
182 changes: 182 additions & 0 deletions spec/amber/cli/templates/jennifer_migration_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
require "../../../spec_helper"
require "./migration_spec_helper"

module Amber::CLI::Jennifer
describe Migration do
described_class = Amber::CLI::Jennifer::Migration

describe ".build" do
context "with empty fields" do
it "builds unspecified migration for non new table migration name" do
described_class.build("add_field_to_tables", %w()).is_a?(Amber::CLI::Jennifer::Migration).should be_true
described_class.build("remove_field_from_tables", %w()).is_a?(Amber::CLI::Jennifer::Migration).should be_true
described_class.build("some_gibberish", %w()).is_a?(Amber::CLI::Jennifer::Migration).should be_true
end

it "builds new table migration for new migration name pattern" do
described_class.build("create_tables", %w()).is_a?(Amber::CLI::Jennifer::CreateTableMigration).should be_true
end
end

context "with new table migration name pattern" do
it do
described_class.build("create_tables", %w(name:string))
.is_a?(Amber::CLI::Jennifer::CreateTableMigration).should be_true
end

it "correctly sets table name" do
described_class.build("create_tables", %w(field:string))
.as(Amber::CLI::Jennifer::CreateTableMigration)
.table_name.should eq("tables")
end
end

context "with add new column migration name pattern" do
it do
migration = described_class.build("add_field_to_tables", %w(field:string))
migration.is_a?(Amber::CLI::Jennifer::ChangeColumnsMigration).should be_true
migration.as(Amber::CLI::Jennifer::ChangeColumnsMigration).add?.should be_true
end

it "correctly sets table name" do
described_class.build("add_field_to_tables", %w(field:string))
.as(Amber::CLI::Jennifer::ChangeColumnsMigration)
.table_name.should eq("tables")
end
end

context "with remove column migration name pattern" do
it do
migration = described_class.build("remove_field_from_tables", %w(field:string))
migration.is_a?(Amber::CLI::Jennifer::ChangeColumnsMigration).should be_true
migration.as(Amber::CLI::Jennifer::ChangeColumnsMigration).remove?.should be_true
end

it "correctly sets table name" do
described_class.build("add_field_to_tables", %w(field:string))
.as(Amber::CLI::Jennifer::ChangeColumnsMigration)
.table_name.should eq("tables")
end
end
end

describe "#render" do
migration = described_class.new("some_migration", %w(field:string))
migration_file = MigrationSpecHelper.text_for(migration)

it do
migration_file.should contain "class SomeMigration < Jennifer::Migration::Base"
end

it do
migration_file.should contain "def up"
end

it do
migration_file.should contain "def down"
end
end
end

describe CreateTableMigration do
describe "#render" do
migration = Amber::CLI::Jennifer::CreateTableMigration.new("create_users", %w(name:string owner:ref admin:bool))
migration_file = MigrationSpecHelper.text_for(migration)

it do
migration_file.should contain "class CreateUser < Jennifer::Migration::Base"
end

it do
migration_file.should contain <<-FILE
def up
create_table(:users) do |t|
t.string :name
t.reference :owner
t.bool :admin
t.timestamps
end
end
FILE
end

it do
migration_file.should contain <<-FILE
def down
drop_table :users
end
FILE
end
end
end

describe ChangeColumnsMigration do
describe "#render" do
describe "adding columns" do
migration = Amber::CLI::Jennifer::ChangeColumnsMigration.new("add_columns_to_users", %w(name:string owner:ref admin:bool), :add)
migration_file = MigrationSpecHelper.text_for(migration)

it do
migration_file.should contain "class AddColumnsToUser < Jennifer::Migration::Base"
end

it do
migration_file.should contain <<-FILE
def up
change_table(:users) do |t|
t.add_column :name, :string
t.add_column :owner, :reference
t.add_column :admin, :bool
end
end
FILE
end

it do
migration_file.should contain <<-FILE
def down
change_table(:users) do |t|
t.drop_column :name
t.drop_column :owner
t.drop_column :admin
end
end
FILE
end
end

describe "removing columns" do
migration = Amber::CLI::Jennifer::ChangeColumnsMigration.new("remove_columns_from_users", %w(name:string owner:ref admin:bool), :remove)
migration_file = MigrationSpecHelper.text_for(migration)

it do
migration_file.should contain "class RemoveColumnsFromUser < Jennifer::Migration::Base"
end

it do
migration_file.should contain <<-FILE
def up
change_table(:users) do |t|
t.drop_column :name
t.drop_column :owner
t.drop_column :admin
end
end
FILE
end

it do
migration_file.should contain <<-FILE
def down
change_table(:users) do |t|
t.add_column :name, :string
t.add_column :owner, :reference
t.add_column :admin, :bool
end
end
FILE
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/amber/cli/templates/migration_spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Amber::CLI
ensure
`rm -rf ./tmp/db`
end
return migration_text
migration_text
end

def self.sample_migration_for(migration_template_type)
Expand Down
25 changes: 25 additions & 0 deletions spec/amber/cli/templates/sam_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "../../../spec_helper"

module Amber::CLI
module SamSpecHelper
def self.text_for(sam : Sam) : String
sam.render("./tmp/app")
File.read("./tmp/app/sam.cr")
ensure
`rm -rf ./tmp/app`
end
end

describe Sam do
describe "#render" do
context "with jennifer model" do
sam = Amber::CLI::Sam.new("jennifer")
file = SamSpecHelper.text_for(sam)

it { file.should contain("Sam.help") }
it { file.should contain(%(require "./db/migrations/*")) }
it { file.should contain(%(load_dependencies "jennifer")) }
end
end
end
end
2 changes: 1 addition & 1 deletion src/amber/cli/commands.cr
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module Amber::CLI
help desc: "# Describe available commands and usages"
string ["-t", "--template"], desc: "# Preconfigure for selected template engine. Options: slang | ecr", default: "slang"
string ["-d", "--database"], desc: "# Preconfigure for selected database. Options: pg | mysql | sqlite", default: "pg"
string ["-m", "--model"], desc: "# Preconfigure for selected model. Options: granite | crecto", default: "granite"
string ["-m", "--model"], desc: "# Preconfigure for selected model. Options: granite | crecto | jennifer", default: "granite"
string ["-r", "--recipe"], desc: "# Use a named recipe. See documentation at https://amberframework.org.", default: nil
end
end
Expand Down
17 changes: 15 additions & 2 deletions src/amber/cli/commands/new.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ module Amber::CLI
arg "name", desc: "name of project", required: true
string "-d", desc: "database", any_of: %w(pg mysql sqlite), default: "pg"
string "-t", desc: "template language", any_of: %w(slang ecr), default: "slang"
string "-m", desc: "model type", any_of: %w(granite crecto), default: "granite"
string "-m", desc: "model type", any_of: %w(granite crecto jennifer), default: "granite"
string "--migration", desc: "migration type", any_of: %w(micrate jennifer unspecified), default: "unspecified"
string "-r", desc: "recipe"
bool "--deps", desc: "installs deps, (shards update)", default: false
bool "--sam", desc: "setup initial sam tasks file", default: false
bool "--sam", desc: "setup initial Sam tasks file", default: false
bool "--no-color", desc: "Disable colored output", default: false
help

setter migration : String?

def migration
(@migration || migration?).not_nil!
end
end

class Help
Expand All @@ -23,6 +30,7 @@ module Amber::CLI
end

def run
provision_args
CLI.toggle_colors(options.no_color?)
full_path_name = File.join(Dir.current, args.name)
if full_path_name =~ /\s+/
Expand All @@ -46,6 +54,11 @@ module Amber::CLI
MainCommand.run ["encrypt", "production", "--noedit"]
Dir.cd(cwd)
end

private def provision_args
return if args.migration != "unspecified"
args.migration = (args.m == "jennifer" ? "jennifer" : "micrate")
end
end
end
end
4 changes: 3 additions & 1 deletion src/amber/cli/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Amber::CLI
property model : String = "granite"
property recipe : (String | Nil) = nil
property recipe_source : (String | Nil) = nil
property migration : String = "micrate"

def initialize
end
Expand All @@ -22,7 +23,8 @@ module Amber::CLI
language: {type: String, default: "slang"},
model: {type: String, default: "granite"},
recipe: String | Nil,
recipe_source: String | Nil
recipe_source: String | Nil,
migration: {type: String, default: "micrate"}
)
end
end
4 changes: 4 additions & 0 deletions src/amber/cli/helpers/helpers.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ module Amber::CLI::Helpers
File.write("./config/application.cr", application.gsub("require \"amber\"", replacement))
end

def current_timestamp
Time.now.to_s("%Y%m%d%H%M%S%L")
end

def self.run(command, wait = true, shell = true)
if wait
Process.run(command, shell: shell, output: Process::Redirect::Inherit, error: Process::Redirect::Inherit)
Expand Down
2 changes: 1 addition & 1 deletion src/amber/cli/recipes/model.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module Amber::Recipes
end

def table_name
@table_name ||= "#{Inflector.pluralize(@name)}"
Inflector.pluralize(@name)
end
end
end
25 changes: 7 additions & 18 deletions src/amber/cli/templates/app.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Amber::CLI
class App < Teeplate::FileTree
directory "#{__DIR__}/app"
getter database_name_base, db_url, wait_for_command
getter database_name_base

@name : String
@database : String
Expand All @@ -14,13 +14,13 @@ module Amber::CLI
@email : String
@github_name : String

def initialize(@name, @database, @language, @model, @sam)
def initialize(@name, @database, @language, @model, @sam)
@database_name_base = generate_database_name_base
@author = fetch_author
@email = fetch_email
@github_name = fetch_github_name

@db_url =
@db_url =
if @database == "pg"
"postgres://admin:password@db:5432/#{@database_name_base}_development"
elsif @database == "mysql"
Expand All @@ -35,27 +35,16 @@ module Amber::CLI
end

def wait_for_command
if pg?
case @database
when "pg"
"while ! nc -q 1 db 5432 </dev/null; do sleep 1; done && "
elsif mysql?
"while ! nc -q 1 db 3306 </dev/null; do sleep 1; done && "
when "mysql"
"while ! nc -q 1 db 3306 </dev/null; do sleep 1; done && "
else
""
end
end

def pg?
@database == "pg"
end

def mysql?
@database == "mysql"
end

def sqlite?
@database == "sqlite"
end

def filter(entries)
entries.reject { |entry| entry.path.includes?("src/views") && !entry.path.includes?("#{@language}") }
end
Expand Down
1 change: 1 addition & 0 deletions src/amber/cli/templates/app/.amber.yml.ecr
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ type: app
database: <%= @database %>
language: <%= @language %>
model: <%= @model %>
migration: micrate
1 change: 0 additions & 1 deletion src/amber/cli/templates/app/.gitignore.ecr
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ production.yml
.DS_Store
/bin/
/node_modules
.idea/
Loading

0 comments on commit d114155

Please sign in to comment.