class Aspen::CLI::Commands::New

Public Instance Methods

call(project_name: , **options) click to toggle source
# File lib/aspen/cli/commands/new.rb, line 27
def call(project_name: , **options)
  f = Dry::CLI::Utils::Files
  root = Pathname.new(__FILE__).join("..")

  if f.exist?("#{project_name}/.aspen")
    raise RuntimeError, "There is already an Aspen project at #{project_name}, stopping."
  end

  URI.parse(options[:database_url])

  puts "\nGenerated:"
  puts "----------"

  f.mkdir "#{project_name}/"
  puts "    #{project_name}/                      -> Project folder"

  f.touch "#{project_name}/.aspen"
  puts "    #{project_name}/.aspen                -> File indicating Aspen project "

  File.open("#{project_name}/manifest.yml", 'w') do |file|
    template = get_template_file('manifest.yml.erb')
    file << ERB.new(template).result_with_hash(project_name: project_name)
  end
  puts "    #{project_name}/manifest.yml          -> Metadata about included files"

  f.touch "#{project_name}/config/db.yml"
  File.open("#{project_name}/config/db.yml", 'w') do |file|
    template = get_template_file('db.yml.erb')
    file << ERB.new(template).result_with_hash(database_url: options[:database_url])
  end
  puts "    #{project_name}/config/db.yml         -> Database configuration"

  if options[:docker]
    f.cp get_template_path("docker-compose.yml"), "#{project_name}/docker-compose.yml"
    puts "    #{project_name}/docker-compose.yml    -> Docker Compose file for Neo4j"
  else
    puts "    Skipping Docker Compose file"
  end

  f.mkdir "#{project_name}/src/"
  puts "    #{project_name}/src/                  -> Source files"

  f.mkdir "#{project_name}/bin/"
  puts "    #{project_name}/bin/                  -> Binary files (scripts)"

  f.cp get_template_path("convert"), "#{project_name}/bin/convert"
  FileUtils.chmod("+x", "#{project_name}/bin/convert")
  puts "    #{project_name}/bin/convert           -> Converts non-Aspen to Aspen"

  f.mkdir "#{project_name}/src/discourses/"
  f.touch "#{project_name}/src/discourses/.gitkeep"
  puts "    #{project_name}/src/discourses/       -> Collection of Discourses"

  f.mkdir "#{project_name}/src/grammars/"
  f.touch "#{project_name}/src/grammars/.gitkeep"
  puts "    #{project_name}/src/grammars/         -> Collection of Grammars"

  f.mkdir "#{project_name}/build/"
  f.touch "#{project_name}/build/.gitkeep"
  puts "    #{project_name}/build/                -> Compilation is output here"

  f.cp get_template_path(".gitignore"), "#{project_name}/.gitignore"
  f.touch "#{project_name}/.gitignore"
  puts "    #{project_name}/.gitignore            -> Ignoring config files, build files"

  if options[:docker]
    puts "\nTo start the Neo4j database, run `docker-compose up` from the #{project_name} folder"
  end
  puts "\n✅ Generated new project '#{project_name}'\n\n"
end
get_template_file(name) click to toggle source
# File lib/aspen/cli/commands/new.rb, line 98
def get_template_file(name)
  File.read(
    get_template_path(name)
  )
end
get_template_path(name) click to toggle source
# File lib/aspen/cli/commands/new.rb, line 104
def get_template_path(name)
  File.expand_path(
    File.join(
      File.dirname(__FILE__), "..", "templates", name
    )
  )
end