Sha256: 101e1dc83aacc888895e20d31c94ee1a667f01b7f2f5663fd7190b82c4773502

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

require "bard/cli/command"
require "bard/github"

class Bard::CLI::New < Bard::CLI::Command
  desc "new <project-name>", "creates new bard app named <project-name>"
  def new project_name
    @project_name = project_name
    validate
    create_project
    push_to_github
    stage
    puts green("Project #{project_name} created!")
    puts "Please cd ../#{project_name}"
  end

  attr_accessor :project_name

  private

  def validate
    if project_name !~ /^[a-z][a-z0-9]*\Z/
      puts red("!!! ") + "Invalid project name: #{yellow(project_name)}."
      puts "The first character must be a lowercase letter, and all following characters must be a lowercase letter or number." 
      exit 1
    end
  end

  def create_project
    run! <<~BASH
      env -i bash -lc '
        export HOME=~
        cd ..
        source ~/.rvm/scripts/rvm
        rvm use --create #{ruby_version}@#{project_name}

        gem list rails -i && gem install rails --no-document
        rails new #{project_name} --skip-kamal -m #{template_path}
      '
    BASH
  end

  def push_to_github
    Bard::Github.new(project_name).create_repo
    run! <<~BASH
      cd ../#{project_name}
      git add -A
      git commit -m"initial commit."
      git remote add origin git@github.com:botandrosedesign/#{project_name}
      git push -u origin master
    BASH
  end

  def stage
    run! <<~BASH
      cd ../#{project_name}
      bard deploy --provision
    BASH
  end

  def ruby_version
    File.read(".ruby-version").chomp
  end

  def template_path
    File.expand_path("new_rails_template.rb", __dir__)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bard-1.3.1 lib/bard/cli/new.rb
bard-1.3.0 lib/bard/cli/new.rb