Class: Aspen::CLI::Commands::New

Inherits:
Dry::CLI::Command
  • Object
show all
Defined in:
lib/aspen/cli/commands/new.rb

Instance Method Summary collapse

Instance Method Details

#call(project_name:, **options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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) ⇒ Object



98
99
100
101
102
# 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) ⇒ Object



104
105
106
107
108
109
110
# 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