Class: MilkTea::ProjectScaffold

Inherits:
Object
  • Object
show all
Defined in:
lib/milk_tea/tooling/project_scaffold.rb

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ ProjectScaffold

Returns a new instance of ProjectScaffold.



15
16
17
# File 'lib/milk_tea/tooling/project_scaffold.rb', line 15

def initialize(path)
  @path = File.expand_path(path)
end

Class Method Details

.create(path) ⇒ Object



11
12
13
# File 'lib/milk_tea/tooling/project_scaffold.rb', line 11

def self.create(path)
  new(path).create
end

Instance Method Details

#createObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/milk_tea/tooling/project_scaffold.rb', line 19

def create
  validate_target!

  FileUtils.mkdir_p(entry_dir)
  File.write(manifest_path, render_manifest)
  File.write(entry_path, render_entry_source)

  Result.new(
    root_path: @path,
    manifest_path: manifest_path,
    entry_path: entry_path,
    package_name: PackageManifest.default_package_name_for_root(@path),
  )
end

#entry_dirObject



49
50
51
# File 'lib/milk_tea/tooling/project_scaffold.rb', line 49

def entry_dir
  File.join(@path, "src")
end

#entry_pathObject



53
54
55
# File 'lib/milk_tea/tooling/project_scaffold.rb', line 53

def entry_path
  File.join(entry_dir, "main.mt")
end

#manifest_pathObject



45
46
47
# File 'lib/milk_tea/tooling/project_scaffold.rb', line 45

def manifest_path
  File.join(@path, "package.toml")
end

#package_nameObject



57
58
59
# File 'lib/milk_tea/tooling/project_scaffold.rb', line 57

def package_name
  PackageManifest.default_package_name_for_root(@path)
end

#render_entry_sourceObject



73
74
75
76
77
78
# File 'lib/milk_tea/tooling/project_scaffold.rb', line 73

def render_entry_source
  <<~MT
    function main() -> int:
        return 0
  MT
end

#render_manifestObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/milk_tea/tooling/project_scaffold.rb', line 61

def render_manifest
  <<~TOML
    [package]
    name = "#{package_name}"
    version = "0.1.0"
    source_root = "src"

    [build]
    entry = "src/main.mt"
  TOML
end

#validate_target!Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/milk_tea/tooling/project_scaffold.rb', line 34

def validate_target!
  if File.exist?(@path) && !File.directory?(@path)
    raise ProjectScaffoldError, "project path is not a directory: #{@path}"
  end

  return unless File.directory?(@path)
  return if Dir.children(@path).empty?

  raise ProjectScaffoldError, "project directory already exists and is not empty: #{@path}"
end