Skip to content

Commit

Permalink
Implement generate status output
Browse files Browse the repository at this point in the history
  • Loading branch information
elct9620 committed Dec 26, 2023
1 parent 866ff92 commit e21468f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
53 changes: 45 additions & 8 deletions lib/boxing/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,32 @@ module Boxing
#
# @since 0.11.0
class Generator
ASCII_CLEAR = "\e[0m"
ASCII_BOLD = "\e[1m"

CREATED_MESSAGE = "#{ASCII_BOLD}create#{ASCII_CLEAR}\t%s"
IDENTICAL_MESSAGE = "#{ASCII_BOLD}identical#{ASCII_CLEAR}\t%s"
CONFLICT_MESSAGE = "#{ASCII_BOLD}conflict#{ASCII_CLEAR}\t%s"

attr_reader :destination, :path

include Utils

# @since 0.11.0
def initialize(destination, content)
pp current_path
@destination = current_path.join(destination)
@destination = destination
@path = current_path.join(@destination)
@content = content
end

# Generate file
#
# @since 0.11.0
def execute
return if identical?

FileUtils.mkdir_p(File.dirname(@destination))
File.write(@destination, render)
with_conflict do
FileUtils.mkdir_p(File.dirname(path))
File.write(path, render)
end
end

# Render content
Expand All @@ -43,7 +52,7 @@ def render
#
# @since 0.11.0
def exist?
@destination.exist?
path.exist?
end

# Check if file identical
Expand All @@ -52,7 +61,35 @@ def exist?
#
# @since 0.11.0
def identical?
exist? && File.binread(@destination) == String.new(render).force_encoding('ASCII-8BIT')
exist? && File.binread(path) == String.new(render).force_encoding('ASCII-8BIT')
end

protected

# With conflict
#
# @param [Proc] block
#
# @since 0.11.0
def with_conflict(&block)
return on_conflict(&block) if exist?

yield
puts format(CREATED_MESSAGE, destination)
end

# On conflict
#
# @param [Proc] block
#
# @since 0.11.0
def on_conflict
if identical?
puts format(IDENTICAL_MESSAGE, destination)
return
end

puts format(CONFLICT_MESSAGE, destination)
end
end
end
3 changes: 0 additions & 3 deletions lib/boxing/tasks/compose.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@

desc 'Generate docker-compose.yml'
task compose: :config do
include Boxing::Utils

puts 'Generating docker-compose.yml'
template('docker-compose.yml', 'docker-compose.yml.tt', context: Boxing.context)
end
2 changes: 2 additions & 0 deletions lib/boxing/tasks/config.rake
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

task :config do
require 'hanami/setup' if defined?(Hanami::CLI)

include Boxing::Utils

config_file = current_path.join('config', 'boxing.rb')
Expand Down
5 changes: 0 additions & 5 deletions lib/boxing/tasks/generate.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

desc 'Generate Dockerfile'
task generate: :config do
include Boxing::Utils

puts 'Generating Dockerfile'
template('Dockerfile', 'Dockerfile.tt', context: Boxing.context)

puts 'Generating .dockerignore'
template('.dockerignore', 'dockerignore.tt', context: Boxing.context)
end

0 comments on commit e21468f

Please sign in to comment.