afterlife 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4a748ce7d12a9ee07b9811468a33b25852ab857dc26e35af99ace77826f150c3
4
- data.tar.gz: d969d2522b6cf783cb43426d3f8556ac5049f009fad5fba08b16065a91fd15cc
3
+ metadata.gz: 785bb4c634a1b73338a56cb64b6119389ca07904457159b14af3a00ad48869f0
4
+ data.tar.gz: 0f24b2547eb32ab72ae1807f2c5bcb813a5cc09f9dee589f6343bbd047c32c26
5
5
  SHA512:
6
- metadata.gz: 873c5cfa91704940ccbc7fff7a70d339c9f73271c63ba60e2f2b7011e61d58b601595982027f26e94f006ece37f2abd4061ac05179ba0a0f7ed444ce21dfc735
7
- data.tar.gz: 1e064e211cd77e8ae137e047eaa877b9174ba6098296c8723aa05d2a9f6bf7bc9268ffa8e43ea877e25b05613499588548e3ee09120e5702133ba380c1e04156
6
+ metadata.gz: 3ff42032daec81547cc0075ab8ed71d250b2cec8ac677383f6a9702e75124ebb27d173c085c8e5d155cfd9c0b63f49e79c3a7f35630de88424127816f3de4593
7
+ data.tar.gz: 48d016ace44ecc8f285eee917e6493766bc914adc324cc582f0e7a7c495fdc9ede0548f5c3529fb6ac296a2e529ed8b83a0208c5f9ae87e5d6d43eafad540afc
@@ -3,10 +3,6 @@
3
3
  module Afterlife
4
4
  module Config
5
5
  class Provider
6
- def self.file
7
- Afterlife.local_path.join('config.yml')
8
- end
9
-
10
6
  def respond_to_missing?(sym, include_all)
11
7
  config.key?(sym) || super
12
8
  end
@@ -15,6 +11,12 @@ module Afterlife
15
11
  config.key?(sym) ? config[sym] : super
16
12
  end
17
13
 
14
+ def file_exist?
15
+ return @file_exist if defined?(@file_exist)
16
+
17
+ @file_exist ||= file.exist?
18
+ end
19
+
18
20
  def stages
19
21
  @stages ||= config.delete(:stages)&.to_h do |name, val|
20
22
  [name, Stage.new(val.merge(name: name.to_s))]
@@ -24,7 +26,28 @@ module Afterlife
24
26
  private
25
27
 
26
28
  def config
27
- @config ||= YAML.load_file(Provider.file).deep_symbolize_keys
29
+ @config ||= !file_exist? && default_config
30
+ @config ||= YAML.load_file(file).deep_symbolize_keys
31
+ end
32
+
33
+ def default_config
34
+ {
35
+ credentials: {
36
+ aws: {
37
+ profile: ENV.fetch('AWS_PROFILE', nil),
38
+ },
39
+ },
40
+ stages: {},
41
+ }
42
+ end
43
+
44
+ def file
45
+ Pathname(
46
+ ENV.fetch(
47
+ 'AFTERLIFE_CONFIG_PATH',
48
+ Afterlife.local_path.join('config.yml'),
49
+ ),
50
+ )
28
51
  end
29
52
  end
30
53
  end
@@ -12,12 +12,14 @@ module Afterlife
12
12
  end
13
13
 
14
14
  def setup
15
- repo.env.merge!(
16
- 'AWS_PROFILE' => Afterlife.config.credentials.dig(:aws, :profile),
15
+ # only use the defined AWS_PROFILE if its not already been defined
16
+ repo.env.set(
17
+ 'AWS_PROFILE' => Afterlife.config&.credentials&.dig(:aws, :profile),
17
18
  'AWS_BUCKET' => Afterlife.current_stage.bucket,
18
19
  'AWS_REGION' => Afterlife.current_stage.region,
19
- 'S3_FULL_PATH' => s3_full_path,
20
20
  )
21
+ # in nested deployments we need the current path, not the parent's
22
+ repo.env.set!('S3_FULL_PATH' => s3_full_path)
21
23
  end
22
24
 
23
25
  def initial_message
@@ -33,14 +35,12 @@ module Afterlife
33
35
 
34
36
  def commands
35
37
  [
36
- repo_command || javascripts,
37
- revision,
38
- ].flatten
38
+ repo_command || upload_javascripts,
39
+ upload_revision,
40
+ ]
39
41
  end
40
42
 
41
- def javascripts
42
- # by default we use no-cache, if something else is needed
43
- # do it in the deploy.command option
43
+ def upload_javascripts
44
44
  <<-BASH
45
45
  aws s3 sync
46
46
  %<DIST_PATH>s
@@ -52,7 +52,7 @@ module Afterlife
52
52
  BASH
53
53
  end
54
54
 
55
- def revision
55
+ def upload_revision
56
56
  <<-BASH
57
57
  echo "#{repo.current_revision}" |
58
58
  aws s3 cp --content-type text/plain --cache-control 'no-cache' - #{s3_revision_path}
@@ -7,19 +7,31 @@ module Afterlife
7
7
 
8
8
  def commands
9
9
  [
10
- add_revision_command,
11
- repo_command || javascripts,
12
- revision,
10
+ repo_command || [upload_parts, upload_esm],
11
+ upload_revision,
13
12
  ]
14
13
  end
15
14
 
16
- def add_revision_command
17
- Dir["#{repo.dist_path}/*.esm.js"].map do |file|
18
- next if file.include?(repo.current_revision)
15
+ def upload_parts
16
+ <<-BASH
17
+ aws s3 sync
18
+ %<DIST_PATH>s
19
+ %<S3_FULL_PATH>s
20
+ --exclude '*.ts'
21
+ --exclude '*.tsx'
22
+ --exclude '*.esm.js'
23
+ --size-only
24
+ --cache-control 'public, max-age=31540000'
25
+ BASH
26
+ end
19
27
 
20
- new_name = file.gsub('.esm.js', "-#{repo.current_revision}.esm.js")
21
- "mv #{file} #{new_name}"
22
- end
28
+ def upload_esm
29
+ <<-BASH
30
+ aws s3 cp
31
+ --cache-control 'public, max-age=31540000'
32
+ #{repo.dist_path}/#{repo.name}.esm.js
33
+ #{s3_full_path}/#{repo.name}-#{repo.current_revision}.esm.js
34
+ BASH
23
35
  end
24
36
 
25
37
  def s3_full_path
@@ -18,7 +18,7 @@ module Afterlife
18
18
  say_status 'Deployed', deployment.output
19
19
  run_after_hooks
20
20
  end
21
- rescue Deploy::Error => e
21
+ rescue Deploy::Error, Afterlife::Error => e
22
22
  fatal!(e.message)
23
23
  end
24
24
 
@@ -8,7 +8,7 @@ module Afterlife
8
8
 
9
9
  def call(stage)
10
10
  deployment = klass.new
11
- setup_stage(stage)
11
+ Afterlife.current_stage = find_stage(stage)
12
12
  fill_env
13
13
  deployment.setup
14
14
  yield deployment
@@ -28,16 +28,19 @@ module Afterlife
28
28
  end
29
29
  end
30
30
 
31
- def setup_stage(stage)
31
+ def find_stage(stage)
32
+ return Stage.build(stage) unless Afterlife.config.file_exist?
33
+
32
34
  unless Afterlife.config.stages.key?(stage.to_sym)
33
35
  fail Error, "invalid stage '#{stage}'. Possible values: #{Afterlife.config.stages.keys.map(&:to_s)}"
34
36
  end
35
37
 
36
- Afterlife.current_stage = Afterlife.config.stages[stage.to_sym]
38
+ Afterlife.config.stages[stage.to_sym]
37
39
  end
38
40
 
39
41
  def fill_env
40
- Afterlife.current_repo.env.merge!(
42
+ # in nested deployments we need the current definition, not the parent's
43
+ Afterlife.current_repo.env.set!(
41
44
  'DIST_PATH' => Afterlife.current_repo.dist_path.to_s,
42
45
  'CURRENT_BRANCH' => Afterlife.current_repo.current_branch,
43
46
  'AFTERLIFE_STAGE' => Afterlife.current_stage.name,
@@ -5,7 +5,7 @@ module Afterlife
5
5
  attr_reader :repo, :env
6
6
 
7
7
  def self.from(repo)
8
- env = new(repo).tap(&:call).env
8
+ env = new(repo).tap(&:call)
9
9
  yield env if block_given?
10
10
  env
11
11
  end
@@ -33,6 +33,20 @@ module Afterlife
33
33
  @env_hash ||= repo.conf.dig(:deploy, :env)
34
34
  end
35
35
 
36
+ # soft set, do not override existent ENVs
37
+ def set(arg)
38
+ arg.each do |k, v|
39
+ @env[k] ||= ENV.fetch(k, v)
40
+ end
41
+ end
42
+
43
+ # forces to be exactly dthe value
44
+ def set!(arg)
45
+ arg.each do |k, v|
46
+ @env[k] = v
47
+ end
48
+ end
49
+
36
50
  private
37
51
 
38
52
  def from_flat_envs
@@ -26,18 +26,22 @@ module Afterlife
26
26
  def run
27
27
  parsed_commands.each do |command|
28
28
  Afterlife.cli.log_info(command) if Afterlife.cli.options['verbose']
29
- system(repo.env, command.squish, exception: true) unless Afterlife.cli.options['dry-run']
29
+ system(env_hash, command.squish, exception: true) unless Afterlife.cli.options['dry-run']
30
30
  end
31
31
  rescue RuntimeError => e
32
32
  raise Error, e
33
33
  end
34
34
 
35
35
  def parsed_commands
36
- Exec.parse(commands, repo.env)
36
+ Exec.parse(commands, env_hash)
37
37
  end
38
38
 
39
39
  def repo
40
40
  Afterlife.current_repo
41
41
  end
42
+
43
+ def env_hash
44
+ repo.env.env
45
+ end
42
46
  end
43
47
  end
@@ -79,7 +79,7 @@ module Afterlife
79
79
  end
80
80
 
81
81
  def name
82
- full_name.split('/').last.gsub('-', '_')
82
+ full_name.split('/').last
83
83
  end
84
84
 
85
85
  def full_name
@@ -7,5 +7,14 @@ module Afterlife
7
7
  :region,
8
8
  :cdn_url,
9
9
  keyword_init: true,
10
- )
10
+ ) do
11
+ def self.build(name = nil)
12
+ Stage.new(
13
+ name: ENV.fetch('AFTERLIFE_STAGE', name),
14
+ bucket: ENV.fetch('AWS_BUCKET', nil),
15
+ region: ENV.fetch('AWS_REGION'),
16
+ cdn_url: ENV.fetch('AWS_BUCKET', nil),
17
+ )
18
+ end
19
+ end
11
20
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Afterlife
4
- VERSION = '1.2.1'
4
+ VERSION = '1.3.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: afterlife
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genaro Madrid
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-11-16 00:00:00.000000000 Z
11
+ date: 2023-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport