Class: ViteRuby::Build

Inherits:
Struct
  • Object
show all
Defined in:
lib/vite_ruby/build.rb

Overview

Internal: Value object with information about the last build.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_digestObject

Returns the value of attribute current_digest

Returns:

  • (Object)

    the current value of current_digest



7
8
9
# File 'lib/vite_ruby/build.rb', line 7

def current_digest
  @current_digest
end

#digestObject

Returns the value of attribute digest

Returns:

  • (Object)

    the current value of digest



7
8
9
# File 'lib/vite_ruby/build.rb', line 7

def digest
  @digest
end

#errorsObject

Returns the value of attribute errors

Returns:

  • (Object)

    the current value of errors



7
8
9
# File 'lib/vite_ruby/build.rb', line 7

def errors
  @errors
end

#last_build_pathObject

Returns the value of attribute last_build_path

Returns:

  • (Object)

    the current value of last_build_path



7
8
9
# File 'lib/vite_ruby/build.rb', line 7

def last_build_path
  @last_build_path
end

#successObject

Returns the value of attribute success

Returns:

  • (Object)

    the current value of success



7
8
9
# File 'lib/vite_ruby/build.rb', line 7

def success
  @success
end

#timestampObject

Returns the value of attribute timestamp

Returns:

  • (Object)

    the current value of timestamp



7
8
9
# File 'lib/vite_ruby/build.rb', line 7

def timestamp
  @timestamp
end

#vite_rubyObject

Returns the value of attribute vite_ruby

Returns:

  • (Object)

    the current value of vite_ruby



7
8
9
# File 'lib/vite_ruby/build.rb', line 7

def vite_ruby
  @vite_ruby
end

Class Method Details

.from_previous(last_build_path, current_digest) ⇒ Object

Internal: Combines information from a previous build with the current digest.



10
11
12
13
14
15
16
# File 'lib/vite_ruby/build.rb', line 10

def from_previous(last_build_path, current_digest)
  new(
    **(last_build_path),
    current_digest: current_digest,
    last_build_path: last_build_path,
  )
end

Instance Method Details

#fresh?Boolean

Internal: A build is considered fresh if watched files have not changed, or the last failed build happened recently.

Returns:

  • (Boolean)


43
44
45
# File 'lib/vite_ruby/build.rb', line 43

def fresh?
  !stale?
end

#retry_failed?Boolean

Internal: To avoid cascading build failures, if the last build failed and it happened within a short time window, a new build should not be triggered.

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/vite_ruby/build.rb', line 49

def retry_failed?
  !success && Time.parse(timestamp) + 3 < Time.now # 3 seconds
rescue ArgumentError
  true
end

#stale?Boolean

Internal: A build is considered stale when watched files have changed since the last build, or when a certain time has ellapsed in case of failure.

Returns:

  • (Boolean)


37
38
39
# File 'lib/vite_ruby/build.rb', line 37

def stale?
  digest != current_digest || retry_failed? || vite_ruby != ViteRuby::VERSION
end

#to_json(*_args) ⇒ Object

Internal: Returns a JSON string with the metadata of the build.



73
74
75
76
77
78
79
80
81
# File 'lib/vite_ruby/build.rb', line 73

def to_json(*_args)
  JSON.pretty_generate(
    success: success,
    errors: errors,
    timestamp: timestamp,
    vite_ruby: vite_ruby,
    digest: digest,
  )
end

#with_result(**attrs) ⇒ Object

Internal: Returns a new build with the specified result.



56
57
58
59
60
61
62
63
64
65
# File 'lib/vite_ruby/build.rb', line 56

def with_result(**attrs)
  self.class.new(
    **attrs,
    timestamp: Time.now.strftime("%F %T"),
    vite_ruby: ViteRuby::VERSION,
    digest: current_digest,
    current_digest: current_digest,
    last_build_path: last_build_path,
  )
end

#write_to_cacheObject

Internal: Writes the result of the new build to a local file.



68
69
70
# File 'lib/vite_ruby/build.rb', line 68

def write_to_cache
  last_build_path.write to_json
end