Class: Rawfeed::Backup

Inherits:
Object
  • Object
show all
Defined in:
lib/rawfeed/command/backup.rb

Constant Summary collapse

DEFAULT_FOLDERS =
%w[assets pub .vscode _posts _pages _data].freeze
STATIC_FILES =
%w[_config.yml index.md robots.txt CNAME].freeze
DYNAMIC_EXTENSIONS =
%w[.sh .py .rb .bat .ps1].freeze
GOOGLE_PATTERN =
/^google.*\.html$/.freeze

Class Method Summary collapse

Class Method Details

.add_dynamic_files_to_zip(zipfile, project_root) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rawfeed/command/backup.rb', line 97

def self.add_dynamic_files_to_zip(zipfile, project_root)
  Dir.glob(File.join(project_root, "*")).each do |path|
    next unless File.file?(path)

    basename = File.basename(path)
    ext = File.extname(path)

    # Check if it's a dynamic file (has one of the extensions or matches google*.html)
    if DYNAMIC_EXTENSIONS.include?(ext) || GOOGLE_PATTERN.match?(basename)
      add_file_to_zip(zipfile, path, basename)
    end
  end
end

.add_file_to_zip(zipfile, file_path, zip_path) ⇒ Object



120
121
122
# File 'lib/rawfeed/command/backup.rb', line 120

def self.add_file_to_zip(zipfile, file_path, zip_path)
  zipfile.add(zip_path, file_path)
end

.add_folder_to_zip(zipfile, folder_path, zip_path) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/rawfeed/command/backup.rb', line 111

def self.add_folder_to_zip(zipfile, folder_path, zip_path)
  Dir.glob(File.join(folder_path, "**/**")).each do |file|
    next if File.directory?(file)

    relative_path = File.join(zip_path, file.sub(Regexp.escape(folder_path), "").sub(/^\//, ""))
    zipfile.add(relative_path, file)
  end
end

.create_backup(project_root, backup_path, append_paths) ⇒ Object



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
# File 'lib/rawfeed/command/backup.rb', line 65

def self.create_backup(project_root, backup_path, append_paths)
  require "zip"
  Zip::File.open(backup_path, Zip::File::CREATE) do |zipfile|
    # Add default folders
    DEFAULT_FOLDERS.each do |folder|
      folder_path = File.join(project_root, folder)
      add_folder_to_zip(zipfile, folder_path, folder) if Dir.exist?(folder_path)
    end

    # Add static files
    STATIC_FILES.each do |file|
      file_path = File.join(project_root, file)
      add_file_to_zip(zipfile, file_path, file) if File.exist?(file_path)
    end

    # Add dynamic files
    add_dynamic_files_to_zip(zipfile, project_root)

    # Add append paths
    append_paths.each do |path|
      full_path = File.join(project_root, path)
      if File.directory?(full_path)
        add_folder_to_zip(zipfile, full_path, path)
      elsif File.exist?(full_path)
        add_file_to_zip(zipfile, full_path, path)
      else
        puts "[!] Warning: Path not found: #{path}".yellow
      end
    end
  end
end

.parse_options(args) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rawfeed/command/backup.rb', line 45

def self.parse_options(args)
  options = { destination: nil, append: [] }
  i = 0

  while i < args.length
    case args[i]
    when "--destination", "-d"
      options[:destination] = args[i + 1]
      i += 2
    when "--append", "-a"
      options[:append] << args[i + 1]
      i += 2
    else
      i += 1
    end
  end

  options
end

.site(*args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rawfeed/command/backup.rb', line 13

def self.site(*args)
  project_root = "."
  gemspec_path = File.join(project_root, "rawfeed.gemspec")

  if File.exist?(gemspec_path)
    puts "[!] Not allowed to create backup from gem source directory (rawfeed.gemspec found)".red
    exit 1
  end

  options = parse_options(args)
  destination = options[:destination] || project_root
  append_paths = options[:append] || []

  unless Dir.exist?(destination)
    puts "[!] Destination directory does not exist: #{destination}".red
    exit 1
  end

  backup_filename = "backup-#{Time.now.strftime('%Y-%m-%d_%H-%M-%S')}.zip"
  backup_path = File.join(destination, backup_filename)

  begin
    create_backup(project_root, backup_path, append_paths)
    puts "[✓] Backup created successfully: #{backup_path}".green
  rescue StandardError => e
    puts "[!] Error creating backup: #{e.message}".red
    exit 1
  end
end