Class: Afterlife::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/afterlife/repo.rb,
lib/afterlife/repo/config.rb,
lib/afterlife/repo/provider.rb

Defined Under Namespace

Classes: Config, Provider

Constant Summary collapse

DEFAULT_DIST =
'dist'
DEFAULT_BUILD_COMMAND =
'yarn build'
DEFAULT_INSTALL =
'yarn install'
VERSION_FILES =
[
  {
    type: :ruby,
    blob: './config/application.rb',
  }, {
    type: :ruby,
    blob: './lib/*/version.rb',
  }, {
    type: :node,
    blob: './package.json',
  },
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Repo

Returns a new instance of Repo.



39
40
41
# File 'lib/afterlife/repo.rb', line 39

def initialize(path)
  @full_path = path.start_with?('/') ? Pathname(path) : Repo.base_path.join(path)
end

Instance Attribute Details

#full_pathObject (readonly)

Returns the value of attribute full_path.



37
38
39
# File 'lib/afterlife/repo.rb', line 37

def full_path
  @full_path
end

Class Method Details

.allObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/afterlife/repo.rb', line 21

def self.all
  @all ||= Afterlife.config.repos[:cdnize]&.flat_map do |path|
    repo = Repo.new(path)
    packages = repo.conf.dig(:cdn, :packages)
    next repo unless packages

    packages.map do |package|
      Repo.new("#{path}/packages/#{package}")
    end
  end
end

.base_pathObject



33
34
35
# File 'lib/afterlife/repo.rb', line 33

def self.base_path
  @base_path ||= Pathname(Afterlife.config.repos[:path])
end

Instance Method Details

#build_commandObject



65
66
67
68
69
# File 'lib/afterlife/repo.rb', line 65

def build_command
  return DEFAULT_BUILD_COMMAND unless conf.key?(:build)

  conf[:build]
end

#confObject



120
121
122
# File 'lib/afterlife/repo.rb', line 120

def conf
  @conf ||= Provider.new(full_path).config
end

#confirmation_messageObject



71
72
73
# File 'lib/afterlife/repo.rb', line 71

def confirmation_message
  conf.dig(:deploy, :confirmation_message)
end

#current_branchObject



75
76
77
# File 'lib/afterlife/repo.rb', line 75

def current_branch
  @current_branch ||= `git rev-parse --abbrev-ref HEAD`.chomp
end

#current_revisionObject



79
80
81
# File 'lib/afterlife/repo.rb', line 79

def current_revision
  @current_revision ||= `git rev-parse HEAD`.chomp
end

#current_revision_shortObject



83
84
85
# File 'lib/afterlife/repo.rb', line 83

def current_revision_short
  @current_revision_short ||= `git rev-parse --short HEAD`.chomp
end

#dist_pathObject



43
44
45
# File 'lib/afterlife/repo.rb', line 43

def dist_path
  full_path.join(conf.dig(:cdn, :dist) || DEFAULT_DIST)
end

#envObject



55
56
57
# File 'lib/afterlife/repo.rb', line 55

def env
  @env ||= Environment.from(self)
end

#full_nameObject



91
92
93
# File 'lib/afterlife/repo.rb', line 91

def full_name
  package_json[:name] || File.basename(full_path)
end

#install_dependencies_commandObject



59
60
61
62
63
# File 'lib/afterlife/repo.rb', line 59

def install_dependencies_command
  return DEFAULT_INSTALL unless conf.key?(:install)

  conf[:install]
end

#nameObject



87
88
89
# File 'lib/afterlife/repo.rb', line 87

def name
  full_name.split('/').last
end

#package_jsonObject



113
114
115
116
117
118
# File 'lib/afterlife/repo.rb', line 113

def package_json
  @pkg_path ||= conf.dig(:cdn, :package_json) || full_path.join('package.json')
  return {} unless File.exist?(@pkg_path)

  @package_json ||= JSON.parse(File.read(@pkg_path), symbolize_names: true)
end

#variable(name) ⇒ Object

  1. Searchs for the AFTERLIFE_##name env var

  2. Searchs the path in the repo .afterlife.yml config



49
50
51
52
53
# File 'lib/afterlife/repo.rb', line 49

def variable(name)
  return env[name] if env[name]

  conf.dig(*name.split('.').map(&:to_sym))
end

#versionObject



107
108
109
110
111
# File 'lib/afterlife/repo.rb', line 107

def version
  # this migth not be the same as the package.json
  # Bump::Semver.version
  package_json[:version]
end

#version_filesObject

Files that define versions



96
97
98
99
100
101
102
103
104
105
# File 'lib/afterlife/repo.rb', line 96

def version_files
  @version_files ||= conf.dig(:release, :version_files)
  @version_files ||= VERSION_FILES.map do |arg|
    val = arg.dup
    val[:real_path] = Dir[val[:blob]].first
    next unless val[:real_path]

    val
  end.compact
end