Class: MilkTea::PackageManifest

Inherits:
Object
  • Object
show all
Defined in:
lib/milk_tea/packages/manifest.rb

Defined Under Namespace

Classes: DataView, DependencyView

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ PackageManifest

Returns a new instance of PackageManifest.



57
58
59
# File 'lib/milk_tea/packages/manifest.rb', line 57

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

Class Method Details

.default_package_name_for_root(root_dir) ⇒ Object



28
29
30
# File 'lib/milk_tea/packages/manifest.rb', line 28

def self.default_package_name_for_root(root_dir)
  snake_case_identifier(File.basename(File.expand_path(root_dir)))
end

.load(path) ⇒ Object



41
42
43
# File 'lib/milk_tea/packages/manifest.rb', line 41

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

.manifest_exists_for?(path) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/milk_tea/packages/manifest.rb', line 45

def self.manifest_exists_for?(path)
  path = File.expand_path(path)
  current = File.directory?(path) ? path : File.dirname(path)
  loop do
    return true if File.file?(File.join(current, "package.toml"))
    parent = File.dirname(current)
    break if parent == current
    current = parent
  end
  false
end

.snake_case_identifier(name) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/milk_tea/packages/manifest.rb', line 32

def self.snake_case_identifier(name)
  name.to_s
    .gsub(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .tr("- ", "__")
    .gsub(/_+/, "_")
    .downcase
end

Instance Method Details

#loadObject



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/milk_tea/packages/manifest.rb', line 61

def load
  manifest_path, root_dir = resolve_manifest_path(@path)
  raise PackageManifestError, "package manifest not found for #{@path}" unless manifest_path

  config = parse_manifest(manifest_path)
  package = config.fetch("package", {})
  build = config.fetch("build", {})
  profile_config = config.fetch("profile", {})
  platform_config = config.fetch("platform", {})

  package_name = package["name"]
  if package_name.nil? || package_name.empty?
    package_name = self.class.default_package_name_for_root(root_dir)
  end
  package_version = package["version"]
  package_version = package_version.to_s unless package_version.nil?
  package_version = nil if package_version == ""

  package_kind = normalize_package_kind(package["kind"] || build["type"] || default_package_kind(build))
  source_root_value = package["source_root"] || default_source_root(root_dir)
  source_root = File.expand_path(source_root_value.to_s, root_dir)
  unless File.directory?(source_root)
    raise PackageManifestError, "package.source_root not found: #{source_root_value} (resolved to #{source_root})"
  end

  profile_name = normalize_profile_name(build["profile"] || profile_config["default"])
  platform_name = normalize_platform_name(build["platform"] || platform_config["default"])

  source_path = resolve_source_path(root_dir, build, package_kind)

  output_path = build["output"]
  output_path = File.expand_path(output_path.to_s, root_dir) if output_path

  assets_paths = parse_assets_paths(build["assets"], root_dir)

  html_template_path = build["html_template"]
  if html_template_path
    html_template_path = File.expand_path(html_template_path.to_s, root_dir)
    unless File.file?(html_template_path)
      raise PackageManifestError, "build.html_template not found: #{build["html_template"]} (resolved to #{html_template_path})"
    end
  end

  dependencies = parse_dependencies(config.fetch("dependencies", {}), root_dir:, manifest_path:)

  DataView.new(
    root_dir:,
    manifest_path:,
    package_name:,
    package_version:,
    package_kind:,
    source_root:,
    source_path:,
    profile: profile_name,
    platform: platform_name,
    output_path:,
    assets_paths:,
    html_template_path:,
    dependencies:,
  )
end